import type { FlowSortKey } from "@yaakapp-internal/proxy-lib"; import { ContextMenu, Dropdown, type DropdownItem, Icon, IconButton, ResizeHandle, } from "@yaakapp-internal/ui"; import classNames from "classnames"; import { useAtomValue, useSetAtom } from "jotai"; import { memo, type MouseEvent, useCallback, useRef, useState } from "react"; import { addSortKey, allColumns, type Column, type ColumnDef, type ColumnId, type ColumnPrefs, type CustomColumnId, canHide, hideOtherColumns, isCustomColumn, removeCustomColumn, resetWidth, resetWidths, resizeColumn, toggleColumn, unavailableColumns, } from "../lib/columns"; import { columnPrefsAtom, hasAppsAtom } from "../lib/store"; import { addColumnDialogAtom } from "./AddColumnDialog"; interface Props { columns: Column[]; template: string; /** The rows' width, as CSS, so the header scrolls sideways with them */ width: string; prefs: ColumnPrefs; sort: FlowSortKey[]; /** A click on a label, or with `add`, an Option-click */ onSort: (column: ColumnId, add: boolean) => void; /** Sets the sort from the menus; empty goes back to arrival order */ onSetSort: (sort: FlowSortKey[]) => void; onResetSort: () => void; } /** * The list's only control surface: a click on a label sorts by it alone, a second reverses it * and a third goes back to arrival order; Option-click adds it to the sort as a further key. * Dragging the edge after a label resizes its column and a double-click on it resets it. A * right-click on a label opens the column's menu; the picker sits in the empty corner on hover. */ export const FlowListHeader = memo(function FlowListHeader({ columns, template, width, prefs, sort, onSort, onSetSort: setSort, onResetSort, }: Props) { const setPrefs = useSetAtom(columnPrefsAtom); const unavailable = unavailableColumns(useAtomValue(hasAppsAtom)); const openAddColumn = useSetAtom(addColumnDialogAtom); const [menu, setMenu] = useState<{ x: number; y: number; column: ColumnDef | null } | null>(null); // Widths when a drag began, so each move sets the width from where it started. const startWidth = useRef(0); // Taking out the sort's only key goes back to arrival order, as Reset Sort does const removeColumn = (id: CustomColumnId) => { if (sort.length > 0 && sort.every((k) => k.column === id)) onResetSort(); setPrefs((p) => removeCustomColumn(p, id)); }; const pickerItems: DropdownItem[] = [ { type: "content", label:
Columns
, }, ...allColumns(prefs).map((c): DropdownItem => { const shown = prefs.visible[c.id] ?? c.defaultVisible; return { label: c.label, keepOpenOnSelect: true, disabled: shown && !canHide(prefs, c.id, unavailable), leftSlot: shown ? ( ) : ( ), onSelect: () => setPrefs((p) => toggleColumn(p, c.id, unavailable)), ...(isCustomColumn(c.id) ? { submenuTrigger: "button" as const, submenu: [ { label: "Remove Column", icon: "trash" as const, color: "danger" as const, onSelect: () => removeColumn(c.id as CustomColumnId), }, ], } : {}), }; }), { label: "Add Column…", icon: "plus", onSelect: () => openAddColumn(true) }, { type: "separator" }, { label: "Reset Sort", disabled: sort.length === 0, onSelect: onResetSort }, { label: "Reset Column Widths", onSelect: () => setPrefs((p) => resetWidths(p)) }, ]; const columnItems = (c: ColumnDef): DropdownItem[] => { const sortBy = (descending: boolean) => setSort([{ column: c.id, descending }]); const addTo = (descending: boolean) => setSort(addSortKey(sort, c.id, descending)); return [ { label: "Sort Ascending", onSelect: () => sortBy(false) }, { label: "Sort Descending", onSelect: () => sortBy(true) }, { label: "Add to Sort Ascending", onSelect: () => addTo(false) }, { label: "Add to Sort Descending", onSelect: () => addTo(true) }, { label: "Clear Sorting", disabled: sort.length === 0, onSelect: onResetSort }, { type: "separator" }, { label: "Hide Column", disabled: !canHide(prefs, c.id, unavailable), onSelect: () => setPrefs((p) => toggleColumn(p, c.id, unavailable)), }, { label: "Hide Other Columns", disabled: columns.length <= 1, onSelect: () => setPrefs((p) => hideOtherColumns(p, c.id)), }, ...(isCustomColumn(c.id) ? [ { label: "Remove Column", color: "danger" as const, onSelect: () => removeColumn(c.id as CustomColumnId), }, ] : []), { type: "separator" }, { label: "Reset Column Widths", onSelect: () => setPrefs((p) => resetWidths(p)) }, ]; }; // A right-click on a label is for its column; anywhere else in the header, the picker. const openMenu = useCallback( (e: MouseEvent) => { e.preventDefault(); const el = (e.target as HTMLElement).closest("[data-column]"); const id = el?.dataset.column as ColumnId | undefined; const column = columns.find((c) => c.id === id) ?? null; setMenu({ x: e.clientX, y: e.clientY, column }); }, [columns], ); return ( // The same margins and padding as the rows, so each label sits over its cells.
{/* oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- a right-click opens the column's menu */}
{columns.map((c) => { const at = sort.findIndex((k) => k.column === c.id); const key = sort[at]; const label = ( <> {key != null && ( )} {key != null && sort.length > 1 && ( {at + 1} )} {c.id === "app" ? "" : c.label} ); const labelClass = classNames( "flex items-center gap-0.5 min-w-0 w-full h-full whitespace-nowrap", c.align === "right" && "justify-end", key != null && "text-text-subtle", ); return ( {/* On the column's right edge, the boundary with the next, resizing this one */} { startWidth.current = c.current; }} onResizeMove={(e) => setPrefs((p) => resizeColumn(p, c.id, startWidth.current + e.x - e.xStart)) } onReset={() => setPrefs((p) => resetWidth(p, c.id))} /> ); })} setMenu(null)} />
); });