|
| 1 | +// ======================================== |
| 2 | +// Inline Template Panel Component |
| 3 | +// ======================================== |
| 4 | +// Compact template list for the left sidebar, uses useTemplates hook |
| 5 | + |
| 6 | +import { useState, useCallback, useMemo } from 'react'; |
| 7 | +import { Search, Loader2, FileText, Download, GitBranch } from 'lucide-react'; |
| 8 | +import { cn } from '@/lib/utils'; |
| 9 | +import { Input } from '@/components/ui/Input'; |
| 10 | +import { Badge } from '@/components/ui/Badge'; |
| 11 | +import { useTemplates, useInstallTemplate } from '@/hooks/useTemplates'; |
| 12 | +import { useFlowStore } from '@/stores'; |
| 13 | +import type { FlowTemplate } from '@/types/execution'; |
| 14 | + |
| 15 | +// ========== Sub-Components ========== |
| 16 | + |
| 17 | +interface TemplateItemProps { |
| 18 | + template: FlowTemplate; |
| 19 | + onInstall: (template: FlowTemplate) => void; |
| 20 | + isInstalling: boolean; |
| 21 | +} |
| 22 | + |
| 23 | +function TemplateItem({ template, onInstall, isInstalling }: TemplateItemProps) { |
| 24 | + return ( |
| 25 | + <button |
| 26 | + onClick={() => onInstall(template)} |
| 27 | + disabled={isInstalling} |
| 28 | + className={cn( |
| 29 | + 'w-full flex items-center gap-3 px-3 py-2.5 rounded-md text-left transition-colors', |
| 30 | + 'hover:bg-muted/60 active:bg-muted', |
| 31 | + isInstalling && 'opacity-50 cursor-wait' |
| 32 | + )} |
| 33 | + > |
| 34 | + <div className="flex-1 min-w-0"> |
| 35 | + <div className="text-sm font-medium text-foreground truncate"> |
| 36 | + {template.name} |
| 37 | + </div> |
| 38 | + <div className="flex items-center gap-2 mt-0.5"> |
| 39 | + <span className="text-xs text-muted-foreground flex items-center gap-1"> |
| 40 | + <GitBranch className="w-3 h-3" /> |
| 41 | + {template.nodeCount} nodes |
| 42 | + </span> |
| 43 | + {template.category && ( |
| 44 | + <Badge variant="secondary" className="text-[10px] px-1.5 py-0"> |
| 45 | + {template.category} |
| 46 | + </Badge> |
| 47 | + )} |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + {isInstalling ? ( |
| 51 | + <Loader2 className="w-4 h-4 animate-spin text-muted-foreground shrink-0" /> |
| 52 | + ) : ( |
| 53 | + <Download className="w-4 h-4 text-muted-foreground shrink-0 opacity-0 group-hover:opacity-100" /> |
| 54 | + )} |
| 55 | + </button> |
| 56 | + ); |
| 57 | +} |
| 58 | + |
| 59 | +// ========== Main Component ========== |
| 60 | + |
| 61 | +interface InlineTemplatePanelProps { |
| 62 | + className?: string; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Compact template browser for the left sidebar. |
| 67 | + * Loads templates via the useTemplates API hook and displays them in a searchable list. |
| 68 | + * Clicking a template installs it as the current flow. |
| 69 | + */ |
| 70 | +export function InlineTemplatePanel({ className }: InlineTemplatePanelProps) { |
| 71 | + const [searchQuery, setSearchQuery] = useState(''); |
| 72 | + const [installingId, setInstallingId] = useState<string | null>(null); |
| 73 | + |
| 74 | + const setCurrentFlow = useFlowStore((state) => state.setCurrentFlow); |
| 75 | + |
| 76 | + const { data, isLoading, error } = useTemplates(); |
| 77 | + const installTemplate = useInstallTemplate(); |
| 78 | + |
| 79 | + // Filter templates by search query |
| 80 | + const filteredTemplates = useMemo(() => { |
| 81 | + if (!data?.templates) return []; |
| 82 | + if (!searchQuery.trim()) return data.templates; |
| 83 | + |
| 84 | + const query = searchQuery.toLowerCase(); |
| 85 | + return data.templates.filter( |
| 86 | + (t) => |
| 87 | + t.name.toLowerCase().includes(query) || |
| 88 | + t.description?.toLowerCase().includes(query) || |
| 89 | + t.category?.toLowerCase().includes(query) || |
| 90 | + t.tags?.some((tag) => tag.toLowerCase().includes(query)) |
| 91 | + ); |
| 92 | + }, [data?.templates, searchQuery]); |
| 93 | + |
| 94 | + // Handle install - load template as current flow |
| 95 | + const handleInstall = useCallback( |
| 96 | + async (template: FlowTemplate) => { |
| 97 | + setInstallingId(template.id); |
| 98 | + try { |
| 99 | + const result = await installTemplate.mutateAsync({ |
| 100 | + templateId: template.id, |
| 101 | + }); |
| 102 | + setCurrentFlow(result.flow); |
| 103 | + } catch (err) { |
| 104 | + console.error('Failed to install template:', err); |
| 105 | + } finally { |
| 106 | + setInstallingId(null); |
| 107 | + } |
| 108 | + }, |
| 109 | + [installTemplate, setCurrentFlow] |
| 110 | + ); |
| 111 | + |
| 112 | + return ( |
| 113 | + <div className={cn('flex-1 flex flex-col overflow-hidden', className)}> |
| 114 | + {/* Search */} |
| 115 | + <div className="px-3 py-2"> |
| 116 | + <div className="relative"> |
| 117 | + <Search className="absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-muted-foreground" /> |
| 118 | + <Input |
| 119 | + value={searchQuery} |
| 120 | + onChange={(e) => setSearchQuery(e.target.value)} |
| 121 | + placeholder="搜索模板..." |
| 122 | + className="pl-8 h-8 text-sm" |
| 123 | + /> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + |
| 127 | + {/* Template List */} |
| 128 | + <div className="flex-1 overflow-y-auto px-1"> |
| 129 | + {isLoading ? ( |
| 130 | + <div className="flex items-center justify-center py-12"> |
| 131 | + <Loader2 className="h-6 w-6 animate-spin text-muted-foreground" /> |
| 132 | + </div> |
| 133 | + ) : error ? ( |
| 134 | + <div className="flex flex-col items-center justify-center py-12 text-muted-foreground px-4"> |
| 135 | + <FileText className="h-8 w-8 mb-2 opacity-50" /> |
| 136 | + <p className="text-xs text-center"> |
| 137 | + 无法加载模板库,请确认 API 服务可用 |
| 138 | + </p> |
| 139 | + </div> |
| 140 | + ) : filteredTemplates.length === 0 ? ( |
| 141 | + <div className="flex flex-col items-center justify-center py-12 text-muted-foreground px-4"> |
| 142 | + <FileText className="h-8 w-8 mb-2 opacity-50" /> |
| 143 | + <p className="text-xs text-center"> |
| 144 | + {searchQuery ? '未找到匹配的模板' : '暂无可用模板'} |
| 145 | + </p> |
| 146 | + </div> |
| 147 | + ) : ( |
| 148 | + <div className="space-y-0.5"> |
| 149 | + {filteredTemplates.map((template) => ( |
| 150 | + <TemplateItem |
| 151 | + key={template.id} |
| 152 | + template={template} |
| 153 | + onInstall={handleInstall} |
| 154 | + isInstalling={installingId === template.id} |
| 155 | + /> |
| 156 | + ))} |
| 157 | + </div> |
| 158 | + )} |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + ); |
| 162 | +} |
| 163 | + |
| 164 | +export default InlineTemplatePanel; |
0 commit comments