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 {
7 DateCell,
8 DateHeader,
9 } from "../../../components/table/table-cells/common/date-cell"
10 import { CountryCell } from "../../../components/table/table-cells/order/country-cell"
11 import {
12 CustomerCell,
13 CustomerHeader,
14 } from "../../../components/table/table-cells/order/customer-cell"
15 import {
16 DisplayIdCell,
17 DisplayIdHeader,
18 } from "../../../components/table/table-cells/order/display-id-cell"
19 import {
20 FulfillmentStatusCell,
21 FulfillmentStatusHeader,
22 } from "../../../components/table/table-cells/order/fulfillment-status-cell"
23 import {
24 PaymentStatusCell,
25 PaymentStatusHeader,
26 } from "../../../components/table/table-cells/order/payment-status-cell"
27 import {
28 SalesChannelCell,
29 SalesChannelHeader,
30 } from "../../../components/table/table-cells/order/sales-channel-cell"
31 import {
32 TotalCell,
33 TotalHeader,
34 } from "../../../components/table/table-cells/order/total-cell"
35 import {
36 TextCell,
37 TextHeader,
38 } from "../../../components/table/table-cells/common/text-cell"
39
40 const columnHelper = createColumnHelper<HttpTypes.AdminOrder>()
41
42 /**
43 * Hook to build columns dynamically based on API columns response
44 */
45 export const useOrderDataTableColumns = (
46 apiColumns: HttpTypes.AdminColumn[] | undefined,
47 visibleColumns: string[]
48 ) => {
49 const { t } = useTranslation()
50
51 return useMemo(() => {
52 if (!apiColumns || apiColumns.length === 0) {
53 // Return default columns if no API columns
54 return [
55 columnHelper.accessor("display_id", {
56 header: () => <DisplayIdHeader />,
57 cell: ({ getValue }) => {
58 const id = getValue()
59 return <DisplayIdCell displayId={id!} />
60 },
61 }),
62 columnHelper.accessor("created_at", {
63 header: () => <DateHeader />,
64 cell: ({ getValue }) => {
65 const date = new Date(getValue())
66 return <DateCell date={date} />
67 },
68 }),
69 columnHelper.accessor("customer", {
70 header: () => <CustomerHeader />,
71 cell: ({ getValue }) => {
72 const customer = getValue()
73 return <CustomerCell customer={customer} />
74 },
75 }),
76 columnHelper.accessor("sales_channel", {
77 header: () => <SalesChannelHeader />,
78 cell: ({ getValue }) => {
79 const channel = getValue()
80 return <SalesChannelCell channel={channel} />
81 },
82 }),
83 columnHelper.accessor("payment_status", {
84 header: () => <PaymentStatusHeader />,
85 cell: ({ getValue }) => {
86 const status = getValue()
87 return <PaymentStatusCell status={status} />
88 },
89 }),
90 columnHelper.accessor("fulfillment_status", {
91 header: () => <FulfillmentStatusHeader />,
92 cell: ({ getValue }) => {
93 const status = getValue()
94 return <FulfillmentStatusCell status={status} />
95 },
96 }),
97 columnHelper.accessor("total", {
98 header: () => <TotalHeader />,
99 cell: ({ getValue, row }) => {
100 const total = getValue()
101 const currencyCode = row.original.currency_code
102 return <TotalCell currencyCode={currencyCode} total={total} />
103 },
104 }),
105 columnHelper.display({
106 id: "country",
107 cell: ({ row }) => {
108 const country = row.original.shipping_address?.country
109 return <CountryCell country={country} />
110 },
111 }),
112 ]
113 }
114
115 // Build columns from API response
116 return apiColumns
117 .filter((col) => visibleColumns.includes(col.id))
118 .sort((a, b) => {
119 const aIndex = visibleColumns.indexOf(a.id)
120 const bIndex = visibleColumns.indexOf(b.id)
121 return aIndex - bIndex
122 })
123 .map((col) => {
124 // Handle special columns with custom cells
125 switch (col.id) {
126 case "display_id":
127 return columnHelper.accessor("display_id", {
128 header: () => <DisplayIdHeader />,
129 cell: ({ getValue }) => {
130 const id = getValue()
131 return <DisplayIdCell displayId={id!} />
132 },
133 })
134
135 case "created_at":
136 case "updated_at":
137 return columnHelper.accessor(col.field as any, {
138 header: () => <DateHeader />,
139 cell: ({ getValue }) => {
140 const date = getValue() ? new Date(getValue() as string) : null
141 return date ? <DateCell date={date} /> : null
142 },
143 })
144
145 case "email":
146 return columnHelper.accessor("email", {
147 header: () => <TextHeader text={col.name} />,
148 cell: ({ getValue }) => {
149 const email = getValue()
150 return <TextCell text={email || ""} />
151 },
152 })
153
154 case "customer_display":
155 return columnHelper.accessor("customer", {
156 header: () => <CustomerHeader />,
157 cell: ({ getValue }) => {
158 const customer = getValue()
159 return <CustomerCell customer={customer} />
160 },
161 })
162
163 case "sales_channel.name":
164 return columnHelper.accessor("sales_channel", {
165 header: () => <SalesChannelHeader />,
166 cell: ({ getValue }) => {
167 const channel = getValue()
168 return <SalesChannelCell channel={channel} />
169 },
170 })
171
172 case "payment_status":
173 return columnHelper.accessor("payment_status", {
174 header: () => <PaymentStatusHeader />,
175 cell: ({ getValue }) => {
176 const status = getValue()
177 return <PaymentStatusCell status={status} />
178 },
179 })
180
181 case "fulfillment_status":
182 return columnHelper.accessor("fulfillment_status", {
183 header: () => <FulfillmentStatusHeader />,
184 cell: ({ getValue }) => {
185 const status = getValue()
186 return <FulfillmentStatusCell status={status} />
187 },
188 })
189
190 case "total":
191 return columnHelper.accessor("total", {
192 header: () => <TotalHeader />,
193 cell: ({ getValue, row }) => {
194 const total = getValue()
195 const currencyCode = row.original.currency_code
196 return <TotalCell currencyCode={currencyCode} total={total} />
197 },
198 })
199
200 case "country":
201 return columnHelper.display({
202 id: "country",
203 cell: ({ row }) => {
204 const country = row.original.shipping_address?.country
205 return <CountryCell country={country} />
206 },
207 })
208
209 default:
210 // Handle relationship fields (e.g., customer.email)
211 if (col.field.includes(".")) {
212 const [relation, field] = col.field.split(".")
213 return columnHelper.accessor(
214 (row: any) => {
215 const relationData = row[relation]
216 return relationData?.[field] || ""
217 },
218 {
219 id: col.id,
220 header: () => <TextHeader text={col.name} />,
221 cell: ({ getValue }) => {
222 const value = getValue()
223 return <TextCell text={value || ""} />
224 },
225 }
226 )
227 }
228
229 // Default text column
230 return columnHelper.accessor(col.field as any, {
231 header: () => <TextHeader text={col.name} />,
232 cell: ({ getValue }) => {
233 const value = getValue()
234 return <TextCell text={value || ""} />
235 },
236 })
237 }
238 })
239 }, [apiColumns, visibleColumns, t])
240 }