front: pass ?preview=all through API calls#21
Conversation
New previewParam() helper in src/api/client.ts reads window.location.search live per request and returns 'all' iff the URL has ?preview=all. Not cached — SPA navigation to/from the preview URL flips visibility immediately. SSR-safe via typeof window check. fetchEvents, fetchNetworks, fetchStats now include ?preview=all in their outbound requests when the caller's URL carries it. Other endpoints (validator profile, leaderboard, insights) are reachable directly and don't list gated networks; leaving them unmodified keeps the PR tight. No new frontend state. No route changes. Users hitting the site without the query param see exactly what they saw before (five networks post-Celestia-gating). Internal users with ?preview=all see all enabled networks.
There was a problem hiding this comment.
Code Review
This pull request introduces a previewParam helper to support gated networks by appending a preview query parameter to API requests in fetchEvents, fetchNetworks, and fetchStats. The review feedback highlights that this parameter should be applied to all relevant API functions for full coverage and notes that React hooks need to be updated to trigger re-fetches upon URL changes. Furthermore, using URLSearchParams is recommended for consistency and better maintainability compared to manual string concatenation.
| function previewParam(): string | null { | ||
| if (typeof window === 'undefined') return null; | ||
| const p = new URLSearchParams(window.location.search).get('preview'); | ||
| return p === 'all' ? 'all' : null; | ||
| } |
There was a problem hiding this comment.
The previewParam helper is a good addition for handling gated content, but there are two significant issues with the current implementation:
- Incomplete Coverage: This parameter is only being passed in
fetchEvents,fetchNetworks, andfetchStats. However, many other functions in this file (such asfetchValidator,fetchLeaderboard,fetchChainData,fetchDelegations, andfetchHealthCheck) likely also need to support this parameter to ensure that gated networks are visible and functional across all views (e.g., when viewing a specific validator's profile on a gated network). - SPA Navigation & React Hooks: The PR description states that visibility flips "immediately" during SPA navigation. While the API client reads the URL live, the corresponding hooks (like
useNetworksanduseStatsinsrc/hooks/) do not include the URL search parameters in their dependency arrays. Consequently, navigating to or from a?preview=allURL within the SPA will not trigger a re-fetch, and the UI will not update until a manual refresh or a different state change occurs.
Consider applying this parameter to all relevant API calls and updating the hooks to react to URL search parameter changes.
| const pv = previewParam(); | ||
| const suffix = pv ? `?preview=${pv}` : ''; | ||
| const res = await fetch(`${BASE_URL}/v1/networks${suffix}`); |
There was a problem hiding this comment.
For better maintainability and consistency with the approach used in fetchEvents, consider using URLSearchParams to construct the query string instead of manual string concatenation.
| const pv = previewParam(); | |
| const suffix = pv ? `?preview=${pv}` : ''; | |
| const res = await fetch(`${BASE_URL}/v1/networks${suffix}`); | |
| const qs = new URLSearchParams(); | |
| const pv = previewParam(); | |
| if (pv) qs.set('preview', pv); | |
| const query = qs.toString(); | |
| const res = await fetch(`${BASE_URL}/v1/networks${query ? `?${query}` : ''}`); |
| const pv = previewParam(); | ||
| const suffix = pv ? `?preview=${pv}` : ''; | ||
| const res = await fetch(`${BASE_URL}/v1/stats${suffix}`); |
There was a problem hiding this comment.
For better maintainability and consistency with the approach used in fetchEvents, consider using URLSearchParams to construct the query string instead of manual string concatenation.
| const pv = previewParam(); | |
| const suffix = pv ? `?preview=${pv}` : ''; | |
| const res = await fetch(`${BASE_URL}/v1/stats${suffix}`); | |
| const qs = new URLSearchParams(); | |
| const pv = previewParam(); | |
| if (pv) qs.set('preview', pv); | |
| const query = qs.toString(); | |
| const res = await fetch(`${BASE_URL}/v1/stats${query ? `?${query}` : ''}`); |
Frontend side of TIA drip-feed gating. Reads
?preview=allfrom the current URL and forwards it on outbound API requests.Implementation
New
previewParam()helper insrc/api/client.ts:window.location.searchlive (per request — SPA navigation flips visibility immediately)'all'iff the URL has?preview=all, elsenulltypeof windowcheck)fetchEvents,fetchNetworks,fetchStatsnow include the param when present.Depends on
slasher-infra/18 + slasher-api PR. Merge order: infra → api → front.
Test plan
npm run build— zero TS errorsslashr.devshows 5 networks (no celestia),slashr.dev/?preview=allshows 6