import type { ComponentProps, ReactElement, ReactNode } from 'react'; import type { ColumnDef } from '@tanstack/react-table'; import { Link, type AnyRouter, type LinkComponentProps, type RegisteredRouter } from '@tanstack/react-router'; import { MoreHorizontal, Pencil, Trash2, type LucideIcon } from 'lucide-react'; import Button from '@/components/elements/Button'; import DropdownMenu from '@/components/elements/dropdown/DropdownMenu'; import Icon from '@/components/elements/Icon'; import Tooltip from '@/components/elements/tooltip/Tooltip'; import { useSkin } from '@/themes/skins'; const iconClassName = 'h-3.5 w-3.5'; /** Links and menu triggers look like the row's icon buttons. */ const useActionButton = () => useSkin('Button', { color: 'grey', size: 'xsmall', secondary: true, loading: false }); type RowActionCount = 1 | 2 | 3; export const actionWidths = { 1: 'w-14', 2: 'w-24', 3: 'w-32' } satisfies Record; /** The trailing, always-visible "Actions" column sized for `actions` icon buttons; `width` overrides the size. */ export function actionsColumn( actions: RowActionCount, cell: (row: TData) => ReactNode, width: string = actionWidths[actions] ): ColumnDef { const className = `${width} text-right`; return { id: 'actions', header: () => Actions, cell: ({ row }) => cell(row.original), enableSorting: false, meta: { headerClassName: className, cellClassName: className }, }; } /** Lays out a row's actions: edit, delete, then any overflow menu. */ export function RowActions({ children }: { children: ReactNode }) { return
{children}
; } const ActionTooltip = ({ content, children }: { content: string; children: ReactElement }) => ( {children} ); interface RowActionButtonProps extends Omit, 'children' | 'color' | 'aria-label'> { 'aria-label': string; icon: LucideIcon; /** Tooltip text; `disabledReason` replaces it while the button is disabled. */ label: string; danger?: boolean; disabledReason?: string; } type PresetActionProps = Omit; export function RowActionButton({ icon, label, danger, disabled, disabledReason, ...props }: RowActionButtonProps) { return ( ); } export const EditAction = (props: PresetActionProps) => ; export const DeleteAction = ({ label = 'Delete', ...props }: PresetActionProps & { label?: string }) => ( ); /** `EditAction` for resources edited on another page. */ export function EditLinkAction< TRouter extends AnyRouter = RegisteredRouter, const TFrom extends string = string, const TTo extends string | undefined = undefined, const TMaskFrom extends string = TFrom, const TMaskTo extends string = '', >(props: LinkComponentProps<'a', TRouter, TFrom, TTo, TMaskFrom, TMaskTo> & { 'aria-label': string }) { const button = useActionButton(); return ( ); } /** The "…" menu for actions other than edit and delete. */ export function RowActionsMenu({ label, children }: { label: string; children: ReactNode }) { const button = useActionButton(); return ( {label} } > {children} ); }