import { forwardRef, useState } from 'react' import type { HTMLAttributes, ButtonHTMLAttributes } from 'react' import { cn } from '@/lib/utils' import type { ReactNode } from 'react' import { Button } from '../basic/button' import { CloseLine, WarningLine } from '../basic/icons-inline' import { Scrollbar } from '../basic/scrollbar' export interface PermissionCardAction extends Omit, 'children'> { label: string } export interface PermissionCardProps extends HTMLAttributes { title?: string description?: string command: string commandIntent?: string isLoadingIntent?: boolean onClose?: () => void actions?: { deny?: PermissionCardAction allowOnce?: PermissionCardAction } closeIcon?: ReactNode warningIcon?: ReactNode /** 主库 i18n:展开/折叠按钮文案 */ expandLabel?: string collapseLabel?: string commaLabel?: string } const COMMAND_COLLAPSE_THRESHOLD = 80 function omitLabel(a: PermissionCardAction | undefined): Omit { if (!a) return {} const { label: _label, ...rest } = a void _label return rest } const defaultTitle = 'Permission required' const defaultDescription = 'This action requires your permission.' const defaultExpand = 'Expand' const defaultCollapse = 'Collapse' const defaultComma = ', ' export const PermissionCard = forwardRef( ( { title, description, command, commandIntent, isLoadingIntent = false, onClose, actions, className, closeIcon, warningIcon, expandLabel = defaultExpand, collapseLabel = defaultCollapse, commaLabel = defaultComma, ...props }, ref ) => { const displayTitle = title ?? defaultTitle const displayDescription = description ?? defaultDescription const shouldCollapse = command.length > COMMAND_COLLAPSE_THRESHOLD const [isExpanded, setIsExpanded] = useState(!shouldCollapse) const defaultClose = closeIcon ?? const defaultWarning = warningIcon ?? return (
{onClose && ( )}
{defaultWarning}

{displayTitle}

{isLoadingIntent ? ( {displayDescription} ) : ( <> {displayDescription} {commandIntent && {commaLabel}{commandIntent}} )}

{isExpanded || !shouldCollapse ? (
{command}
) : (
{command}
)} {shouldCollapse && ( )}
{actions?.deny && ( )} {actions?.allowOnce && ( )}
) } ) PermissionCard.displayName = 'PermissionCard'