import { DescendingSorting } from "@medusajs/icons" import { DropdownMenu, IconButton } from "@medusajs/ui" import { useState } from "react" import { useTranslation } from "react-i18next" import { useSearchParams } from "react-router-dom" import { useDocumentDirection } from "../../../../hooks/use-document-direction" export type DataTableOrderByKey = { key: keyof TData label: string } type DataTableOrderByProps = { keys: DataTableOrderByKey[] prefix?: string } enum SortDirection { ASC = "asc", DESC = "desc", } type SortState = { key?: string dir: SortDirection } const initState = (params: URLSearchParams, prefix?: string): SortState => { const param = prefix ? `${prefix}_order` : "order" const sortParam = params.get(param) if (!sortParam) { return { dir: SortDirection.ASC, } } const dir = sortParam.startsWith("-") ? SortDirection.DESC : SortDirection.ASC const key = sortParam.replace("-", "") return { key, dir, } } export const DataTableOrderBy = ({ keys, prefix, }: DataTableOrderByProps) => { const [searchParams, setSearchParams] = useSearchParams() const [state, setState] = useState<{ key?: string dir: SortDirection }>(initState(searchParams, prefix)) const param = prefix ? `${prefix}_order` : "order" const { t } = useTranslation() const direction = useDocumentDirection() const handleDirChange = (dir: string) => { setState((prev) => ({ ...prev, dir: dir as SortDirection, })) updateOrderParam({ key: state.key, dir: dir as SortDirection, }) } const handleKeyChange = (value: string) => { setState((prev) => ({ ...prev, key: value, })) updateOrderParam({ key: value, dir: state.dir, }) } const updateOrderParam = (state: SortState) => { if (!state.key) { setSearchParams((prev) => { prev.delete(param) return prev }) return } const orderParam = state.dir === SortDirection.ASC ? state.key : `-${state.key}` setSearchParams((prev) => { prev.set(param, orderParam) return prev }) } return ( {keys.map((key) => { const stringKey = String(key.key) return ( event.preventDefault()} > {key.label} ) })} event.preventDefault()} > {t("general.ascending")} 1 - 30 event.preventDefault()} > {t("general.descending")} 30 - 1 ) }