import { createContext, Fragment, useContext, type ReactNode } from 'react' import { MoreHorizontal, Loader2, AlertCircle } from 'lucide-react' import { Button } from '@/components/ui/button' import { DESKTOP_MEDIA_QUERY, useMediaQuery } from '@/hooks/useMediaQuery' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu' import { cn } from '@/lib/utils' export const RowActionsMenuContext = createContext(false) export interface SettingsListRowAction { label: string onClick: () => void icon?: ReactNode destructive?: boolean disabled?: boolean separatorBefore?: boolean } interface SettingsListProps { isLoading?: boolean error?: Error | null isEmpty: boolean emptyTitle?: string emptyHint?: string loadingLabel?: string errorTitle?: string maxHeightClassName?: string children: ReactNode } interface SettingsListRowProps { title: ReactNode titleClassName?: string description?: ReactNode badges?: ReactNode belowDescription?: ReactNode trailing?: ReactNode primaryAction?: { label: string; onClick: () => void } actions?: SettingsListRowAction[] actionsLabel?: string onClick?: () => void className?: string } export function SettingsList({ isLoading, error, isEmpty, emptyTitle, emptyHint, loadingLabel, errorTitle, maxHeightClassName, children, }: SettingsListProps) { if (isLoading) { return (
{loadingLabel ?? 'Loading...'}
) } if (error) { return (

{errorTitle ?? 'Failed to load'}

{error.message}

) } if (isEmpty) { return (

{emptyTitle}

{emptyHint}

) } return (
{children}
) } interface SettingsListRowActionsMenuProps { actions: SettingsListRowAction[] label?: string } export function SettingsListRowActionsMenu({ actions, label }: SettingsListRowActionsMenuProps) { return ( {actions.map((action, i) => ( {action.separatorBefore && i > 0 && } setTimeout(action.onClick, 0)} className={action.destructive ? 'text-destructive focus:text-destructive' : undefined} > {action.icon} {action.label} ))} ) } export function SettingsListRow({ title, titleClassName, description, badges, belowDescription, trailing, primaryAction, actions, actionsLabel, onClick, className, }: SettingsListRowProps) { const inSettingsScope = useContext(RowActionsMenuContext) const isDesktop = useMediaQuery(DESKTOP_MEDIA_QUERY) const actionsInMenu = inSettingsScope && !isDesktop const overflowActions = primaryAction && actionsInMenu ? [ { label: primaryAction.label, onClick: primaryAction.onClick }, ...(actions ?? []).map((action, index) => (index === 0 ? { ...action, separatorBefore: true } : action)), ] : actions return (

{title}

{badges}
{description &&
{description}
} {belowDescription}
e.stopPropagation()} > {trailing} {primaryAction && !actionsInMenu && ( )} {overflowActions && overflowActions.length > 0 && ( )}
) }