'use client'; import { Text, XStack, YStack } from '@hanzo/gui'; import * as React from 'react'; // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- export interface ExpirationOption { date: string; dte: number; type: 'weekly' | 'monthly' | 'quarterly'; } export interface ExpirationSelectorProps extends Omit, 'onSelect'> { readonly expirations: ExpirationOption[]; readonly selected: string; readonly onSelect: (date: string) => void; readonly showGroups?: boolean; } // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- function formatExpDate(date: string): string { const d = new Date(date); return d.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }); } const GROUP_ORDER: ExpirationOption['type'][] = ['weekly', 'monthly', 'quarterly']; const GROUP_LABELS: Record = { weekly: 'Weekly', monthly: 'Monthly', quarterly: 'Quarterly', }; // --------------------------------------------------------------------------- // ExpirationPill // --------------------------------------------------------------------------- interface ExpirationPillProps { readonly exp: ExpirationOption; readonly isSelected: boolean; readonly onClick: () => void; } // Resting and selected are the same two states every selectable pill in this // package uses (tabs, menu rows): a solid fill when selected, a bordered card // otherwise. Stacks carry no `color` of their own (only Text does — same // reason they carry no `fontSize`), so ink is computed per-state below and // set directly on each Text rather than left to cascade from the button. const SHAPE = { selected: { borderColor: 'transparent', backgroundColor: 'var(--color-selected-control-bg)', }, resting: { borderColor: 'var(--color-border-divider)', backgroundColor: 'var(--card)', hoverStyle: { borderColor: 'var(--color-hover)' }, }, } as const; function ExpirationPill({ exp, isSelected, onClick }: ExpirationPillProps) { const shape = isSelected ? SHAPE.selected : SHAPE.resting; const dateColor = isSelected ? 'var(--color-selected-control-text)' : 'var(--color-text-secondary)'; // Near-expiry is a notice, and a notice in Lux is the loudest neutral — it // only shows while resting; a selected pill's own fill already carries the // emphasis. const dteColor = exp.dte <= 7 && !isSelected ? 'var(--color-status-warn)' : isSelected ? 'var(--color-selected-control-text)' : 'var(--muted-foreground)'; return ( } onClick={ onClick } alignItems="center" gap={ 2 } borderRadius={ 6 } paddingHorizontal={ 12 } paddingVertical={ 6 } flexShrink={ 0 } borderWidth={ 1 } borderStyle="solid" cursor="pointer" { ...(shape as object) } > { formatExpDate(exp.date) } { exp.dte }d ); } // --------------------------------------------------------------------------- // ExpirationSelector // --------------------------------------------------------------------------- export const ExpirationSelector = React.forwardRef( function ExpirationSelector(props, ref) { const { expirations, selected, onSelect, showGroups = false, className, ...rest } = props; const scrollRef = React.useRef(null); // Scroll selected pill into view on mount / change React.useEffect(() => { const container = scrollRef.current; if (!container) return; const selectedEl = container.querySelector('[data-selected="true"]'); if (selectedEl) { selectedEl.scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'center' }); } }, [selected]); if (showGroups) { const grouped = GROUP_ORDER.map((type) => ({ type, label: GROUP_LABELS[type], items: expirations.filter((e) => e.type === type), })).filter((g) => g.items.length > 0); return ( { grouped.map((group) => ( { group.label } { group.items.map((exp) => (
onSelect(exp.date) }/>
)) }
)) }
); } return ( { expirations.map((exp) => (
onSelect(exp.date) }/>
)) }
); }, );