import type { MouseEvent } from "@opentui/core";
import type { BoxProps as OpenTuiBoxProps } from "@opentui/react";
import { useState, type ReactNode } from "react";
import { BAR, NO_BORDER } from "./edges.ts";
import { colors } from "./theme.ts";
/** Hover state for a clickable box. Moving between a box and its own text sends out then over, so it settles on the right value. */
export function useHover() {
const [hover, setHover] = useState(false);
return [hover, { onMouseOver: () => setHover(true), onMouseOut: () => setHover(false) }] as const;
}
/** True for a plain left click. */
export const isLeftClick = (event: MouseEvent) => event.button === 0;
export const isRightClick = (event: MouseEvent) => event.button === 2;
/**
* A clickable label with its key beside it: `Approve y`. It takes the click for itself, so the
* card or pane under it doesn't react too, and its text can't be drag-selected.
*/
export function Button(props: { readonly label: string; readonly shortcut?: string | undefined; readonly color?: string | undefined; /** No background or padding: a bare clickable label, as in the header. */ readonly plain?: boolean; readonly onPress: () => void }) {
const [hover, hoverProps] = useHover();
return (
{
event.stopPropagation();
if (isLeftClick(event)) props.onPress();
}}
>
{props.label}
{props.shortcut ? {` ${props.shortcut}`} : null}
);
}
/** Buttons in a row, wrapping on narrow screens. */
export function Buttons({ children }: { readonly children: ReactNode }) {
return (
{children}
);
}
type BoxProps = Omit;
/** A list row that lights up under the pointer: a click picks it, a right-click opens its menu when it has one. */
export function ClickRow({ selected, selectedBg, onPress, onMenu, children, ...box }: BoxProps & { readonly selected?: boolean; readonly selectedBg?: string; readonly onPress: () => void; readonly onMenu?: (() => void) | undefined; readonly children: ReactNode }) {
const [hover, hoverProps] = useHover();
return (
{
if (isRightClick(event) && onMenu) return onMenu();
if (isLeftClick(event)) onPress();
}}
>
{children}
);
}
/** Wheel direction as a step: -1 up, 1 down, 0 for anything else. */
export function wheelStep(event: MouseEvent): -1 | 0 | 1 {
return event.scroll?.direction === "up" ? -1 : event.scroll?.direction === "down" ? 1 : 0;
}
/**
* The 1-column drag target between the conversation and the list or details, or with
* `horizontal` the 1-row one above the terminal drawer. At rest it is invisible
* (the panel/background shade change is the edge, and the bar is drawn in the background
* colour, which also keeps it findable on screen for tests); under the pointer or while
* dragging it shows as an accent bar. OpenTUI sends a drag to whatever is under the
* pointer, so the app's root follows the drag (see App).
*/
export function ResizeHandle(props: { readonly active: boolean; readonly horizontal?: boolean; readonly onStart: (event: MouseEvent) => void; readonly onReset: () => void }) {
const [hover, hoverProps] = useHover();
return (
{
event.stopPropagation();
if (isRightClick(event)) props.onReset();
else if (isLeftClick(event)) props.onStart(event);
}}
/>
);
}
/**
* A one-line strip of tabs: the active one in the accent on a raised block, the rest muted.
* A click switches; anything after the tabs (a `+`, a key hint) goes in `children`.
*/
export function Tabs(props: { readonly tabs: readonly { readonly id: string; readonly label: string }[]; readonly active: string | null; readonly onPick: (id: string) => void; readonly children?: ReactNode }) {
return (
{props.tabs.map((tab) => (
props.onPick(tab.id)} />
))}
{props.children}
);
}
export function Tab(props: { readonly label: string; readonly active?: boolean; readonly onPress: () => void }) {
const [hover, hoverProps] = useHover();
return (
{
event.stopPropagation();
if (isLeftClick(event)) props.onPress();
}}
>
{props.label}
);
}