2025-10-16 11:18:55 +00:00
|
|
|
import {
|
2025-11-16 21:11:46 +00:00
|
|
|
Button,
|
|
|
|
|
DropdownMenu,
|
2025-10-16 11:18:55 +00:00
|
|
|
DropdownMenuContent,
|
|
|
|
|
DropdownMenuItem,
|
2025-11-16 21:11:46 +00:00
|
|
|
DropdownMenuTrigger,
|
|
|
|
|
} from "@repo/shadcn-ui/components";
|
|
|
|
|
import { cn } from "@repo/shadcn-ui/lib/utils";
|
|
|
|
|
import type { Column } from "@tanstack/react-table";
|
2025-11-19 16:05:01 +00:00
|
|
|
import { ArrowDown, ArrowDownIcon, ArrowUp, ArrowUpIcon, ChevronsUpDownIcon } from "lucide-react";
|
2025-11-16 21:11:46 +00:00
|
|
|
|
|
|
|
|
import { useTranslation } from "../../locales/i18n.ts";
|
2025-10-16 11:18:55 +00:00
|
|
|
|
2025-11-16 21:11:46 +00:00
|
|
|
interface DataTableColumnHeaderProps<TData, TValue> extends React.HTMLAttributes<HTMLDivElement> {
|
|
|
|
|
column: Column<TData, TValue>;
|
|
|
|
|
title: string;
|
2025-10-16 11:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function DataTableColumnHeader<TData, TValue>({
|
|
|
|
|
column,
|
|
|
|
|
title,
|
|
|
|
|
className,
|
|
|
|
|
}: DataTableColumnHeaderProps<TData, TValue>) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
if (!column.getCanSort()) {
|
2025-11-16 21:11:46 +00:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"text-xs text-muted-foreground text-nowrap cursor-default font-semibold",
|
|
|
|
|
className
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{title}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2025-10-16 11:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={cn("flex items-center gap-2", className)}>
|
|
|
|
|
<DropdownMenu>
|
|
|
|
|
<DropdownMenuTrigger asChild>
|
|
|
|
|
<Button
|
2025-11-19 16:05:01 +00:00
|
|
|
className="data-[state=open]:bg-accent -ml-4 h-8 text-xs text-muted-foreground font-semibold text-nowrap cursor-pointer"
|
2025-10-16 17:59:13 +00:00
|
|
|
type="button"
|
2025-10-16 11:18:55 +00:00
|
|
|
variant="ghost"
|
|
|
|
|
>
|
|
|
|
|
<span>{title}</span>
|
|
|
|
|
{column.getIsSorted() === "desc" ? (
|
2025-11-19 16:05:01 +00:00
|
|
|
<ArrowDownIcon className="ml-2 size-4" />
|
2025-10-16 11:18:55 +00:00
|
|
|
) : column.getIsSorted() === "asc" ? (
|
2025-11-19 16:05:01 +00:00
|
|
|
<ArrowUpIcon className="ml-2 size-4" />
|
2025-10-16 11:18:55 +00:00
|
|
|
) : (
|
2025-11-19 16:05:01 +00:00
|
|
|
<ChevronsUpDownIcon className="ml-2 size-4" />
|
2025-10-16 11:18:55 +00:00
|
|
|
)}
|
|
|
|
|
</Button>
|
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
|
<DropdownMenuContent align="start">
|
|
|
|
|
<DropdownMenuItem onClick={() => column.toggleSorting(false)}>
|
|
|
|
|
<ArrowUp />
|
2025-11-16 21:11:46 +00:00
|
|
|
{t("components.datatable.asc")}
|
2025-10-16 11:18:55 +00:00
|
|
|
</DropdownMenuItem>
|
|
|
|
|
<DropdownMenuItem onClick={() => column.toggleSorting(true)}>
|
|
|
|
|
<ArrowDown />
|
2025-11-16 21:11:46 +00:00
|
|
|
{t("components.datatable.desc")}
|
2025-10-16 11:18:55 +00:00
|
|
|
</DropdownMenuItem>
|
|
|
|
|
</DropdownMenuContent>
|
|
|
|
|
</DropdownMenu>
|
|
|
|
|
</div>
|
2025-11-16 21:11:46 +00:00
|
|
|
);
|
2025-10-16 11:18:55 +00:00
|
|
|
}
|