Skip to content

Commit f9ed277

Browse files
committed
fix the sorting order issue of childrens
1 parent 273878e commit f9ed277

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/views/DataThread.tsx

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,34 @@ export const DataThread: FC<{}> = function ({ }) {
441441
})
442442

443443

444-
let refTables = tables;
445-
let leafTables = refTables.filter(t => !refTables.some(t2 => t2.derive?.trigger.tableId == t.id));
444+
445+
let leafTables = tables.filter(t => !tables.some(t2 => t2.derive?.trigger.tableId == t.id));
446+
447+
// we want to sort the leaf tables by the order of their ancestors
448+
// for example if ancestor of list a is [0, 3] and the ancestor of list b is [0, 2] then b should come before a
449+
let tableOrder = Object.fromEntries(tables.map((table, index) => [table.id, index]));
450+
let getAncestorOrders = (leafTable: DictTable) => {
451+
let triggers = getTriggers(leafTable, tables);
452+
return [...triggers.map(t => tableOrder[t.tableId]), tableOrder[leafTable.id]];
453+
}
454+
for (let i = 0; i < leafTables.length; i++) {
455+
let aAncestors = getAncestorOrders(leafTables[i]);
456+
console.log(leafTables[i].id, aAncestors);
457+
}
458+
leafTables.sort((a, b) => {
459+
let aAncestors = getAncestorOrders(a);
460+
let bAncestors = getAncestorOrders(b);
461+
462+
// If lengths are equal, compare ancestors in order
463+
for (let i = 0; i < Math.min(aAncestors.length, bAncestors.length); i++) {
464+
if (aAncestors[i] !== bAncestors[i]) {
465+
return aAncestors[i] - bAncestors[i];
466+
}
467+
}
468+
469+
// If all ancestors are equal, compare the leaf tables themselves
470+
return aAncestors.length - bAncestors.length;
471+
});
446472

447473
let drawerOpen = leafTables.length > 1 && threadDrawerOpen;
448474
let threadDrawerWidth = Math.max(Math.min(600, leafTables.length * 200), 212)
@@ -452,6 +478,7 @@ export const DataThread: FC<{}> = function ({ }) {
452478
position: 'relative',
453479
display: 'flex',
454480
flexDirection: drawerOpen ? 'row-reverse' : 'column',
481+
minHeight: '100%',
455482
}}>
456483
{leafTables.map((lt, i) => {
457484
let usedTableIds = leafTables.slice(0, i)
@@ -464,12 +491,12 @@ export const DataThread: FC<{}> = function ({ }) {
464491
chartElements={chartElements}
465492
usedTableIds={usedTableIds}
466493
sx={{
467-
backgroundColor: (i % 2 == 1 ? "rgba(0, 0, 0, 0.02)" : 'white'),
494+
backgroundColor: (i % 2 == 1 ? "rgba(0, 0, 0, 0.03)" : 'white'),
468495
padding: '8px 8px',
469496
flex: 1,
470497
display: 'flex',
471498
flexDirection: 'column',
472-
height: '100%'
499+
height: 'calc(100% - 16px)'
473500
}} />
474501
})}
475502
</Box>

0 commit comments

Comments
 (0)