import * as React from "react";
import { ExternalLinkIcon, type LucideIcon } from "lucide-react";
import { Badge, type BadgeProps } from "./badge";
import { Button } from "./button";
import { NewTabHint } from "./component-labels";
import { cn } from "../lib/cn";
import { UiLink } from "./link";
// The design system's components take a slot as an element or as plain data:
// an action as { label, variant, icon }, a badge as { tone, label }, a link as
// { label, href, external }. The data keeps the design system's names; this
// file turns it into the primitives. Where the design system names an icon of
// its own set, the dashboard passes the Lucide component.
/** A Lucide component, or an element that draws the icon itself. */
export type IconSlot = LucideIcon | React.ReactNode;
/** What a page writes in a list to withhold an entry: `canEdit && { … }`.
* Every list these components take skips such an entry. */
export type Falsy = false | null | undefined;
// The boards' markup names an icon ("plus", "alert-triangle"), and nothing in
// this app's types stops a page from copying that. Drawn as it is, the name
// would be a word inside the button, and in its accessible name.
function reportIconName(name: string) {
if (process.env.NODE_ENV !== "production") {
console.error(
`An icon is a Lucide component here, not the design system's icon name ("${name}"): import it from lucide-react and pass the component.`,
);
}
}
function isComponent(icon: unknown): icon is LucideIcon {
// `typeof null` is "object", and `in` throws on null.
if (icon == null) return false;
if (typeof icon === "function") return true;
return (
typeof icon === "object" &&
!React.isValidElement(icon) &&
"$$typeof" in icon
);
}
/** A component is drawn as decoration; an element is left as it is. */
export function renderIcon(icon: IconSlot, className?: string) {
if (icon == null || icon === false) return null;
if (typeof icon === "string") {
reportIconName(icon);
return null;
}
if (!isComponent(icon)) return icon;
const Icon = icon;
return ;
}
/** An action given as data, wherever a component takes `actions`. */
export interface ActionData {
label: React.ReactNode;
/**
* The design system's names: `primary` (the default), `secondary`,
* `neutral`, `danger`; `text` is a button drawn as a link, `link` a link to
* `href` in the text's own size (without an `href` it is a text button: a
* link that leads nowhere is not a link).
*/
variant?: "primary" | "secondary" | "neutral" | "link" | "danger" | "text";
icon?: LucideIcon;
iconRight?: LucideIcon;
/** Renders a link with the same look. */
href?: string;
/** sm 40px (the default), md 48px. The dashboard has no 54px button: lg is
* 48px too. */
size?: "sm" | "md" | "lg";
disabled?: boolean;
/** Gets the click: its `currentTarget` is the button, which Safari does not
* focus (ACC-644 J7). */
onClick?: (event: React.MouseEvent) => void;
/** Tells this action from the others of its list. An action is told by
* its place, which holds while its words change ("Salva", "Salvataggio…")
* and while another is withheld (`canEdit && { … }` keeps the place). Give
* a key when the list itself gets shorter or changes order: otherwise the
* button of an action that leaves becomes the next one in place, with the
* focus still on it. */
key?: string;
}
const BUTTON_VARIANT = {
primary: "default",
secondary: "secondary",
neutral: "outline",
danger: "destructive",
text: "link",
} as const;
const BUTTON_SIZE = { sm: "default", md: "lg", lg: "lg" } as const;
/** The design system's TextLink: Electric Blue words, underlined on hover.
* Whatever draws one marks it `data-slot="text-link"`, so a dark surface (the
* navy `ActionBar`) can turn it white. */
export const textLinkClassName =
"text-primary active:text-secondary-violet-100 focus-visible:outline-ring aria-disabled:text-primary-black-40 inline-flex items-center gap-1 rounded-sm underline-offset-6 hover:underline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-solid aria-disabled:pointer-events-none";
/**
* The line under a link inside a sentence, as under the design system's link
* button: 1px, 3px below the words, 2px under the pointer. The rule (Marco,
* 2026-10-19): a link among words is always underlined, since Electric Blue
* is 1.70:1 against the secondary text and colour alone cannot tell it apart
* (WCAG 1.4.1). A link on its own — a TextLink with its arrow, a menu, the
* trail, a cell or a line holding only the link — is not: that is
* `textLinkClassName`, unless it has the colour of the words around it (the
* trail, a link cell): then the line is what marks it, and it takes this one.
* On its own for those and for a ground of another colour (white on navy);
* otherwise `inlineLinkClassName`.
*/
export const inlineLinkLine =
"underline decoration-1 underline-offset-3 hover:decoration-2";
/** A link inside a sentence: the TextLink's colours and states, flowing with
* the words rather than a box beside them, and underlined. */
export const inlineLinkClassName = `text-primary hover:text-primary-electric-blue-120 active:text-secondary-violet-100 focus-visible:outline-ring aria-disabled:text-primary-black-40 rounded-sm ${inlineLinkLine} focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-solid aria-disabled:pointer-events-none`;
function isData(value: unknown): value is Record {
return (
typeof value === "object" &&
value !== null &&
!Array.isArray(value) &&
!React.isValidElement(value)
);
}
export function isActionData(value: unknown): value is ActionData {
return isData(value) && "label" in value;
}
export function renderAction(action: React.ReactNode | ActionData) {
if (!isActionData(action)) return action;
const {
label,
variant = "primary",
icon,
iconRight,
href,
size = "sm",
disabled,
onClick,
} = action;
if (variant === "link" && (href != null || disabled)) {
const classes = cn(textLinkClassName, "text-base leading-6");
// A link that leads nowhere for now: named as a link, and out of reach.
if (disabled) {
return (
{label}
);
}
return (
{label}
);
}
const look = {
variant: BUTTON_VARIANT[variant === "link" ? "text" : variant],
size: BUTTON_SIZE[size],
};
const content = (
<>
{renderIcon(icon)}
{label}
{renderIcon(iconRight)}
>
);
if (href && !disabled) {
return (
);
}
return (
);
}
/**
* Whether a slot has anything to draw. A list with nothing in it counts as
* empty: a page writes `actions={[canEdit && { … }]}` to withhold a button
* from a reader, and the box of the actions must not be left behind.
*/
export function hasSlot(
slot: React.ReactNode | object | ReadonlyArray,
): boolean {
return Array.isArray(slot) ? slot.some(Boolean) : Boolean(slot);
}
/** One action, or a list of them: data and elements can be mixed. */
export function renderActions(
actions:
| React.ReactNode
| ActionData
| ReadonlyArray,
) {
if (!Array.isArray(actions)) {
return renderAction(actions as React.ReactNode | ActionData);
}
// An action is its place in the list unless it brings a key (see
// `ActionData.key`). Not its label: a button whose words change would be
// made anew under the finger that pressed it, and lose the focus.
return actions.map((action, index) => {
let key = `at:${index}`;
if (isActionData(action)) {
if (action.key != null) key = `key:${action.key}`;
} else if (React.isValidElement(action) && action.key != null) {
key = `element:${action.key}`;
}
return {renderAction(action)};
});
}
type BadgeTone = Extract<
NonNullable,
"success" | "warning" | "error" | "info" | "progress" | "neutral" | "locked"
>;
/** A badge given as data. */
export interface BadgeData {
tone: BadgeTone;
label: React.ReactNode;
/** Replaces the tone's icon (a Lucide component, or an element); `null`
* for none. */
icon?: IconSlot | null;
}
export function isBadgeData(value: unknown): value is BadgeData {
return isData(value) && "tone" in value;
}
export function renderBadge(badge: React.ReactNode | BadgeData) {
if (!isBadgeData(badge)) return badge;
// An icon of the caller's is drawn here and goes in as the badge's first
// child, which `Badge` sizes and colours like its own. As the `icon` prop
// it would be a function handed to a client component, and a page rendered
// on the server cannot do that.
const own = badge.icon == null ? null : renderIcon(badge.icon);
return (
// On one line, as the design system's badge: in a table cell that wraps
// its text the status would otherwise break in two.
{own}
{badge.label}
);
}
/** A link given as data. `external` opens another tab, and says so. */
export interface LinkData {
label: React.ReactNode;
href: string;
external?: boolean;
}
export function isLinkData(value: unknown): value is LinkData {
return isData(value) && "href" in value && "label" in value;
}
export function renderLink(link: LinkData, className?: string) {
const classes = cn(textLinkClassName, className);
if (!link.external) {
return (
{link.label}
);
}
return (
{link.label}
);
}