Merge pull request #419 from getmaxun/sort-rows

feat: sort robots and runs table
This commit is contained in:
Karishma Shukla
2025-01-30 17:10:33 +05:30
committed by GitHub
2 changed files with 56 additions and 23 deletions

View File

@@ -189,23 +189,39 @@ export const RecordingsTable = ({
setPage(0);
}, []);
const parseDateString = (dateStr: string): Date => {
try {
if (dateStr.includes('PM') || dateStr.includes('AM')) {
return new Date(dateStr);
}
return new Date(dateStr.replace(/(\d+)\/(\d+)\//, '$2/$1/'))
} catch {
return new Date(0);
}
};
const fetchRecordings = useCallback(async () => {
setIsLoading(true);
try {
const recordings = await getStoredRecordings();
if (recordings) {
const parsedRows = recordings
.map((recording: any, index: number) => {
if (recording?.recording_meta) {
return {
id: index,
...recording.recording_meta,
content: recording.recording
};
}
return null;
})
.filter(Boolean);
.map((recording: any, index: number) => {
if (recording?.recording_meta) {
const parsedDate = parseDateString(recording.recording_meta.createdAt);
return {
id: index,
...recording.recording_meta,
content: recording.recording,
parsedDate
};
}
return null;
})
.filter(Boolean)
.sort((a, b) => b.parsedDate.getTime() - a.parsedDate.getTime());
setRecordings(parsedRows.map((recording) => recording.name));
setRows(parsedRows);

View File

@@ -245,18 +245,6 @@ export const RunsTable: React.FC<RunsTableProps> = ({
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<string, Data[]>),
[filteredRows]
);
const parseDateString = (dateStr: string): Date => {
try {
if (dateStr.includes('PM') || dateStr.includes('AM')) {
@@ -269,6 +257,35 @@ export const RunsTable: React.FC<RunsTableProps> = ({
}
};
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<string, Data[]>);
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<string, Data[]>);
}, [filteredRows]);
const renderTableRows = useCallback((data: Data[], robotMetaId: string) => {
const { page, rowsPerPage } = getPaginationState(robotMetaId);
const start = page * rowsPerPage;