/** * @file globalHistoryAutomat.js * @description Virtualized grid automat (`globalHistoryGridAutomat`) for * infinite-scrolling global audit/change history via `react-virtual-aggridautomat`. * * Upstream Triggers: * - `authAutomat`: Subscribes to auth state changes — calls `reload()` when * status reaches `'authed'` with a `selectedCompanyId`. * - AG Grid: Triggers `fetchRows()` on viewport scroll, sort, or filter changes. */ import { createGridAutomat } from 'react-virtual-aggridautomat/createGridAutomat'; import { globalHistory } from '../api'; import authAutomat from './authAutomat'; export const globalHistoryGridAutomat = createGridAutomat({ name: 'globalHistory', blockSize: 50, fetchRows: async ({ startRow, endRow, sortModel, filterModel }) => { try { const res = await globalHistory.list({ startRow, endRow, sortModel, filterModel, }); const rows = res?.rows ?? res?.history ?? res?.entries ?? (Array.isArray(res) ? res : []); const total = res?.total ?? res?.totalRows ?? res?.totalCount; const lastRow = total != null ? total : (rows.length < (endRow - startRow) ? startRow + rows.length : undefined); return { rows, lastRow }; } catch (err) { console.warn('Failed to fetch global history (backend route pending):', err); return { rows: [], lastRow: 0 }; } }, }); // Reload on company switch or auth change authAutomat.subscribe((auth) => { if (auth.status === 'authed' && auth.user?.selectedCompanyId) { globalHistoryGridAutomat.actions.reload(); } }); export default globalHistoryGridAutomat;