import { ContextMenu, Dropdown, type DropdownItem, Icon, IconButton, ResizeHandle, } from "@yaakapp-internal/ui"; import classNames from "classnames"; import { useSetAtom } from "jotai"; import { memo, type MouseEvent, useCallback, useRef, useState } from "react"; import { type Column, COLUMNS, type ColumnId, type ColumnPrefs, type FlowSort, resetWidths, resizeColumn, toggleColumn, } from "../lib/columns"; import { useExportShownHar } from "../lib/flowActions"; import { columnPrefsAtom } from "../lib/store"; interface Props { columns: Column[]; template: string; prefs: ColumnPrefs; sort: FlowSort | null; onSort: (column: ColumnId) => void; onSortByStart: () => void; } /** * The list's only control surface: a click on a label sorts by it and a second reverses it, * dragging the edge after a label resizes its column, and the picker sits in the empty corner on * hover, or anywhere on a right-click. */ export const FlowListHeader = memo(function FlowListHeader({ columns, template, prefs, sort, onSort, onSortByStart, }: Props) { const setPrefs = useSetAtom(columnPrefsAtom); const exportShown = useExportShownHar(); const [menu, setMenu] = useState<{ x: number; y: number } | null>(null); // Widths when a drag began, so each move sets the width from where it started. const startWidth = useRef(0); const items: DropdownItem[] = [ { type: "content", label:
Columns
, }, ...COLUMNS.map((c): DropdownItem => ({ label: c.label, keepOpenOnSelect: true, disabled: c.id === "path", leftSlot: (prefs.visible[c.id] ?? c.defaultVisible) || c.id === "path" ? ( ) : ( ), rightSlot: c.id === "path" ? always : undefined, onSelect: () => setPrefs((p) => toggleColumn(p, c.id)), })), { type: "separator" }, { label: "Sort by Start Time", disabled: sort == null, onSelect: onSortByStart }, { label: "Reset Column Widths", onSelect: () => setPrefs((p) => resetWidths(p)) }, { type: "separator" }, exportShown(), ]; const openMenu = useCallback((e: MouseEvent) => { e.preventDefault(); setMenu({ x: e.clientX, y: e.clientY }); }, []); return ( // oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- a right-click opens the same menu as the picker
{columns.map((c) => ( {c.id !== "path" && ( { startWidth.current = c.current; }} onResizeMove={(e) => setPrefs((p) => resizeColumn(p, c.id, startWidth.current + e.x - e.xStart)) } onReset={() => setPrefs((p) => ({ ...p, widths: { ...p.widths, [c.id]: undefined } })) } /> )} ))} setMenu(null)} />
); });