Fixed legal screen & connection stuff
This commit is contained in:
parent
ea72de0f08
commit
18d4add22d
3 changed files with 55 additions and 17 deletions
|
|
@ -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<typeof legalDocsSchema> | undefined
|
||||
>(undefined);
|
||||
const [localDocs, setLocalDocs] = useState<
|
||||
z.infer<typeof legalDocsSchema> | 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 <ErrorScreen error={error} description={errorDescription} />;
|
||||
|
|
@ -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}</>;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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({
|
||||
return;
|
||||
}
|
||||
|
||||
setLoggedIn(false);
|
||||
void navigate({
|
||||
to: "/login",
|
||||
});
|
||||
setLoggedIn(false);
|
||||
}
|
||||
});
|
||||
}, [location]);
|
||||
|
||||
return () => {
|
||||
active = false;
|
||||
};
|
||||
}, [load, navigate]);
|
||||
|
||||
if (loggedIn !== true) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<Socket blockConnection={!loggedIn}>
|
||||
<Socket>
|
||||
<User>
|
||||
<Conversation>
|
||||
<div className="w-full h-full flex bg-sidebar">
|
||||
|
|
|
|||
|
|
@ -387,7 +387,7 @@ export default function Provider(props: {
|
|||
setIdentifying(false);
|
||||
identificationStartedRef.current = false;
|
||||
};
|
||||
}, []);
|
||||
}, [props.blockConnection]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!connected) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue