All files / src/apps/user/pages/projects Project.tsx

83.33% Statements 15/18
75% Branches 18/24
100% Functions 6/6
88.23% Lines 15/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193                                                1x           4x                                       1x 4x 4x           4x     2x 2x         4x   4x     4x                                             4x                           6x                                                                                                   4x               8x                                                  
import React from "react";
import ResourceHistorySection from "@/components/diff/ResourceHistorySection/ResourceHistorySection";
import { Link, useParams } from "react-router-dom";
import { useQuery } from "@tanstack/react-query";
import { useApiHandler } from "@/services/useApiHandler";
import { QUERY_KEYS } from "@/services/query_keys";
import { APP_ROUTES, ROUTE_PARAMS } from "@/routes/const";
import SubPage from "@/components/page/SubPage/SubPage";
import {
  FolderKanban,
  Calendar,
  Info,
  Bot,
  Wrench,
  Settings,
} from "lucide-react";
import {
  MetaItem,
  Skeleton,
} from "@/components/detail";
import { parseServerDate } from "@/util/time";
 
// ─── Primitives ───────────────────────────────────────────────────────────────
 
const NavCard: React.FC<{
  icon: React.ElementType;
  label: string;
  description: string;
  to: string;
}> = ({ icon: Icon, label, description, to }) => (
  <Link
    to={to}
    className="flex items-start gap-3 p-4 rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 hover:border-gray-300 dark:hover:border-gray-500 hover:shadow-sm transition-all"
  >
    <div className="p-2 rounded-lg bg-gray-100 dark:bg-gray-700 shrink-0">
      <Icon className="size-4 text-gray-600 dark:text-gray-300" />
    </div>
    <div className="min-w-0">
      <div className="text-sm font-semibold text-gray-800 dark:text-gray-200">
        {label}
      </div>
      <div className="text-xs text-gray-400 dark:text-gray-500 mt-0.5 leading-relaxed">
        {description}
      </div>
    </div>
  </Link>
);
 
// ─── ProjectViewer ────────────────────────────────────────────────────────────
 
const ProjectViewer: React.FC = () => {
  const { getApiHandler } = useApiHandler();
  const { projectId } = useParams();
 
  const {
    data: project,
    error,
    isLoading,
  } = useQuery({
    queryKey: [QUERY_KEYS.GET.PROJECT_BY_ID, projectId],
    queryFn: async () => {
      Iif (!projectId) return null;
      return getApiHandler().project_router.id(atob(projectId));
    },
    enabled: !!projectId,
  });
 
  Iif (!projectId)
    return <div className="p-4 text-red-500">Invalid project ID.</div>;
  Iif (error)
    return <div className="p-4 text-red-500">Error: {error.toString()}</div>;
 
  const navItems = projectId
    ? [
        {
          icon: Bot,
          label: "Agents",
          description: "Manage LLM agents scoped to this project.",
          to: APP_ROUTES.PROJECT.agents.replace(
            ROUTE_PARAMS.PROJECT.id,
            projectId,
          ),
        },
        {
          icon: Settings,
          label: "Settings",
          description: "Configure project-level preferences.",
          to: APP_ROUTES.PROJECT.settings.replace(
            ROUTE_PARAMS.PROJECT.id,
            projectId,
          ),
        },
      ]
    : [];
 
  return (
    <SubPage
      title={
        isLoading ? <Skeleton className="h-7 w-48" /> : (project?.name ?? "—")
      }
      title_icon={
        <FolderKanban className="size-12 text-gray-400 dark:text-gray-500" />
      }
      links={[]}
    >
      {/* Metadata strip */}
      <div className="grid grid-cols-2 sm:grid-cols-3 gap-5 p-5 rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-sm mb-6">
        {isLoading ? (
          Array.from({ length: 3 }).map((_, i) => (
            <div key={i} className="flex flex-col gap-1.5">
              <Skeleton className="h-3 w-16" />
              <Skeleton className="h-4 w-24" />
            </div>
          ))
        ) : (
          <>
            <MetaItem
              icon={FolderKanban}
              label="Type"
              value={
                <span className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-indigo-50 dark:bg-indigo-950/40 text-indigo-700 dark:text-indigo-300 border border-indigo-200 dark:border-indigo-800">
                  Project
                </span>
              }
            />
            <MetaItem
              icon={Info}
              label="Description"
              value={
                <span className="truncate block">
                  {project?.description || (
                    <span className="text-gray-400 dark:text-gray-500 italic font-normal">
                      None
                    </span>
                  )}
                </span>
              }
            />
            <MetaItem
              icon={Calendar}
              label="Created"
              value={
                project?.created_at
                  ? parseServerDate(project.created_at).toLocaleDateString(undefined, {
                      year: "numeric",
                      month: "short",
                      day: "numeric",
                    })
                  : "—"
              }
            />
          </>
        )}
      </div>
 
      {/* Navigation cards */}
      {!isLoading && (
        <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
          {navItems.map((item) => (
            <NavCard key={item.label} {...item} />
          ))}
        </div>
      )}
 
      {isLoading && (
        <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
          {Array.from({ length: 4 }).map((_, i) => (
            <div
              key={i}
              className="animate-pulse rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 p-4 flex items-start gap-3"
            >
              <div className="size-8 rounded-lg bg-gray-200 dark:bg-gray-700 shrink-0" />
              <div className="flex-1 space-y-2">
                <div className="h-4 w-24 rounded bg-gray-200 dark:bg-gray-700" />
                <div className="h-3 w-full rounded bg-gray-200 dark:bg-gray-700" />
              </div>
            </div>
          ))}
        </div>
      )}
 
      <ResourceHistorySection
        source={getApiHandler().project_router}
        resourceId={projectId ? atob(projectId) : null}
        queryKey={QUERY_KEYS.GET.PROJECT_HISTORY}
        fieldLabels={{ name: "Name", description: "Description" }}
      />
    </SubPage>
  );
};
 
export default ProjectViewer;