This commit is contained in:
Julien Froidefond
2025-08-20 15:43:24 +02:00
commit 09d2c5cbe1
100 changed files with 12494 additions and 0 deletions

176
hooks/use-evaluation.ts Normal file
View File

@@ -0,0 +1,176 @@
"use client";
import { useState, useEffect } from "react";
import {
UserEvaluation,
SkillCategory,
Team,
CategoryEvaluation,
UserProfile,
SkillLevel,
} from "@/lib/types";
import {
loadUserEvaluation,
saveUserEvaluation,
createEmptyEvaluation,
} from "@/lib/evaluation-utils";
import { loadSkillCategories, loadTeams } from "@/lib/data-loader";
export function useEvaluation() {
const [userEvaluation, setUserEvaluation] = useState<UserEvaluation | null>(
null
);
const [skillCategories, setSkillCategories] = useState<SkillCategory[]>([]);
const [teams, setTeams] = useState<Team[]>([]);
const [loading, setLoading] = useState(true);
// Load initial data
useEffect(() => {
async function initializeData() {
try {
const [categories, teamsData] = await Promise.all([
loadSkillCategories(),
loadTeams(),
]);
setSkillCategories(categories);
setTeams(teamsData);
// Try to load existing evaluation
const saved = loadUserEvaluation();
if (saved) {
setUserEvaluation(saved);
}
} catch (error) {
console.error("Failed to initialize data:", error);
} finally {
setLoading(false);
}
}
initializeData();
}, []);
const updateProfile = (profile: UserProfile) => {
const evaluations =
userEvaluation?.evaluations || createEmptyEvaluation(skillCategories);
const newEvaluation: UserEvaluation = {
profile,
evaluations,
lastUpdated: new Date().toISOString(),
};
setUserEvaluation(newEvaluation);
saveUserEvaluation(newEvaluation);
};
const updateSkillLevel = (
category: string,
skillId: string,
level: SkillLevel
) => {
if (!userEvaluation) return;
const updatedEvaluations = userEvaluation.evaluations.map((catEval) => {
if (catEval.category === category) {
const existingSkill = catEval.skills.find((s) => s.skillId === skillId);
const updatedSkills = existingSkill
? catEval.skills.map((skill) =>
skill.skillId === skillId ? { ...skill, level } : skill
)
: [...catEval.skills, { skillId, level }];
return {
...catEval,
skills: updatedSkills,
};
}
return catEval;
});
const newEvaluation: UserEvaluation = {
...userEvaluation,
evaluations: updatedEvaluations,
lastUpdated: new Date().toISOString(),
};
setUserEvaluation(newEvaluation);
saveUserEvaluation(newEvaluation);
};
const addSkillToEvaluation = (category: string, skillId: string) => {
if (!userEvaluation) return;
const updatedEvaluations = userEvaluation.evaluations.map((catEval) => {
if (catEval.category === category) {
if (!catEval.selectedSkillIds.includes(skillId)) {
return {
...catEval,
selectedSkillIds: [...catEval.selectedSkillIds, skillId],
skills: [...catEval.skills, { skillId, level: null }],
};
}
}
return catEval;
});
const newEvaluation: UserEvaluation = {
...userEvaluation,
evaluations: updatedEvaluations,
lastUpdated: new Date().toISOString(),
};
setUserEvaluation(newEvaluation);
saveUserEvaluation(newEvaluation);
};
const removeSkillFromEvaluation = (category: string, skillId: string) => {
if (!userEvaluation) return;
const updatedEvaluations = userEvaluation.evaluations.map((catEval) => {
if (catEval.category === category) {
return {
...catEval,
selectedSkillIds: catEval.selectedSkillIds.filter(
(id) => id !== skillId
),
skills: catEval.skills.filter((skill) => skill.skillId !== skillId),
};
}
return catEval;
});
const newEvaluation: UserEvaluation = {
...userEvaluation,
evaluations: updatedEvaluations,
lastUpdated: new Date().toISOString(),
};
setUserEvaluation(newEvaluation);
saveUserEvaluation(newEvaluation);
};
const initializeEmptyEvaluation = (profile: UserProfile) => {
const evaluations = createEmptyEvaluation(skillCategories);
const newEvaluation: UserEvaluation = {
profile,
evaluations,
lastUpdated: new Date().toISOString(),
};
setUserEvaluation(newEvaluation);
saveUserEvaluation(newEvaluation);
};
return {
userEvaluation,
skillCategories,
teams,
loading,
updateProfile,
updateSkillLevel,
addSkillToEvaluation,
removeSkillFromEvaluation,
initializeEmptyEvaluation,
};
}