Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 13 additions & 36 deletions frontend/app/[locale]/(protected)/documents/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";

import { useDocumentStatusSocket } from "@/hooks/useDocumentStatusSocket";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { useParams } from "next/navigation";
import { useTranslations } from "next-intl";
Expand All @@ -20,7 +21,6 @@ import {
// Configuration
// ─────────────────────────────────────────────────────────────────────────────

const WS_BASE = (process.env.NEXT_PUBLIC_WS_URL ?? "ws://localhost:3001").replace(/^http/, "ws");

// ─────────────────────────────────────────────────────────────────────────────
// Types
Expand Down Expand Up @@ -277,34 +277,6 @@ export default function DocumentDetailPage() {
const [savingLocation, setSavingLocation] = useState(false);
const [locationError, setLocationError] = useState<string | null>(null);

// ── WebSocket for real-time status updates (FE-06) ──────────────────────
const wsRef = useRef<WebSocket | null>(null);

const setupWebSocket = useCallback(() => {
if (!docId || typeof window === "undefined") return;
const ws = new WebSocket(`${WS_BASE}/documents/${docId}/status`);
wsRef.current = ws;

ws.onmessage = (event) => {
try {
const payload = JSON.parse(event.data) as { status?: DocumentStatus };
if (payload.status) {
setDoc((prev) => prev ? { ...prev, status: payload.status as DocumentStatus } : prev);
}
} catch {
// malformed message — ignore
}
};

ws.onerror = () => ws.close();
ws.onclose = () => {
// Reconnect after 5 s (simple back-off)
setTimeout(setupWebSocket, 5000);
};

return () => ws.close();
}, [docId]);

// ── Fetch document + risk data ─────────────────────────────────────────
const fetchData = useCallback(async () => {
if (!docId) return;
Expand Down Expand Up @@ -336,13 +308,18 @@ export default function DocumentDetailPage() {
fetchData();
}, [fetchData]);

useEffect(() => {
const cleanup = setupWebSocket();
return () => {
cleanup?.();
wsRef.current?.close();
};
}, [setupWebSocket]);
// ── Live status updates over Socket.IO (#1643) ──────────────────────────
// The detail page updates without a reload when the backend moves the
// document through PENDING -> ANALYZING -> VERIFIED/FLAGGED, and refetches
// the risk data on every change.
useDocumentStatusSocket(docId, {
onStatus: (event) => {
setDoc((prev) =>
prev ? { ...prev, status: event.status.toUpperCase() as DocumentStatus } : prev,
);
void fetchData();
},
});

// ── Verify on Stellar ──────────────────────────────────────────────────
async function handleVerify() {
Expand Down
167 changes: 65 additions & 102 deletions frontend/app/[locale]/(protected)/documents/[id]/verify/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ import { useDateFormatting } from "@/i18n/formatting";
import { ApiError, request } from "@/lib/api-client";
import { apiUrl } from "@/lib/api-config";
import { stellarTxUrl } from "@/lib/stellar";

// ─────────────────────────────────────────────────────────────────────────────
// Configuration
// ─────────────────────────────────────────────────────────────────────────────

const WS_BASE = (
process.env.NEXT_PUBLIC_WS_URL ?? "ws://localhost:3001"
).replace(/^http/, "ws");
import { useDocumentStatusSocket } from "@/hooks/useDocumentStatusSocket";

// ─────────────────────────────────────────────────────────────────────────────
// Types
Expand Down Expand Up @@ -54,24 +47,6 @@ function truncateHash(hash: string, chars = 10): string {
// Sub-components
// ─────────────────────────────────────────────────────────────────────────────

/** Thin animated progress bar. */
function ProgressBar({ value }: { value: number }) {
return (
<div
className="h-2 w-full overflow-hidden rounded-full bg-gray-200"
role="progressbar"
aria-valuenow={value}
aria-valuemin={0}
aria-valuemax={100}
>
<div
className="h-full rounded-full bg-blue-500 transition-all duration-500"
style={{ width: `${Math.max(4, value)}%` }}
/>
</div>
);
}

/** Copyable text field. */
function CopyField({
label,
Expand Down Expand Up @@ -203,13 +178,7 @@ function PreVerification({
// Processing state
// ─────────────────────────────────────────────────────────────────────────────

function ProcessingView({
progress,
statusMsg,
}: {
progress: number;
statusMsg: string;
}) {
function ProcessingView({ statusMsg }: { statusMsg: string }) {
return (
<div className="flex flex-col items-center gap-6 py-8">
<svg
Expand Down Expand Up @@ -240,8 +209,13 @@ function ProcessingView({
>
{statusMsg}
</p>
<ProgressBar value={progress} />
<p className="text-center text-xs text-gray-400">{progress}%</p>
{/* Indeterminate: the job reports no percentage, so none is faked. */}
<div
role="progressbar"
aria-busy="true"
aria-label={statusMsg}
className="h-2 w-full animate-pulse rounded-full bg-blue-500/60"
/>
</div>
</div>
);
Expand Down Expand Up @@ -433,13 +407,13 @@ export default function VerifyPage() {
const [viewState, setViewState] = useState<VerifyState>("pre");
const [anchoring, setAnchoring] = useState(false);
const [anchorError, setAnchorError] = useState<string | null>(null);
const [progress, setProgress] = useState(0);
const [statusMsg, setStatusMsg] = useState(() => t("submitting"));
const [verification, setVerification] = useState<VerificationRecord | null>(
null,
);

const wsRef = useRef<WebSocket | null>(null);
const pollTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
const finished = useRef(false);

// ── Fetch document metadata ────────────────────────────────────────────
useEffect(() => {
Expand All @@ -464,34 +438,6 @@ export default function VerifyPage() {
.finally(() => setPageLoading(false));
}, [docId, router, t]);

// ── WebSocket progress handler ─────────────────────────────────────────
const connectProgressWs = useCallback(() => {
if (!docId || typeof window === "undefined") return;
const ws = new WebSocket(`${WS_BASE}/documents/${docId}/verify/progress`);
wsRef.current = ws;

ws.onmessage = (event) => {
try {
const payload = JSON.parse(event.data) as {
progress?: number;
message?: string;
status?: string;
};
if (payload.progress !== undefined) setProgress(payload.progress);
if (payload.message) setStatusMsg(payload.message);
if (payload.status === "VERIFIED") {
ws.close();
// Fetch final verification record
fetchVerification();
}
} catch {
// ignore malformed messages
}
};

ws.onerror = () => ws.close();
}, [docId]); // eslint-disable-line react-hooks/exhaustive-deps

// ── Fetch verification record after anchoring ──────────────────────────
const fetchVerification = useCallback(async () => {
if (!docId) return;
Expand All @@ -513,50 +459,64 @@ export default function VerifyPage() {
}, [docId, t]);

// ── Anchor handler ─────────────────────────────────────────────────────
// Completion comes from the Socket.IO event below; polling with backoff is
// only the fallback, so the page reflects the real job outcome however long
// anchoring takes.
const finishVerification = useCallback(async () => {
if (finished.current) return;
finished.current = true;
if (pollTimer.current) clearTimeout(pollTimer.current);
await fetchVerification();
}, [fetchVerification]);

const startPolling = useCallback(() => {
if (!docId) return;
let attempt = 0;
const MAX_ATTEMPTS = 12;
const poll = async () => {
if (finished.current) return;
attempt += 1;
try {
const data = await request<VerificationRecord>(
documentPath(docId, "/verification"),
);
if (data?.transactionHash) {
finished.current = true;
setVerification(data);
setViewState("post");
return;
}
} catch {
// Not anchored yet (404) or a transient error: keep waiting.
}
if (attempt >= MAX_ATTEMPTS) {
setStatusMsg(t("stillProcessing"));
return;
}
pollTimer.current = setTimeout(poll, Math.min(2000 * 2 ** (attempt - 1), 30000));
};
pollTimer.current = setTimeout(poll, 2000);
}, [docId, t]);

useDocumentStatusSocket(viewState === "processing" ? docId : null, {
onVerification: (event) => {
if (event.status === "confirmed" || event.status === "failed") {
void finishVerification();
}
},
});

async function handleAnchor() {
if (!docId) return;
setAnchoring(true);
setAnchorError(null);
setProgress(0);
setStatusMsg(t("submitting"));
finished.current = false;

try {
await request(documentPath(docId, "/verify"), { method: "POST" });

// Switch to processing view and open WS for real-time updates
setViewState("processing");
connectProgressWs();

// Simulate progress advancement when WS is unavailable
const interval = setInterval(() => {
setProgress((prev) => {
if (prev >= 90) {
clearInterval(interval);
return prev;
}
return prev + 10;
});
}, 800);

// Poll for completion (fallback when WS is not available)
const poll = async () => {
await new Promise((r) => setTimeout(r, 8000));
const data = await request<VerificationRecord>(
documentPath(docId, "/verification"),
);
clearInterval(interval);
setProgress(100);
setVerification(data);
setViewState("post");
wsRef.current?.close();
};

poll().catch(() => {
// WS/poll failed — move to post with partial info
clearInterval(interval);
setProgress(100);
fetchVerification();
});
startPolling();
} catch {
setAnchorError(t("anchorError"));
setAnchoring(false);
Expand All @@ -565,7 +525,10 @@ export default function VerifyPage() {

// ── Cleanup ────────────────────────────────────────────────────────────
useEffect(() => {
return () => wsRef.current?.close();
return () => {
finished.current = true;
if (pollTimer.current) clearTimeout(pollTimer.current);
};
}, []);

// ── Render ─────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -650,7 +613,7 @@ export default function VerifyPage() {
/>
)}
{viewState === "processing" && (
<ProcessingView progress={progress} statusMsg={statusMsg} />
<ProcessingView statusMsg={statusMsg} />
)}
{viewState === "post" && verification && (
<PostVerification docId={docId} verification={verification} />
Expand Down
Loading
Loading