From c0c811834b38f47bc0073293202a5e82d550ae46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Ign=C3=A1cio=20Fontoura=20Solla?= Date: Thu, 6 Aug 2026 01:32:55 -0300 Subject: [PATCH] fix: stabilize rowKey function to prevent virtualizer recalculation on every render --- .../campaign/demand/DemandListTable.tsx | 4 ++- .../leadership/LeadershipListTable.tsx | 4 ++- .../municipality/MunicipalityList.tsx | 4 ++- .../organization/OrganizationListTable.tsx | 4 ++- .../campaign/shared/CampaignInfiniteTable.tsx | 25 +++++++++++-------- .../stateDeputy/StateDeputyListTable.tsx | 4 ++- 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/components/campaign/demand/DemandListTable.tsx b/src/components/campaign/demand/DemandListTable.tsx index 70b0972d..b7dbed67 100644 --- a/src/components/campaign/demand/DemandListTable.tsx +++ b/src/components/campaign/demand/DemandListTable.tsx @@ -24,6 +24,8 @@ import { import type { DemandRowViewModel } from '@/utilities/campaignDemandData' import type { DemandListState } from '@/utilities/demand/demandListUrl' +const rowKeyById = (row: T) => row.id + const statusVariant: Record< CampaignDemandStatus, 'secondary' | 'estimate-pending' | 'destructive' | 'estimate-confirmed' @@ -112,7 +114,7 @@ export const DemandListTable = ({ columns={demandColumns} columnVisibility={columnVisibility} rows={rows} - rowKey={(row) => row.id} + rowKey={rowKeyById} totalDocs={totalDocs} pageSize={pageSize} query={query} diff --git a/src/components/campaign/leadership/LeadershipListTable.tsx b/src/components/campaign/leadership/LeadershipListTable.tsx index 60433cfd..a590b08c 100644 --- a/src/components/campaign/leadership/LeadershipListTable.tsx +++ b/src/components/campaign/leadership/LeadershipListTable.tsx @@ -59,6 +59,8 @@ import { } from '@/utilities/leadership/leadershipListFilters' import type { LeadershipListState } from '@/utilities/leadership/leadershipListUrl' +const rowKeyById = (row: T) => row.id + const dateFormatter = new Intl.DateTimeFormat('pt-BR') const LeadershipListEmptyState = ({ state }: { state: LeadershipListState }) => ( @@ -352,7 +354,7 @@ export const LeadershipListTable = ({ columns={columns} columnVisibility={columnVisibility} rows={rows} - rowKey={(row) => row.id} + rowKey={rowKeyById} totalDocs={totalDocs} pageSize={pageSize} query={query} diff --git a/src/components/campaign/municipality/MunicipalityList.tsx b/src/components/campaign/municipality/MunicipalityList.tsx index fbee3d11..c1d131ec 100644 --- a/src/components/campaign/municipality/MunicipalityList.tsx +++ b/src/components/campaign/municipality/MunicipalityList.tsx @@ -87,6 +87,8 @@ import type { } from '@/utilities/municipality/municipalityViewModels' import { toMunicipalityPledgeCoverageView } from '@/utilities/votePledgeViews' +const rowKeyById = (row: T) => row.id + const dateFormatter = new Intl.DateTimeFormat('pt-BR') type MunicipalityStaffFormAction = ( @@ -715,7 +717,7 @@ export const MunicipalityList = (props: MunicipalityListProps) => { columns={columns} columnVisibility={props.columnVisibility} rows={municipalities} - rowKey={(municipality) => municipality.id} + rowKey={rowKeyById} totalDocs={props.totalDocs ?? municipalities.length} pageSize={props.pageSize ?? municipalityPageSize} query={activeQuery} diff --git a/src/components/campaign/organization/OrganizationListTable.tsx b/src/components/campaign/organization/OrganizationListTable.tsx index af34d9b9..8dc0a9df 100644 --- a/src/components/campaign/organization/OrganizationListTable.tsx +++ b/src/components/campaign/organization/OrganizationListTable.tsx @@ -20,6 +20,8 @@ import { organizationKindLabels } from '@/lib/schemas/organization' import type { OrganizationListState } from '@/utilities/organization/organizationListUrl' import type { OrganizationRowViewModel } from '@/utilities/organizationData' +const rowKeyById = (row: T) => row.id + // B161 — columns are client-defined now (appended rows render here, not in RSC). const organizationColumns: Array> = [ { @@ -83,7 +85,7 @@ export const OrganizationListTable = ({ columns={organizationColumns} columnVisibility={columnVisibility} rows={rows} - rowKey={(row) => row.id} + rowKey={rowKeyById} totalDocs={totalDocs} pageSize={pageSize} query={query} diff --git a/src/components/campaign/shared/CampaignInfiniteTable.tsx b/src/components/campaign/shared/CampaignInfiniteTable.tsx index 26ef138a..4ab17dfa 100644 --- a/src/components/campaign/shared/CampaignInfiniteTable.tsx +++ b/src/components/campaign/shared/CampaignInfiniteTable.tsx @@ -252,16 +252,21 @@ export const CampaignInfiniteTable = ({ : 0 const itemCount = loadedRows.length + skeletonCount - const virtualizer = useVirtualizer({ - count: itemCount, - estimateSize: () => ESTIMATED_ROW_HEIGHT, - getScrollElement: () => scrollElement, - overscan: 8, - getItemKey: (index) => { - const row = loadedRows[index] - return row ? rowKey(row) : `skeleton-${index}` - }, - }) + const virtualizerOptions = useMemo( + () => ({ + count: itemCount, + estimateSize: () => ESTIMATED_ROW_HEIGHT, + getScrollElement: () => scrollElement, + overscan: 8, + getItemKey: (index: number) => { + const row = loadedRows[index] + return row ? rowKey(row) : `skeleton-${index}` + }, + }), + [itemCount, scrollElement, loadedRows, rowKey], + ) + + const virtualizer = useVirtualizer(virtualizerOptions) const virtualItems = virtualizer.getVirtualItems() const firstItem = virtualItems[0] diff --git a/src/components/campaign/stateDeputy/StateDeputyListTable.tsx b/src/components/campaign/stateDeputy/StateDeputyListTable.tsx index afa3626f..5fe4cc2f 100644 --- a/src/components/campaign/stateDeputy/StateDeputyListTable.tsx +++ b/src/components/campaign/stateDeputy/StateDeputyListTable.tsx @@ -48,6 +48,8 @@ import { type StateDeputyFilterOption, } from '@/utilities/stateDeputyListFilters' import { + +const rowKeyById = (row: T) => row.id stateDeputyListSortLabels, type StateDeputyListState, } from '@/utilities/stateDeputyListUrl' @@ -238,7 +240,7 @@ export const StateDeputyListTable = ({ columns={columns} columnVisibility={columnVisibility} rows={rows} - rowKey={(row) => row.id} + rowKey={rowKeyById} totalDocs={totalDocs} pageSize={pageSize} query={query}