-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Expand file tree
/
Copy pathApp.tsx
More file actions
146 lines (129 loc) · 5.3 KB
/
Copy pathApp.tsx
File metadata and controls
146 lines (129 loc) · 5.3 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
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
import { useState, useEffect, useCallback, lazy, Suspense } from "react";
import { Navbar } from "./components/Navbar";
import { IdentityPanel } from "./components/IdentityPanel";
import { TokenManagerDialog } from "./components/TokenManagerDialog";
import { ThemeProvider } from "./lib/theme";
import { getUuid, setUuid, apiBind, setActiveApiToken } from "./api/client";
import { ACPDirectView } from "./components/ACPDirectView";
import { useTokens } from "./hooks/useTokens";
const Dashboard = lazy(() => import("./pages/Dashboard").then((m) => ({ default: m.Dashboard })));
const SessionDetail = lazy(() => import("./pages/SessionDetail").then((m) => ({ default: m.SessionDetail })));
export default function App() {
const [currentSessionId, setCurrentSessionId] = useState<string | null>(null);
const [identityOpen, setIdentityOpen] = useState(false);
const [tokenDialogOpen, setTokenDialogOpen] = useState(false);
const [acpDirect, setAcpDirect] = useState<{ url: string; token: string } | null>(null);
const { tokens, activeTokenId, activeLabel, activeTokenValue, setActiveTokenId, addToken, removeToken, updateToken } = useTokens();
// Sync active token to API client
useEffect(() => {
setActiveApiToken(activeTokenValue);
}, [activeTokenValue]);
const handleSetActiveToken = useCallback((id: string) => {
setActiveTokenId(id);
}, [setActiveTokenId]);
// Simple hash-based router
const parseRoute = useCallback(() => {
// Ensure UUID exists
getUuid();
const path = window.location.pathname;
// Check for UUID import from QR scan (?uuid=xxx)
const params = new URLSearchParams(window.location.search);
const importUuid = params.get("uuid");
if (importUuid) {
setUuid(importUuid);
const url = new URL(window.location.href);
url.searchParams.delete("uuid");
window.history.replaceState(null, "", url);
}
// Check for ACP direct connection (?acp=1)
const acpParam = params.get("acp");
if (acpParam === "1") {
const stored = sessionStorage.getItem("acp_connection");
if (stored) {
try {
const acpData = JSON.parse(stored);
if (acpData.url && acpData.token) {
setAcpDirect({ url: acpData.url, token: acpData.token });
sessionStorage.removeItem("acp_connection");
// Clean URL
const url = new URL(window.location.href);
url.searchParams.delete("acp");
window.history.replaceState(null, "", url);
return;
}
} catch {
sessionStorage.removeItem("acp_connection");
}
}
}
// Check for CLI session bind (?sid=xxx) — bind session to current UUID
const sid = params.get("sid");
if (sid) {
const url = new URL(window.location.href);
url.searchParams.delete("sid");
window.history.replaceState(null, "", `/code/${sid}`);
setCurrentSessionId(sid);
// Bind this session to the current user's UUID for ownership
apiBind(sid).catch((err: unknown) => {
console.warn("Failed to bind session:", err);
});
return;
}
// Path-based routing: /code/session_xxx → session detail
const match = path.match(/^\/code\/([^/]+)/);
if (match && match[1]) {
setCurrentSessionId(match[1]);
} else {
setCurrentSessionId(null);
}
}, []);
useEffect(() => {
parseRoute();
window.addEventListener("popstate", parseRoute);
return () => window.removeEventListener("popstate", parseRoute);
}, [parseRoute]);
const navigateToSession = useCallback((sessionId: string) => {
window.history.pushState(null, "", `/code/${sessionId}`);
setCurrentSessionId(sessionId);
}, []);
const navigateToDashboard = useCallback(() => {
window.history.pushState(null, "", "/code/");
setCurrentSessionId(null);
setAcpDirect(null);
}, []);
return (
<ThemeProvider defaultTheme="system">
<div className="flex h-screen flex-col bg-surface-0 text-text-primary">
<Navbar
onIdentityClick={() => setIdentityOpen(true)}
onTokenClick={() => setTokenDialogOpen(true)}
activeTokenLabel={currentSessionId ? undefined : activeLabel}
sessionTitle={currentSessionId || (acpDirect ? "ACP" : undefined)}
onBack={(currentSessionId || acpDirect) ? navigateToDashboard : undefined}
/>
<Suspense fallback={<div className="flex flex-1 items-center justify-center text-text-muted">Loading...</div>}>
{acpDirect ? (
<ACPDirectView url={acpDirect.url} token={acpDirect.token} onBack={navigateToDashboard} />
) : currentSessionId ? (
<SessionDetail key={currentSessionId} sessionId={currentSessionId} />
) : (
<div className="flex-1 overflow-y-auto">
<Dashboard onNavigateSession={navigateToSession} />
</div>
)}
</Suspense>
<IdentityPanel open={identityOpen} onClose={() => setIdentityOpen(false)} />
<TokenManagerDialog
open={tokenDialogOpen}
onClose={() => setTokenDialogOpen(false)}
tokens={tokens}
activeTokenId={activeTokenId}
onSetActive={handleSetActiveToken}
onAdd={addToken}
onRemove={removeToken}
onUpdate={updateToken}
/>
</div>
</ThemeProvider>
);
}