Repository navigation
Expand file tree
/
Copy pathpage.tsx
More file actions
121 lines (104 loc) · 4.09 KB
/
Copy pathpage.tsx
File metadata and controls
121 lines (104 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"use client";
import { Suspense, useEffect } from "react";
import { useRouter, useSearchParams } from "next/navigation";
import Navbar from "@/components/Navbar";
import type { AnalysisResult, UserAnswer } from "@/lib/claim-analysis";
import { PROFILE_EMAIL_STORAGE_KEY } from "@/lib/profile";
function isAnalysisResult(payload: unknown): payload is AnalysisResult {
return Boolean(payload && typeof payload === "object" && "scanId" in payload && "claimOMeter" in payload);
}
function ProcessingContent() {
const router = useRouter();
const searchParams = useSearchParams();
const scanId = searchParams.get("scanId") ?? "";
useEffect(() => {
let isMounted = true;
const runAnalysis = async () => {
if (!scanId) {
router.replace("/");
return;
}
try {
const answersRaw = sessionStorage.getItem(`bitespy:answers:${scanId}`);
const answers = answersRaw ? (JSON.parse(answersRaw) as UserAnswer[]) : [];
const response = await fetch("/api/analyze", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ scanId, answers }),
});
const payload = (await response.json()) as AnalysisResult | { error?: string };
if (!response.ok || !isAnalysisResult(payload)) {
const message = "error" in payload && payload.error ? payload.error : "Could not build result report.";
throw new Error(message);
}
sessionStorage.setItem(`bitespy:result:${scanId}`, JSON.stringify(payload));
sessionStorage.setItem("bitespy:lastResultScanId", scanId);
try {
const meResponse = await fetch("/api/auth/me", { cache: "no-store" });
const mePayload = (await meResponse.json()) as { user?: { email?: string } | null };
const email = mePayload.user?.email || localStorage.getItem(PROFILE_EMAIL_STORAGE_KEY) || "";
if (email.trim()) {
const sourceRaw = sessionStorage.getItem("bitespy:lastScanSource");
const source = sourceRaw === "image" || sourceRaw === "name" ? sourceRaw : "unknown";
await fetch("/api/history", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email,
source,
result: payload,
}),
});
}
} catch {
// History persistence is best-effort and should not block result navigation.
}
if (isMounted) {
router.replace(`/result?scanId=${encodeURIComponent(scanId)}`);
}
} catch {
if (isMounted) {
router.replace(`/result?scanId=${encodeURIComponent(scanId)}&fallback=1`);
}
}
};
void runAnalysis();
return () => {
isMounted = false;
};
}, [router, scanId]);
return (
<main className="min-h-screen">
<Navbar />
<section className="mx-auto flex min-h-screen w-full max-w-3xl flex-col items-center justify-center px-6 pt-24">
<div className="h-20 w-20 animate-spin rounded-full border-4 border-cyan-200/40 border-t-cyan-200" />
<h1 className="mt-8 text-center text-3xl font-bold text-white">Crunching label truth data...</h1>
<p className="mt-3 text-center text-blue-100">
BiteSpy is checking false claims, harmful ingredients, and safer alternatives.
</p>
</section>
</main>
);
}
function ProcessingFallback() {
return (
<main className="min-h-screen">
<Navbar />
<section className="mx-auto flex min-h-screen w-full max-w-3xl flex-col items-center justify-center px-6 pt-24">
<div className="h-20 w-20 animate-spin rounded-full border-4 border-cyan-200/40 border-t-cyan-200" />
<h1 className="mt-8 text-center text-3xl font-bold text-white">Preparing your report...</h1>
</section>
</main>
);
}
export default function ProcessingPage() {
return (
<Suspense fallback={<ProcessingFallback />}>
<ProcessingContent />
</Suspense>
);
}