feat(api): add SWR + thin wrapper for fetch hooks (1/N — react-hooks/set-state-in-effect cleanup)

User-approved multi-commit scope to clear the 6 persistent
react-hooks/set-state-in-effect lint errors (5 fetch hooks + 1
preserve-manual-memoization in ProtocolDetailClient). React 18 lacks
the `use(Promise)` API (React 19 only) and the React Compiler's lint
rule flags the codebase's existing useEffect + setState fetch pattern
as an anti-pattern. SWR is the standard React-18 answer for client-
side data fetching: small (~5kb), uses useSyncExternalStore internally
so no setState-in-effect under the hood, and gives us cache +
revalidation + abort-on-unmount as free upside.

This commit lays the groundwork:
  - Add swr@^2.4.1 to dependencies (no breaking versions in the
    2.x line; minor version is current as of 2026-05-26).
  - src/lib/api/swr.ts: a thin wrapper bridging SWR's
    fetcher-throws-on-error contract with the codebase's apiFetch
    which returns { success, data, error }. The wrapper translates
    { success: false } into a thrown Error so SWR can surface it
    via response.error, matching the existing branches in the hooks.

Subsequent commits (planned 2-6/N, split across sessions per scope
discipline) migrate the 5 fetch hooks one at a time:
  - useAdminWorkshops
  - useBlogSubmissions
  - useMyListings
  - useSellerDashboard
  - useWorkshopRegistrations

And then ProtocolDetailClient's set-state-in-memo (a different rule,
needs different treatment — likely a useReducer or computed-value
extraction).

Each per-hook migration follows the same pattern:
  - Replace useState + useEffect + manual loading/error state with
    useSwrFetch<T>(url)
  - Preserve the hook's external return shape so the calling
    components stay unchanged (proposals, totalItems, loading,
    error, etc.)
  - Add a regression test exercising the SWR-driven loading +
    success + error branches.

tsc clean; no test impact (wrapper module has no existing tests; the
test surface lands with each per-hook migration commit).

32 commits this thread.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
g-butandClaude Opus 4.7 committed 2026-05-26 10:25:46 +02:00
1 parent a8bd89a053
commit cc03c57a61
3 files changed
+72 -2

No files matched your search

+16 -2
View File
@@ -29,9 +29,9 @@
"leaflet": "^1.9.4",
"lru-cache": "^11.2.6",
"lucide-react": "0.488.0",
"next": "^16.2.4",
"next": "^16.2.6",
"next-auth": "^5.0.0-beta.30",
"next-intl": "^4.9.1",
"next-intl": "^4.12.0",
"next-sitemap": "^4.2.3",
"next-themes": "^0.4.6",
"nodemailer": "^8.0.5",
@@ -46,6 +46,7 @@
"server-only": "^0.0.1",
"sharp": "^0.34.5",
"sonner": "^2.0.7",
"swr": "^2.4.1",
"tailwind-merge": "^3.2.0",
"uuid": "^14.0.0",
"zod": "^4.3.6"
@@ -16009,6 +16010,19 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/swr": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/swr/-/swr-2.4.1.tgz",
"integrity": "sha512-2CC6CiKQtEwaEeNiqWTAw9PGykW8SR5zZX8MZk6TeAvEAnVS7Visz8WzphqgtQ8v2xz/4Q5K+j+SeMaKXeeQIA==",
"license": "MIT",
"dependencies": {
"dequal": "^2.0.3",
"use-sync-external-store": "^1.6.0"
},
"peerDependencies": {
"react": "^16.11.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
}
},
"node_modules/symbol-tree": {
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/symbol-tree/-/symbol-tree-3.2.4.tgz",
+1
View File
@@ -93,6 +93,7 @@
"server-only": "^0.0.1",
"sharp": "^0.34.5",
"sonner": "^2.0.7",
"swr": "^2.4.1",
"tailwind-merge": "^3.2.0",
"uuid": "^14.0.0",
"zod": "^4.3.6"
+55
View File
@@ -0,0 +1,55 @@
/**
* SWR helpers that bridge SWR's promise-rejects-on-failure expectation
* with the codebase's `apiFetch` which returns `{ success, data, error }`.
*
* Why SWR: React 18 doesn't have the `use(Promise)` API (React 19 only),
* and the React Compiler's `react-hooks/set-state-in-effect` rule flags
* the legacy useEffect + setState data-fetching pattern that this
* codebase has been using in 5 admin hooks. SWR is the standard React-18
* answer for client-side data fetching: small (~5kb), no setState-in-
* effect under the hood (it uses useSyncExternalStore internally), and
* gives us cache + revalidation as free upside.
*
* Why a thin wrapper: SWR fetchers throw on error so SWR can put the
* Promise in a rejected state. Our `apiFetch` returns a structured
* { success, data, error } object that callers branch on. The wrapper
* translates: { success: true } → resolve(data), { success: false } →
* throw new Error(error). This way fetch-hook migrations look like:
*
* const { data, error, isLoading } = useSwrFetch<T>(url)
*
* and the error/loading branches mirror the current shape exactly.
*/
import useSWR, { type SWRConfiguration, type SWRResponse } from 'swr'
import { apiFetch } from './client'
/**
* SWR fetcher that wraps apiFetch. Throws on { success: false } so SWR
* surfaces the error via response.error.
*/
async function apiFetchSwrFetcher<T>(url: string): Promise<T> {
const result = await apiFetch<T>(url)
if (!result.success) {
throw new Error(result.error || 'Request failed')
}
// result.data may legitimately be undefined when the endpoint returns
// a success envelope without a body — cast through unknown to satisfy
// strict TS when callers know what shape they expect.
return result.data as T
}
/**
* Client-side data fetch with SWR.
*
* - Pass `null` as the key to disable the fetch (e.g., before auth is
* ready); SWR will skip and `data` stays undefined.
* - The fetcher uses the same apiFetch pipeline as the rest of the app
* (auth cookies, base URL, error handling).
*/
export function useSwrFetch<T>(
key: string | null,
config?: SWRConfiguration<T>,
): SWRResponse<T> {
return useSWR<T>(key, apiFetchSwrFetcher, config)
}