[PR #4541] [MERGED] feat(frontend): TanStack Query + React Router migration & in-panel API docs #5714

Closed
opened 2026-11-02 04:03:32 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/4541
Author: @MHSanaei
Created: 5/24/2026
Status: ✅ Merged
Merged: 5/24/2026
Merged by: @MHSanaei

Base: main ← Head: feat/tanstack-query-react-router


📝 Commits (10+)

  • 538473b feat(frontend): introduce TanStack Query with status polling
  • d9def73 feat(frontend): collapse panel pages into a single React Router SPA
  • dff509b feat(frontend): migrate useNodes to TanStack Query
  • bbb7af6 feat(frontend): migrate useAllSetting to TanStack Query
  • 8643154 feat(frontend): route useInbounds fetches through TanStack Query
  • 967b9ab feat(frontend): migrate useClients to TanStack Query
  • 6a6f44c feat(frontend): route useXraySetting fetches through TanStack Query
  • 77099f9 fix(frontend): restore per-route document titles in the SPA
  • 47ef765 feat(api-docs): expose OpenAPI spec + render Swagger UI in panel
  • d67eb49 style(api-docs): dark/ultra theme for Swagger UI

📊 Changes

60 files changed (+8950 additions, -2348 deletions)

View changed files

➖ frontend/api-docs.html (+0 -13)
➖ frontend/clients.html (+0 -13)
➖ frontend/inbounds.html (+0 -13)
📝 frontend/index.html (+2 -2)
➖ frontend/nodes.html (+0 -13)
📝 frontend/package-lock.json (+2345 -263)
📝 frontend/package.json (+9 -3)
➕ frontend/public/openapi.json (+4944 -0)
➕ frontend/scripts/build-openapi.mjs (+218 -0)
➖ frontend/settings.html (+0 -13)
➕ frontend/src/api/QueryProvider.tsx (+16 -0)
➕ frontend/src/api/queries/useAllSettings.ts (+67 -0)
➕ frontend/src/api/queries/useNodeMutations.ts (+63 -0)
➕ frontend/src/api/queries/useNodesQuery.ts (+108 -0)
➕ frontend/src/api/queries/useStatusQuery.ts (+33 -0)
➕ frontend/src/api/queryKeys.ts (+30 -0)
➕ frontend/src/api/websocketBridge.ts (+77 -0)
📝 frontend/src/components/AppSidebar.tsx (+24 -28)
➖ frontend/src/entries/clients.tsx (+0 -28)
➖ frontend/src/entries/inbounds.tsx (+0 -28)

...and 40 more files

📄 Description

Summary

Migrates the panel frontend from a 9-entry MPA with hand-rolled useState/useEffect hooks to a single React Router SPA backed by TanStack Query, and replaces the API-docs page with a Swagger UI rendering an auto-generated OpenAPI 3 spec.

Phase 1 — TanStack Query foundation

  • QueryClient singleton with sensible defaults (staleTime 30s, refetchOnWindowFocus, retry 1) + dev-only DevTools
  • Centralized query-key factory at frontend/src/api/queryKeys.ts
  • useStatusQuery as the proof-of-concept (2s polling)

Phase 2 — Multi-entry → SPA

  • 9 HTML entries collapsed to 3: index.html, login.html, subpage.html
  • Declarative createBrowserRouter with lazy-loaded page chunks
  • Backend /panel/* collapses to one catch-all serving index.html
  • Single WebSocket bridge mounted in PanelLayout dispatches to queryClient (replaces per-page useWebSocket glue)
  • AppSidebar uses <Link> / useNavigate — no more full-page reloads between panel routes

Phase 3 — Hook migrations (one commit per resource)

  • 3a Nodes — useNodesQuery + useNodeMutations
  • 3b Settings — useAllSettingsQuery + save mutation; draft state stays local
  • 3c Inbounds — slim list + detail queries; WS bridge handles invalidation
  • 3d Clients — paged query w/ keepPreviousData + 9 mutations; in-place WS merges preserved

Phase 4 — Xray (partial)

Editor state and 1s dirty-check polling kept; only network calls (config, save, test-outbound, reset, restart, default-config, outbounds traffic) moved to Query.

Phase 5 — Per-route document titles

SPA collapse broke per-page <title> — added usePageTitle hook with a route→title map, mounted in PanelLayout.

Phase 6 — In-panel API docs

  • frontend/scripts/build-openapi.mjs converts the existing endpoints.js into an OpenAPI 3.0.3 spec (106 paths, 12 tags, Bearer + cookie security)
  • Spec rebuilt on every `npm run build` and served by Go from `/panel/api/openapi.json`
  • ApiDocsPage renders swagger-ui-react against that URL — gives readable docs + Try-it-out tester + Postman-compatible spec, replacing the hand-written endpoint cards

Phase 7 — Dark/ultra theme for Swagger UI

Swagger ships no dark theme, so every visual surface gets an override scoped to .api-docs-page.is-dark / .is-ultra: opblocks, tables, model boxes, form inputs, code blocks, modals, Servers dropdown (with a custom light-fill SVG arrow), per-endpoint padlocks and expand chevrons. Method chips kept their semantic colors.

Test plan

  • `npm run lint && npm run typecheck && npm run build` clean
  • Navigate every sidebar route with no full-page reloads (Network tab → no document requests after first)
  • Refresh in the middle of any route (e.g. `/panel/xray`) → SPA re-renders correct page
  • 401 (force-expire session) redirects to login as before
  • Edit a client → modal closes → list updates with one PUT + one paged GET
  • WebSocket: trigger a traffic event → onlines indicator updates without per-page handler
  • React Query DevTools shows every expected query key
  • `/panel/api-docs` renders Swagger UI; toggle Light → Dark → Ultra and verify all surfaces readable (Servers dropdown, lock icons, Execute/Authorize/Cancel buttons)
  • Try-it-out on any endpoint succeeds and uses the current session
  • Per-route `
## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/4541 **Author:** [@MHSanaei](https://github.com/MHSanaei) **Created:** 5/24/2026 **Status:** ✅ Merged **Merged:** 5/24/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `feat/tanstack-query-react-router` --- ### 📝 Commits (10+) - [`538473b`](https://github.com/MHSanaei/3x-ui/commit/538473b2cc35e49ae1fe0b131ab97c8c49a149e2) feat(frontend): introduce TanStack Query with status polling - [`d9def73`](https://github.com/MHSanaei/3x-ui/commit/d9def73ee50f05e1e85e7f0f28c3011fc13c553c) feat(frontend): collapse panel pages into a single React Router SPA - [`dff509b`](https://github.com/MHSanaei/3x-ui/commit/dff509b394660be5b89190984eae68bc73a9938e) feat(frontend): migrate useNodes to TanStack Query - [`bbb7af6`](https://github.com/MHSanaei/3x-ui/commit/bbb7af65f6c3de9fa764a46ed1e956fb9f5149dc) feat(frontend): migrate useAllSetting to TanStack Query - [`8643154`](https://github.com/MHSanaei/3x-ui/commit/864315448eeced124d20736b9669ebf45e0da8e2) feat(frontend): route useInbounds fetches through TanStack Query - [`967b9ab`](https://github.com/MHSanaei/3x-ui/commit/967b9aba4b5b183bdf2ef6256544758a2f5fdca7) feat(frontend): migrate useClients to TanStack Query - [`6a6f44c`](https://github.com/MHSanaei/3x-ui/commit/6a6f44c884842be19401258a7d9dd630089f7f8a) feat(frontend): route useXraySetting fetches through TanStack Query - [`77099f9`](https://github.com/MHSanaei/3x-ui/commit/77099f91e8a68a4b9070bc858336ae479df6d991) fix(frontend): restore per-route document titles in the SPA - [`47ef765`](https://github.com/MHSanaei/3x-ui/commit/47ef765c1d122a52faf237de3289e6ac027566bd) feat(api-docs): expose OpenAPI spec + render Swagger UI in panel - [`d67eb49`](https://github.com/MHSanaei/3x-ui/commit/d67eb4911043bb3e3021b3337eec342d084f8f78) style(api-docs): dark/ultra theme for Swagger UI ### 📊 Changes **60 files changed** (+8950 additions, -2348 deletions) <details> <summary>View changed files</summary> ➖ `frontend/api-docs.html` (+0 -13) ➖ `frontend/clients.html` (+0 -13) ➖ `frontend/inbounds.html` (+0 -13) 📝 `frontend/index.html` (+2 -2) ➖ `frontend/nodes.html` (+0 -13) 📝 `frontend/package-lock.json` (+2345 -263) 📝 `frontend/package.json` (+9 -3) ➕ `frontend/public/openapi.json` (+4944 -0) ➕ `frontend/scripts/build-openapi.mjs` (+218 -0) ➖ `frontend/settings.html` (+0 -13) ➕ `frontend/src/api/QueryProvider.tsx` (+16 -0) ➕ `frontend/src/api/queries/useAllSettings.ts` (+67 -0) ➕ `frontend/src/api/queries/useNodeMutations.ts` (+63 -0) ➕ `frontend/src/api/queries/useNodesQuery.ts` (+108 -0) ➕ `frontend/src/api/queries/useStatusQuery.ts` (+33 -0) ➕ `frontend/src/api/queryKeys.ts` (+30 -0) ➕ `frontend/src/api/websocketBridge.ts` (+77 -0) 📝 `frontend/src/components/AppSidebar.tsx` (+24 -28) ➖ `frontend/src/entries/clients.tsx` (+0 -28) ➖ `frontend/src/entries/inbounds.tsx` (+0 -28) _...and 40 more files_ </details> ### 📄 Description ## Summary Migrates the panel frontend from a 9-entry MPA with hand-rolled `useState`/`useEffect` hooks to a single React Router SPA backed by TanStack Query, and replaces the API-docs page with a Swagger UI rendering an auto-generated OpenAPI 3 spec. ### Phase 1 — TanStack Query foundation - `QueryClient` singleton with sensible defaults (staleTime 30s, refetchOnWindowFocus, retry 1) + dev-only DevTools - Centralized query-key factory at `frontend/src/api/queryKeys.ts` - `useStatusQuery` as the proof-of-concept (2s polling) ### Phase 2 — Multi-entry → SPA - 9 HTML entries collapsed to 3: `index.html`, `login.html`, `subpage.html` - Declarative `createBrowserRouter` with lazy-loaded page chunks - Backend `/panel/*` collapses to one catch-all serving `index.html` - Single WebSocket bridge mounted in `PanelLayout` dispatches to `queryClient` (replaces per-page `useWebSocket` glue) - `AppSidebar` uses `<Link>` / `useNavigate` — no more full-page reloads between panel routes ### Phase 3 — Hook migrations (one commit per resource) - **3a Nodes** — `useNodesQuery` + `useNodeMutations` - **3b Settings** — `useAllSettingsQuery` + save mutation; draft state stays local - **3c Inbounds** — slim list + detail queries; WS bridge handles invalidation - **3d Clients** — paged query w/ `keepPreviousData` + 9 mutations; in-place WS merges preserved ### Phase 4 — Xray (partial) Editor state and 1s dirty-check polling kept; only network calls (config, save, test-outbound, reset, restart, default-config, outbounds traffic) moved to Query. ### Phase 5 — Per-route document titles SPA collapse broke per-page `<title>` — added `usePageTitle` hook with a route→title map, mounted in `PanelLayout`. ### Phase 6 — In-panel API docs - `frontend/scripts/build-openapi.mjs` converts the existing `endpoints.js` into an OpenAPI 3.0.3 spec (106 paths, 12 tags, Bearer + cookie security) - Spec rebuilt on every \`npm run build\` and served by Go from \`/panel/api/openapi.json\` - `ApiDocsPage` renders `swagger-ui-react` against that URL — gives readable docs + Try-it-out tester + Postman-compatible spec, replacing the hand-written endpoint cards ### Phase 7 — Dark/ultra theme for Swagger UI Swagger ships no dark theme, so every visual surface gets an override scoped to `.api-docs-page.is-dark` / `.is-ultra`: opblocks, tables, model boxes, form inputs, code blocks, modals, Servers dropdown (with a custom light-fill SVG arrow), per-endpoint padlocks and expand chevrons. Method chips kept their semantic colors. ## Test plan - [ ] \`npm run lint && npm run typecheck && npm run build\` clean - [ ] Navigate every sidebar route with no full-page reloads (Network tab → no document requests after first) - [ ] Refresh in the middle of any route (e.g. \`/panel/xray\`) → SPA re-renders correct page - [ ] 401 (force-expire session) redirects to login as before - [ ] Edit a client → modal closes → list updates with one PUT + one paged GET - [ ] WebSocket: trigger a traffic event → onlines indicator updates without per-page handler - [ ] React Query DevTools shows every expected query key - [ ] \`/panel/api-docs\` renders Swagger UI; toggle Light → Dark → Ultra and verify all surfaces readable (Servers dropdown, lock icons, Execute/Authorize/Cancel buttons) - [ ] Try-it-out on any endpoint succeeds and uses the current session - [ ] Per-route \`<title>\` updates on navigation --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:03:32 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#5714
No description provided.