Raw
1 import { HttpTypes } from "@medusajs/types"
2 import {
3 ColumnDef,
4 ColumnDefBase,
5 createColumnHelper,
6 } from "@tanstack/react-table"
7 import { useMemo } from "react"
8 import {
9 DateCell,
10 DateHeader,
11 } from "../../../components/table/table-cells/common/date-cell"
12 import { countries } from "../../../lib/data/countries"
13 import { CountryCell } from "../../../components/table/table-cells/order/country-cell"
14 import {
15 CustomerCell,
16 CustomerHeader,
17 } from "../../../components/table/table-cells/order/customer-cell"
18 import {
19 DisplayIdCell,
20 DisplayIdHeader,
21 } from "../../../components/table/table-cells/order/display-id-cell"
22 import {
23 FulfillmentStatusCell,
24 FulfillmentStatusHeader,
25 } from "../../../components/table/table-cells/order/fulfillment-status-cell"
26 import {
27 PaymentStatusCell,
28 PaymentStatusHeader,
29 } from "../../../components/table/table-cells/order/payment-status-cell"
30 import {
31 SalesChannelCell,
32 SalesChannelHeader,
33 } from "../../../components/table/table-cells/order/sales-channel-cell"
34 import {
35 TotalCell,
36 TotalHeader,
37 } from "../../../components/table/table-cells/order/total-cell"
38
39 // We have to use any here, as the type of Order is so complex that it lags the TS server
40 const columnHelper = createColumnHelper<HttpTypes.AdminOrder>()
41
42 type UseOrderTableColumnsProps = {
43 exclude?: string[]
44 }
45
46 export const useOrderTableColumns = (props: UseOrderTableColumnsProps) => {
47 const { exclude = [] } = props ?? {}
48
49 const columns = useMemo(
50 () => [
51 columnHelper.accessor("display_id", {
52 header: () => <DisplayIdHeader />,
53 cell: ({ getValue }) => {
54 const id = getValue()
55
56 return <DisplayIdCell displayId={id!} />
57 },
58 }),
59 columnHelper.accessor("created_at", {
60 header: () => <DateHeader />,
61 cell: ({ getValue }) => {
62 const date = new Date(getValue())
63
64 return <DateCell date={date} />
65 },
66 }),
67 columnHelper.accessor("customer", {
68 header: () => <CustomerHeader />,
69 cell: ({ getValue }) => {
70 const customer = getValue()
71
72 return <CustomerCell customer={customer} />
73 },
74 }),
75 columnHelper.accessor("sales_channel", {
76 header: () => <SalesChannelHeader />,
77 cell: ({ getValue }) => {
78 const channel = getValue()
79
80 return <SalesChannelCell channel={channel} />
81 },
82 }),
83 columnHelper.accessor("payment_status", {
84 header: () => <PaymentStatusHeader />,
85 cell: ({ getValue }) => {
86 const status = getValue()
87
88 return <PaymentStatusCell status={status} />
89 },
90 }),
91 columnHelper.accessor("fulfillment_status", {
92 header: () => <FulfillmentStatusHeader />,
93 cell: ({ getValue }) => {
94 const status = getValue()
95
96 return <FulfillmentStatusCell status={status} />
97 },
98 }),
99 columnHelper.accessor("total", {
100 header: () => <TotalHeader />,
101 cell: ({ getValue, row }) => {
102 const isFullyRefunded = row.original.payment_status === "refunded"
103 const total = !isFullyRefunded
104 ? getValue()
105 : row.original.payment_collections?.reduce(
106 (acc, payCol) => acc + (payCol.refunded_amount ?? 0),
107 0
108 ) ?? 0
109 const currencyCode = row.original.currency_code
110
111 return (
112 <TotalCell
113 currencyCode={currencyCode}
114 total={total}
115 className={isFullyRefunded ? "text-ui-fg-muted line-through" : ""}
116 />
117 )
118 },
119 }),
120 columnHelper.display({
121 id: "country",
122 cell: ({ row }) => {
123 const countryCode = row.original.shipping_address?.country_code
124 const country = countries.find((c) => c.iso_2 === countryCode)
125
126 return <CountryCell country={country} />
127 },
128 }),
129 ],
130 []
131 )
132
133 const isAccessorColumnDef = (
134 c: any
135 ): c is ColumnDef<HttpTypes.AdminOrder> & { accessorKey: string } => {
136 return c.accessorKey !== undefined
137 }
138
139 const isDisplayColumnDef = (
140 c: any
141 ): c is ColumnDef<HttpTypes.AdminOrder> & { id: string } => {
142 return c.id !== undefined
143 }
144
145 const shouldExclude = <TDef extends ColumnDefBase<HttpTypes.AdminOrder, any>>(
146 c: TDef
147 ) => {
148 if (isAccessorColumnDef(c)) {
149 return exclude.includes(c.accessorKey)
150 } else if (isDisplayColumnDef(c)) {
151 return exclude.includes(c.id)
152 }
153
154 return false
155 }
156
157 return columns.filter(
158 (c) => !shouldExclude(c)
159 ) as ColumnDef<HttpTypes.AdminOrder>[]
160 }