1 import { HttpTypes } from "@medusajs/types"
2 import { ColumnDef } from "@tanstack/react-table"
3 import { TFunction } from "i18next"
4 import { useMemo } from "react"
5 import { FieldPath, FieldValues } from "react-hook-form"
6 import { useTranslation } from "react-i18next"
7 import { IncludesTaxTooltip } from "../../../../components/common/tax-badge/tax-badge"
8 import {
9 createDataGridHelper,
10 DataGrid,
11 } from "../../../../components/data-grid"
12 import { FieldContext } from "../../../../components/data-grid/types"
13 import { ShippingOptionPriceCell } from "../components/shipping-option-price-cell"
14
15 const columnHelper = createDataGridHelper()
16
17 export const useShippingOptionPriceColumns = ({
18 name,
19 currencies = [],
20 regions = [],
21 pricePreferences = [],
22 }: {
23 name: string
24 currencies?: string[]
25 regions?: HttpTypes.AdminRegion[]
26 pricePreferences?: HttpTypes.AdminPricePreference[]
27 }) => {
28 const { t } = useTranslation()
29
30 return useMemo(() => {
31 return [
32 columnHelper.column({
33 id: "name",
34 name: t("fields.name"),
35 disableHiding: true,
36 header: t("fields.name"),
37 cell: (context) => {
38 return (
39 <DataGrid.ReadonlyCell context={context}>
40 {name}
41 </DataGrid.ReadonlyCell>
42 )
43 },
44 }),
45 ...createDataGridPriceColumns({
46 currencies,
47 regions,
48 pricePreferences,
49 getFieldName: (context, value) => {
50 if (context.column.id?.startsWith("currency_prices")) {
51 return `currency_prices.${value}`
52 }
53
54 return `region_prices.${value}`
55 },
56 t,
57 }),
58 ]
59 }, [t, currencies, regions, pricePreferences, name])
60 }
61
62 type CreateDataGridPriceColumnsProps<
63 TData,
64 TFieldValues extends FieldValues
65 > = {
66 currencies?: string[]
67 regions?: HttpTypes.AdminRegion[]
68 pricePreferences?: HttpTypes.AdminPricePreference[]
69 getFieldName: (
70 context: FieldContext<TData>,
71 value: string
72 ) => FieldPath<TFieldValues> | null
73 t: TFunction
74 }
75
76 export const createDataGridPriceColumns = <
77 TData,
78 TFieldValues extends FieldValues
79 >({
80 currencies,
81 regions,
82 pricePreferences,
83 getFieldName,
84 t,
85 }: CreateDataGridPriceColumnsProps<TData, TFieldValues>): ColumnDef<
86 TData,
87 unknown
88 >[] => {
89 const columnHelper = createDataGridHelper<TData, TFieldValues>()
90
91 return [
92 ...(currencies?.map((currency) => {
93 const preference = pricePreferences?.find(
94 (p) => p.attribute === "currency_code" && p.value === currency
95 )
96
97 const translatedCurrencyName = t("fields.priceTemplate", {
98 regionOrCurrency: currency.toUpperCase(),
99 })
100
101 return columnHelper.column({
102 id: `currency_prices.${currency}`,
103 name: t("fields.priceTemplate", {
104 regionOrCurrency: currency.toUpperCase(),
105 }),
106 field: (context) => {
107 return getFieldName(context, currency)
108 },
109 type: "number",
110 header: () => (
111 <div className="flex w-full items-center justify-between gap-3">
112 <span className="truncate" title={translatedCurrencyName}>
113 {translatedCurrencyName}
114 </span>
115 <IncludesTaxTooltip includesTax={preference?.is_tax_inclusive} />
116 </div>
117 ),
118 cell: (context) => {
119 return (
120 <ShippingOptionPriceCell
121 type="currency"
122 header={translatedCurrencyName}
123 code={currency}
124 context={context}
125 />
126 )
127 },
128 })
129 }) ?? []),
130 ...(regions?.map((region) => {
131 const preference = pricePreferences?.find(
132 (p) => p.attribute === "region_id" && p.value === region.id
133 )
134
135 const translatedRegionName = t("fields.priceTemplate", {
136 regionOrCurrency: region.name,
137 })
138
139 return columnHelper.column({
140 id: `region_prices.${region.id}`,
141 name: t("fields.priceTemplate", {
142 regionOrCurrency: region.name,
143 }),
144 field: (context) => {
145 return getFieldName(context, region.id)
146 },
147 type: "number",
148 header: () => (
149 <div className="flex w-full items-center justify-between gap-3">
150 <span className="truncate" title={translatedRegionName}>
151 {translatedRegionName}
152 </span>
153 <IncludesTaxTooltip includesTax={preference?.is_tax_inclusive} />
154 </div>
155 ),
156 cell: (context) => {
157 const currency = currencies?.find((c) => c === region.currency_code)
158 if (!currency) {
159 return null
160 }
161
162 return (
163 <ShippingOptionPriceCell
164 type="region"
165 header={translatedRegionName}
166 code={region.currency_code}
167 context={context}
168 />
169 )
170 },
171 })
172 }) ?? []),
173 ]
174 }