Files
accounted/lib/hooks/use-fetch.ts
T
Jakob Wennberg f266c386f3 chore: repo-wide bloat sweep, remove dead code and fold duplicate helpers (#2150)
* chore: repo-wide bloat sweep, remove dead code and fold duplicate helpers

Remove 33 dead files, ~270 unreferenced exports/types, 13 dead i18n
namespaces and 4 unused dependencies; fold byte-identical helper copies
into one canonical home each (lib/utils chunk/sleep/utcDateStamp,
lib/dates/iso, lib/invariants/uuid, lib/xml/escape, lib/reports/sru/format,
lib/pdf/number-text, lib/browser/panel-request, lib/api/v1/body +
v1ValidationError rolled out to ~55 v1 routes, booking-template schemas).

No behaviour change: v1 bodies and status codes, MCP tool schemas, DB
writes and money math are untouched. Naive ore rounding was deliberately
not swapped for roundOre; see DECISIONS.md 2026-09-02 for the full list
of things left alone on purpose.

tsc, lint, 19588 unit tests and check:guards green; antipattern baseline
ratcheted (naive-ore-round 622 -> 620, hand-rolled-invariant 115 -> 113).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* test(transactions): import RawTransaction from @/types after the ingest re-export removal

CI's type ratchet (check:types, full tsconfig) caught the one test file
that still imported the type through lib/transactions/ingest.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-02 11:51:16 +02:00

114 lines
4.1 KiB
TypeScript

'use client'
import { useCallback, useEffect, useRef, useState } from 'react'
import { useLocale } from 'next-intl'
import { getErrorMessage, type ErrorLocale } from '@/lib/errors/get-error-message'
/**
* Canonical client data-fetching hook.
*
* Replaces the hand-rolled `useState(loading)` + `useState(error)` + `useEffect`
* + bare `fetch()` block repeated across ~85 components. Gives every caller the
* same behaviour for free:
*
* - cancels the in-flight request on unmount / url change (AbortController), so
* a slow response can't land after the component moved on (no stale state,
* no "set state on unmounted component" races);
* - routes errors through the bilingual `getErrorMessage()` using the active
* UI locale, so error copy is consistent and localized;
* - exposes `refetch()` for retry / post-mutation refresh.
*
* Behaviour notes (intentional):
* - `data` is NOT cleared on `refetch()` or url change: it keeps the previous
* result while the new request is in flight (keep-previous-data), so lists
* don't blank out on refresh. Read `loading` to show a pending indicator.
* - When `url`/`enabled` start inactive and later become active, `loading`
* flips true on the effect tick, not synchronously on the activating render.
* Branch on `loading` first to avoid a flash.
*
* Response convention: the JSON body is returned as-is, typed as `T`. Most
* Accounted routes wrap payloads as `{ data: ... }`, so the common usage is
* `useFetch<{ data: Account[] }>(...)` then read `result.data?.data`. Pass
* `select` to unwrap/transform at the hook boundary instead.
*
* @example
* const { data, loading, error, refetch } = useFetch<Account[]>(
* '/api/bookkeeping/accounts',
* { select: (body) => body.data ?? [] },
* )
*/
export interface UseFetchOptions<T, R> {
/** Skip the request until true (e.g. waiting on a dependency). Default true. */
enabled?: boolean
/** Transform/unwrap the parsed JSON body before it reaches `data`. */
select?: (body: T) => R
/** Extra `fetch` init (headers, etc.). The AbortController signal is merged in. */
init?: Omit<RequestInit, 'signal'>
}
export interface UseFetchResult<R> {
data: R | null
loading: boolean
error: string | null
/** Re-run the request. Safe to call from event handlers. */
refetch: () => void
}
export function useFetch<T = unknown, R = T>(
url: string | null,
options: UseFetchOptions<T, R> = {},
): UseFetchResult<R> {
const { enabled = true, select, init } = options
const locale = useLocale() as ErrorLocale
// Keep select/init out of the effect deps without re-running on every render.
const selectRef = useRef(select)
selectRef.current = select
const initRef = useRef(init)
initRef.current = init
const active = enabled && url != null
const [data, setData] = useState<R | null>(null)
const [loading, setLoading] = useState<boolean>(active)
const [error, setError] = useState<string | null>(null)
const [nonce, setNonce] = useState(0)
const refetch = useCallback(() => setNonce((n) => n + 1), [])
useEffect(() => {
if (!active || url == null) {
setLoading(false)
return
}
const controller = new AbortController()
setLoading(true)
setError(null)
;(async () => {
try {
const res = await fetch(url, { ...initRef.current, signal: controller.signal })
const body = await res.json().catch(() => null)
if (!res.ok) {
throw new Error(
getErrorMessage(body ?? { error: res.statusText }, { locale, statusCode: res.status }),
)
}
if (controller.signal.aborted) return
const transform = selectRef.current
setData((transform ? transform(body as T) : (body as unknown as R)))
} catch (err) {
if (controller.signal.aborted || (err as Error)?.name === 'AbortError') return
setError(getErrorMessage(err, { locale }))
} finally {
if (!controller.signal.aborted) setLoading(false)
}
})()
return () => controller.abort()
}, [url, active, nonce, locale])
return { data, loading, error, refetch }
}