⬆️ v3.0.0 (#704)

This commit is contained in:
Luke Vella 2023-06-19 17:17:00 +01:00 committed by GitHub
parent 735056f25f
commit c22b3abc4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
385 changed files with 19912 additions and 5250 deletions

View file

@ -1,5 +1,4 @@
import { trpc, UserSession } from "@rallly/backend";
import { useRouter } from "next/router";
import { useTranslation } from "next-i18next";
import React from "react";
@ -10,7 +9,6 @@ import { useRequiredContext } from "./use-required-context";
export const UserContext = React.createContext<{
user: UserSession & { shortName: string };
refresh: () => void;
logout: () => void;
ownsObject: (obj: { userId: string | null }) => boolean;
} | null>(null);
@ -45,29 +43,21 @@ export const IfGuest = (props: { children?: React.ReactNode }) => {
return <>{props.children}</>;
};
export const UserProvider = (props: {
children?: React.ReactNode;
forceUserId?: string;
}) => {
export const UserProvider = (props: { children?: React.ReactNode }) => {
const { t } = useTranslation();
const queryClient = trpc.useContext();
const { data: user } = trpc.whoami.get.useQuery();
const router = useRouter();
const logout = trpc.whoami.destroy.useMutation({
onSuccess: async () => {
router.push("/logout");
},
});
const { data: user } = trpc.whoami.get.useQuery();
const { data: userPreferences } = trpc.userPreferences.get.useQuery();
const shortName = user
? user.isGuest === false
? user.name
? user.name.split(" ")[0]
: user.id.substring(0, 10)
: t("guest");
if (!user) {
if (!user || userPreferences === undefined) {
return null;
}
@ -79,16 +69,7 @@ export const UserProvider = (props: {
return queryClient.whoami.invalidate();
},
ownsObject: ({ userId }) => {
if (
(userId && user.id === userId) ||
(props.forceUserId && props.forceUserId === userId)
) {
return true;
}
return false;
},
logout: () => {
logout.mutate();
return userId ? [user.id].includes(userId) : false;
},
}}
>