|
1 | 1 | import { useCallback, useEffect, useMemo, useState } from "react"; |
2 | | -import { useNavigate } from "react-router-dom"; |
| 2 | +import { useLocation, useNavigate } from "react-router-dom"; |
3 | 3 | import Debugger from "./components/Debugger"; |
4 | 4 | import Trigger from "./components/Trigger"; |
5 | 5 | import { SPOTLIGHT_OPEN_CLASS_NAME } from "./constants"; |
6 | 6 | import type { Integration, IntegrationData } from "./integrations/integration"; |
| 7 | +import { getPanelsFromIntegrations } from "./integrations/utils/extractPanelsFromIntegrations"; |
7 | 8 | import { base64Decode } from "./lib/base64"; |
8 | 9 | import * as db from "./lib/db"; |
9 | 10 | import { getSpotlightEventTarget } from "./lib/eventTarget"; |
10 | 11 | import { log } from "./lib/logger"; |
11 | 12 | import useKeyPress from "./lib/useKeyPress"; |
| 13 | +import { getRouteStorageKey } from "./overlay/utils/routePersistence"; |
12 | 14 | import { connectToSidecar } from "./sidecar"; |
13 | 15 | import type { NotificationCount, SpotlightOverlayOptions } from "./types"; |
14 | 16 |
|
@@ -164,6 +166,16 @@ export default function App({ |
164 | 166 | // as our <Route>s are scattered around a bit |
165 | 167 | // See https://github.com/remix-run/react-router/issues/7634 |
166 | 168 | const navigate = useNavigate(); |
| 169 | + const location = useLocation(); |
| 170 | + |
| 171 | + // contextId for namespacing of routes in sessionStorage |
| 172 | + const contextId = sidecarUrl; |
| 173 | + |
| 174 | + // helper to get valid panel routes |
| 175 | + const getValidRoutes = useCallback(() => { |
| 176 | + return new Set(getPanelsFromIntegrations(integrations, integrationData).map(panel => `/${panel.id}`)); |
| 177 | + }, [integrationData, integrations]); |
| 178 | + |
167 | 179 | const clearEvents = useCallback(async () => { |
168 | 180 | try { |
169 | 181 | const clearEventsUrl: string = new URL("/clear", sidecarUrl).href; |
@@ -192,9 +204,24 @@ export default function App({ |
192 | 204 | ) => { |
193 | 205 | log("Open"); |
194 | 206 | setOpen(true); |
195 | | - if (e.detail.path) navigate(e.detail.path); |
| 207 | + if (e.detail.path) { |
| 208 | + navigate(e.detail.path); |
| 209 | + } else { |
| 210 | + try { |
| 211 | + const lastRoute = sessionStorage.getItem(getRouteStorageKey(contextId)); |
| 212 | + const validRoutes = getValidRoutes(); |
| 213 | + if (lastRoute && lastRoute !== location.pathname && validRoutes.has(lastRoute)) { |
| 214 | + navigate(lastRoute); |
| 215 | + } |
| 216 | + } catch (error) { |
| 217 | + log("Failed to retrieve or navigate to the last route from browser storage", { |
| 218 | + error, |
| 219 | + currentPath: location.pathname, |
| 220 | + }); |
| 221 | + } |
| 222 | + } |
196 | 223 | }, |
197 | | - [navigate], |
| 224 | + [navigate, location.pathname, contextId, integrationData, integrations], |
198 | 225 | ); |
199 | 226 |
|
200 | 227 | const onClose = useCallback(() => { |
@@ -275,6 +302,7 @@ export default function App({ |
275 | 302 | setTriggerButtonCount={setTriggerButtonCount} |
276 | 303 | fullPage={fullPage} |
277 | 304 | showClearEventsButton={showClearEventsButton} |
| 305 | + contextId={contextId} |
278 | 306 | /> |
279 | 307 | </> |
280 | 308 | ); |
|
0 commit comments