mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-05 20:21:50 +02:00
⬆️ v3.0.0 (#704)
This commit is contained in:
parent
735056f25f
commit
c22b3abc4d
385 changed files with 19912 additions and 5250 deletions
|
@ -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;
|
||||
},
|
||||
}}
|
||||
>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue