All files / client/src/pages/model-map my-models.tsx

0% Statements 0/24
0% Branches 0/68
0% Functions 0/16
0% Lines 0/24

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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
import { useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { Button } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card";
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import { Plus, Star, ArrowRight, Cpu } from "lucide-react";
import { apiRequest } from "@/lib/queryClient";
import { ModelAvatar } from "@/components/model-avatar";
import { AddModelDialog } from "@/components/add-model-dialog";
import type { AiModel, AiTool, UserModel, UserTool } from "@shared/schema";
 
// Extended type for user models with stats
type UserModelWithStats = UserModel & {
  model: AiModel;
  stats?: {
    testCount: number;
    avgOverall: number;
    avgAccuracy: number;
    avgStyle: number;
  };
};
 
type UserToolWithStats = UserTool & {
  tool: AiTool;
  stats?: {
    testCount: number;
    avgOverall: number;
    avgAccuracy: number;
    avgStyle: number;
  };
};
 
// Star rating display component
function StarDisplay({ rating, maxStars = 5 }: { rating: number; maxStars?: number }) {
  return (
    <div className="flex gap-0.5">
      {Array.from({ length: maxStars }).map((_, i) => (
        <Star
          key={i}
          className={`h-3 w-3 ${
            i < Math.floor(rating)
              ? "text-amber-400 fill-amber-400"
              : i < rating
              ? "text-amber-400 fill-amber-400 opacity-50"
              : "text-muted-foreground"
          }`}
        />
      ))}
    </div>
  );
}
 
// Model performance card component
function ModelCard({
  name,
  shortName,
  provider,
  testCount,
  avgOverall,
  avgAccuracy,
  avgStyle,
  onClick,
}: {
  name: string;
  shortName?: string | null;
  provider: string;
  testCount: number;
  avgOverall: number;
  avgAccuracy: number;
  avgStyle: number;
  onClick?: () => void;
}) {
  return (
    <Card className="hover:border-primary/50 transition-colors">
      <CardContent className="p-4">
        <div className="flex items-start gap-3 mb-4">
          <ModelAvatar
            name={name}
            shortName={shortName}
            provider={provider}
            size="lg"
          />
          <div className="flex-1 min-w-0">
            <h3 className="font-semibold truncate">{name}</h3>
            <p className="text-sm text-muted-foreground">{provider}</p>
          </div>
        </div>
 
        {/* Overall Rating */}
        <div className="mb-4">
          <div className="flex items-center justify-between mb-1">
            <span className="text-xs font-medium text-muted-foreground uppercase tracking-wide">
              Overall
            </span>
            <span className="text-sm font-semibold">
              {avgOverall.toFixed(1)}/5
            </span>
          </div>
          <StarDisplay rating={avgOverall} />
        </div>
 
        {/* Sub-scores */}
        <div className="grid grid-cols-2 gap-3 mb-4">
          <div className="bg-muted/50 rounded-lg p-2 text-center">
            <p className="text-xs text-muted-foreground mb-0.5">Accuracy</p>
            <p className="text-sm font-semibold">{avgAccuracy.toFixed(1)}</p>
          </div>
          <div className="bg-muted/50 rounded-lg p-2 text-center">
            <p className="text-xs text-muted-foreground mb-0.5">Style</p>
            <p className="text-sm font-semibold">{avgStyle.toFixed(1)}</p>
          </div>
        </div>
 
        {/* Footer */}
        <div className="flex items-center justify-between pt-3 border-t">
          <span className="text-xs text-muted-foreground flex items-center gap-1">
            <Cpu className="h-3 w-3" />
            {testCount} test{testCount !== 1 ? "s" : ""}
          </span>
          <Button
            variant="ghost"
            size="sm"
            className="p-0 h-auto text-primary hover:bg-transparent"
            onClick={onClick}
          >
            View Details
            <ArrowRight className="h-3 w-3 ml-1" />
          </Button>
        </div>
      </CardContent>
    </Card>
  );
}
 
export default function MyModelsPage() {
  const [addDialogOpen, setAddDialogOpen] = useState(false);
  const [selectedModel, setSelectedModel] = useState<UserModelWithStats | null>(null);
  const [selectedTool, setSelectedTool] = useState<UserToolWithStats | null>(null);
 
  // Fetch user's models with stats
  const { data: userModels = [] } = useQuery<UserModelWithStats[]>({
    queryKey: ["/api/model-map/user/models"],
    queryFn: async () => {
      const res = await apiRequest("GET", "/api/model-map/user/models?includeStats=true");
      return res.json();
    },
  });
 
  // Fetch user's tools with stats
  const { data: userTools = [] } = useQuery<UserToolWithStats[]>({
    queryKey: ["/api/model-map/user/tools"],
    queryFn: async () => {
      const res = await apiRequest("GET", "/api/model-map/user/tools?includeStats=true");
      return res.json();
    },
  });
 
  const hasItems = userModels.length > 0 || userTools.length > 0;
 
  return (
    <div className="container max-w-6xl mx-auto py-8 px-4">
      {/* Header */}
      <div className="flex items-center justify-between mb-8">
        <div>
          <h1 className="text-3xl font-bold mb-2">My Models</h1>
          <p className="text-muted-foreground">
            Your personal AI model roster with performance insights
          </p>
        </div>
        <Button onClick={() => setAddDialogOpen(true)}>
          <Plus className="h-4 w-4 mr-2" />
          Add Models
        </Button>
      </div>
 
      {/* Empty State */}
      {!hasItems && (
        <Card className="py-12">
          <CardContent className="text-center">
            <Cpu className="h-12 w-12 mx-auto text-muted-foreground mb-4" />
            <h3 className="text-lg font-semibold mb-2">No models added yet</h3>
            <p className="text-muted-foreground mb-4">
              Add models to your roster to track their performance across tests
            </p>
            <Button onClick={() => setAddDialogOpen(true)}>
              <Plus className="h-4 w-4 mr-2" />
              Add Your First Model
            </Button>
          </CardContent>
        </Card>
      )}
 
      {/* Models Grid */}
      {userModels.length > 0 && (
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
          {userModels.map((um) => (
            <ModelCard
              key={um.id}
              name={um.model.name}
              shortName={um.model.shortName}
              provider={um.model.provider}
              testCount={um.stats?.testCount || 0}
              avgOverall={um.stats?.avgOverall || 0}
              avgAccuracy={um.stats?.avgAccuracy || 0}
              avgStyle={um.stats?.avgStyle || 0}
              onClick={() => setSelectedModel(um)}
            />
          ))}
        </div>
      )}
 
      {/* Tools Section */}
      {userTools.length > 0 && (
        <>
          <h2 className="text-xl font-semibold mt-10 mb-4">My Tools</h2>
          <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
            {userTools.map((ut) => (
              <ModelCard
                key={ut.id}
                name={ut.tool.name}
                shortName={ut.tool.shortName}
                provider={ut.tool.provider}
                testCount={ut.stats?.testCount || 0}
                avgOverall={ut.stats?.avgOverall || 0}
                avgAccuracy={ut.stats?.avgAccuracy || 0}
                avgStyle={ut.stats?.avgStyle || 0}
                onClick={() => setSelectedTool(ut)}
              />
            ))}
          </div>
        </>
      )}
 
      {/* Add Model Dialog */}
      <AddModelDialog
        open={addDialogOpen}
        onOpenChange={setAddDialogOpen}
      />
 
      {/* Model Detail Dialog */}
      <Dialog open={!!selectedModel} onOpenChange={(open) => !open && setSelectedModel(null)}>
        <DialogContent className="max-w-md">
          <DialogHeader>
            <DialogTitle>Model Details</DialogTitle>
          </DialogHeader>
          {selectedModel && (
            <div className="space-y-4">
              <div className="flex items-center gap-4">
                <ModelAvatar
                  name={selectedModel.model.name}
                  shortName={selectedModel.model.shortName}
                  provider={selectedModel.model.provider}
                  size="lg"
                />
                <div>
                  <h3 className="font-semibold text-lg">{selectedModel.model.name}</h3>
                  <p className="text-sm text-muted-foreground">{selectedModel.model.provider}</p>
                </div>
              </div>
 
              {selectedModel.model.description && (
                <div>
                  <p className="text-xs font-medium text-muted-foreground uppercase tracking-wide mb-1">
                    Description
                  </p>
                  <p className="text-sm">{selectedModel.model.description}</p>
                </div>
              )}
 
              <div className="grid grid-cols-2 gap-3">
                <div className="bg-muted rounded-lg p-3">
                  <p className="text-xs text-muted-foreground mb-1">Overall Rating</p>
                  <p className="text-lg font-semibold">{selectedModel.stats?.avgOverall.toFixed(1) || "0"}/5</p>
                </div>
                <div className="bg-muted rounded-lg p-3">
                  <p className="text-xs text-muted-foreground mb-1">Test Count</p>
                  <p className="text-lg font-semibold">{selectedModel.stats?.testCount || 0}</p>
                </div>
              </div>
 
              <div className="grid grid-cols-2 gap-3">
                <div className="bg-muted rounded-lg p-3">
                  <p className="text-xs text-muted-foreground mb-1">Accuracy</p>
                  <p className="font-semibold">{selectedModel.stats?.avgAccuracy.toFixed(1) || "0"}</p>
                </div>
                <div className="bg-muted rounded-lg p-3">
                  <p className="text-xs text-muted-foreground mb-1">Style</p>
                  <p className="font-semibold">{selectedModel.stats?.avgStyle.toFixed(1) || "0"}</p>
                </div>
              </div>
 
              {selectedModel.model.capabilities && selectedModel.model.capabilities.length > 0 && (
                <div>
                  <p className="text-xs font-medium text-muted-foreground uppercase tracking-wide mb-2">
                    Capabilities
                  </p>
                  <div className="flex flex-wrap gap-1">
                    {selectedModel.model.capabilities.map((cap) => (
                      <span key={cap} className="text-xs bg-primary/10 text-primary px-2 py-1 rounded">
                        {cap}
                      </span>
                    ))}
                  </div>
                </div>
              )}
            </div>
          )}
        </DialogContent>
      </Dialog>
 
      {/* Tool Detail Dialog */}
      <Dialog open={!!selectedTool} onOpenChange={(open) => !open && setSelectedTool(null)}>
        <DialogContent className="max-w-md">
          <DialogHeader>
            <DialogTitle>Tool Details</DialogTitle>
          </DialogHeader>
          {selectedTool && (
            <div className="space-y-4">
              <div className="flex items-center gap-4">
                <ModelAvatar
                  name={selectedTool.tool.name}
                  shortName={selectedTool.tool.shortName}
                  provider={selectedTool.tool.provider}
                  size="lg"
                />
                <div>
                  <h3 className="font-semibold text-lg">{selectedTool.tool.name}</h3>
                  <p className="text-sm text-muted-foreground">{selectedTool.tool.provider}</p>
                </div>
              </div>
 
              {selectedTool.tool.description && (
                <div>
                  <p className="text-xs font-medium text-muted-foreground uppercase tracking-wide mb-1">
                    Description
                  </p>
                  <p className="text-sm">{selectedTool.tool.description}</p>
                </div>
              )}
 
              <div className="grid grid-cols-2 gap-3">
                <div className="bg-muted rounded-lg p-3">
                  <p className="text-xs text-muted-foreground mb-1">Overall Rating</p>
                  <p className="text-lg font-semibold">{selectedTool.stats?.avgOverall.toFixed(1) || "0"}/5</p>
                </div>
                <div className="bg-muted rounded-lg p-3">
                  <p className="text-xs text-muted-foreground mb-1">Test Count</p>
                  <p className="text-lg font-semibold">{selectedTool.stats?.testCount || 0}</p>
                </div>
              </div>
 
              <div className="grid grid-cols-2 gap-3">
                <div className="bg-muted rounded-lg p-3">
                  <p className="text-xs text-muted-foreground mb-1">Accuracy</p>
                  <p className="font-semibold">{selectedTool.stats?.avgAccuracy.toFixed(1) || "0"}</p>
                </div>
                <div className="bg-muted rounded-lg p-3">
                  <p className="text-xs text-muted-foreground mb-1">Style</p>
                  <p className="font-semibold">{selectedTool.stats?.avgStyle.toFixed(1) || "0"}</p>
                </div>
              </div>
 
              {selectedTool.tool.url && (
                <Button
                  variant="outline"
                  className="w-full"
                  onClick={() => window.open(selectedTool.tool.url ?? undefined, "_blank")}
                >
                  Visit Tool Website
                </Button>
              )}
            </div>
          )}
        </DialogContent>
      </Dialog>
    </div>
  );
}