Raw
1 import { useMemo } from "react"
2 import { createDataTableColumnHelper } from "@medusajs/ui"
3 import { HttpTypes } from "@medusajs/types"
4 import { useTranslation } from "react-i18next"
5 import {
6 getCellRenderer,
7 getColumnValue,
8 } from "../../../lib/table/cell-renderers"
9 import { TableAdapter } from "../../../lib/table/table-adapters"
10
11 export function useConfigurableTableColumns<TData = any>(
12 apiColumns: HttpTypes.AdminColumn[] | undefined,
13 adapter?: TableAdapter<TData>
14 ) {
15 const columnHelper = useMemo(() => createDataTableColumnHelper<TData>(), [])
16 const { t } = useTranslation()
17
18 return useMemo(() => {
19 if (!apiColumns?.length) {
20 return []
21 }
22
23 const generatedColumns = apiColumns.map((apiColumn) => {
24 // Virtual selection column: the ui select column (checkbox), kept as a
25 // normal column (id "select") so it participates in ordering — its low
26 // default_order keeps it first.
27 if (apiColumn.render_mode === "select") {
28 return columnHelper.select()
29 }
30
31 // Virtual actions column: rendered from the adapter's row actions rather
32 // than the cell-renderer registry, but kept as a normal column so it
33 // participates in visibility/ordering.
34 if (apiColumn.render_mode === "actions") {
35 return columnHelper.accessor(() => null, {
36 id: apiColumn.field,
37 header: () => null,
38 cell: ({ row }: { row: any }) => {
39 const content = adapter?.renderRowActions?.(row.original)
40 if (!content) {
41 return null
42 }
43 return (
44 <div
45 className="flex items-center justify-end"
46 onClick={(e) => e.stopPropagation()}
47 >
48 {content}
49 </div>
50 )
51 },
52 meta: {
53 name: apiColumn.name,
54 column: apiColumn,
55 },
56 computed: {
57 required_fields: [],
58 optional_fields: [],
59 },
60 enableHiding: apiColumn.hideable,
61 enableSorting: false,
62 align: "right",
63 } as any)
64 }
65
66 let renderType = apiColumn.computed?.type
67
68 if (!renderType) {
69 renderType = apiColumn.render_mode
70 }
71
72 const renderer = getCellRenderer(renderType, apiColumn.data_type)
73 const align = adapter?.getColumnAlignment?.(apiColumn) ?? renderer.align
74 // Per-column override wins; otherwise fall back to the renderer's default
75 // (renderers that self-handle overflow set this to false), then to `true`.
76 const truncateTooltip =
77 (apiColumn.metadata as any)?.truncate_tooltip ??
78 renderer.truncateTooltip ??
79 true
80
81 const accessor = (row: TData) => getColumnValue(row, apiColumn)
82
83 return columnHelper.accessor(accessor, {
84 id: apiColumn.field,
85 header: () => apiColumn.name,
86 cell: ({ getValue, row }: { getValue: any; row: any }) => {
87 const value = getValue()
88
89 return renderer.render(value, row.original, apiColumn, t)
90 },
91 meta: {
92 name: apiColumn.name,
93 column: apiColumn, // Store column metadata for future use
94 },
95 enableHiding: apiColumn.hideable,
96 enableSorting: apiColumn.sortable,
97 sortLabel: apiColumn.name,
98 align,
99 truncateTooltip,
100 } as any)
101 })
102
103 return generatedColumns
104 }, [apiColumns, adapter, t, columnHelper])
105 }