1 import { HttpTypes } from "@medusajs/types"
2 import { useQueryParams } from "../../use-query-params"
3
4 type UseProductOptionTableQueryProps = {
5 prefix?: string
6 pageSize?: number
7 }
8
9 export const useProductOptionTableQuery = ({
10 prefix,
11 pageSize = 20,
12 }: UseProductOptionTableQueryProps) => {
13 const queryObject = useQueryParams(
14 ["offset", "q", "order", "created_at", "updated_at", "is_exclusive"],
15 prefix
16 )
17
18 const { offset, created_at, updated_at, q, order, is_exclusive } = queryObject
19
20 const searchParams: HttpTypes.AdminProductOptionListParams = {
21 limit: pageSize,
22 offset: offset ? Number(offset) : 0,
23 order,
24 created_at: created_at ? JSON.parse(created_at) : undefined,
25 updated_at: updated_at ? JSON.parse(updated_at) : undefined,
26 is_exclusive: parseIsExclusive(is_exclusive),
27 // ^ undefined when the user explicitly removed the chip, so all options
28 // are returned regardless of type. The list seeds a default on first load.
29 q,
30 }
31
32 return {
33 searchParams,
34 raw: queryObject,
35 }
36 }
37
38 // Tolerate both the quoted form produced by the DataTable filter UI
39 // (`"false"`) and the plain form a human might write when sharing/bookmarking
40 // a URL (`false`). Returns undefined when the filter is absent so the user
41 // can clear the chip to see all options regardless of type.
42 const parseIsExclusive = (raw: string | undefined): boolean | undefined => {
43 if (raw === undefined) {
44 return undefined
45 }
46 try {
47 const parsed = JSON.parse(raw)
48 if (typeof parsed === "boolean") {
49 return parsed
50 }
51 if (typeof parsed === "string") {
52 return parsed === "true"
53 }
54 return undefined
55 } catch {
56 return raw === "true"
57 }
58 }