|
1
|
import { HttpTypes } from "@medusajs/types" |
|
2
|
|
|
3
|
export enum ColumnAlignment { |
|
4
|
LEFT = "left", |
|
5
|
CENTER = "center", |
|
6
|
RIGHT = "right", |
|
7
|
} |
|
8
|
|
|
9
|
const DEFAULT_COLUMN_ORDER = 500 |
|
10
|
|
|
11
|
/** |
|
12
|
* Determines the appropriate column alignment based on the column metadata |
|
13
|
*/ |
|
14
|
export function getColumnAlignment( |
|
15
|
column: HttpTypes.AdminColumn |
|
16
|
): ColumnAlignment { |
|
17
|
// Currency columns should be right-aligned |
|
18
|
if (column.semantic_type === "currency" || column.data_type === "currency") { |
|
19
|
return ColumnAlignment.RIGHT |
|
20
|
} |
|
21
|
|
|
22
|
// Number columns should be right-aligned (except identifiers) |
|
23
|
if (column.data_type === "number" && column.context !== "identifier") { |
|
24
|
return ColumnAlignment.RIGHT |
|
25
|
} |
|
26
|
|
|
27
|
// Total/amount/price columns should be right-aligned |
|
28
|
if ( |
|
29
|
column.field.includes("total") || |
|
30
|
column.field.includes("amount") || |
|
31
|
column.field.includes("price") |
|
32
|
) { |
|
33
|
return ColumnAlignment.RIGHT |
|
34
|
} |
|
35
|
|
|
36
|
// Country columns should be center-aligned |
|
37
|
if (column.field === "country" || column.field.includes("country_code")) { |
|
38
|
return ColumnAlignment.CENTER |
|
39
|
} |
|
40
|
|
|
41
|
// Default to left alignment |
|
42
|
return ColumnAlignment.LEFT |
|
43
|
} |
|
44
|
|
|
45
|
/** |
|
46
|
* Gets the initial column visibility state from API columns |
|
47
|
*/ |
|
48
|
export function getInitialColumnVisibility( |
|
49
|
apiColumns: HttpTypes.AdminColumn[] |
|
50
|
): Record<string, boolean> { |
|
51
|
const visibility: Record<string, boolean> = {} |
|
52
|
|
|
53
|
apiColumns.forEach((column) => { |
|
54
|
visibility[column.field] = column.default_visible |
|
55
|
}) |
|
56
|
|
|
57
|
return visibility |
|
58
|
} |
|
59
|
|
|
60
|
/** |
|
61
|
* Gets the initial column order from API columns |
|
62
|
*/ |
|
63
|
export function getInitialColumnOrder( |
|
64
|
apiColumns: HttpTypes.AdminColumn[] |
|
65
|
): string[] { |
|
66
|
const sortedColumns = [...apiColumns].sort((a, b) => { |
|
67
|
const orderA = a.default_order ?? DEFAULT_COLUMN_ORDER |
|
68
|
const orderB = b.default_order ?? DEFAULT_COLUMN_ORDER |
|
69
|
return orderA - orderB |
|
70
|
}) |
|
71
|
|
|
72
|
return sortedColumns.map((col) => col.field) |
|
73
|
} |