|
| 1 | +import { useCallback, useEffect, useState } from 'react' |
| 2 | +import { useTranslation } from 'react-i18next' |
| 3 | +import { AlertTriangle, FileText, Loader2, Lock, X } from 'lucide-react' |
| 4 | +import { Button } from '@/shared/components/ui/button' |
| 5 | +import { InfiniteScrollSentinel } from '@/shared/components/ui/infinite-scroll' |
| 6 | +import { soarTemplatesService } from '../services/soar-templates.service' |
| 7 | +import type { ActionTemplate } from '../types/soar.types' |
| 8 | + |
| 9 | +const PAGE_SIZE = 20 |
| 10 | + |
| 11 | +export function TemplatePicker({ |
| 12 | + onPick, |
| 13 | + onScratch, |
| 14 | + onClose, |
| 15 | +}: { |
| 16 | + onPick: (t: ActionTemplate) => void |
| 17 | + onScratch: () => void |
| 18 | + onClose: () => void |
| 19 | +}) { |
| 20 | + const { t } = useTranslation() |
| 21 | + const [items, setItems] = useState<ActionTemplate[]>([]) |
| 22 | + const [total, setTotal] = useState(0) |
| 23 | + const [page, setPage] = useState(0) |
| 24 | + const [loading, setLoading] = useState(true) |
| 25 | + const [error, setError] = useState(false) |
| 26 | + |
| 27 | + const loadPage = useCallback(async (p: number) => { |
| 28 | + setLoading(true) |
| 29 | + setError(false) |
| 30 | + try { |
| 31 | + const res = await soarTemplatesService.list({ page: p, size: PAGE_SIZE }) |
| 32 | + setTotal(res.total) |
| 33 | + setItems((prev) => (p === 0 ? res.data : [...prev, ...res.data])) |
| 34 | + } catch { |
| 35 | + setError(true) |
| 36 | + } finally { |
| 37 | + setLoading(false) |
| 38 | + } |
| 39 | + }, []) |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + loadPage(page) |
| 43 | + }, [page, loadPage]) |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 backdrop-blur-sm" onClick={onClose}> |
| 47 | + <div className="flex max-h-[80vh] w-full max-w-[640px] flex-col overflow-hidden rounded-xl border border-border bg-card shadow-xl" onClick={(e) => e.stopPropagation()}> |
| 48 | + <header className="flex items-start justify-between gap-4 border-b border-border px-5 py-3"> |
| 49 | + <div className="min-w-0"> |
| 50 | + <h2 className="truncate text-base font-semibold">{t('soar.templates.title')}</h2> |
| 51 | + <p className="mt-0.5 text-[11px] text-muted-foreground">{t('soar.templates.subtitle')}</p> |
| 52 | + </div> |
| 53 | + <button onClick={onClose} className="flex h-7 w-7 items-center justify-center rounded-md text-muted-foreground hover:bg-muted hover:text-foreground"> |
| 54 | + <X size={15} /> |
| 55 | + </button> |
| 56 | + </header> |
| 57 | + |
| 58 | + <div className="min-h-0 flex-1 overflow-y-auto"> |
| 59 | + {loading && items.length === 0 ? ( |
| 60 | + <div className="flex items-center justify-center gap-2 px-6 py-16 text-xs text-muted-foreground"> |
| 61 | + <Loader2 className="h-4 w-4 animate-spin" /> {t('soar.templates.loading')} |
| 62 | + </div> |
| 63 | + ) : error && items.length === 0 ? ( |
| 64 | + <div className="flex items-center justify-center gap-2 px-6 py-16 text-xs text-muted-foreground"> |
| 65 | + <AlertTriangle size={14} className="text-amber-500" /> {t('soar.templates.loadError')} |
| 66 | + <Button variant="outline" size="sm" className="ml-2" onClick={() => loadPage(0)}>{t('soar.retry')}</Button> |
| 67 | + </div> |
| 68 | + ) : items.length === 0 ? ( |
| 69 | + <div className="px-6 py-16 text-center text-xs text-muted-foreground">{t('soar.templates.empty')}</div> |
| 70 | + ) : ( |
| 71 | + <div className="divide-y divide-border"> |
| 72 | + {items.map((tpl) => ( |
| 73 | + <button |
| 74 | + key={tpl.id} |
| 75 | + type="button" |
| 76 | + onClick={() => onPick(tpl)} |
| 77 | + className="flex w-full items-start gap-3 px-5 py-3 text-left transition-colors hover:bg-muted/50" |
| 78 | + > |
| 79 | + <FileText size={14} className="mt-0.5 shrink-0 text-muted-foreground" /> |
| 80 | + <div className="min-w-0 flex-1"> |
| 81 | + <div className="flex items-center gap-2"> |
| 82 | + <span className="truncate text-sm font-medium">{tpl.title}</span> |
| 83 | + {tpl.systemOwner && ( |
| 84 | + <span className="inline-flex items-center gap-1 rounded bg-violet-500/15 px-1.5 py-0.5 text-[10px] font-medium text-violet-500"> |
| 85 | + <Lock size={9} /> {t('soar.system')} |
| 86 | + </span> |
| 87 | + )} |
| 88 | + </div> |
| 89 | + {tpl.description && ( |
| 90 | + <p className="mt-0.5 line-clamp-2 text-[11px] text-muted-foreground">{tpl.description}</p> |
| 91 | + )} |
| 92 | + </div> |
| 93 | + </button> |
| 94 | + ))} |
| 95 | + <InfiniteScrollSentinel |
| 96 | + onReach={() => setPage((p) => p + 1)} |
| 97 | + hasMore={items.length < total} |
| 98 | + loading={loading} |
| 99 | + endLabel={t('common.allLoaded', { count: total })} |
| 100 | + /> |
| 101 | + </div> |
| 102 | + )} |
| 103 | + </div> |
| 104 | + |
| 105 | + <footer className="flex items-center justify-end gap-2 border-t border-border px-5 py-3"> |
| 106 | + <Button variant="ghost" size="sm" onClick={onClose}>{t('soar.templates.cancel')}</Button> |
| 107 | + <Button size="sm" onClick={onScratch}>{t('soar.templates.scratch')}</Button> |
| 108 | + </footer> |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + ) |
| 112 | +} |
0 commit comments