Spaces:
Sleeping
Sleeping
File size: 7,031 Bytes
fc5156b 165a274 2695640 375d687 2695640 375d687 f22b512 165a274 fc5156b 375d687 f22b512 375d687 fc5156b 90adb95 fc5156b 375d687 fc5156b 375d687 2695640 165a274 2695640 fc5156b 90adb95 165a274 90adb95 165a274 90adb95 165a274 fc5156b 375d687 fc5156b 375d687 fc5156b 90adb95 fc5156b 165a274 fc5156b 165a274 90adb95 165a274 90adb95 165a274 fc5156b 375d687 165a274 90adb95 165a274 90adb95 165a274 375d687 fc5156b 375d687 a38a802 165a274 a38a802 fc5156b 375d687 2695640 90adb95 f22b512 90adb95 2695640 fc5156b 2695640 375d687 fc5156b 375d687 2695640 375d687 a38a802 375d687 fc5156b f22b512 375d687 fc5156b | 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | import { useCallback, useEffect, useState } from "react";
import { api, setOnUnauthorized, tokenStore } from "./api";
import { IconSpark } from "./lib/icons";
import Sidebar from "./components/Sidebar";
import Button from "./components/ui/Button";
import { ErrorState } from "./components/ui/States";
import Overview from "./components/views/Overview";
import Ask from "./components/views/Ask";
import Evaluation from "./components/views/Evaluation";
import Audit from "./components/views/Audit";
import AuthScreen from "./components/views/AuthScreen";
const HEADINGS = {
ask: { title: "Ask", sub: "Questions are answered only from retrieved evidence." },
audit: { title: "Audit", sub: "Evidence-backed checks with citations you can verify." },
evaluation: { title: "Evaluation", sub: "How well the assistant stays grounded." },
};
function randHex(bytes) {
const a = new Uint8Array(bytes);
crypto.getRandomValues(a);
return Array.from(a, (b) => b.toString(16).padStart(2, "0")).join("");
}
export default function App() {
const [view, setView] = useState("overview");
const [documents, setDocuments] = useState([]);
const [loading, setLoading] = useState(true);
const [health, setHealth] = useState(null);
const [online, setOnline] = useState(true);
const [seeding, setSeeding] = useState(false);
const [demoSignal, setDemoSignal] = useState(0);
const [scrolled, setScrolled] = useState(false);
// ---- Auth state ----
const [user, setUser] = useState(null);
const [authReady, setAuthReady] = useState(false);
const [authError, setAuthError] = useState("");
const [authBusy, setAuthBusy] = useState(false);
useEffect(() => {
const onScroll = () => setScrolled(window.scrollY > 60);
window.addEventListener("scroll", onScroll, { passive: true });
return () => window.removeEventListener("scroll", onScroll);
}, []);
// Mount: load public health (so the auth screen knows if Google is enabled) and
// validate any stored token. A 401 later surfaces a friendly expiry message.
useEffect(() => {
setOnUnauthorized((msg) => {
setAuthError(msg || "");
setUser(null);
});
api
.health()
.then((h) => {
setHealth(h);
setOnline(true);
})
.catch(() => setOnline(false));
if (!tokenStore.get()) {
setAuthReady(true);
return;
}
api
.me()
.then(setUser)
.catch(() => {})
.finally(() => setAuthReady(true));
}, []);
const reload = useCallback(async () => {
setLoading(true);
try {
setDocuments(await api.listDocuments());
setOnline(true);
} catch (_) {
setDocuments([]);
setOnline(false);
} finally {
setLoading(false);
}
}, []);
// Load documents once authenticated.
useEffect(() => {
if (!user) return;
reload();
}, [reload, user]);
async function handleAuth(fn, creds) {
setAuthError("");
setAuthBusy(true);
try {
const res = await fn(creds);
setUser(res.user);
} catch (err) {
setAuthError(err.message);
} finally {
setAuthBusy(false);
}
}
function handleGoogle(credential) {
return handleAuth(() => api.googleLogin(credential));
}
// Guest path: a throwaway account with CSPRNG creds (no predictable secrets).
async function continueAsGuest() {
const res = await api.register({
email: `guest_${randHex(8)}@financeflow-guest.com`,
password: randHex(24),
});
setUser(res.user);
}
async function onDemo() {
setAuthError("");
setAuthBusy(true);
try {
await continueAsGuest();
await onTryDemo();
} catch (err) {
setAuthError(err.message);
} finally {
setAuthBusy(false);
}
}
function onLogout() {
api.logout();
setUser(null);
setDocuments([]);
setView("overview");
}
// "Try Demo": seed the sample documents, jump to Ask, auto-run a showcase Q.
async function onTryDemo() {
setSeeding(true);
try {
await api.seedDemo();
await reload();
setView("ask");
setDemoSignal((n) => n + 1);
} catch (_) {
setOnline(false);
} finally {
setSeeding(false);
}
}
// ---- Gate: validating -> nothing; logged out -> auth screen ----
if (!authReady) return null;
if (!user) {
return (
<AuthScreen
loading={authBusy}
serverError={authError}
googleClientId={health?.google_client_id}
onSignIn={(c) => handleAuth(api.login, c)}
onSignUp={(c) => handleAuth(api.register, c)}
onGoogle={handleGoogle}
onDemo={onDemo}
/>
);
}
const head = HEADINGS[view];
return (
<div className="app-shell">
<Sidebar
view={view}
setView={setView}
online={online}
llmAvailable={health?.llm_available}
userEmail={user.email}
onLogout={onLogout}
/>
<main className="main">
<div className={`topbar ${scrolled ? "show" : ""}`}>
<span className="topbar-brand">
<span
style={{
width: 22, height: 22, borderRadius: 7,
background: "var(--accent-grad)", display: "inline-grid", placeItems: "center",
}}
>
<IconSpark size={13} />
</span>
FinanceFlow AI
</span>
<nav className="topbar-nav">
<button type="button" onClick={() => setView("overview")}>Overview</button>
<button type="button" onClick={() => setView("ask")}>Ask</button>
<button type="button" onClick={() => setView("audit")}>Audit</button>
<button type="button" onClick={() => setView("evaluation")}>Evaluation</button>
</nav>
<Button variant="primary" size="sm" loading={seeding} onClick={onTryDemo}>
Try demo
</Button>
</div>
{view !== "overview" && (
<div className="view-head">
<h1>{head.title}</h1>
<p>{head.sub}</p>
</div>
)}
{!online && (
<div style={{ marginBottom: 20 }}>
<ErrorState>
Can’t reach the API. Start the backend with{" "}
<code>uvicorn app.main:app --port 8000</code> and refresh.
</ErrorState>
</div>
)}
{view === "overview" && (
<Overview
documents={documents}
loading={loading}
reload={reload}
onTryDemo={onTryDemo}
seeding={seeding}
onOpenEval={() => setView("evaluation")}
/>
)}
{view === "ask" && (
<Ask
documents={documents}
demoSignal={demoSignal}
llmAvailable={health?.llm_available}
onDemoConsumed={() => {}}
/>
)}
{view === "audit" && <Audit documents={documents} />}
{view === "evaluation" && <Evaluation />}
</main>
</div>
);
}
|