diff --git a/package-lock.json b/package-lock.json index c8e830867..8fbc373af 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index bf4079d96..4a4572139 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/lib/api/swr.ts b/src/lib/api/swr.ts new file mode 100644 index 000000000..e92c4819b --- /dev/null +++ b/src/lib/api/swr.ts @@ -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(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(url: string): Promise { + const result = await apiFetch(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( + key: string | null, + config?: SWRConfiguration, +): SWRResponse { + return useSWR(key, apiFetchSwrFetcher, config) +}