diff --git a/src/components/run/RunsTable.tsx b/src/components/run/RunsTable.tsx index 92233214..563fd55c 100644 --- a/src/components/run/RunsTable.tsx +++ b/src/components/run/RunsTable.tsx @@ -194,18 +194,6 @@ export const RunsTable: React.FC = ({ return result; }, [rows, searchTerm]); - // Group filtered rows by robot meta id - const groupedRows = useMemo(() => - filteredRows.reduce((acc, row) => { - if (!acc[row.robotMetaId]) { - acc[row.robotMetaId] = []; - } - acc[row.robotMetaId].push(row); - return acc; - }, {} as Record), - [filteredRows] - ); - const parseDateString = (dateStr: string): Date => { try { if (dateStr.includes('PM') || dateStr.includes('AM')) { @@ -218,6 +206,35 @@ export const RunsTable: React.FC = ({ } }; + const groupedRows = useMemo(() => { + const groupedData = filteredRows.reduce((acc, row) => { + if (!acc[row.robotMetaId]) { + acc[row.robotMetaId] = []; + } + acc[row.robotMetaId].push(row); + return acc; + }, {} as Record); + + Object.keys(groupedData).forEach(robotId => { + groupedData[robotId].sort((a, b) => + parseDateString(b.startedAt).getTime() - parseDateString(a.startedAt).getTime() + ); + }); + + const robotEntries = Object.entries(groupedData).map(([robotId, runs]) => ({ + robotId, + runs, + latestRunDate: parseDateString(runs[0].startedAt).getTime() + })); + + robotEntries.sort((a, b) => b.latestRunDate - a.latestRunDate); + + return robotEntries.reduce((acc, { robotId, runs }) => { + acc[robotId] = runs; + return acc; + }, {} as Record); + }, [filteredRows]); + const renderTableRows = useCallback((data: Data[], robotMetaId: string) => { const start = page * rowsPerPage; const end = start + rowsPerPage;