import { Dispatch, SetStateAction, useEffect } from "react"; import { eventSummary } from "shared/lib/slickgrid"; import { getTotals } from "entities/popup-summary/lib"; import { ITotal } from "entities/popup-summary/model"; export function useSubscribeToEvent( setIsOpened: Dispatch>, setRowsNumber: Dispatch>, setTotals: Dispatch>, tableId: string, ) { useEffect(() => { function callback() { const { open, table } = eventSummary; if (table?.id !== tableId) return; if (open) { const table = eventSummary.table; const grid = table?.grid; setRowsNumber(grid?.getSelectedRows().length || 0); setTotals(getTotals(table)); } setIsOpened(open); } eventSummary.subscribe(callback); return () => { eventSummary.unsubscribe(callback); }; }, [tableId]); }