39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { GridColDef, GridSortModel } from "@mui/x-data-grid";
|
|
import { AuditRow } from "./auditTransforms";
|
|
|
|
export const buildSortParam = (sortModel: GridSortModel) =>
|
|
sortModel.length && sortModel[0].field && sortModel[0].sort
|
|
? `${sortModel[0].field}:${sortModel[0].sort}`
|
|
: undefined;
|
|
|
|
export const parseSortModel = (sortParam?: string): GridSortModel => {
|
|
if (!sortParam) return [];
|
|
const [field, direction] = sortParam.split(":");
|
|
if (!field || (direction !== "asc" && direction !== "desc")) {
|
|
return [];
|
|
}
|
|
|
|
return [{ field, sort: direction }];
|
|
};
|
|
|
|
export const toTitle = (field: string) =>
|
|
field
|
|
.replace(/_/g, " ")
|
|
.replace(/-/g, " ")
|
|
.replace(/([a-z])([A-Z])/g, "$1 $2")
|
|
.replace(/\s+/g, " ")
|
|
.trim()
|
|
.replace(/^\w/g, char => char.toUpperCase());
|
|
|
|
export const deriveColumns = (rows: AuditRow[]): GridColDef[] => {
|
|
if (!rows.length) return [];
|
|
|
|
return Object.keys(rows[0]).map(field => ({
|
|
field,
|
|
headerName: toTitle(field),
|
|
flex: field === "id" ? 0 : 1,
|
|
minWidth: field === "id" ? 140 : 200,
|
|
sortable: true,
|
|
}));
|
|
};
|