Files
DataClaw/frontend/src/pages/Skills.tsx
T

238 lines
9.5 KiB
TypeScript
Raw Normal View History

2026-03-14 15:52:27 +08:00
import { useState, useEffect } from 'react';
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card";
2026-03-16 16:12:35 +08:00
import { Trash2, Edit2, Plus, Terminal, Loader2, FolderOpen } from "lucide-react";
2026-03-14 15:52:27 +08:00
import { ScrollArea } from "@/components/ui/scroll-area";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, DialogFooter } from "@/components/ui/dialog";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { api } from "@/lib/api";
2026-03-16 16:12:35 +08:00
import { useProjectStore } from "@/store/projectStore";
2026-03-14 15:52:27 +08:00
interface Skill {
id: string;
name: string;
description: string;
content: string;
type: 'python' | 'sql' | 'api';
2026-03-16 16:12:35 +08:00
project_id?: number;
2026-03-14 15:52:27 +08:00
}
export function Skills() {
const [skills, setSkills] = useState<Skill[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [isDialogOpen, setIsDialogOpen] = useState(false);
2026-03-16 16:12:35 +08:00
const [editingSkill, setEditingSkill] = useState<Skill | null>(null);
2026-03-14 15:52:27 +08:00
const [newSkill, setNewSkill] = useState<Partial<Skill>>({ type: 'python', content: '' });
2026-03-16 16:12:35 +08:00
const { currentProject } = useProjectStore();
2026-03-14 15:52:27 +08:00
useEffect(() => {
2026-03-16 16:12:35 +08:00
if (currentProject) {
fetchSkills();
}
}, [currentProject]);
2026-03-14 15:52:27 +08:00
const fetchSkills = async () => {
2026-03-16 16:12:35 +08:00
if (!currentProject) return;
2026-03-14 15:52:27 +08:00
setIsLoading(true);
try {
2026-03-16 16:12:35 +08:00
const data = await api.get<Skill[]>(`/api/v1/skills?project_id=${currentProject.id}`);
2026-03-14 15:52:27 +08:00
setSkills(data);
} catch (error) {
console.error("Failed to fetch skills", error);
} finally {
setIsLoading(false);
}
};
const handleAddSkill = async () => {
2026-03-16 16:12:35 +08:00
if (!currentProject) return;
2026-03-14 15:52:27 +08:00
if (newSkill.name && newSkill.description && newSkill.content) {
try {
2026-03-16 16:12:35 +08:00
if (editingSkill) {
const updatedSkill = await api.put<Skill>(`/api/v1/skills/${editingSkill.id}?project_id=${currentProject.id}`, {
...newSkill,
project_id: currentProject.id
});
setSkills(skills.map(s => s.id === editingSkill.id ? updatedSkill : s));
} else {
const skillToCreate = {
...newSkill,
id: Date.now().toString(),
project_id: currentProject.id
};
const createdSkill = await api.post<Skill>('/api/v1/skills', skillToCreate);
setSkills([...skills, createdSkill]);
}
2026-03-14 15:52:27 +08:00
setNewSkill({ type: 'python', content: '' });
2026-03-16 16:12:35 +08:00
setEditingSkill(null);
2026-03-14 15:52:27 +08:00
setIsDialogOpen(false);
} catch (error) {
2026-03-16 16:12:35 +08:00
console.error("Failed to save skill", error);
2026-03-14 15:52:27 +08:00
}
}
};
2026-03-16 16:12:35 +08:00
const handleEditSkill = (skill: Skill) => {
setEditingSkill(skill);
setNewSkill(skill);
setIsDialogOpen(true);
};
2026-03-14 15:52:27 +08:00
const handleDeleteSkill = async (id: string) => {
2026-03-16 16:12:35 +08:00
if (!currentProject) return;
if (!window.confirm("确定要删除这个技能吗?")) return;
2026-03-14 15:52:27 +08:00
try {
2026-03-16 16:12:35 +08:00
await api.delete(`/api/v1/skills/${id}?project_id=${currentProject.id}`);
2026-03-14 15:52:27 +08:00
setSkills(skills.filter(s => s.id !== id));
} catch (error) {
console.error("Failed to delete skill", error);
}
};
2026-03-16 16:12:35 +08:00
if (!currentProject) {
return (
<div className="h-full flex flex-col items-center justify-center text-zinc-500 gap-4">
<FolderOpen className="h-12 w-12 text-zinc-200" />
<p></p>
</div>
);
}
2026-03-14 15:52:27 +08:00
return (
<div className="p-6 h-full flex flex-col overflow-hidden">
<div className="flex justify-between items-center mb-6 shrink-0">
<div>
2026-03-16 16:12:35 +08:00
<h1 className="text-2xl font-bold"> - {currentProject.name}</h1>
<p className="text-muted-foreground"> AI </p>
2026-03-14 15:52:27 +08:00
</div>
2026-03-16 16:12:35 +08:00
<Dialog open={isDialogOpen} onOpenChange={(open) => {
setIsDialogOpen(open);
if (!open) {
setEditingSkill(null);
setNewSkill({ type: 'python', content: '' });
}
}}>
2026-03-14 15:52:27 +08:00
<DialogTrigger render={
2026-03-16 16:12:35 +08:00
<Button onClick={() => {
setEditingSkill(null);
setNewSkill({ type: 'python', content: '' });
setIsDialogOpen(true);
}}>
2026-03-14 15:52:27 +08:00
<Plus className="h-4 w-4 mr-2" />
2026-03-16 16:12:35 +08:00
2026-03-14 15:52:27 +08:00
</Button>
} />
<DialogContent className="sm:max-w-[500px]">
<DialogHeader>
2026-03-16 16:12:35 +08:00
<DialogTitle>{editingSkill ? '编辑技能' : '添加新技能'}</DialogTitle>
2026-03-14 15:52:27 +08:00
</DialogHeader>
<div className="grid gap-4 py-4">
<div className="grid grid-cols-4 items-center gap-4">
2026-03-16 16:12:35 +08:00
<Label htmlFor="name" className="text-right"></Label>
2026-03-14 15:52:27 +08:00
<Input
id="name"
value={newSkill.name || ''}
onChange={(e) => setNewSkill({...newSkill, name: e.target.value})}
className="col-span-3"
/>
</div>
<div className="grid grid-cols-4 items-center gap-4">
2026-03-16 16:12:35 +08:00
<Label htmlFor="type" className="text-right"></Label>
2026-03-14 15:52:27 +08:00
<Select
value={newSkill.type}
onValueChange={(val: any) => setNewSkill({...newSkill, type: val})}
>
<SelectTrigger className="col-span-3">
2026-03-16 16:12:35 +08:00
<SelectValue placeholder="选择类型" />
2026-03-14 15:52:27 +08:00
</SelectTrigger>
<SelectContent>
<SelectItem value="python">Python</SelectItem>
<SelectItem value="sql">SQL</SelectItem>
<SelectItem value="api">API</SelectItem>
</SelectContent>
</Select>
</div>
<div className="grid grid-cols-4 items-center gap-4">
2026-03-16 16:12:35 +08:00
<Label htmlFor="description" className="text-right"></Label>
2026-03-14 15:52:27 +08:00
<Textarea
id="description"
value={newSkill.description || ''}
onChange={(e) => setNewSkill({...newSkill, description: e.target.value})}
className="col-span-3"
/>
</div>
<div className="grid grid-cols-4 items-center gap-4">
2026-03-16 16:12:35 +08:00
<Label htmlFor="content" className="text-right"></Label>
2026-03-14 15:52:27 +08:00
<Textarea
id="content"
value={newSkill.content || ''}
onChange={(e) => setNewSkill({...newSkill, content: e.target.value})}
className="col-span-3 font-mono text-xs"
2026-03-16 16:12:35 +08:00
placeholder="Python 代码、SQL 查询模板或 API 规范..."
2026-03-14 15:52:27 +08:00
/>
</div>
</div>
<DialogFooter>
2026-03-16 16:12:35 +08:00
<Button onClick={handleAddSkill}></Button>
2026-03-14 15:52:27 +08:00
</DialogFooter>
</DialogContent>
</Dialog>
</div>
<ScrollArea className="flex-1">
{isLoading ? (
<div className="flex items-center justify-center h-40">
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
</div>
) : (
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 pb-4">
{skills.map((skill) => (
<Card key={skill.id} className="hover:shadow-md transition-shadow">
<CardHeader className="flex flex-row items-start justify-between space-y-0 pb-2">
<div className="space-y-1">
<CardTitle className="text-base font-medium flex items-center gap-2">
<Terminal className="h-4 w-4 text-muted-foreground" />
{skill.name}
</CardTitle>
<CardDescription>{skill.type.toUpperCase()}</CardDescription>
</div>
<div className="flex gap-1">
2026-03-16 16:12:35 +08:00
<Button
variant="ghost"
size="icon"
className="h-8 w-8 text-muted-foreground hover:text-foreground"
onClick={() => handleEditSkill(skill)}
>
2026-03-14 15:52:27 +08:00
<Edit2 className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="icon"
className="h-8 w-8 text-muted-foreground hover:text-destructive"
onClick={() => handleDeleteSkill(skill.id)}
>
<Trash2 className="h-4 w-4" />
</Button>
</div>
</CardHeader>
<CardContent>
2026-03-16 16:12:35 +08:00
<p className="text-sm text-muted-foreground line-clamp-2">{skill.description}</p>
2026-03-14 15:52:27 +08:00
</CardContent>
</Card>
))}
2026-03-16 16:12:35 +08:00
{skills.length === 0 && (
<div className="col-span-full py-12 text-center text-zinc-500 bg-zinc-50 rounded-xl border-2 border-dashed border-zinc-100">
<Terminal className="h-12 w-12 mx-auto mb-4 opacity-20" />
<p></p>
</div>
)}
2026-03-14 15:52:27 +08:00
</div>
)}
</ScrollArea>
</div>
);
}