Improved legal screen
This commit is contained in:
parent
76ca664298
commit
82a14f65fa
1 changed files with 65 additions and 75 deletions
|
|
@ -51,104 +51,94 @@ export default function Screen(props: { children: React.ReactNode }) {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let active = true;
|
let active = true;
|
||||||
|
|
||||||
const shouldBlockRender = userId === undefined || userId === 0;
|
setLoading(true);
|
||||||
|
|
||||||
if (shouldBlockRender) {
|
|
||||||
setLoading(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
setError("");
|
setError("");
|
||||||
setErrorDescription("");
|
setErrorDescription("");
|
||||||
|
|
||||||
void load("user_id").then(async (id) => {
|
void (async () => {
|
||||||
if (!active) {
|
try {
|
||||||
return;
|
const id = await load("user_id");
|
||||||
}
|
if (!active) {
|
||||||
|
return;
|
||||||
setUserId(id);
|
|
||||||
|
|
||||||
if (id === 0) {
|
|
||||||
if (shouldBlockRender) {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const current = await fetch("https://legal.tensamin.net/api/current")
|
setUserId(id);
|
||||||
.then((res) => res.json())
|
|
||||||
.catch((err) => {
|
if (id === 0) {
|
||||||
if (!active) {
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const current = await fetch("https://legal.tensamin.net/api/current")
|
||||||
|
.then((res) => res.json())
|
||||||
|
.catch((err) => {
|
||||||
|
if (!active) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
setError("Failed to load legal documents");
|
||||||
|
setErrorDescription(
|
||||||
|
"An error occurred while fetching the legal documents from the server. Please try again later.",
|
||||||
|
);
|
||||||
|
log(0, "Legal", "red", "Failed to fetch legal documents", err);
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
});
|
||||||
|
|
||||||
|
if (!active || current === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const safeCurrent = legalDocsSchema.safeParse(current);
|
||||||
|
if (!safeCurrent.success) {
|
||||||
setError("Failed to load legal documents");
|
setError("Failed to load legal documents");
|
||||||
setErrorDescription(
|
setErrorDescription(
|
||||||
"An error occurred while fetching the legal documents from the server. Please try again later.",
|
"The legal documents data received from the server is invalid. Please try again later.",
|
||||||
);
|
);
|
||||||
log(0, "Legal", "red", "Failed to fetch legal documents", err);
|
log(
|
||||||
return undefined;
|
0,
|
||||||
});
|
"Legal",
|
||||||
|
"red",
|
||||||
|
"Invalid legal documents data",
|
||||||
|
safeCurrent.error,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!active || current === undefined) {
|
setRemoteDocs(safeCurrent.data);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const safeCurrent = legalDocsSchema.safeParse(current);
|
const currentLocalDocs = await load("legal_docs");
|
||||||
if (!safeCurrent.success) {
|
setLocalDocs(currentLocalDocs);
|
||||||
setError("Failed to load legal documents");
|
|
||||||
setErrorDescription(
|
const [loadedAcceptedPP, loadedAcceptedTOS] = await Promise.all([
|
||||||
"The legal documents data received from the server is invalid. Please try again later.",
|
load("accepted_privacy_policy"),
|
||||||
|
load("accepted_terms_of_service"),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!active) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
acceptPP(
|
||||||
|
loadedAcceptedPP &&
|
||||||
|
!!currentLocalDocs &&
|
||||||
|
currentLocalDocs.pp.hash === safeCurrent.data.pp.hash,
|
||||||
);
|
);
|
||||||
log(
|
acceptTOS(
|
||||||
0,
|
loadedAcceptedTOS &&
|
||||||
"Legal",
|
!!currentLocalDocs &&
|
||||||
"red",
|
currentLocalDocs.tos.hash === safeCurrent.data.tos.hash,
|
||||||
"Invalid legal documents data",
|
|
||||||
safeCurrent.error,
|
|
||||||
);
|
);
|
||||||
return;
|
} finally {
|
||||||
|
if (active) {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
})();
|
||||||
setRemoteDocs(safeCurrent.data);
|
|
||||||
|
|
||||||
const currentLocalDocs = await load("legal_docs");
|
|
||||||
setLocalDocs(currentLocalDocs);
|
|
||||||
|
|
||||||
const [loadedAcceptedPP, loadedAcceptedTOS] = await Promise.all([
|
|
||||||
load("accepted_privacy_policy"),
|
|
||||||
load("accepted_terms_of_service"),
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (!active) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
acceptPP(loadedAcceptedPP);
|
|
||||||
acceptTOS(loadedAcceptedTOS);
|
|
||||||
|
|
||||||
if (
|
|
||||||
!currentLocalDocs ||
|
|
||||||
currentLocalDocs.pp.hash !== safeCurrent.data.pp.hash
|
|
||||||
) {
|
|
||||||
acceptPP(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
!currentLocalDocs ||
|
|
||||||
currentLocalDocs.tos.hash !== safeCurrent.data.tos.hash
|
|
||||||
) {
|
|
||||||
acceptTOS(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (shouldBlockRender) {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
active = false;
|
active = false;
|
||||||
};
|
};
|
||||||
}, [load, location.pathname, userId]);
|
}, [load, location.pathname]);
|
||||||
|
|
||||||
if (error !== "" && errorDescription !== "") {
|
if (error !== "" && errorDescription !== "") {
|
||||||
return <ErrorScreen error={error} description={errorDescription} />;
|
return <ErrorScreen error={error} description={errorDescription} />;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue