All files / web/src/components/agents/skills/evaluations evaluation-edit-view.tsx

4.27% Statements 25/585
100% Branches 0/0
0% Functions 0/1
4.27% Lines 25/585

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 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762    1x   1x 1x 1x 1x             1x 1x               1x 1x 1x 1x         1x 1x           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x               1x                 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
'use client';
 
import { type AIProvider, PrettyAIProvider } from '@shared/types/constants';
import type { EvaluationMethodDetails } from '@shared/types/evaluations';
import { getEvaluationMethods } from '@web/api/v1/super-agents/skills';
import { Badge } from '@web/components/ui/badge';
import { Button } from '@web/components/ui/button';
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from '@web/components/ui/card';
import { Checkbox } from '@web/components/ui/checkbox';
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from '@web/components/ui/command';
import { Input } from '@web/components/ui/input';
import { Label } from '@web/components/ui/label';
import { PageHeader } from '@web/components/ui/page-header';
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from '@web/components/ui/popover';
import { Skeleton } from '@web/components/ui/skeleton';
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from '@web/components/ui/tooltip';
import { useSmartBack } from '@web/hooks/use-smart-back';
import { useToast } from '@web/hooks/use-toast';
import { useAIProviders } from '@web/providers/ai-providers';
import { useModels } from '@web/providers/models';
import { useNavigation } from '@web/providers/navigation';
import { useSkillOptimizationEvaluations } from '@web/providers/skill-optimization-evaluations';
import { useSkills } from '@web/providers/skills';
import { sortModels } from '@web/utils/model-sorting';
import { cn } from '@web/utils/ui/utils';
import {
  CheckIcon,
  ChevronsUpDownIcon,
  Loader2,
  RotateCcw,
  SaveIcon,
} from 'lucide-react';
import type { ReactElement } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
 
interface ModelOption {
  id: string;
  name: string;
  provider: AIProvider;
  modelType: 'text' | 'embed';
}
 
export function EvaluationEditView(): ReactElement {
  const { selectedSkill } = useSkills();
  const { navigationState } = useNavigation();
  const goBack = useSmartBack();
  const { toast } = useToast();
 
  const { evaluations, updateEvaluation, setSkillId } =
    useSkillOptimizationEvaluations();
  const { models, setQueryParams } = useModels();
  const { aiProviderConfigs } = useAIProviders();
 
  // Fetch all models on mount
  useEffect(() => {
    setQueryParams({});
  }, [setQueryParams]);
 
  const [isSaving, setIsSaving] = useState(false);
  const [evaluationMethods, setEvaluationMethods] = useState<
    EvaluationMethodDetails[]
  >([]);
  const [isLoadingMethods, setIsLoadingMethods] = useState(true);
  const [modelPopoverOpen, setModelPopoverOpen] = useState(false);
 
  // Form state
  const [weight, setWeight] = useState<string>('1.0');
  const [params, setParams] = useState<Record<string, unknown>>({});
  const [modelId, setModelId] = useState<string | null>(null);
 
  // Build model options (text models only for evaluations), sorted alphabetically
  const textModelOptions = useMemo((): ModelOption[] => {
    const options = models
      .filter((model) => model.model_type === 'text')
      .map((model) => {
        const provider = aiProviderConfigs.find(
          (p) => p.id === model.ai_provider_id,
        );
        return {
          id: model.id,
          name: model.model_name,
          provider: (provider?.ai_provider ?? 'openai') as AIProvider,
          modelType: model.model_type,
        };
      });
    // Sort alphabetically by model name, then by provider name
    return sortModels(options);
  }, [models, aiProviderConfigs]);
 
  // Fetch evaluation methods from server
  useEffect(() => {
    const fetchMethods = async () => {
      try {
        setIsLoadingMethods(true);
        const methods = await getEvaluationMethods();
        setEvaluationMethods(methods);
      } catch (error) {
        console.error('Failed to fetch evaluation methods:', error);
        toast({
          title: 'Failed to load evaluation methods',
          description: 'Please try refreshing the page.',
          variant: 'destructive',
        });
      } finally {
        setIsLoadingMethods(false);
      }
    };
 
    fetchMethods();
  }, [toast]);
 
  // Set skill ID when skill changes
  useEffect(() => {
    if (!selectedSkill) {
      setSkillId(null);
      return;
    }
    setSkillId(selectedSkill.id);
  }, [selectedSkill, setSkillId]);
 
  // Find the evaluation being edited
  const evaluation = useMemo(() => {
    if (!navigationState.selectedEvaluationId) return undefined;
    return evaluations.find(
      (e) => e.id === navigationState.selectedEvaluationId,
    );
  }, [evaluations, navigationState.selectedEvaluationId]);
 
  // Find evaluation method details
  const evaluationMethod = useMemo(() => {
    if (!evaluation) return undefined;
    return evaluationMethods.find(
      (m) => m.method === evaluation.evaluation_method,
    );
  }, [evaluation, evaluationMethods]);
 
  // Initialize form when evaluation loads
  useEffect(() => {
    if (evaluation && evaluationMethod) {
      setWeight(evaluation.weight.toString());
      setModelId(evaluation.model_id);
 
      // Extract defaults from JSON schema if available
      if (evaluationMethod.parameterSchema) {
        const schemaDefaults: Record<string, unknown> = {};
 
        // Extract default values from JSON schema properties
        // biome-ignore lint/suspicious/noExplicitAny: JSON schema type is dynamic
        const properties = (evaluationMethod.parameterSchema as any)
          ?.properties;
        if (properties) {
          for (const [key, prop] of Object.entries(properties)) {
            // biome-ignore lint/suspicious/noExplicitAny: JSON schema property type is dynamic
            const propDef = prop as any;
            if ('default' in propDef) {
              schemaDefaults[key] = propDef.default;
            }
          }
        }
 
        // Merge schema defaults with existing params (existing params take precedence)
        const mergedParams = {
          ...schemaDefaults,
          ...evaluation.params,
        };
        setParams(mergedParams);
      } else {
        // Fall back to just using existing params
        setParams(evaluation.params);
      }
    }
  }, [evaluation, evaluationMethod]);
 
  const handleSave = async () => {
    if (!selectedSkill || !evaluation) return;
 
    const numericWeight = Number.parseFloat(weight);
 
    // Validate weight
    if (Number.isNaN(numericWeight) || numericWeight <= 0) {
      toast({
        title: 'Invalid weight',
        description: 'Weight must be a positive number.',
        variant: 'destructive',
      });
      return;
    }
 
    setIsSaving(true);
    try {
      // Send all params including model_id
      await updateEvaluation(selectedSkill.id, evaluation.id, {
        weight: numericWeight,
        params,
        model_id: modelId,
      });
 
      toast({
        title: 'Evaluation updated',
        description: 'The evaluation has been updated successfully.',
      });
 
      goBack();
    } catch (error) {
      console.error('Failed to update evaluation:', error);
      toast({
        title: 'Failed to update evaluation',
        description:
          error instanceof Error ? error.message : 'Please try again.',
        variant: 'destructive',
      });
    } finally {
      setIsSaving(false);
    }
  };
 
  const handleParamChange = (key: string, value: unknown) => {
    setParams((prev) => ({
      ...prev,
      [key]: value,
    }));
  };
 
  const handleResetToDefault = (key: string) => {
    // Get the default value from the schema
    if (!evaluationMethod?.parameterSchema) return;
 
    // biome-ignore lint/suspicious/noExplicitAny: JSON schema type is dynamic
    const properties = (evaluationMethod.parameterSchema as any)?.properties;
    if (!properties || !properties[key] || !('default' in properties[key]))
      return;
 
    // Reset to default value
    setParams((prev) => ({
      ...prev,
      [key]: properties[key].default,
    }));
  };
 
  // Check if a param has a default value in the schema
  const hasDefault = (key: string): boolean => {
    if (!evaluationMethod?.parameterSchema) return false;
 
    // biome-ignore lint/suspicious/noExplicitAny: JSON schema type is dynamic
    const properties = (evaluationMethod.parameterSchema as any)?.properties;
    if (!properties || !properties[key]) return false;
 
    return 'default' in properties[key];
  };
 
  // Check if the current value differs from the default
  // biome-ignore lint/correctness/useExhaustiveDependencies: params dependency needed to trigger re-render when values change
  const isDifferentFromDefault = useCallback(
    (key: string, value: unknown): boolean => {
      if (!evaluationMethod?.parameterSchema) return false;
 
      // biome-ignore lint/suspicious/noExplicitAny: JSON schema type is dynamic
      const properties = (evaluationMethod.parameterSchema as any)?.properties;
      if (!properties || !properties[key] || !('default' in properties[key]))
        return false;
 
      const defaultValue = properties[key].default;
 
      // Deep equality check for objects/arrays, otherwise simple equality
      if (typeof value === 'object' && value !== null) {
        return JSON.stringify(value) !== JSON.stringify(defaultValue);
      }
 
      return value !== defaultValue;
    },
    [evaluationMethod?.parameterSchema, params],
  );
 
  const renderParamInput = (key: string, value: unknown) => {
    const showReset = hasDefault(key) && isDifferentFromDefault(key, value);
 
    // Determine the type of input based on the value
    if (typeof value === 'boolean') {
      return (
        <div key={key} className="flex items-center gap-2 h-8">
          <Checkbox
            id={key}
            checked={value}
            onCheckedChange={(checked: boolean) =>
              handleParamChange(key, checked)
            }
            disabled={isSaving}
          />
          <Label htmlFor={key} className="capitalize">
            {key.replace(/_/g, ' ')}
          </Label>
          {showReset && (
            <TooltipProvider>
              <Tooltip>
                <TooltipTrigger asChild>
                  <Button
                    variant="ghost"
                    size="icon"
                    onClick={() => handleResetToDefault(key)}
                    disabled={isSaving}
                    className="h-8 w-8"
                  >
                    <RotateCcw className="h-4 w-4" />
                  </Button>
                </TooltipTrigger>
                <TooltipContent>
                  <p>Reset to default</p>
                </TooltipContent>
              </Tooltip>
            </TooltipProvider>
          )}
        </div>
      );
    }
 
    if (typeof value === 'number') {
      // Extract min/max from schema
      let min: number | undefined;
      let max: number | undefined;
      if (evaluationMethod?.parameterSchema) {
        // biome-ignore lint/suspicious/noExplicitAny: JSON schema type is dynamic
        const properties = (evaluationMethod.parameterSchema as any)
          ?.properties;
        if (properties?.[key]) {
          min = properties[key].minimum;
          max = properties[key].maximum;
        }
      }
 
      return (
        <div key={key} className="space-y-2">
          <div className="inline-flex items-center gap-2 h-8">
            <Label htmlFor={key} className="capitalize">
              {key.replace(/_/g, ' ')}
            </Label>
            {showReset && (
              <TooltipProvider>
                <Tooltip>
                  <TooltipTrigger asChild>
                    <Button
                      variant="ghost"
                      size="icon"
                      onClick={() => handleResetToDefault(key)}
                      disabled={isSaving}
                      className="h-8 w-8"
                    >
                      <RotateCcw className="h-4 w-4" />
                    </Button>
                  </TooltipTrigger>
                  <TooltipContent>
                    <p>Reset to default</p>
                  </TooltipContent>
                </Tooltip>
              </TooltipProvider>
            )}
          </div>
          <Input
            id={key}
            type="number"
            value={value}
            onChange={(e) => {
              const numValue = Number.parseFloat(e.target.value);
              if (!Number.isNaN(numValue)) {
                handleParamChange(key, numValue);
              }
            }}
            disabled={isSaving}
            step="any"
            min={min}
            max={max}
            className="w-32"
          />
        </div>
      );
    }
 
    if (typeof value === 'string') {
      return (
        <div key={key} className="space-y-2">
          <div className="inline-flex items-center gap-2 h-8">
            <Label htmlFor={key} className="capitalize flex-1">
              {key.replace(/_/g, ' ')}
            </Label>
            {showReset && (
              <TooltipProvider>
                <Tooltip>
                  <TooltipTrigger asChild>
                    <Button
                      variant="ghost"
                      size="icon"
                      onClick={() => handleResetToDefault(key)}
                      disabled={isSaving}
                      className="h-8 w-8"
                    >
                      <RotateCcw className="h-4 w-4" />
                    </Button>
                  </TooltipTrigger>
                  <TooltipContent>
                    <p>Reset to default</p>
                  </TooltipContent>
                </Tooltip>
              </TooltipProvider>
            )}
          </div>
          <Input
            id={key}
            type="text"
            value={value}
            onChange={(e) => handleParamChange(key, e.target.value)}
            disabled={isSaving}
          />
        </div>
      );
    }
 
    // For complex types, show as JSON
    return (
      <div key={key} className="space-y-2">
        <div className="flex items-center gap-2 h-8">
          <Label htmlFor={key} className="capitalize">
            {key.replace(/_/g, ' ')}
          </Label>
          {showReset && (
            <TooltipProvider>
              <Tooltip>
                <TooltipTrigger asChild>
                  <Button
                    variant="ghost"
                    size="icon"
                    onClick={() => handleResetToDefault(key)}
                    disabled={isSaving}
                    className="h-8 w-8"
                  >
                    <RotateCcw className="h-4 w-4" />
                  </Button>
                </TooltipTrigger>
                <TooltipContent>
                  <p>Reset to default</p>
                </TooltipContent>
              </Tooltip>
            </TooltipProvider>
          )}
        </div>
        <Input
          id={key}
          type="text"
          value={JSON.stringify(value)}
          onChange={(e) => {
            try {
              const parsed = JSON.parse(e.target.value);
              handleParamChange(key, parsed);
            } catch {
              // Invalid JSON, ignore
            }
          }}
          disabled={isSaving}
        />
      </div>
    );
  };
 
  // Show error if no evaluation ID in navigation state
  if (!navigationState.selectedEvaluationId) {
    return (
      <>
        <PageHeader
          title="Edit Evaluation"
          description="No evaluation selected"
          showBackButton={true}
          onBack={goBack}
        />
        <div className="p-6">
          <Card>
            <CardContent className="pt-6 text-center">
              <p className="text-muted-foreground">
                No evaluation selected. Please go back and select an evaluation
                to edit.
              </p>
            </CardContent>
          </Card>
        </div>
      </>
    );
  }
 
  // Show loading state
  if (isLoadingMethods || !evaluation) {
    return (
      <>
        <PageHeader
          title="Edit Evaluation"
          description="Loading evaluation data..."
          showBackButton={true}
          onBack={goBack}
        />
        <div className="p-6">
          <Card>
            <CardHeader>
              <Skeleton className="h-6 w-48 mb-2" />
              <Skeleton className="h-4 w-64" />
            </CardHeader>
            <CardContent className="space-y-4">
              <Skeleton className="h-10 w-full" />
              <Skeleton className="h-10 w-full" />
              <Skeleton className="h-10 w-full" />
            </CardContent>
          </Card>
        </div>
      </>
    );
  }
 
  return (
    <>
      <PageHeader
        title={`Edit ${evaluationMethod?.name || 'Evaluation'}`}
        description={`Configure the parameters for this evaluation method`}
        showBackButton={true}
        onBack={goBack}
        actions={
          <Button onClick={handleSave} disabled={isSaving}>
            {isSaving ? (
              <>
                <Loader2 className="h-4 w-4 mr-2 animate-spin" />
                Saving...
              </>
            ) : (
              <>
                <SaveIcon className="h-4 w-4 mr-2" />
                Save Changes
              </>
            )}
          </Button>
        }
      />
 
      <div className="p-6 space-y-6">
        {/* Evaluation Info */}
        <Card>
          <CardHeader>
            <CardTitle>{evaluationMethod?.name || 'Evaluation'}</CardTitle>
            <CardDescription>
              {evaluationMethod?.description ||
                'Configure this evaluation method'}
            </CardDescription>
          </CardHeader>
        </Card>
 
        {/* Weight Configuration */}
        <Card>
          <CardHeader>
            <CardTitle>Weight</CardTitle>
            <CardDescription>
              The relative importance of this evaluation in the overall score
            </CardDescription>
          </CardHeader>
          <CardContent>
            <div className="space-y-2">
              <Label>Weight (must be positive)</Label>
              <Input
                type="number"
                value={weight}
                onChange={(e) => setWeight(e.target.value)}
                disabled={isSaving}
                step="0.1"
                min="0.01"
              />
            </div>
          </CardContent>
        </Card>
 
        {/* Model Configuration */}
        <Card>
          <CardHeader>
            <CardTitle>Model</CardTitle>
            <CardDescription>
              The model used to run this evaluation. If not set, the system
              default will be used.
            </CardDescription>
          </CardHeader>
          <CardContent>
            <div className="space-y-2">
              <Label>Evaluation Model</Label>
              <Popover
                open={modelPopoverOpen}
                onOpenChange={setModelPopoverOpen}
              >
                <PopoverTrigger asChild>
                  <Button
                    variant="outline"
                    aria-expanded={modelPopoverOpen}
                    className="w-full justify-between"
                    disabled={isSaving}
                  >
                    {modelId ? (
                      (() => {
                        const selectedModel = textModelOptions.find(
                          (m) => m.id === modelId,
                        );
                        return selectedModel ? (
                          <div className="flex items-center gap-2 truncate">
                            <span className="truncate">
                              {selectedModel.name}
                            </span>
                            <Badge
                              variant="outline"
                              className="text-xs shrink-0"
                            >
                              {PrettyAIProvider[selectedModel.provider] ??
                                selectedModel.provider}
                            </Badge>
                          </div>
                        ) : (
                          'Select a model...'
                        );
                      })()
                    ) : (
                      <span className="text-muted-foreground">
                        Use system default
                      </span>
                    )}
                    <ChevronsUpDownIcon className="ml-2 h-4 w-4 shrink-0 opacity-50" />
                  </Button>
                </PopoverTrigger>
                <PopoverContent className="w-[400px] p-0" align="start">
                  <Command>
                    <CommandInput placeholder="Search models..." />
                    <CommandList>
                      <CommandEmpty>No models found.</CommandEmpty>
                      <CommandGroup>
                        <CommandItem
                          value="__none__"
                          onSelect={() => {
                            setModelId(null);
                            setModelPopoverOpen(false);
                          }}
                        >
                          <CheckIcon
                            className={cn(
                              'mr-2 h-4 w-4',
                              modelId === null ? 'opacity-100' : 'opacity-0',
                            )}
                          />
                          <span className="text-muted-foreground">
                            Use system default
                          </span>
                        </CommandItem>
                        {textModelOptions.map((model) => (
                          <CommandItem
                            key={model.id}
                            value={`${model.name} ${PrettyAIProvider[model.provider] ?? model.provider}`}
                            onSelect={() => {
                              setModelId(model.id);
                              setModelPopoverOpen(false);
                            }}
                          >
                            <CheckIcon
                              className={cn(
                                'mr-2 h-4 w-4',
                                modelId === model.id
                                  ? 'opacity-100'
                                  : 'opacity-0',
                              )}
                            />
                            <div className="flex items-center gap-2">
                              <span>{model.name}</span>
                              <Badge variant="outline" className="text-xs">
                                {PrettyAIProvider[model.provider] ??
                                  model.provider}
                              </Badge>
                            </div>
                          </CommandItem>
                        ))}
                      </CommandGroup>
                    </CommandList>
                  </Command>
                </PopoverContent>
              </Popover>
              {textModelOptions.length === 0 && (
                <p className="text-sm text-muted-foreground">
                  No text models available. Add models in AI Providers &amp;
                  Models settings.
                </p>
              )}
            </div>
          </CardContent>
        </Card>
 
        {/* Parameters Configuration */}
        {Object.keys(params).length > 0 && (
          <Card>
            <CardHeader>
              <CardTitle>Parameters</CardTitle>
              <CardDescription>
                Configure the specific parameters for this evaluation method
              </CardDescription>
            </CardHeader>
            <CardContent className="space-y-4">
              {/* Render params in schema order if available, otherwise alphabetical */}
              {(() => {
                // biome-ignore lint/suspicious/noExplicitAny: JSON schema type is dynamic
                const properties = (evaluationMethod?.parameterSchema as any)
                  ?.properties;
                const schemaKeys = properties ? Object.keys(properties) : [];
 
                // If we have schema keys, use that order; otherwise use params keys
                const orderedKeys =
                  schemaKeys.length > 0
                    ? schemaKeys.filter((key) => key in params)
                    : Object.keys(params);
 
                return orderedKeys.map((key) =>
                  renderParamInput(key, params[key]),
                );
              })()}
            </CardContent>
          </Card>
        )}
 
        {/* Save Button (mobile) */}
        <div className="lg:hidden">
          <Button onClick={handleSave} disabled={isSaving} className="w-full">
            {isSaving ? (
              <>
                <Loader2 className="h-4 w-4 mr-2 animate-spin" />
                Saving...
              </>
            ) : (
              <>
                <SaveIcon className="h-4 w-4 mr-2" />
                Save Changes
              </>
            )}
          </Button>
        </div>
      </div>
    </>
  );
}