-
-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy pathcolumn-presets.ts
More file actions
54 lines (48 loc) · 1.83 KB
/
Copy pathcolumn-presets.ts
File metadata and controls
54 lines (48 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { ItemTableListColumnConfig } from '/@/renderer/store';
import { TableColumn } from '/@/shared/types/types';
/**
* Flat, artwork-free column layout: index, title, artist, album, date added, duration.
*/
export const FLAT_COLUMN_ORDER: TableColumn[] = [
TableColumn.ROW_INDEX,
TableColumn.TITLE,
TableColumn.ARTIST,
TableColumn.ALBUM,
TableColumn.DATE_ADDED,
TableColumn.DURATION,
];
/**
* Enables and reorders the columns in `order`, keeping every other column in place but disabled.
* Widths, pins and alignment the user configured are preserved.
*/
export const applyColumnOrder = (
columns: ItemTableListColumnConfig[],
order: TableColumn[],
): ItemTableListColumnConfig[] => {
const byId = new Map(columns.map((column) => [column.id, column]));
const ordered = order
.map((id) => byId.get(id))
.filter((column): column is ItemTableListColumnConfig => column !== undefined)
.map((column) => ({ ...column, isEnabled: true }));
const orderedIds = new Set(ordered.map((column) => column.id));
const rest = columns
.filter((column) => !orderedIds.has(column.id))
.map((column) => ({ ...column, isEnabled: false }));
return [...ordered, ...rest];
};
/**
* True when the enabled columns are exactly `order` (minus any column this list doesn't have).
*/
export const isColumnOrderActive = (
columns: ItemTableListColumnConfig[],
order: TableColumn[],
): boolean => {
const availableIds = new Set(columns.map((column) => column.id));
const expected = order.filter((id) => availableIds.has(id));
const enabled = columns.filter((column) => column.isEnabled).map((column) => column.id);
return (
expected.length > 0 &&
enabled.length === expected.length &&
enabled.every((id, index) => id === expected[index])
);
};