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

87.27% Statements 96/110
69.85% Branches 95/136
33.33% Functions 6/18
93.82% Lines 76/81

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                                            54x                                                         54x 54x 54x 54x   54x 11x 11x 11x 119x   54x 22x 84x 54x     51x           54x   54x 54x 87x   6x         54x   60x 11x 11x         65x 65x   249x   54x   11x     54x 54x 76x 44x       16x     102x   142x     142x   142x   33x         54x   120x 186x 47x         54x   144x 242x 39x       54x   93x     132x 47x 6x       54x       60x 47x 3x   54x           3x   54x 11x 11x       65x 65x 84x 11x           76x 84x 47x       54x       148x   54x   195x 148x 148x 254x   207x      
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 [createName, setCreateName] = useState("New study profile");
  const [copyName, setCopyName] = useState("");
  const [deleteProfileId, setDeleteProfileId] = useState("");
 
  useEffect(() => {
    setProfileName(selectedProfile?.name ?? "");
    setCopyName(selectedProfile ? `${selectedProfile.name} Copy` : "");
    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"
            name={createName}
            busy={creating}
            disabled={!canEdit}
            onNameChange={setCreateName}
            onSubmit={() => onCreateProfile(createName.trim())}
          />
          <ProfileNameDialog
            title="Copy settings profile"
            triggerLabel="Copy"
            name={copyName}
            busy={copying}
            disabled={!canEdit || !selectedProfile}
            onNameChange={setCopyName}
            onSubmit={() => onCopyProfile(copyName.trim())}
          />
          <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>
  );
}