1 import { HttpTypes } from "@medusajs/types"
2 import { createColumnHelper } from "@tanstack/react-table"
3 import { useMemo } from "react"
4 import { useTranslation } from "react-i18next"
5
6 import { DateCell } from "../../../components/table/table-cells/common/date-cell"
7 import { TextCell } from "../../../components/table/table-cells/common/text-cell"
8 import { getFormattedShippingOptionLocationName } from "../../../lib/shipping-options"
9
10 const columnHelper = createColumnHelper<HttpTypes.AdminShippingOption>()
11
12 export const useShippingOptionTableColumns = () => {
13 const { t } = useTranslation()
14
15 return useMemo(
16 () => [
17 columnHelper.accessor("name", {
18 header: () => t("fields.name"),
19 cell: ({ getValue }) => <TextCell text={getValue()} />,
20 }),
21 columnHelper.accessor("shipping_profile", {
22 header: () => t("fields.shippingProfile"),
23 cell: ({ row }) => (
24 <TextCell text={row.original.shipping_profile?.name || "N/A"} />
25 ),
26 }),
27 columnHelper.display({
28 id: "location",
29 header: () => t("fields.location"),
30 cell: ({ row }) => {
31 const locationName = getFormattedShippingOptionLocationName(
32 row.original
33 )
34
35 return <TextCell text={locationName} />
36 },
37 }),
38 columnHelper.display({
39 id: "service_zone",
40 header: () => t("fields.serviceZone"),
41 cell: ({ row }) => {
42 const serviceZoneName = row.original.service_zone?.name
43
44 return <TextCell text={serviceZoneName || "N/A"} />
45 },
46 }),
47 columnHelper.display({
48 id: "enabled_in_store",
49 header: () => t("fields.enabledInStore"),
50 cell: ({ row }) => {
51 let text = "N/A"
52 const val = row.original.rules?.find(
53 (r) => r.attribute === "enabled_in_store"
54 )
55
56 if (val) {
57 text = val.value === "true" ? "Yes" : "No"
58 }
59
60 return <TextCell text={text} />
61 },
62 }),
63 columnHelper.display({
64 id: "is_return",
65 header: () => t("fields.isReturn"),
66 cell: ({ row }) => {
67 let text = "N/A"
68 const val = row.original.rules?.find(
69 (r) => r.attribute === "is_return"
70 )
71
72 if (val) {
73 text = val.value === "true" ? "Yes" : "No"
74 }
75
76 return <TextCell text={text} />
77 },
78 }),
79 columnHelper.accessor("created_at", {
80 header: () => t("fields.createdAt"),
81
82 cell: ({ getValue }) => {
83 return <DateCell date={getValue()} />
84 },
85 }),
86 ],
87 [t]
88 )
89 }