|
1 | | -import { useCallback, useEffect, useMemo, useRef } from "react"; |
2 | | -import { |
3 | | - useLocation, |
4 | | - useNavigate, |
5 | | - useParams, |
6 | | - useSearchParams, |
7 | | -} from "react-router-dom"; |
| 1 | +import { useEffect } from "react"; |
8 | 2 |
|
9 | 3 | import { ProbeViewer } from "./components/ProbeViewer"; |
10 | 4 | import { Sidebar } from "./components/Sidebar"; |
11 | 5 | import { useAppStore } from "./state/useAppStore"; |
| 6 | +import { useProbeRouteSync } from "./state/useProbeRouteSync"; |
| 7 | +import { useRestoreCameraFromUrl } from "./state/useRestoreCameraFromUrl"; |
| 8 | +import { useSyncCameraToUrl } from "./state/useSyncCameraToUrl"; |
12 | 9 | import "./App.css"; |
13 | 10 |
|
14 | | -const DEFAULT_PROBE_ID = "plexon:8S1024"; |
15 | | - |
16 | | -function roundForUrl(value: number, decimals = 1): number { |
17 | | - const factor = Math.pow(10, decimals); |
18 | | - return Math.round(value * factor) / factor; |
19 | | -} |
20 | | - |
21 | 11 | function App() { |
22 | | - const { manufacturer, model } = useParams(); |
23 | | - const location = useLocation(); |
24 | | - const navigate = useNavigate(); |
25 | | - const [searchParams, setSearchParams] = useSearchParams(); |
26 | | - |
27 | | - const manifestStatus = useAppStore((state) => state.manifestStatus); |
28 | | - const manifest = useAppStore((state) => state.manifest); |
29 | | - const selectedProbeId = useAppStore((state) => state.selectedProbeId); |
30 | 12 | const loadManifest = useAppStore((state) => state.loadManifest); |
31 | | - const selectProbe = useAppStore((state) => state.selectProbe); |
32 | | - |
33 | | - const view = useAppStore((state) => state.view); |
34 | | - const setZoom = useAppStore((state) => state.setZoom); |
35 | | - const setViewCenter = useAppStore((state) => state.setViewCenter); |
36 | 13 |
|
37 | 14 | useEffect(() => { |
38 | 15 | void loadManifest(); |
39 | 16 | }, [loadManifest]); |
40 | 17 |
|
41 | | - // Track whether we've initialized from URL to avoid overwriting on first render |
42 | | - const initializedFromUrl = useRef(false); |
43 | | - |
44 | | - // Read view params from URL on initial load |
45 | | - useEffect(() => { |
46 | | - if (initializedFromUrl.current) return; |
47 | | - initializedFromUrl.current = true; |
48 | | - |
49 | | - const zoomParam = searchParams.get("zoom"); |
50 | | - const cxParam = searchParams.get("cx"); |
51 | | - const cyParam = searchParams.get("cy"); |
52 | | - |
53 | | - if (zoomParam) { |
54 | | - const zoom = parseFloat(zoomParam); |
55 | | - if (!isNaN(zoom)) setZoom(zoom); |
56 | | - } |
57 | | - if (cxParam && cyParam) { |
58 | | - const cx = parseFloat(cxParam); |
59 | | - const cy = parseFloat(cyParam); |
60 | | - if (!isNaN(cx) && !isNaN(cy)) setViewCenter(cx, cy); |
61 | | - } |
62 | | - }, [searchParams, setZoom, setViewCenter]); |
63 | | - |
64 | | - // Debounced URL update when view state changes |
65 | | - const updateUrlTimeout = useRef<ReturnType<typeof setTimeout> | undefined>(undefined); |
66 | | - const updateSearchParams = useCallback(() => { |
67 | | - const { zoom, viewCenterX, viewCenterY } = view; |
68 | | - const isDefault = zoom === 1 && viewCenterX === null && viewCenterY === null; |
69 | | - |
70 | | - setSearchParams((prev) => { |
71 | | - const next = new URLSearchParams(prev); |
72 | | - if (isDefault) { |
73 | | - next.delete("zoom"); |
74 | | - next.delete("cx"); |
75 | | - next.delete("cy"); |
76 | | - } else { |
77 | | - next.set("zoom", String(roundForUrl(zoom, 2))); |
78 | | - if (viewCenterX !== null && viewCenterY !== null) { |
79 | | - next.set("cx", String(roundForUrl(viewCenterX, 1))); |
80 | | - next.set("cy", String(roundForUrl(viewCenterY, 1))); |
81 | | - } else { |
82 | | - next.delete("cx"); |
83 | | - next.delete("cy"); |
84 | | - } |
85 | | - } |
86 | | - return next; |
87 | | - }, { replace: true }); |
88 | | - }, [view, setSearchParams]); |
89 | | - |
90 | | - useEffect(() => { |
91 | | - if (!initializedFromUrl.current) return; |
92 | | - |
93 | | - clearTimeout(updateUrlTimeout.current); |
94 | | - updateUrlTimeout.current = setTimeout(updateSearchParams, 300); |
95 | | - |
96 | | - return () => clearTimeout(updateUrlTimeout.current); |
97 | | - }, [view.zoom, view.viewCenterX, view.viewCenterY, updateSearchParams]); |
98 | | - |
99 | | - const manifestById = useMemo(() => { |
100 | | - const map = new Map<string, typeof manifest[number]>(); |
101 | | - manifest.forEach((entry) => map.set(entry.id, entry)); |
102 | | - return map; |
103 | | - }, [manifest]); |
104 | | - |
105 | | - useEffect(() => { |
106 | | - if (manifestStatus !== "success" || manifest.length === 0) { |
107 | | - return; |
108 | | - } |
109 | | - |
110 | | - const routeId = |
111 | | - manufacturer && model ? `${manufacturer}:${model}` : undefined; |
112 | | - const routeEntry = routeId ? manifestById.get(routeId) : undefined; |
113 | | - const currentSelected = selectedProbeId |
114 | | - ? manifestById.get(selectedProbeId) |
115 | | - : undefined; |
116 | | - |
117 | | - const getDefaultProbe = () => |
118 | | - manifestById.get(DEFAULT_PROBE_ID) ?? manifest[0]; |
119 | | - |
120 | | - if (selectedProbeId && !currentSelected) { |
121 | | - const fallback = routeEntry ?? getDefaultProbe(); |
122 | | - if (fallback && fallback.id !== selectedProbeId) { |
123 | | - selectProbe(fallback.id); |
124 | | - } |
125 | | - return; |
126 | | - } |
127 | | - |
128 | | - if (!selectedProbeId) { |
129 | | - if (routeEntry) { |
130 | | - selectProbe(routeEntry.id); |
131 | | - } else { |
132 | | - const fallback = getDefaultProbe(); |
133 | | - if (fallback) { |
134 | | - selectProbe(fallback.id); |
135 | | - } |
136 | | - } |
137 | | - } |
138 | | - }, [ |
139 | | - manifestStatus, |
140 | | - manifest, |
141 | | - manifestById, |
142 | | - manufacturer, |
143 | | - model, |
144 | | - selectedProbeId, |
145 | | - selectProbe, |
146 | | - ]); |
147 | | - |
148 | | - useEffect(() => { |
149 | | - if ( |
150 | | - manifestStatus !== "success" || |
151 | | - !selectedProbeId || |
152 | | - manifest.length === 0 |
153 | | - ) { |
154 | | - return; |
155 | | - } |
156 | | - |
157 | | - const selectedEntry = manifestById.get(selectedProbeId); |
158 | | - if (!selectedEntry) { |
159 | | - return; |
160 | | - } |
161 | | - |
162 | | - const routeId = |
163 | | - manufacturer && model ? `${manufacturer}:${model}` : undefined; |
164 | | - if (routeId === selectedEntry.id) { |
165 | | - return; |
166 | | - } |
| 18 | + // Selected probe <-> URL path (/probes/:manufacturer/:model). |
| 19 | + useProbeRouteSync(); |
167 | 20 |
|
168 | | - const targetPath = `/probes/${selectedEntry.manufacturer}/${selectedEntry.model}`; |
169 | | - const replace = location.pathname === "/"; |
170 | | - navigate(targetPath, { replace }); |
171 | | - }, [ |
172 | | - manifestStatus, |
173 | | - selectedProbeId, |
174 | | - manifestById, |
175 | | - manufacturer, |
176 | | - model, |
177 | | - navigate, |
178 | | - location.pathname, |
179 | | - manifest.length, |
180 | | - ]); |
| 21 | + // Camera <-> URL query string: restore from a shared link on load, then keep |
| 22 | + // the URL updated as the user zooms/pans. Coordinated via the store's |
| 23 | + // `cameraInitialized` flag so the writer can't clobber the link at mount. |
| 24 | + useRestoreCameraFromUrl(); |
| 25 | + useSyncCameraToUrl(); |
181 | 26 |
|
182 | 27 | return ( |
183 | 28 | <div className="app-shell"> |
|
0 commit comments