diff --git a/apps/web/src/features/legal/screen.tsx b/apps/web/src/features/legal/screen.tsx index 1ea3367..b2d1a9c 100644 --- a/apps/web/src/features/legal/screen.tsx +++ b/apps/web/src/features/legal/screen.tsx @@ -10,10 +10,12 @@ import { legalDocsSchema } from "@tensamin/shared/features/legal/schema"; import { log } from "@tensamin/shared/log"; import Link from "@tensamin/ui/link"; import { Label } from "@tensamin/ui/cmp/label"; +import { useLocation } from "@tanstack/react-router"; // Prevents the user from using Tensamin without accepting the privacy policy and terms of service. export default function Screen(props: { children: React.ReactNode }) { const { load, save } = useStorage(); + const location = useLocation(); const [error, setError] = useState(""); const [errorDescription, setErrorDescription] = useState(""); @@ -21,6 +23,9 @@ export default function Screen(props: { children: React.ReactNode }) { const [remoteDocs, setRemoteDocs] = useState< z.infer | undefined >(undefined); + const [localDocs, setLocalDocs] = useState< + z.infer | undefined + >(undefined); const [loading, setLoading] = useState(true); @@ -46,6 +51,11 @@ export default function Screen(props: { children: React.ReactNode }) { useEffect(() => { let active = true; + setLoading(true); + setError(""); + setErrorDescription(""); + setHasContinued(false); + void load("user_id").then(async (id) => { if (!active) { return; @@ -95,7 +105,8 @@ export default function Screen(props: { children: React.ReactNode }) { setRemoteDocs(safeCurrent.data); - const localDocs = await load("legal_docs"); + const currentLocalDocs = await load("legal_docs"); + setLocalDocs(currentLocalDocs); const [loadedAcceptedPP, loadedAcceptedTOS] = await Promise.all([ load("accepted_privacy_policy"), @@ -109,11 +120,17 @@ export default function Screen(props: { children: React.ReactNode }) { acceptPP(loadedAcceptedPP); acceptTOS(loadedAcceptedTOS); - if (localDocs.pp.hash !== safeCurrent.data.pp.hash) { + if ( + !currentLocalDocs || + currentLocalDocs.pp.hash !== safeCurrent.data.pp.hash + ) { acceptPP(false); } - if (localDocs.tos.hash !== safeCurrent.data.tos.hash) { + if ( + !currentLocalDocs || + currentLocalDocs.tos.hash !== safeCurrent.data.tos.hash + ) { acceptTOS(false); } @@ -123,7 +140,7 @@ export default function Screen(props: { children: React.ReactNode }) { return () => { active = false; }; - }, [load]); + }, [load, location.pathname]); if (error !== "" && errorDescription !== "") { return ; @@ -133,7 +150,16 @@ export default function Screen(props: { children: React.ReactNode }) { return null; } - if ((acceptedPP && acceptedTOS && hasContinued) || userId === 0) { + const docsMatch = + localDocs !== undefined && + remoteDocs !== undefined && + localDocs.pp.hash === remoteDocs.pp.hash && + localDocs.tos.hash === remoteDocs.tos.hash; + + if ( + (acceptedPP && acceptedTOS && (docsMatch || hasContinued)) || + userId === 0 + ) { return <>{props.children}; } diff --git a/apps/web/src/routes/app/layout.tsx b/apps/web/src/routes/app/layout.tsx index dcd20e3..e7fa7c0 100644 --- a/apps/web/src/routes/app/layout.tsx +++ b/apps/web/src/routes/app/layout.tsx @@ -1,7 +1,7 @@ import Socket from "@tensamin/ttp/context"; import User from "@tensamin/user/context"; import { useEffect, useState, type ReactNode } from "react"; -import { useNavigate, useLocation } from "@tanstack/react-router"; +import { useNavigate } from "@tanstack/react-router"; import Sidebar from "@/components/sidebar"; import Conversation from "@/features/conversation/context"; @@ -18,26 +18,38 @@ export default function Layout(props: { children: ReactNode }) { const { load } = useStorage(); - const location = useLocation(); const navigate = useNavigate(); useEffect(() => { - console.log(location); + let active = true; + + void load("user_id").then((userId) => { + if (!active) { + return; + } - load("user_id").then((userId) => { if (userId !== 0) { setLoggedIn(true); - } else { - navigate({ - to: "/login", - }); - setLoggedIn(false); + return; } + + setLoggedIn(false); + void navigate({ + to: "/login", + }); }); - }, [location]); + + return () => { + active = false; + }; + }, [load, navigate]); + + if (loggedIn !== true) { + return null; + } return ( - +
diff --git a/packages/ttp/src/context.tsx b/packages/ttp/src/context.tsx index d9dbc3e..ce9d2f9 100644 --- a/packages/ttp/src/context.tsx +++ b/packages/ttp/src/context.tsx @@ -387,7 +387,7 @@ export default function Provider(props: { setIdentifying(false); identificationStartedRef.current = false; }; - }, []); + }, [props.blockConnection]); useEffect(() => { if (!connected) {