import { HttpTypes } from "@medusajs/types" import { ColumnDef, ColumnDefBase, createColumnHelper, } from "@tanstack/react-table" import { useMemo } from "react" import { DateCell, DateHeader, } from "../../../components/table/table-cells/common/date-cell" import { countries } from "../../../lib/data/countries" import { CountryCell } from "../../../components/table/table-cells/order/country-cell" import { CustomerCell, CustomerHeader, } from "../../../components/table/table-cells/order/customer-cell" import { DisplayIdCell, DisplayIdHeader, } from "../../../components/table/table-cells/order/display-id-cell" import { FulfillmentStatusCell, FulfillmentStatusHeader, } from "../../../components/table/table-cells/order/fulfillment-status-cell" import { PaymentStatusCell, PaymentStatusHeader, } from "../../../components/table/table-cells/order/payment-status-cell" import { SalesChannelCell, SalesChannelHeader, } from "../../../components/table/table-cells/order/sales-channel-cell" import { TotalCell, TotalHeader, } from "../../../components/table/table-cells/order/total-cell" // We have to use any here, as the type of Order is so complex that it lags the TS server const columnHelper = createColumnHelper() type UseOrderTableColumnsProps = { exclude?: string[] } export const useOrderTableColumns = (props: UseOrderTableColumnsProps) => { const { exclude = [] } = props ?? {} const columns = useMemo( () => [ columnHelper.accessor("display_id", { header: () => , cell: ({ getValue }) => { const id = getValue() return }, }), columnHelper.accessor("created_at", { header: () => , cell: ({ getValue }) => { const date = new Date(getValue()) return }, }), columnHelper.accessor("customer", { header: () => , cell: ({ getValue }) => { const customer = getValue() return }, }), columnHelper.accessor("sales_channel", { header: () => , cell: ({ getValue }) => { const channel = getValue() return }, }), columnHelper.accessor("payment_status", { header: () => , cell: ({ getValue }) => { const status = getValue() return }, }), columnHelper.accessor("fulfillment_status", { header: () => , cell: ({ getValue }) => { const status = getValue() return }, }), columnHelper.accessor("total", { header: () => , cell: ({ getValue, row }) => { const isFullyRefunded = row.original.payment_status === "refunded" const total = !isFullyRefunded ? getValue() : row.original.payment_collections?.reduce( (acc, payCol) => acc + (payCol.refunded_amount ?? 0), 0 ) ?? 0 const currencyCode = row.original.currency_code return ( ) }, }), columnHelper.display({ id: "actions", cell: ({ row }) => { const countryCode = row.original.shipping_address?.country_code const country = countries.find((c) => c.iso_2 === countryCode) return }, }), ], [] ) const isAccessorColumnDef = ( c: any ): c is ColumnDef & { accessorKey: string } => { return c.accessorKey !== undefined } const isDisplayColumnDef = ( c: any ): c is ColumnDef & { id: string } => { return c.id !== undefined } const shouldExclude = >( c: TDef ) => { if (isAccessorColumnDef(c)) { return exclude.includes(c.accessorKey) } else if (isDisplayColumnDef(c)) { return exclude.includes(c.id) } return false } return columns.filter( (c) => !shouldExclude(c) ) as ColumnDef[] }