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 { log } from "@tensamin/shared/log";
|
||||||
import Link from "@tensamin/ui/link";
|
import Link from "@tensamin/ui/link";
|
||||||
import { Label } from "@tensamin/ui/cmp/label";
|
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.
|
// Prevents the user from using Tensamin without accepting the privacy policy and terms of service.
|
||||||
export default function Screen(props: { children: React.ReactNode }) {
|
export default function Screen(props: { children: React.ReactNode }) {
|
||||||
const { load, save } = useStorage();
|
const { load, save } = useStorage();
|
||||||
|
const location = useLocation();
|
||||||
|
|
||||||
const [error, setError] = useState("");
|
const [error, setError] = useState("");
|
||||||
const [errorDescription, setErrorDescription] = useState("");
|
const [errorDescription, setErrorDescription] = useState("");
|
||||||
|
|
@ -21,6 +23,9 @@ export default function Screen(props: { children: React.ReactNode }) {
|
||||||
const [remoteDocs, setRemoteDocs] = useState<
|
const [remoteDocs, setRemoteDocs] = useState<
|
||||||
z.infer<typeof legalDocsSchema> | undefined
|
z.infer<typeof legalDocsSchema> | undefined
|
||||||
>(undefined);
|
>(undefined);
|
||||||
|
const [localDocs, setLocalDocs] = useState<
|
||||||
|
z.infer<typeof legalDocsSchema> | undefined
|
||||||
|
>(undefined);
|
||||||
|
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
|
|
@ -46,6 +51,11 @@ export default function Screen(props: { children: React.ReactNode }) {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let active = true;
|
let active = true;
|
||||||
|
|
||||||
|
setLoading(true);
|
||||||
|
setError("");
|
||||||
|
setErrorDescription("");
|
||||||
|
setHasContinued(false);
|
||||||
|
|
||||||
void load("user_id").then(async (id) => {
|
void load("user_id").then(async (id) => {
|
||||||
if (!active) {
|
if (!active) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -95,7 +105,8 @@ export default function Screen(props: { children: React.ReactNode }) {
|
||||||
|
|
||||||
setRemoteDocs(safeCurrent.data);
|
setRemoteDocs(safeCurrent.data);
|
||||||
|
|
||||||
const localDocs = await load("legal_docs");
|
const currentLocalDocs = await load("legal_docs");
|
||||||
|
setLocalDocs(currentLocalDocs);
|
||||||
|
|
||||||
const [loadedAcceptedPP, loadedAcceptedTOS] = await Promise.all([
|
const [loadedAcceptedPP, loadedAcceptedTOS] = await Promise.all([
|
||||||
load("accepted_privacy_policy"),
|
load("accepted_privacy_policy"),
|
||||||
|
|
@ -109,11 +120,17 @@ export default function Screen(props: { children: React.ReactNode }) {
|
||||||
acceptPP(loadedAcceptedPP);
|
acceptPP(loadedAcceptedPP);
|
||||||
acceptTOS(loadedAcceptedTOS);
|
acceptTOS(loadedAcceptedTOS);
|
||||||
|
|
||||||
if (localDocs.pp.hash !== safeCurrent.data.pp.hash) {
|
if (
|
||||||
|
!currentLocalDocs ||
|
||||||
|
currentLocalDocs.pp.hash !== safeCurrent.data.pp.hash
|
||||||
|
) {
|
||||||
acceptPP(false);
|
acceptPP(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (localDocs.tos.hash !== safeCurrent.data.tos.hash) {
|
if (
|
||||||
|
!currentLocalDocs ||
|
||||||
|
currentLocalDocs.tos.hash !== safeCurrent.data.tos.hash
|
||||||
|
) {
|
||||||
acceptTOS(false);
|
acceptTOS(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -123,7 +140,7 @@ export default function Screen(props: { children: React.ReactNode }) {
|
||||||
return () => {
|
return () => {
|
||||||
active = false;
|
active = false;
|
||||||
};
|
};
|
||||||
}, [load]);
|
}, [load, location.pathname]);
|
||||||
|
|
||||||
if (error !== "" && errorDescription !== "") {
|
if (error !== "" && errorDescription !== "") {
|
||||||
return <ErrorScreen error={error} description={errorDescription} />;
|
return <ErrorScreen error={error} description={errorDescription} />;
|
||||||
|
|
@ -133,7 +150,16 @@ export default function Screen(props: { children: React.ReactNode }) {
|
||||||
return null;
|
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}</>;
|
return <>{props.children}</>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import Socket from "@tensamin/ttp/context";
|
import Socket from "@tensamin/ttp/context";
|
||||||
import User from "@tensamin/user/context";
|
import User from "@tensamin/user/context";
|
||||||
import { useEffect, useState, type ReactNode } from "react";
|
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 Sidebar from "@/components/sidebar";
|
||||||
import Conversation from "@/features/conversation/context";
|
import Conversation from "@/features/conversation/context";
|
||||||
|
|
@ -18,26 +18,38 @@ export default function Layout(props: { children: ReactNode }) {
|
||||||
|
|
||||||
const { load } = useStorage();
|
const { load } = useStorage();
|
||||||
|
|
||||||
const location = useLocation();
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log(location);
|
let active = true;
|
||||||
|
|
||||||
|
void load("user_id").then((userId) => {
|
||||||
|
if (!active) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
load("user_id").then((userId) => {
|
|
||||||
if (userId !== 0) {
|
if (userId !== 0) {
|
||||||
setLoggedIn(true);
|
setLoggedIn(true);
|
||||||
} else {
|
return;
|
||||||
navigate({
|
|
||||||
to: "/login",
|
|
||||||
});
|
|
||||||
setLoggedIn(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setLoggedIn(false);
|
||||||
|
void navigate({
|
||||||
|
to: "/login",
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}, [location]);
|
|
||||||
|
return () => {
|
||||||
|
active = false;
|
||||||
|
};
|
||||||
|
}, [load, navigate]);
|
||||||
|
|
||||||
|
if (loggedIn !== true) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Socket blockConnection={!loggedIn}>
|
<Socket>
|
||||||
<User>
|
<User>
|
||||||
<Conversation>
|
<Conversation>
|
||||||
<div className="w-full h-full flex bg-sidebar">
|
<div className="w-full h-full flex bg-sidebar">
|
||||||
|
|
|
||||||
|
|
@ -387,7 +387,7 @@ export default function Provider(props: {
|
||||||
setIdentifying(false);
|
setIdentifying(false);
|
||||||
identificationStartedRef.current = false;
|
identificationStartedRef.current = false;
|
||||||
};
|
};
|
||||||
}, []);
|
}, [props.blockConnection]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!connected) {
|
if (!connected) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue