"use client"
import * as React from "react"
import { cn } from "../../lib/utils"
import { filterPresetForCellKind } from "../../lib/column-cell-kind"
import { initialsFromDisplayName } from "../../lib/column-filter-range-policy"
import { AvatarInitials } from "../ui/avatar"
import { Badge } from "../ui/badge"
import { ROW_DRAG_COLUMN_KEY } from "./row-reorder-handle"
import type { ColumnDef, ColumnCellKind, SortDir, StatusBadgeTone } from "./types"
/**
* DataTable cells use `max-w-0` + `overflow-hidden` for truncation. Outset
* focus rings paint outside the control and get clipped to corner stubs.
* Force inset rings on common focusable descendants (hub cell renderers,
* links, icon buttons, checkboxes, Tip `tabIndex={0}` glyphs).
*
* `p-1 -m-1` covers what `ring-inset` cannot: a plain `outline`, which several
* focusable descendants use instead of a ring and which has no inset form. The
* padding grows the clip box while the negative margin keeps the content box the
* same size and in the same place, so truncation is unchanged and a link or Tip
* glyph flush with the cell's leading edge keeps its whole indicator.
*/
export const CELL_CLIP_FOCUS_INSET_CLASS =
"p-1 -m-1 [&_a:focus-visible]:ring-inset [&_button:focus-visible]:ring-inset [&_[role=checkbox]:focus-visible]:ring-inset [&_[tabindex]:focus-visible]:ring-inset"
/** When `ColumnDef.label` is empty, use a standard name for select/actions columns. */
export function defaultColumnHeaderLabel(key: string): string | undefined {
switch (key) {
case "select":
return "Select"
case "actions":
return "Actions"
case ROW_DRAG_COLUMN_KEY:
return "Reorder"
default:
return undefined
}
}
/**
* Side padding for a column whose content is a control rather than text.
*
* `px-3` is right for text, which has nothing to lose by starting 12px in. It is
* wrong for a fixed-size control: a 48px actions column left 24px of cell for a
* 32px icon button, and since cells clip (`max-w-0` + `overflow-hidden`, so long
* text truncates) the button lost 4px off each side along with the near edges of
* its focus ring. Keyboard focus then read as a ring sliced flat top and bottom.
*
* 4px keeps the control's own box intact in a 48px column and still reads as a
* gutter, because the control is centred and narrower than the cell.
*/
export const CONTROL_COLUMN_PADDING = "px-1"
/**
* The group divider's own box. Shared by the row in the table and by the
* clone that pins under the column header, because the clone only reads as
* the same bar if it is measured and painted as one — the pin maths uses the
* real row's height for the hand-off.
*/
export const GROUP_BAR_CLASS =
"flex items-center gap-2 px-4 py-1.5 text-xs font-semibold text-muted-foreground tracking-wide bg-dt-group-bg select-none"
/**
* With a checkbox leading the bar, the bar's own start padding is dropped: the
* checkbox is centred inside a slot the width of the select column instead, so
* it lands on the same axis as the row checkboxes beneath it rather than 4px to
* their right (the column is 40px with a 16px control, so its centre is 20px;
* `px-4` put the bar's box at 16px).
*/
export const GROUP_BAR_WITH_SELECT_CLASS = "ps-0 gap-3"
/**
* How far a group divider travels from the neutral divider grey toward its
* status fill. Not the full fill: the divider is a full-bleed band, and at 100%
* it stops reading as a divider and starts reading as a section of coloured
* rows. 55% is far enough to name the status at a glance and near enough to keep
* the band quieter than the chip sitting on it.
*/
export const GROUP_TINT_STRENGTH = 55
/**
* The divider's fill for a group whose column declared a tone for this value.
* Mixed in `oklab`, not `oklch`: OKLCH interpolates hue the short way round, and
* amber (80°) toward the grey divider (270°) swings through red, so a warning
* band came out pink.
*/
export function groupBarTint(tone: StatusBadgeTone): string {
return `color-mix(in oklab, var(--status-badge-${tone}-fill) ${GROUP_TINT_STRENGTH}%, var(--dt-group-bg))`
}
/**
* `select` is the group's own checkbox, and the slot it sits in is the select
* column's box. `gap-3` after it matches the data columns' `px-3`, so the group
* label starts on the same axis as the first column's text.
*
* The pinned clone passes a non-interactive mirror, so the bar does not change
* shape as it pins.
*/
export function groupBarContent(
groupLabel: React.ReactNode,
count: number,
select?: React.ReactNode,
selectSlotWidth?: number,
): React.ReactNode {
return (
<>
{select ? (
{select}
) : null}
{groupLabel}
{count}
>
)
}
/**
* What `useTableState` groups rows under when the grouped field is empty.
* The divider says `None` instead: a lone dash is not a word, and the band is
* a heading (`exxat-copy-discipline`).
*/
export const GROUP_EMPTY_KEY = "—"
/** Sub-rows a split cell shows before it collapses the rest into "+N more". */
export const SPLIT_MAX_VISIBLE = 4
/** Cell kinds whose own icon would say less than the group's name already does. */
export const GROUP_KIND_PLAIN: ReadonlySet = new Set([
"text",
"tags",
"attachment",
"external-link",
])
/**
* A group heading in the grouped column's own vocabulary, for columns that do
* not ship `filter.options` to render from.
*
* A person column groups on the display name, so the divider can rebuild the
* identity the cells show — avatar plus name, no email, because the band is one
* line and a heading, not a record (`exxat-person-identity-display`). Every
* other kind leads with the icon its filter menu uses, so "Type", "Level", and
* "Due" dividers are told apart at a glance by the same mark the reader already
* associates with that column.
*/
export function groupKindNode(
label: string,
kind: ColumnCellKind | undefined,
): React.ReactNode {
if (!kind) return label
if (kind === "person" || kind === "people-rail") {
return (
{label}
)
}
if (GROUP_KIND_PLAIN.has(kind)) return label
return (
{label}
)
}
/** Columns the table lays out around a control, not around text. */
export function isControlColumn(key: string): boolean {
return key === "select" || key === "actions" || key === ROW_DRAG_COLUMN_KEY
}
export function resolvedColumnLabel(col: ColumnDef): string {
const t = col.label?.trim()
if (t) return t
return defaultColumnHeaderLabel(col.key) ?? col.key
}
// ─────────────────────────────────────────────────────────────────────────────
// Internal sub-components
// ─────────────────────────────────────────────────────────────────────────────
export const SortChevron = React.memo(function SortChevron({ dir }: { dir: SortDir }) {
return (
)
})
// ─────────────────────────────────────────────────────────────────────────────
// Sticky shadow utility
// ─────────────────────────────────────────────────────────────────────────────
export function stickyShadow(pin: "left" | "right" | undefined): string {
if (!pin) return ""
const base = "after:content-[''] after:absolute after:top-0 after:bottom-0 after:w-3 after:pointer-events-none"
if (pin === "left") {
return cn(
base,
"after:start-full",
"after:bg-[linear-gradient(to_right,var(--sticky-edge-fade),transparent)]",
)
}
return cn(
base,
"after:end-full",
"after:bg-[linear-gradient(to_left,var(--sticky-edge-fade),transparent)]",
)
}
/**
* A mouse click leaves DOM focus parked on the element (a column's ⋯ trigger)
* with no visible ring, and Radix restores that focus when the menu closes.
* The sticky header only needs to yield to focus that is actually *drawn*, so
* plain `:focus` would let one mouse click suppress the sticky clone for the
* rest of the session.
*/
export function hasVisibleFocus(el: Element | null | undefined): boolean {
if (!el) return false
try {
return el.matches(":focus-visible")
} catch {
// Engines without `:focus-visible` (older jsdom) keep the permissive path.
return true
}
}