2026-10-01 18:20:30 +03:00

55 lines
1.1 KiB
JavaScript

const ROW_TYPE_LEVELS = {
ROOT: 0,
GROUP: 1,
ITEM: 2,
SUB_ITEM: 3,
INPUT: 4,
};
export function buildRowHierarchy(items, mapItem = (item) => item) {
const result = [];
const stack = [];
for (const [index, item] of items.entries()) {
const currentLevel = ROW_TYPE_LEVELS[item.row_type] ?? 999;
while (stack.length > 0 && (ROW_TYPE_LEVELS[stack.at(-1).row_type] ?? 999) >= currentLevel) {
stack.pop();
}
const parent = stack.at(-1);
const newNode = { ...mapItem(item, index), subRows: [] };
if (parent) parent.subRows.push(newNode);
else result.push(newNode);
if (item.row_type !== 'INPUT') stack.push(newNode);
}
return result;
}
export function getDataRowId(data) {
return data?.id || data?.line_id;
}
export function getRowId(row) {
return getDataRowId(row.original.data);
}
export function isVspRow(row) {
if (row?.original?.data?.id) {
return true;
}
return false;
}
export function isVspNewRow(rowData) {
if (!rowData) return false;
const newRow = rowData[3];
return !!newRow.id;
}
export function getParentId(data) {
return data?.project_id || data?.expense_item_id || null;
}