import type { MouseEvent, ReactNode } from 'react' import { MoreVertical, Power, PowerOff, RefreshCw, Save, Trash2 } from 'lucide-react' import type { Library, LibraryRoot } from '../types' import type { RootDraft } from './adminLibraryPanelModel' import { displayLibraryRootName, displayLibraryRootPath, fallbackLibraryRoot } from './adminLibraryPanelModel' type LibraryTableProps = { libs: Library[] editableRootDraft: (libraryID: string, root: LibraryRoot) => RootDraft onEditableRootChange: (libraryID: string, root: LibraryRoot, patch: Partial) => void onSaveRoot: (libraryID: string, root: LibraryRoot) => void onScanRoot: (libraryID: string, root: LibraryRoot) => void onToggleRoot: (libraryID: string, root: LibraryRoot) => void onRemoveRoot: (library: Library, root: LibraryRoot) => void onScanLibrary: (library: Library) => void onRemoveLibrary: (library: Library) => void } export function AdminLibraryTable({ libs, ...actions }: LibraryTableProps) { return (
{libs.map((library) => ( ))}
名称 路径 类型 操作
) } type LibraryTableRowProps = Omit & { library: Library } function LibraryTableRow({ library, ...actions }: LibraryTableRowProps) { return ( {library.name} {library.type} ) } function LibraryRootsCell({ library, ...actions }: LibraryTableRowProps) { const roots = library.roots?.length ? library.roots : [fallbackLibraryRoot(library)] return (
{roots.map((root) => ( ))}
) } type RootEditorProps = Omit & { library: Library root: LibraryRoot } function ExistingRootEditor({ library, root, ...actions }: RootEditorProps) { const draft = actions.editableRootDraft(library.id, root) return (
{root.id ? : }
) } function ReadonlyRootFields({ root }: { root: LibraryRoot }) { return ( <> {displayLibraryRootName(root.name, root.path)} {displayLibraryRootPath(root.path)} ) } function EditableRootFields({ library, root, draft, onEditableRootChange }: RootEditorProps & { draft: RootDraft }) { return ( <> onEditableRootChange(library.id, root, { name: e.target.value })} /> onEditableRootChange(library.id, root, { path: e.target.value })} /> ) } function RootStatus({ enabled }: { enabled: boolean }) { return ( {enabled ? '启用' : '禁用'} ) } function RootActionButtons({ library, root, draft, ...actions }: RootEditorProps & { draft: RootDraft }) { const enabled = draft.enabled ?? root.enabled return ( {root.id && ( } label="保存" onClick={() => actions.onSaveRoot(library.id, root)} > 保存 )} } label="扫描" onClick={() => actions.onScanRoot(library.id, root)} > 扫描 {root.id && ( : } label={enabled ? '禁用' : '启用'} onClick={() => actions.onToggleRoot(library.id, root)} > {enabled ? '禁用' : '启用'} )} {root.id && ( } label="删除" onClick={() => actions.onRemoveRoot(library, root)} > 删除 )} ) } function LibraryActionsCell({ library, onScanLibrary, onRemoveLibrary }: LibraryTableRowProps) { return ( } label="扫描" onClick={() => onScanLibrary(library)}> 扫描 } label="删除" onClick={() => onRemoveLibrary(library)}> 删除 ) } function ActionMenu({ label, children }: { label: string; children: ReactNode }) { return (
{children}
) } function MenuButton({ icon, label, danger, onClick, children, }: { icon: ReactNode label: string danger?: boolean onClick: () => void children: ReactNode }) { const handleClick = (event: MouseEvent) => { event.currentTarget.closest('details')?.removeAttribute('open') onClick() } return ( ) }