File size: 11,551 Bytes
eeb9404 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | 'use client';
import React, { useState, useEffect, useCallback } from 'react';
import type { InterviewTemplate } from '@/lib/interview/types';
import { interviewTemplatesService } from '@/lib/interview/templates-service';
import { track } from '@/lib/telemetry';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Badge } from '@/components/ui/badge';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { toast } from 'sonner';
import { Search, Plus, Edit, Copy, Trash2, Eye, ClipboardList, FileText } from 'lucide-react';
import { InterviewTemplateEditor } from './InterviewTemplateEditor';
interface InterviewTemplatesPanelProps {
initialMode?: 'list' | 'create';
onChanged?: () => void;
}
type View =
| 'list'
| { mode: 'create' }
| { mode: 'edit'; template: InterviewTemplate }
| { mode: 'view'; template: InterviewTemplate };
export function InterviewTemplatesPanel({
initialMode = 'list',
onChanged,
}: InterviewTemplatesPanelProps) {
const [templates, setTemplates] = useState<InterviewTemplate[]>([]);
const [view, setView] = useState<View>(initialMode === 'create' ? { mode: 'create' } : 'list');
const [searchQuery, setSearchQuery] = useState('');
const [showBuiltIn, setShowBuiltIn] = useState(true);
const [showCustom, setShowCustom] = useState(true);
const [templateToDelete, setTemplateToDelete] = useState<InterviewTemplate | null>(null);
const reloadList = useCallback(async () => {
try {
const all = await interviewTemplatesService.getAllTemplates();
setTemplates(all);
} catch {
toast.error('Failed to load interview templates');
}
}, []);
useEffect(() => {
reloadList();
}, [reloadList]);
const handleDuplicate = async (src: InterviewTemplate) => {
try {
const id = await interviewTemplatesService.generateId(src.title + ' copy');
await interviewTemplatesService.createTemplate({
...src,
id,
title: `${src.title} copy`,
isBuiltIn: false,
});
track('interview_template_created');
await reloadList();
onChanged?.();
const created = await interviewTemplatesService.getTemplate(id);
if (created) {
toast.success(`Duplicated: ${src.title}`);
setView({ mode: 'edit', template: created });
}
} catch (e) {
const message = e instanceof Error ? e.message : 'Failed to duplicate template';
toast.error(message);
}
};
const confirmDelete = async () => {
if (!templateToDelete) return;
try {
await interviewTemplatesService.deleteTemplate(templateToDelete.id);
track('interview_template_deleted');
toast.success(`Deleted: ${templateToDelete.title}`);
await reloadList();
onChanged?.();
} catch (e) {
const message = e instanceof Error ? e.message : 'Failed to delete template';
toast.error(message);
} finally {
setTemplateToDelete(null);
}
};
const handleEditorSaved = async () => {
await reloadList();
setView('list');
onChanged?.();
};
const filtered = templates.filter(t => {
const q = searchQuery.toLowerCase();
const matchesSearch =
t.title.toLowerCase().includes(q) || t.description.toLowerCase().includes(q);
if (!matchesSearch) return false;
if (t.isBuiltIn && !showBuiltIn) return false;
if (!t.isBuiltIn && !showCustom) return false;
return true;
}).sort((a, b) => Number(!!a.isBuiltIn) - Number(!!b.isBuiltIn)); // custom first, then built-in
const inEditor = view !== 'list';
const editorTemplate =
inEditor && view.mode === 'create' ? null : inEditor ? view.template : null;
return (
<>
<div className="flex flex-col h-full">
<div className="px-6 pt-6 pb-3 shrink-0">
<div className="flex items-center gap-2">
<ClipboardList className="w-5 h-5" />
<h2 className="text-lg font-semibold leading-none tracking-tight">Interview Templates</h2>
</div>
<p className="text-sm text-muted-foreground mt-1.5">
Manage the guided interviews available in interview mode.
</p>
</div>
<div className="px-6 pb-3 shrink-0 flex flex-col gap-3">
<div className="flex flex-col sm:flex-row gap-3">
<div className="relative flex-1">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
<Input
placeholder="Search templates..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
className="pl-9"
/>
</div>
<Button size="sm" onClick={() => setView({ mode: 'create' })}>
<Plus className="w-4 h-4 mr-2" />
New
</Button>
</div>
<div className="flex items-center gap-2 text-xs">
<span className="text-muted-foreground">Show:</span>
<Button
variant={showBuiltIn ? 'default' : 'outline'}
size="sm"
className="h-7 px-2 gap-1.5"
onClick={() => setShowBuiltIn(v => !v)}
aria-pressed={showBuiltIn}
>
<FileText className="w-3 h-3" />
Built-in
</Button>
<Button
variant={showCustom ? 'default' : 'outline'}
size="sm"
className="h-7 px-2 gap-1.5"
onClick={() => setShowCustom(v => !v)}
aria-pressed={showCustom}
>
<ClipboardList className="w-3 h-3" />
Custom
</Button>
</div>
</div>
<div className="flex-1 overflow-y-auto px-6 pb-6">
{filtered.length === 0 ? (
<div className="text-center py-12">
<ClipboardList className="w-12 h-12 mx-auto mb-4 text-muted-foreground" />
<h3 className="text-lg font-semibold mb-2">No templates found</h3>
<p className="text-muted-foreground mb-4">
{!showBuiltIn && !showCustom
? 'Both Built-in and Custom are hidden. Enable at least one above.'
: searchQuery
? 'Try a different search query'
: 'Create your first interview template'}
</p>
{!searchQuery && (
<Button onClick={() => setView({ mode: 'create' })}>
<Plus className="w-4 h-4 mr-2" />
New Template
</Button>
)}
</div>
) : (
<div className="grid gap-3">
{filtered.map(t => (
<div key={t.id} className="border rounded-lg p-4">
<div className="flex items-start justify-between gap-4">
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1 flex-wrap">
<h3 className="font-semibold truncate">{t.title}</h3>
<Badge variant={t.isBuiltIn ? 'secondary' : 'outline'} className="text-xs">
{t.isBuiltIn ? 'Built-in' : 'Custom'}
</Badge>
</div>
<p className="text-sm text-muted-foreground line-clamp-2">{t.description}</p>
{t.artifacts[0] && (
<p className="text-xs text-muted-foreground/80 mt-1 font-mono truncate">
{t.artifacts[0].path}
</p>
)}
</div>
<div className="flex items-center gap-1 shrink-0">
{t.isBuiltIn ? (
<>
<Button
variant="ghost"
size="sm"
onClick={() => setView({ mode: 'view', template: t })}
title="View"
>
<Eye className="w-4 h-4" />
</Button>
<Button
variant="ghost"
size="sm"
onClick={() => handleDuplicate(t)}
title="Duplicate"
>
<Copy className="w-4 h-4" />
</Button>
</>
) : (
<>
<Button
variant="ghost"
size="sm"
onClick={() => setView({ mode: 'edit', template: t })}
title="Edit"
>
<Edit className="w-4 h-4" />
</Button>
<Button
variant="ghost"
size="sm"
onClick={() => handleDuplicate(t)}
title="Duplicate"
>
<Copy className="w-4 h-4" />
</Button>
<Button
variant="ghost"
size="sm"
onClick={() => setTemplateToDelete(t)}
title="Delete"
>
<Trash2 className="w-4 h-4" />
</Button>
</>
)}
</div>
</div>
</div>
))}
</div>
)}
</div>
</div>
{/* Editor dialog (matches the Skills editor: a modal over the list) */}
<Dialog open={inEditor} onOpenChange={(o) => !o && setView('list')}>
<DialogContent className="max-w-[90vw] sm:max-w-[85vw] lg:max-w-3xl h-[90vh] p-0 overflow-hidden">
<DialogHeader className="sr-only">
<DialogTitle>
{editorTemplate ? `Edit ${editorTemplate.title}` : 'Create interview template'}
</DialogTitle>
</DialogHeader>
{inEditor && (
<InterviewTemplateEditor
template={editorTemplate}
onSaved={handleEditorSaved}
onCancel={() => setView('list')}
/>
)}
</DialogContent>
</Dialog>
{/* Delete confirmation */}
<Dialog open={!!templateToDelete} onOpenChange={(o) => !o && setTemplateToDelete(null)}>
<DialogContent>
<DialogHeader>
<DialogTitle>Delete Template</DialogTitle>
<DialogDescription>
{templateToDelete
? `Are you sure you want to delete "${templateToDelete.title}"? This action cannot be undone.`
: ''}
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button variant="outline" onClick={() => setTemplateToDelete(null)}>
Cancel
</Button>
<Button variant="destructive" onClick={confirmDelete}>
Delete
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</>
);
}
|