import { useTranslate } from "@probo/i18n"; import { Button, IconChevronDown, IconChevronTriangleDownSmall, Spinner, Table, Th, } from "@probo/ui"; import clsx from "clsx"; import { createContext, startTransition, useContext, useState, type ComponentProps, } from "react"; type Order = { direction: string; field: string; }; export const SortableContext = createContext({ order: { direction: "DESC", field: "CREATED_AT", }, onOrderChange: (() => {}) as (order: Order) => void, }); const defaultOrder = { direction: "DESC", field: "CREATED_AT", } as Order; export function SortableTable({ refetch, hasNext, loadNext, isLoadingNext, ...props }: ComponentProps & { refetch: (o: { order: Order }) => void; hasNext?: boolean; loadNext?: (...args: any[]) => void; isLoadingNext?: boolean; }) { const { __ } = useTranslate(); const [order, setOrder] = useState(defaultOrder); const onOrderChange = (o: Order) => { startTransition(() => { setOrder(o); refetch({ order: o }); }); }; return (
{hasNext && loadNext && ( )} ); } export function SortableTh({ children, field, ...props }: ComponentProps & { field: string }) { const { order, onOrderChange } = useContext(SortableContext); const isCurrentField = order.field === field; const isDesc = order.direction === "DESC"; const changeOrder = () => { onOrderChange({ direction: isDesc && isCurrentField ? "ASC" : "DESC", field, }); }; return ( ); }