import { useEffect } from "react"; import { useNavigate, useParams } from "react-router-dom"; import { useQuery } from "@tanstack/react-query"; import { goalsApi } from "../api/goals"; import { projectsApi } from "../api/projects"; import { usePanel } from "../context/PanelContext"; import { useCompany } from "../context/CompanyContext"; import { useBreadcrumbs } from "../context/BreadcrumbContext"; import { queryKeys } from "../lib/queryKeys"; import { GoalProperties } from "../components/GoalProperties"; import { GoalTree } from "../components/GoalTree"; import { StatusBadge } from "../components/StatusBadge"; import { EntityRow } from "../components/EntityRow"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import type { Goal, Project } from "@paperclip/shared"; export function GoalDetail() { const { goalId } = useParams<{ goalId: string }>(); const { selectedCompanyId } = useCompany(); const { openPanel, closePanel } = usePanel(); const { setBreadcrumbs } = useBreadcrumbs(); const navigate = useNavigate(); const { data: goal, isLoading, error } = useQuery({ queryKey: queryKeys.goals.detail(goalId!), queryFn: () => goalsApi.get(goalId!), enabled: !!goalId, }); const { data: allGoals } = useQuery({ queryKey: queryKeys.goals.list(selectedCompanyId!), queryFn: () => goalsApi.list(selectedCompanyId!), enabled: !!selectedCompanyId, }); const { data: allProjects } = useQuery({ queryKey: queryKeys.projects.list(selectedCompanyId!), queryFn: () => projectsApi.list(selectedCompanyId!), enabled: !!selectedCompanyId, }); const childGoals = (allGoals ?? []).filter((g) => g.parentId === goalId); const linkedProjects = (allProjects ?? []).filter((p) => p.goalId === goalId); useEffect(() => { setBreadcrumbs([ { label: "Goals", href: "/goals" }, { label: goal?.title ?? goalId ?? "Goal" }, ]); }, [setBreadcrumbs, goal, goalId]); useEffect(() => { if (goal) { openPanel(); } return () => closePanel(); }, [goal]); // eslint-disable-line react-hooks/exhaustive-deps if (isLoading) return

Loading...

; if (error) return

{error.message}

; if (!goal) return null; return (
{goal.level}

{goal.title}

{goal.description && (

{goal.description}

)}
Sub-Goals ({childGoals.length}) Projects ({linkedProjects.length}) {childGoals.length === 0 ? (

No sub-goals.

) : ( navigate(`/goals/${g.id}`)} /> )}
{linkedProjects.length === 0 ? (

No linked projects.

) : (
{linkedProjects.map((project) => ( navigate(`/projects/${project.id}`)} trailing={} /> ))}
)}
); }