"use client"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Plus, Search, Sparkles } from "lucide-react"; import { SkillCategory, CategoryEvaluation } from "@/lib/types"; import { TechIcon } from "./icons/tech-icon"; import { CreateSkillForm } from "./create-skill-form"; interface SkillSelectorProps { categories: SkillCategory[]; evaluations: CategoryEvaluation[]; selectedCategory: string; onAddSkill: (category: string, skillId: string) => void; onAddMultipleSkills?: (category: string, skillIds: string[]) => void; onRemoveSkill: (category: string, skillId: string) => void; } export function SkillSelector({ categories, evaluations, selectedCategory, onAddSkill, onAddMultipleSkills, onRemoveSkill, }: SkillSelectorProps) { const [isOpen, setIsOpen] = useState(false); const [isCreateOpen, setIsCreateOpen] = useState(false); const [searchTerm, setSearchTerm] = useState(""); const [isAddingAll, setIsAddingAll] = useState(false); const [addingSkillIds, setAddingSkillIds] = useState>(new Set()); const currentCategory = categories.find( (cat) => cat.category === selectedCategory ); const currentEvaluation = evaluations.find( (evaluation) => evaluation.category === selectedCategory ); if (!currentCategory) return null; const selectedSkillIds = currentEvaluation?.selectedSkillIds || []; const filteredSkills = currentCategory.skills.filter( (skill) => skill.name.toLowerCase().includes(searchTerm.toLowerCase()) || skill.description.toLowerCase().includes(searchTerm.toLowerCase()) ); const availableSkills = filteredSkills.filter( (skill) => !selectedSkillIds.includes(skill.id) ); const selectedSkills = currentCategory.skills.filter((skill) => selectedSkillIds.includes(skill.id) ); const handleAddSkill = async (skillId: string) => { setAddingSkillIds((prev) => new Set(prev).add(skillId)); try { await onAddSkill(selectedCategory, skillId); } catch (error) { console.error("Failed to add skill:", error); } finally { setAddingSkillIds((prev) => { const next = new Set(prev); next.delete(skillId); return next; }); } }; const handleAddAllSkills = async () => { setIsAddingAll(true); try { const skillIds = availableSkills.map((skill) => skill.id); // Utiliser la fonction d'ajout multiple si disponible, sinon fallback sur l'ajout individuel if (onAddMultipleSkills) { await onAddMultipleSkills(selectedCategory, skillIds); } else { // Fallback: ajouter en séquentiel pour éviter les conflits d'état for (const skill of availableSkills) { await onAddSkill(selectedCategory, skill.id); } } // Petit délai pour permettre à l'UI de se mettre à jour avant de fermer setTimeout(() => { setIsOpen(false); }, 100); } catch (error) { console.error("Failed to add all skills:", error); } finally { setIsAddingAll(false); } }; return (
{/* Selected Skills */}

Mes compétences sélectionnées

Ajouter des compétences - {selectedCategory} Choisissez les compétences que vous souhaitez évaluer dans cette catégorie.
{/* Search and Add All */}
setSearchTerm(e.target.value)} className="pl-10" />
{availableSkills.length > 1 && ( )}
{/* Create New Skill Button */}
Créer une nouvelle compétence Ajoutez une compétence manquante dans la catégorie{" "} {selectedCategory} { setIsCreateOpen(false); setIsOpen(false); // Ajouter automatiquement la skill créée à l'évaluation onAddSkill(selectedCategory, skillId); }} onCancel={() => setIsCreateOpen(false)} />
{/* Available Skills Grid */}
{availableSkills.length > 0 ? ( availableSkills.map((skill) => (
{skill.name}

{skill.description}

)) ) : (
{searchTerm ? "Aucune compétence trouvée" : "Toutes les compétences ont été ajoutées"}
)}
{selectedSkills.length > 0 ? (

{selectedSkills.length} compétence {selectedSkills.length > 1 ? "s" : ""} sélectionnée {selectedSkills.length > 1 ? "s" : ""}

Supprimez directement depuis les lignes ci-dessous
) : (

Aucune compétence sélectionnée

Cliquez sur "Ajouter une compétence" pour commencer

)}
); }