|
1
|
import { Container, Heading, Text } from "@medusajs/ui" |
|
2
|
import { useMemo } from "react" |
|
3
|
import { useTranslation } from "react-i18next" |
|
4
|
|
|
5
|
import { DataTable } from "../../../components/data-table" |
|
6
|
import { useEntities } from "../../../hooks/api/views" |
|
7
|
import { usePropertyLabelsTableColumns } from "../../../hooks/table/columns" |
|
8
|
import { useQueryParams } from "../../../hooks/use-query-params" |
|
9
|
|
|
10
|
const PAGE_SIZE = 15 |
|
11
|
const PREFIX = "pl" |
|
12
|
|
|
13
|
export const PropertyLabelsList = () => { |
|
14
|
const { t } = useTranslation() |
|
15
|
// The entities endpoint returns the full list in one call, so pagination is |
|
16
|
// applied client-side by slicing the current page out of `entities`. |
|
17
|
const { entities, isPending, isError, error } = useEntities() |
|
18
|
const columns = usePropertyLabelsTableColumns() |
|
19
|
|
|
20
|
const { offset, order } = useQueryParams(["offset", "order"], PREFIX) |
|
21
|
const start = offset ? parseInt(offset, 10) : 0 |
|
22
|
|
|
23
|
const { field, desc } = useMemo(() => { |
|
24
|
const value = order || "module" |
|
25
|
return value.startsWith("-") |
|
26
|
? { field: value.slice(1), desc: true } |
|
27
|
: { field: value, desc: false } |
|
28
|
}, [order]) |
|
29
|
|
|
30
|
const paginatedEntities = useMemo(() => { |
|
31
|
const sorted = [...(entities ?? [])].sort((a, b) => { |
|
32
|
const comparison = String(a[field] ?? "").localeCompare( |
|
33
|
String(b[field] ?? "") |
|
34
|
) |
|
35
|
return desc ? -comparison : comparison |
|
36
|
}) |
|
37
|
return sorted.slice(start, start + PAGE_SIZE) |
|
38
|
}, [entities, field, desc, start]) |
|
39
|
|
|
40
|
if (isError) { |
|
41
|
throw error |
|
42
|
} |
|
43
|
|
|
44
|
return ( |
|
45
|
<Container className="divide-y p-0"> |
|
46
|
<div className="px-6 py-4"> |
|
47
|
<Heading>{t("propertyLabels.title")}</Heading> |
|
48
|
<Text>{t("propertyLabels.subtitle")}</Text> |
|
49
|
</div> |
|
50
|
|
|
51
|
<DataTable |
|
52
|
columns={columns} |
|
53
|
enablePagination |
|
54
|
enableSearch={false} |
|
55
|
data={paginatedEntities} |
|
56
|
rowCount={entities?.length ?? 0} |
|
57
|
pageSize={PAGE_SIZE} |
|
58
|
prefix={PREFIX} |
|
59
|
isLoading={isPending} |
|
60
|
getRowId={(row) => `${row.module}-${row.name}`} |
|
61
|
/> |
|
62
|
</Container> |
|
63
|
) |
|
64
|
} |