mirror of
https://github.com/bitbaum/evig.git
synced 2026-12-05 02:19:53 +01:00
refactor(hooks): migrate useAdminWorkshops to SWR (5/N — lint cleanup)
Fifth in the SWR migration sequence. More complex than the prior
hooks because the fetch depends on filter + pagination state, not
just session presence. SWR's key-based reactivity handles this
cleanly — the key is computed from filter+pagination state, and
SWR refetches automatically when any input changes. No manual
useEffect-with-deps + AbortController dance needed.
Specific changes:
- proposals/totalItems/loading useState removed; useSwrFetch
consumes a derived key built from filters.status, filters.category,
currentPage, and searchTerm. When sessionStatus !== 'authenticated',
the key is null and SWR skips the fetch (matches the prior
useEffect guard).
- loadProposals useCallback + the AbortController-equipped useEffect
deleted. SWR handles in-flight request cancellation internally via
useSyncExternalStore, so we don't need to roll our own.
- doApprove() and handleReject() call mutate() after success to
revalidate the current SWR key — same end-user effect as the
prior loadProposals() calls but tied to whatever key the user is
currently viewing (preserves their filter/pagination context).
- displayError = error || (swrError ? load-error-fallback : '')
so local approve/reject errors take precedence over the SWR
load error in the UI.
The swrKey computation is inline (an IIFE) rather than a useMemo
because the derivation is cheap (string concatenation + URL params)
and the inputs are simple primitives — useMemo would just add
ceremony.
Lint: 3 → 2 errors. tsc clean.
36 commits this thread. Remaining for the cleanup: useMyListings
(next session — couples to pagination buttons in
dashboard/listings/page.tsx that call fetchListings(page, filter)
directly, needs coupled consumer change), ProtocolDetailClient's
set-state-in-memo (final cleanup, separate rule).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
592774ab2d
commit
76b64ca458
1 file changed
+32
-29
@@ -1,8 +1,9 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { useState } from 'react'
|
||||
import { useSession } from 'next-auth/react'
|
||||
import { apiFetch } from '@/lib/api/client'
|
||||
import { useSwrFetch } from '@/lib/api/swr'
|
||||
import { ERROR_MESSAGES } from '@/config/error-messages'
|
||||
import { PROPOSAL_STATUS, type ProposalStatus } from '@/config/workshops'
|
||||
import type { WorkshopProposalWithProposer } from '@/components/workshops/types'
|
||||
@@ -22,16 +23,13 @@ export interface WorkshopFilters {
|
||||
export function useAdminWorkshops() {
|
||||
const { status: sessionStatus } = useSession()
|
||||
|
||||
const [proposals, setProposals] = useState<WorkshopProposalWithProposer[]>([])
|
||||
const [totalItems, setTotalItems] = useState(0)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [error, setError] = useState('')
|
||||
const [searchTerm, setSearchTerm] = useState('')
|
||||
const [filters, setFilters] = useState<WorkshopFilters>({
|
||||
status: PROPOSAL_STATUS.PENDING,
|
||||
category: 'all',
|
||||
})
|
||||
const [currentPage, setCurrentPage] = useState(1)
|
||||
const [error, setError] = useState('')
|
||||
const [rejectingId, setRejectingId] = useState<string | null>(null)
|
||||
const [rejectionReason, setRejectionReason] = useState('')
|
||||
const [rejectError, setRejectError] = useState<string | null>(null)
|
||||
@@ -39,8 +37,12 @@ export function useAdminWorkshops() {
|
||||
const [approveLoading, setApproveLoading] = useState(false)
|
||||
const [rejectLoading, setRejectLoading] = useState(false)
|
||||
|
||||
const loadProposals = useCallback(async (signal?: AbortSignal) => {
|
||||
setLoading(true)
|
||||
// Build the SWR key from filter + pagination state. When any of these
|
||||
// change, SWR refetches automatically against the new key — no manual
|
||||
// useEffect-with-deps + AbortController dance needed. When the session
|
||||
// isn't authenticated yet, pass null so SWR skips the fetch entirely.
|
||||
const swrKey = (() => {
|
||||
if (sessionStatus !== 'authenticated') return null
|
||||
const params = new URLSearchParams({
|
||||
status: filters.status,
|
||||
limit: String(PAGINATION.DEFAULT),
|
||||
@@ -48,26 +50,24 @@ export function useAdminWorkshops() {
|
||||
})
|
||||
if (filters.category !== 'all') params.set('category', filters.category)
|
||||
if (searchTerm.trim()) params.set('q', searchTerm.trim())
|
||||
return `/api/admin/workshops/proposals?${params}`
|
||||
})()
|
||||
|
||||
const result = await apiFetch<{ items: WorkshopProposalWithProposer[]; pagination?: { total: number } }>(
|
||||
`/api/admin/workshops/proposals?${params}`
|
||||
)
|
||||
if (signal?.aborted) return
|
||||
if (result.success && result.data) {
|
||||
setProposals(result.data.items || [])
|
||||
setTotalItems(result.data.pagination?.total || 0)
|
||||
} else {
|
||||
setError(result.error || ERROR_MESSAGES.WORKSHOP_PROPOSALS_LOAD_FAILED)
|
||||
}
|
||||
setLoading(false)
|
||||
}, [filters.status, filters.category, currentPage, searchTerm])
|
||||
const {
|
||||
data,
|
||||
error: swrError,
|
||||
isLoading,
|
||||
mutate,
|
||||
} = useSwrFetch<{
|
||||
items: WorkshopProposalWithProposer[]
|
||||
pagination?: { total: number }
|
||||
}>(swrKey)
|
||||
|
||||
useEffect(() => {
|
||||
if (sessionStatus !== 'authenticated') return
|
||||
const controller = new AbortController()
|
||||
loadProposals(controller.signal)
|
||||
return () => controller.abort()
|
||||
}, [sessionStatus, loadProposals])
|
||||
const proposals = data?.items ?? []
|
||||
const totalItems = data?.pagination?.total ?? 0
|
||||
|
||||
// displayError priority: local action errors override SWR load errors.
|
||||
const displayError = error || (swrError ? ERROR_MESSAGES.WORKSHOP_PROPOSALS_LOAD_FAILED : '')
|
||||
|
||||
const handleApprove = (proposalId: string) => {
|
||||
setApproveConfirmId(proposalId)
|
||||
@@ -83,7 +83,10 @@ export function useAdminWorkshops() {
|
||||
setApproveLoading(false)
|
||||
if (result.success) {
|
||||
setApproveConfirmId(null)
|
||||
loadProposals()
|
||||
// Revalidate the SWR cache so the approved proposal disappears
|
||||
// from the pending list (or appears in the approved tab depending
|
||||
// on the current filter).
|
||||
await mutate()
|
||||
} else {
|
||||
setError(result.error || STRINGS.APPROVE_ERROR)
|
||||
setApproveConfirmId(null)
|
||||
@@ -105,7 +108,7 @@ export function useAdminWorkshops() {
|
||||
if (result.success) {
|
||||
setRejectingId(null)
|
||||
setRejectionReason('')
|
||||
loadProposals()
|
||||
await mutate()
|
||||
} else {
|
||||
setError(result.error || STRINGS.REJECT_ERROR)
|
||||
}
|
||||
@@ -129,8 +132,8 @@ export function useAdminWorkshops() {
|
||||
proposals,
|
||||
totalItems,
|
||||
totalPages,
|
||||
loading,
|
||||
error,
|
||||
loading: isLoading,
|
||||
error: displayError,
|
||||
filters,
|
||||
searchTerm,
|
||||
currentPage,
|
||||
|
||||
Reference in new issue
Block a user