Files
opencode-manager/frontend/src/components/ui/settings-list.tsx
T

192 lines
6.5 KiB
TypeScript

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 (
<div className="flex items-center justify-center py-8">
<Loader2 className="w-4 h-4 animate-spin text-primary" />
<span className="ml-2 text-sm text-muted-foreground">{loadingLabel ?? 'Loading...'}</span>
</div>
)
}
if (error) {
return (
<div className="text-center py-6 text-muted-foreground">
<AlertCircle className="w-10 h-10 mx-auto mb-3 opacity-50 text-destructive" />
<p className="text-sm">{errorTitle ?? 'Failed to load'}</p>
<p className="text-xs mt-1">{error.message}</p>
</div>
)
}
if (isEmpty) {
return (
<div className="h-auto max-h-40 min-h-24 rounded-lg border border-dashed border-border bg-card/50 p-6 text-center text-muted-foreground [[data-opencode-settings]_&]:rounded-none [[data-opencode-settings]_&]:border-0 [[data-opencode-settings]_&]:bg-transparent">
<p className="text-sm font-medium text-foreground">{emptyTitle}</p>
<p className="text-xs mt-1">{emptyHint}</p>
</div>
)
}
return (
<div className={cn(maxHeightClassName ?? 'max-h-[420px]', 'overflow-y-auto rounded-lg border border-border [[data-opencode-settings]_&]:rounded-none [[data-opencode-settings]_&]:border-0')}>
<div className="divide-y divide-border [[data-opencode-settings]_&]:divide-y-0">{children}</div>
</div>
)
}
interface SettingsListRowActionsMenuProps {
actions: SettingsListRowAction[]
label?: string
}
export function SettingsListRowActionsMenu({ actions, label }: SettingsListRowActionsMenuProps) {
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button type="button" variant="ghost" size="icon" className="h-8 w-8 [[data-opencode-settings]_&]:max-sm:h-11 [[data-opencode-settings]_&]:max-sm:w-11" aria-label={label}>
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{actions.map((action, i) => (
<Fragment key={action.label}>
{action.separatorBefore && i > 0 && <DropdownMenuSeparator />}
<DropdownMenuItem
disabled={action.disabled}
onSelect={() => setTimeout(action.onClick, 0)}
className={action.destructive ? 'text-destructive focus:text-destructive' : undefined}
>
{action.icon}
{action.label}
</DropdownMenuItem>
</Fragment>
))}
</DropdownMenuContent>
</DropdownMenu>
)
}
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 (
<div
onClick={onClick}
className={cn(
'group flex flex-col gap-2 bg-card px-3 py-3 hover:bg-accent/50 sm:flex-row sm:items-center sm:gap-3',
'[[data-opencode-settings]_&]:flex-row [[data-opencode-settings]_&]:items-start [[data-opencode-settings]_&]:gap-2 [[data-opencode-settings]_&]:bg-transparent [[data-opencode-settings]_&]:px-2 [[data-opencode-settings]_&]:py-2',
onClick && 'cursor-pointer',
className,
)}
>
<div className="min-w-0 flex-1 self-stretch sm:self-auto [[data-opencode-settings]_&]:self-auto">
<div className="flex min-w-0 items-start gap-2 [[data-opencode-settings]_&]:items-center [[data-opencode-settings]_&]:max-sm:min-h-11 [[data-opencode-settings]_&]:sm:min-h-8">
<p className={cn('min-w-0 flex-1 truncate text-sm font-medium [[data-opencode-settings]_&]:whitespace-normal [[data-opencode-settings]_&]:break-words', titleClassName)}>{title}</p>
{badges}
</div>
{description && <div className="mt-1 truncate text-xs text-muted-foreground [[data-opencode-settings]_&]:mt-0.5">{description}</div>}
{belowDescription}
</div>
<div
className="flex shrink-0 items-center justify-end gap-1 sm:justify-start"
onClick={(e) => e.stopPropagation()}
>
{trailing}
{primaryAction && !actionsInMenu && (
<Button
type="button"
size="sm"
onClick={primaryAction.onClick}
className="flex-1 sm:flex-none [[data-opencode-settings]_&]:flex-none [[data-opencode-settings]_&]:bg-transparent [[data-opencode-settings]_&]:text-foreground [[data-opencode-settings]_&]:hover:bg-accent [[data-opencode-settings]_&]:hover:text-accent-foreground"
>
{primaryAction.label}
</Button>
)}
{overflowActions && overflowActions.length > 0 && (
<SettingsListRowActionsMenu actions={overflowActions} label={actionsLabel} />
)}
</div>
</div>
)
}