"use client"; import { useMemo } from "react"; import Link from "next/link"; import { trpc } from "~/lib/trpc/client.js"; import type { WidgetProps } from "../widget-registry.js"; import { InfoTooltip } from "~/components/ui/InfoTooltip.js"; import { FadeIn } from "~/components/ui/FadeIn.js"; const STATUS_DOT: Record = { ACTIVE: "bg-green-500", DRAFT: "bg-gray-400", ON_HOLD: "bg-yellow-500", COMPLETED: "bg-blue-500", CANCELLED: "bg-red-400", }; export function MyProjectsWidget({ config }: WidgetProps) { const showFavorites = (config as { showFavorites?: boolean }).showFavorites !== false; const showResponsible = (config as { showResponsible?: boolean }).showResponsible !== false; const { data: favoriteIds } = trpc.user.getFavoriteProjectIds.useQuery(undefined, { staleTime: 30_000, enabled: showFavorites, }); const { data: projectsData } = trpc.project.listWithCosts.useQuery( { limit: 200 }, { staleTime: 30_000 }, ); const { data: session } = trpc.user.me.useQuery(undefined, { staleTime: 60_000 }); const favorites = favoriteIds ?? []; const allProjects = (projectsData?.projects ?? []) as Array<{ id: string; shortCode: string; name: string; status: string; responsiblePerson?: string | null; budgetCents?: number; startDate?: Date | string; endDate?: Date | string; }>; const userName = session?.name ?? ""; const { favoriteProjects, responsibleProjects } = useMemo(() => { const favSet = new Set(favorites); const favProjects = showFavorites ? allProjects.filter((p) => favSet.has(p.id)) : []; const respProjects = showResponsible && userName ? allProjects.filter((p) => p.responsiblePerson?.toLowerCase().includes(userName.toLowerCase()) && !favSet.has(p.id)) : []; return { favoriteProjects: favProjects, responsibleProjects: respProjects }; }, [allProjects, favorites, showFavorites, showResponsible, userName]); const toggleMutation = trpc.user.toggleFavoriteProject.useMutation({ onSuccess: () => { void trpc.useUtils().user.getFavoriteProjectIds.invalidate(); }, }); const isEmpty = favoriteProjects.length === 0 && responsibleProjects.length === 0; return (
{isEmpty && (

No favorite projects yet.

Star projects from the project list to see them here.

)} {!isEmpty && (
{favoriteProjects.length > 0 && (
Favorites
{favoriteProjects.map((p, i) => ( toggleMutation.mutate({ projectId: p.id })} /> ))}
)} {responsibleProjects.length > 0 && (
Responsible
{responsibleProjects.map((p, i) => ( toggleMutation.mutate({ projectId: p.id })} /> ))}
)}
)}
); } function ProjectRow({ project, isFavorite, onToggleFavorite, }: { project: { id: string; shortCode: string; name: string; status: string }; isFavorite: boolean; onToggleFavorite: () => void; }) { return (
{project.shortCode} {project.name} {project.status}
); }