mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-27 06:13:06 +00:00
perf: refactor FetchState object to primitive status to prevent unnecessary re-renders
Using an object for fetch state caused re-renders even when the status
was the same, because each setFetchState({ status: 'idle' }) created a
new object reference. Now using separate primitive state variables:
- fetchStatus: 'idle' | 'fetching' | 'error'
- fetchError: string | null
React's useState does reference equality checks, so primitives only
trigger re-renders when the value actually changes.
This commit is contained in:
parent
c0c811834b
commit
69c970aa0a
1 changed files with 20 additions and 15 deletions
|
|
@ -43,7 +43,7 @@ const FETCH_AHEAD_MARGIN = '0px 0px 600px 0px'
|
||||||
*/
|
*/
|
||||||
const CONTROLS_BOTTOM_PADDING = 12
|
const CONTROLS_BOTTOM_PADDING = 12
|
||||||
|
|
||||||
type FetchState = { status: 'idle' } | { status: 'fetching' } | { status: 'error'; message: string }
|
type FetchStatus = 'idle' | 'fetching' | 'error'
|
||||||
|
|
||||||
type CampaignInfiniteTableProps<Row> = {
|
type CampaignInfiniteTableProps<Row> = {
|
||||||
columns: Array<CampaignTableColumn<Row>>
|
columns: Array<CampaignTableColumn<Row>>
|
||||||
|
|
@ -146,7 +146,8 @@ export const CampaignInfiniteTable = <Row,>({
|
||||||
hasMore: boolean
|
hasMore: boolean
|
||||||
total: number
|
total: number
|
||||||
}>({ query, pages: [], hasMore: totalDocs > rows.length, total: totalDocs })
|
}>({ query, pages: [], hasMore: totalDocs > rows.length, total: totalDocs })
|
||||||
const [fetchState, setFetchState] = useState<FetchState>({ status: 'idle' })
|
const [fetchStatus, setFetchStatus] = useState<FetchStatus>('idle')
|
||||||
|
const [fetchError, setFetchError] = useState<string | null>(null)
|
||||||
/** Invalidates in-flight fetches when the filter/sort signature changes. */
|
/** Invalidates in-flight fetches when the filter/sort signature changes. */
|
||||||
const fetchGenerationRef = useRef(0)
|
const fetchGenerationRef = useRef(0)
|
||||||
const appendedPagesRef = useRef(appended.pages)
|
const appendedPagesRef = useRef(appended.pages)
|
||||||
|
|
@ -163,7 +164,8 @@ export const CampaignInfiniteTable = <Row,>({
|
||||||
if (appended.query !== query) {
|
if (appended.query !== query) {
|
||||||
fetchGenerationRef.current += 1
|
fetchGenerationRef.current += 1
|
||||||
setAppended({ query, pages: [], hasMore: totalDocs > rows.length, total: totalDocs })
|
setAppended({ query, pages: [], hasMore: totalDocs > rows.length, total: totalDocs })
|
||||||
setFetchState({ status: 'idle' })
|
setFetchStatus('idle')
|
||||||
|
setFetchError(null)
|
||||||
} else if (appended.pages.length === 0 && appended.total !== totalDocs) {
|
} else if (appended.pages.length === 0 && appended.total !== totalDocs) {
|
||||||
setAppended({ ...appended, total: totalDocs, hasMore: totalDocs > rows.length })
|
setAppended({ ...appended, total: totalDocs, hasMore: totalDocs > rows.length })
|
||||||
}
|
}
|
||||||
|
|
@ -213,7 +215,8 @@ export const CampaignInfiniteTable = <Row,>({
|
||||||
const loadNextPage = useCallback(async () => {
|
const loadNextPage = useCallback(async () => {
|
||||||
const generation = ++fetchGenerationRef.current
|
const generation = ++fetchGenerationRef.current
|
||||||
const nextPage = appendedPagesRef.current.length + 2
|
const nextPage = appendedPagesRef.current.length + 2
|
||||||
setFetchState({ status: 'fetching' })
|
setFetchStatus('fetching')
|
||||||
|
setFetchError(null)
|
||||||
|
|
||||||
let result: CampaignListNextPageResult<Row>
|
let result: CampaignListNextPageResult<Row>
|
||||||
try {
|
try {
|
||||||
|
|
@ -224,7 +227,8 @@ export const CampaignInfiniteTable = <Row,>({
|
||||||
if (fetchGenerationRef.current !== generation) return
|
if (fetchGenerationRef.current !== generation) return
|
||||||
|
|
||||||
if (result.status === 'error') {
|
if (result.status === 'error') {
|
||||||
setFetchState({ status: 'error', message: result.message })
|
setFetchStatus('error')
|
||||||
|
setFetchError(result.message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -234,7 +238,8 @@ export const CampaignInfiniteTable = <Row,>({
|
||||||
total: result.totalDocs,
|
total: result.totalDocs,
|
||||||
hasMore: result.hasMore,
|
hasMore: result.hasMore,
|
||||||
}))
|
}))
|
||||||
setFetchState({ status: 'idle' })
|
setFetchStatus('idle')
|
||||||
|
setFetchError(null)
|
||||||
}, [fetchNextPage])
|
}, [fetchNextPage])
|
||||||
|
|
||||||
const { ref: sentinelRef, inView } = useInView({
|
const { ref: sentinelRef, inView } = useInView({
|
||||||
|
|
@ -243,11 +248,11 @@ export const CampaignInfiniteTable = <Row,>({
|
||||||
})
|
})
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (inView && hasMore && fetchState.status === 'idle') void loadNextPage()
|
if (inView && hasMore && fetchStatus === 'idle') void loadNextPage()
|
||||||
}, [inView, hasMore, fetchState.status, loadNextPage])
|
}, [inView, hasMore, fetchStatus, loadNextPage])
|
||||||
|
|
||||||
const skeletonCount =
|
const skeletonCount =
|
||||||
fetchState.status === 'fetching'
|
fetchStatus === 'fetching'
|
||||||
? Math.min(pageSize, Math.max(0, appended.total - loadedRows.length))
|
? Math.min(pageSize, Math.max(0, appended.total - loadedRows.length))
|
||||||
: 0
|
: 0
|
||||||
const itemCount = loadedRows.length + skeletonCount
|
const itemCount = loadedRows.length + skeletonCount
|
||||||
|
|
@ -338,7 +343,7 @@ export const CampaignInfiniteTable = <Row,>({
|
||||||
'overflow-x-clip overflow-y-visible rounded-xl border bg-background',
|
'overflow-x-clip overflow-y-visible rounded-xl border bg-background',
|
||||||
tableClassName,
|
tableClassName,
|
||||||
)}
|
)}
|
||||||
aria-busy={fetchState.status === 'fetching'}
|
aria-busy={fetchStatus === 'fetching'}
|
||||||
>
|
>
|
||||||
<Table
|
<Table
|
||||||
containerClassName="overflow-visible"
|
containerClassName="overflow-visible"
|
||||||
|
|
@ -422,18 +427,18 @@ export const CampaignInfiniteTable = <Row,>({
|
||||||
{/* Live region stays mounted so assistive tech observes the change
|
{/* Live region stays mounted so assistive tech observes the change
|
||||||
(CampaignListResults pattern). */}
|
(CampaignListResults pattern). */}
|
||||||
<p className="sr-only" aria-live="polite">
|
<p className="sr-only" aria-live="polite">
|
||||||
{fetchState.status === 'fetching'
|
{fetchStatus === 'fetching'
|
||||||
? 'Carregando mais resultados…'
|
? 'Carregando mais resultados…'
|
||||||
: fetchState.status === 'error'
|
: fetchStatus === 'error'
|
||||||
? fetchState.message
|
? fetchError
|
||||||
: ''}
|
: ''}
|
||||||
</p>
|
</p>
|
||||||
{fetchState.status === 'error' ? (
|
{fetchStatus === 'error' ? (
|
||||||
<div
|
<div
|
||||||
role="status"
|
role="status"
|
||||||
className="flex flex-wrap items-center justify-center gap-3 text-sm text-muted-foreground"
|
className="flex flex-wrap items-center justify-center gap-3 text-sm text-muted-foreground"
|
||||||
>
|
>
|
||||||
<span>{fetchState.message}</span>
|
<span>{fetchError}</span>
|
||||||
<Button type="button" variant="ghost" className="min-h-11" onClick={loadNextPage}>
|
<Button type="button" variant="ghost" className="min-h-11" onClick={loadNextPage}>
|
||||||
Tentar novamente
|
Tentar novamente
|
||||||
</Button>
|
</Button>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue