"use client"; import type { Table } from "@tanstack/react-table"; import type { LucideIcon } from "lucide-react"; import { useEffect, useState } from "react"; import { useDebounce } from "@calcom/lib/hooks/useDebounce"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { Button } from "../button"; import { Input } from "../form"; import { DataTableFilter } from "./DataTableFilter"; export type FilterableItems = { title: string; tableAccessor: string; options: { label: string; value: string; icon?: LucideIcon; }[]; }[]; interface DataTableToolbarProps { table: Table; filterableItems?: FilterableItems; searchKey?: string; tableCTA?: React.ReactNode; onSearch?: (value: string) => void; } export function DataTableToolbar({ table, filterableItems, tableCTA, searchKey, onSearch, }: DataTableToolbarProps) { // TODO: Is there a better way to check if the table is filtered? // If you select ALL filters for a column, the table is not filtered and we dont get a reset button const isFiltered = table.getState().columnFilters.length > 0; const [searchTerm, setSearchTerm] = useState(""); const debouncedSearchTerm = useDebounce(searchTerm, 500); useEffect(() => { onSearch?.(debouncedSearchTerm); }, [debouncedSearchTerm, onSearch]); const { t } = useLocale(); return (
{searchKey && ( table.getColumn(searchKey)?.setFilterValue(event.target.value.trim())} /> )} {onSearch && ( { setSearchTerm(e.target.value); }} /> )} {isFiltered && ( )} {filterableItems && filterableItems?.map((item) => { const foundColumn = table.getColumn(item.tableAccessor); if (foundColumn?.getCanFilter()) { return ( ); } })} {tableCTA ? tableCTA : null}
); }