import { HttpTypes } from "@medusajs/types" import { createColumnHelper } from "@tanstack/react-table" import { useMemo } from "react" import { useTranslation } from "react-i18next" import { DateCell, DateHeader, } from "../../../components/table/table-cells/common/date-cell" 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" import { TextCell, TextHeader, } from "../../../components/table/table-cells/common/text-cell" const columnHelper = createColumnHelper() /** * Hook to build columns dynamically based on API columns response */ export const useOrderDataTableColumns = ( apiColumns: HttpTypes.AdminColumn[] | undefined, visibleColumns: string[] ) => { const { t } = useTranslation() return useMemo(() => { if (!apiColumns || apiColumns.length === 0) { // Return default columns if no API columns return [ 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 total = getValue() const currencyCode = row.original.currency_code return }, }), columnHelper.display({ id: "country", cell: ({ row }) => { const country = row.original.shipping_address?.country return }, }), ] } // Build columns from API response return apiColumns .filter((col) => visibleColumns.includes(col.id)) .sort((a, b) => { const aIndex = visibleColumns.indexOf(a.id) const bIndex = visibleColumns.indexOf(b.id) return aIndex - bIndex }) .map((col) => { // Handle special columns with custom cells switch (col.id) { case "display_id": return columnHelper.accessor("display_id", { header: () => , cell: ({ getValue }) => { const id = getValue() return }, }) case "created_at": case "updated_at": return columnHelper.accessor(col.field as any, { header: () => , cell: ({ getValue }) => { const date = getValue() ? new Date(getValue() as string) : null return date ? : null }, }) case "email": return columnHelper.accessor("email", { header: () => , cell: ({ getValue }) => { const email = getValue() return }, }) case "customer_display": return columnHelper.accessor("customer", { header: () => , cell: ({ getValue }) => { const customer = getValue() return }, }) case "sales_channel.name": return columnHelper.accessor("sales_channel", { header: () => , cell: ({ getValue }) => { const channel = getValue() return }, }) case "payment_status": return columnHelper.accessor("payment_status", { header: () => , cell: ({ getValue }) => { const status = getValue() return }, }) case "fulfillment_status": return columnHelper.accessor("fulfillment_status", { header: () => , cell: ({ getValue }) => { const status = getValue() return }, }) case "total": return columnHelper.accessor("total", { header: () => , cell: ({ getValue, row }) => { const total = getValue() const currencyCode = row.original.currency_code return }, }) case "country": return columnHelper.display({ id: "country", cell: ({ row }) => { const country = row.original.shipping_address?.country return }, }) default: // Handle relationship fields (e.g., customer.email) if (col.field.includes(".")) { const [relation, field] = col.field.split(".") return columnHelper.accessor( (row: any) => { const relationData = row[relation] return relationData?.[field] || "" }, { id: col.id, header: () => , cell: ({ getValue }) => { const value = getValue() return }, } ) } // Default text column return columnHelper.accessor(col.field as any, { header: () => , cell: ({ getValue }) => { const value = getValue() return }, }) } }) }, [apiColumns, visibleColumns, t]) }