All files / src/pages/flowsheet-page/economics/settings-panel/profiles SettingsProfileControls.tsx

87.25% Statements 89/102
68.7% Branches 90/131
35.71% Functions 5/14
93.5% Lines 72/77

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                                            94x                                                         94x 94x   94x 26x 26x 214x   94x 49x 162x 94x     82x           94x   94x 94x 140x   13x         94x   107x 26x 26x         120x 120x   419x   94x   21x     94x 94x 136x 72x       34x     196x   238x     238x   238x   46x         94x   232x 66x     94x   94x   358x 64x       94x   158x     222x 80x 13x       94x       107x 80x 7x   94x           7x   94x 26x 26x       120x 120x 162x 27x           148x 162x 80x       94x       254x   94x   334x 254x 254x 418x   340x      
import { useEffect, useState } from "react";
import { Button } from "@/ahuora-design-system/ui/button";
import {
  Dialog,
  DialogContent,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/ahuora-design-system/ui/dialog";
import { Input } from "@/ahuora-design-system/ui/input";
import { Label } from "@/ahuora-design-system/ui/label";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/ahuora-design-system/ui/select";
import type { EconomicsSettingsProfileRead } from "@/api/apiStore.gen";
import { ProfileNameDialog } from "./ProfileNameDialog";
 
export function SettingsProfileControls({
  profiles,
  selectedProfile,
  canEdit,
  loading,
  creating,
  copying,
  deleting,
  onSelectProfile,
  onRenameProfile,
  onSetDefaultProfile,
  onCreateProfile,
  onCopyProfile,
  onDeleteProfile,
}: {
  profiles: EconomicsSettingsProfileRead[];
  selectedProfile?: EconomicsSettingsProfileRead;
  canEdit: boolean;
  loading: boolean;
  creating: boolean;
  copying: boolean;
  deleting: boolean;
  onSelectProfile: (profileId: number) => Promise<void>;
  onRenameProfile: (name: string) => Promise<void>;
  onSetDefaultProfile: () => Promise<void>;
  onCreateProfile: (name: string) => Promise<void>;
  onCopyProfile: (name: string) => Promise<void>;
  onDeleteProfile: (profileId: number) => Promise<void>;
}) {
  const [profileName, setProfileName] = useState(selectedProfile?.name ?? "");
  const [deleteProfileId, setDeleteProfileId] = useState("");
 
  useEffect(() => {
    setProfileName(selectedProfile?.name ?? "");
    setDeleteProfileId(selectedProfile ? String(selectedProfile.id) : "");
  }, [selectedProfile]);
 
  const deleteProfileTarget = profiles.find(
    (profile) => String(profile.id) === deleteProfileId,
  );
  const deleteBlocked = (deleteProfileTarget?.usage_count ?? 0) > 0;
 
  return (
    <section
      className="rounded-md border bg-card p-4"
      aria-label="Settings profiles"
    >
      <div className="grid gap-3 lg:grid-cols-[minmax(12rem,18rem)_minmax(12rem,1fr)_auto] lg:items-end">
        <div className="space-y-1.5">
          <Label htmlFor="economics-settings-profile">Profile</Label>
          <Select
            value={selectedProfile ? String(selectedProfile.id) : ""}
            disabled={!canEdit || loading || profiles.length === 0}
            onValueChange={(value) => void onSelectProfile(Number(value))}
          >
            <SelectTrigger
              id="economics-settings-profile"
              aria-label="Select settings profile"
            >
              <SelectValue
                placeholder={loading ? "Loading profiles" : "No profiles"}
              />
            </SelectTrigger>
            <SelectContent>
              {profiles.map((profile) => (
                <SelectItem key={profile.id} value={String(profile.id)}>
                  {profile.name}
                  {profile.is_default ? " (default)" : ""}
                </SelectItem>
              ))}
            </SelectContent>
          </Select>
        </div>
        <div className="space-y-1.5">
          <Label htmlFor="economics-settings-profile-name">Profile name</Label>
          <div className="flex gap-2">
            <Input
              id="economics-settings-profile-name"
              value={profileName}
              disabled={!canEdit || !selectedProfile}
              onChange={(event) => setProfileName(event.target.value)}
            />
            <Button
              type="button"
              variant="outline"
              disabled={
                !canEdit ||
                !selectedProfile ||
                !profileName.trim() ||
                profileName.trim() === selectedProfile.name
              }
              onClick={() => void onRenameProfile(profileName.trim())}
            >
              Save
            </Button>
          </div>
        </div>
        <div className="flex flex-wrap justify-start gap-2 lg:justify-end">
          <ProfileNameDialog
            title="Create settings profile"
            triggerLabel="Create"
            initialName="New project profile"
            busy={creating}
            disabled={!canEdit}
            onSubmit={onCreateProfile}
          />
          <ProfileNameDialog
            title="Copy settings profile"
            triggerLabel="Copy"
            initialName={selectedProfile ? `${selectedProfile.name} Copy` : ""}
            busy={copying}
            disabled={!canEdit || !selectedProfile}
            onSubmit={onCopyProfile}
          />
          <Button
            type="button"
            variant="outline"
            disabled={
              !canEdit || !selectedProfile || selectedProfile.is_default
            }
            onClick={() => void onSetDefaultProfile()}
          >
            Make default
          </Button>
          <Dialog>
            <DialogTrigger asChild>
              <Button
                type="button"
                variant="outline"
                disabled={!canEdit || profiles.length === 0}
              >
                Delete
              </Button>
            </DialogTrigger>
            <DialogContent>
              <DialogHeader>
                <DialogTitle>Delete settings profile</DialogTitle>
              </DialogHeader>
              <div className="space-y-3">
                <Select
                  value={deleteProfileId}
                  onValueChange={setDeleteProfileId}
                >
                  <SelectTrigger aria-label="Select profile to delete">
                    <SelectValue placeholder="Select profile" />
                  </SelectTrigger>
                  <SelectContent>
                    {profiles.map((profile) => (
                      <SelectItem key={profile.id} value={String(profile.id)}>
                        {profile.name}
                      </SelectItem>
                    ))}
                  </SelectContent>
                </Select>
                {deleteProfileTarget ? (
                  <p className="text-xs text-muted-foreground">
                    {deleteBlocked
                      ? `This profile is used by ${deleteProfileTarget.usage_count} studies. Select another profile for those studies before deleting it.`
                      : "This profile is not used by any study."}
                  </p>
                ) : null}
              </div>
              <DialogFooter>
                <Button
                  type="button"
                  variant="destructive"
                  disabled={!deleteProfileTarget || deleteBlocked || deleting}
                  onClick={() => {
                    Iif (!deleteProfileTarget) return;
                    void onDeleteProfile(deleteProfileTarget.id);
                  }}
                >
                  {deleting ? "Deleting" : "Delete"}
                </Button>
              </DialogFooter>
            </DialogContent>
          </Dialog>
        </div>
      </div>
    </section>
  );
}