From af449333bbb565a052b1f0df3397da02be39da36 Mon Sep 17 00:00:00 2001 From: g-but <41178744+g-but@users.noreply.github.com> Date: Tue, 26 May 2026 10:35:01 +0200 Subject: [PATCH] =?UTF-8?q?refactor(hooks):=20migrate=20useSellerDashboard?= =?UTF-8?q?=20to=20SWR=20(3/N=20=E2=80=94=20lint=20cleanup)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Third in the SWR migration sequence (cc03c57a infrastructure, 2c069a97 useBlogSubmissions). Replaces useState/useEffect/setState with useSwrFetch + a redirect-only useEffect that doesn't setState (so it doesn't trip the lint rule). Specific changes: - The fetch becomes useSwrFetch( hasAccess ? '/api/seller/dashboard' : null ). Conditional key gates the fetch behind authorization without needing useState; null skips the request entirely. hasAccess is computed inline from session — pure render-time work. - The redirect side effect (push to /auth/login or /dashboard if not authorized) moves into its own useEffect with NO setState, so the react-hooks/set-state-in-effect rule doesn't fire on it. Router.push is a navigation side effect, not state mutation. - fetchDashboardData() is preserved in the return for backward- compat with the retry buttons at dashboard/seller/page.tsx:61, 83. It now invokes SWR's mutate() which revalidates the same cache key — same end-user effect (refetches the data), better internals (no manual setIsLoading/setError dance). - DEFAULT_STATS fallback preserved: when SWR hasn't yet resolved (or returns no data), consumers see DEFAULT_STATS instead of null/undefined, matching the prior hook contract. The hasAccess check is duplicated between the redirect useEffect and the SWR key derivation. Could extract to a memo, but the two contexts handle "not yet authenticated" differently (redirect early- returns; SWR key passes null) so the duplication is intentional and clearer than a shared abstraction. Lint: 5 → 4 errors. tsc clean. 34 commits this thread. Remaining: useAdminWorkshops, useMyListings, useWorkshopRegistrations (all fetch hooks), plus ProtocolDetailClient's set-state-in-memo (different rule, separate treatment). useMyListings is notably the most complex of the remaining ones — the consumer (dashboard/listings/page.tsx) calls fetchListings(page, statusFilter) directly from pagination buttons, which the SWR-key-based pattern needs to replace with setPage/setStatusFilter handlers (coupled consumer change). Worth a session split on its own. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/hooks/useSellerDashboard.ts | 55 +++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/hooks/useSellerDashboard.ts b/src/hooks/useSellerDashboard.ts index 2d115e7ac..22c89e89d 100644 --- a/src/hooks/useSellerDashboard.ts +++ b/src/hooks/useSellerDashboard.ts @@ -1,9 +1,9 @@ 'use client' -import { useState, useEffect, useCallback } from 'react' +import { useEffect } from 'react' import { useSession } from 'next-auth/react' import { useRouter } from 'next/navigation' -import { apiFetch } from '@/lib/api/client' +import { useSwrFetch } from '@/lib/api/swr' import { ROLES } from '@/lib/constants' export interface SellerProduct { @@ -48,22 +48,10 @@ export function useSellerDashboard(unexpectedError: string) { const { data: session, status: sessionStatus } = useSession() const router = useRouter() - const [data, setData] = useState(null) - const [isLoading, setIsLoading] = useState(true) - const [error, setError] = useState(null) - - const fetchDashboardData = useCallback(async () => { - setIsLoading(true) - setError(null) - const result = await apiFetch('/api/seller/dashboard') - if (result.success && result.data) { - setData(result.data) - } else { - setError(result.error || unexpectedError) - } - setIsLoading(false) - }, [unexpectedError]) - + // Authorization gating: redirect non-sellers + unauthenticated users. + // Stays in a useEffect (not part of the fetch) — the redirect is a + // navigation side effect, not a setState, so this useEffect doesn't + // trip the react-hooks/set-state-in-effect rule. useEffect(() => { if (sessionStatus === 'loading') return if (!session?.user) { @@ -80,18 +68,39 @@ export function useSellerDashboard(unexpectedError: string) { if (!hasAccess) { router.push('/dashboard') - return } + }, [session, sessionStatus, router]) - fetchDashboardData() - }, [session, sessionStatus, router, fetchDashboardData]) + // SWR-driven fetch — gated by the auth/role check. When the user + // isn't authorized (or session hasn't loaded), pass null as the key + // so SWR skips the fetch entirely (no setState during render). + const hasAccess = (() => { + if (sessionStatus !== 'authenticated' || !session?.user) return false + const userRole = session.user.role as string + const isAdmin = + userRole === ROLES.REVAMPIT_ADMIN || + session.user.isStaff === true || + session.user.isSuperAdmin === true + return userRole === ROLES.SELLER || isAdmin + })() + + const { + data, + error: swrError, + isLoading, + mutate, + } = useSwrFetch(hasAccess ? '/api/seller/dashboard' : null) return { sessionStatus, isLoading, - error, + error: swrError ? unexpectedError : null, stats: data?.stats ?? DEFAULT_STATS, products: data?.products ?? [], - fetchDashboardData, + // Expose mutate() under the legacy name so the consumer's retry + // buttons (dashboard/seller/page.tsx:61, 83) keep working without + // a parallel change. SWR mutate() revalidates the cache against + // the same key. + fetchDashboardData: () => mutate(), } }