import type { Project } from "~/types/api"; export default function useProject() { const project = useCookie('project'); const id = useState("projectId", () => parseInt(project.value ?? '0')); const name = useState("projectName", undefined); const owner = useState("projectOwner", undefined); const home = useState("projectHomepage", () => null); const summary = useState("projectSummary", () => null); return { id, name, owner, home, summary, get, set }; } async function get(): Promise { const id = useState("projectId"); if (!id.value) return false; try { const result = await $fetch(`/api/project/${id.value}`) as Project; const name = useState("projectName"); const owner = useState("projectOwner"); const home = useState("projectHomepage"); const summary = useState("projectSummary"); name.value = result.name; owner.value = result.owner; home.value = result.home; summary.value = result.summary; return true; } catch(e) { return false; } } async function set(id: number): Promise { const _id = useState("projectId"); _id.value = id; const project = useCookie('project'); project.value = id.toString(); return await get(); }