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

65.34% Statements 66/101
78.18% Branches 86/110
40% Functions 2/5
79.36% Lines 50/63

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                                  92x         92x   99x 92x   99x 92x 92x 92x   92x   92x   92x 118x 103x 92x     146x 137x 107x   92x 1x       1x 230x       73x                     1x         1x       1x         1x 440x             230x     1x         1x 348x               380x 1041x 26x       144x 46x       230x 68x       99x       92x   92x     772x 68x           92x 92x     568x 477x                 60x                             27x 25x          
import type { EconomicsSettingsProfileRead } from "@/api/apiStore.gen";
import {
  useEconomicsDefaultRatesListQuery,
  useEconomicsIndexSeriesListQuery,
  useEconomicsSettingsProfilesCopyCreateMutation,
  useEconomicsSettingsProfilesCreateMutation,
  useEconomicsSettingsProfilesDestroyMutation,
  useEconomicsSettingsProfilesListQuery,
  useEconomicsSettingsProfilesPartialUpdateMutation,
  useEconomicsStudiesPartialUpdateMutation,
} from "@/api/apiStore.gen";
import { EconomicsScheduleSelector } from "../schedule-selector";
import { BaselineForm } from "./forms/BaselineForm";
import { SetupForm } from "./forms/SetupForm";
import type { EconomicsSettingsPanelProps } from "./model/settingsTypes";
import { SettingsProfileControls } from "./profiles/SettingsProfileControls";
 
export function EconomicsSettingsPanel({
  study,
  canEdit,
  onConfigurationSaved,
}: EconomicsSettingsPanelProps) {
  const profilesQuery = useEconomicsSettingsProfilesListQuery({
    flowsheet: study.flowsheet,
  });
  const indexSeriesQuery = useEconomicsIndexSeriesListQuery({
    flowsheet: study.flowsheet,
  });
  const defaultRatesQuery = useEconomicsDefaultRatesListQuery({});
  const [saveProfile] = useEconomicsSettingsProfilesPartialUpdateMutation();
  const [updateStudy] = useEconomicsStudiesPartialUpdateMutation();
  const [createProfile, createProfileState] =
    useEconomicsSettingsProfilesCreateMutation();
  const [copyProfile, copyProfileState] =
    useEconomicsSettingsProfilesCopyCreateMutation();
  const [deleteProfile, deleteProfileState] =
    useEconomicsSettingsProfilesDestroyMutation();
  const profiles = profilesQuery.currentData ?? [];
  const indexSeries = indexSeriesQuery.currentData ?? [];
  const selectedProfile = selectedSettingsProfile(
    profiles,
    study.settings_profile,
  );
  const profilesLoading = activeQueryLoading(profilesQuery);
  const indexSeriesLoading = activeQueryLoading(indexSeriesQuery);
 
  const selectProfile = async (profileId: number) => {
    await updateStudy({
      id: study.id,
      patchedEconomicsStudy: { settings_profile: profileId },
    }).unwrap();
    onConfigurationSaved?.();
  };
 
  return (
    <div className="mt-5 space-y-5" aria-label="Economics settings">
      <SettingsProfileControls
        profiles={profiles}
        selectedProfile={selectedProfile}
        canEdit={canEdit}
        loading={profilesLoading}
        creating={createProfileState.isLoading}
        copying={copyProfileState.isLoading}
        deleting={deleteProfileState.isLoading}
        onSelectProfile={selectProfile}
        onRenameProfile={async (name) => {
          Iif (!selectedProfile) return;
          await saveProfile({
            flowsheet: study.flowsheet,
            id: selectedProfile.id,
            patchedEconomicsSettingsProfile: { name },
          }).unwrap();
          onConfigurationSaved?.();
        }}
        onSetDefaultProfile={async () => {
          Iif (!selectedProfile) return;
          await saveProfile({
            flowsheet: study.flowsheet,
            id: selectedProfile.id,
            patchedEconomicsSettingsProfile: { is_default: true },
          }).unwrap();
          onConfigurationSaved?.();
        }}
        onCreateProfile={async (name) => {
          const profile = await createProfile({
            flowsheet: study.flowsheet,
            economicsSettingsProfile: { name },
          }).unwrap();
          await selectProfile(profile.id);
        }}
        onCopyProfile={async (name) => {
          Iif (!selectedProfile) return;
          const profile = await copyProfile({
            flowsheet: study.flowsheet,
            id: selectedProfile.id,
            settingsProfileCopyRequest: { name },
          }).unwrap();
          await selectProfile(profile.id);
        }}
        onDeleteProfile={async (profileId) => {
          await deleteProfile({
            flowsheet: study.flowsheet,
            id: profileId,
          }).unwrap();
          profilesQuery.refetch();
          onConfigurationSaved?.();
        }}
      />
      {!selectedProfile && !profilesLoading ? (
        <section className="rounded-md border bg-card p-4 text-sm text-muted-foreground">
          Create a settings profile before editing project defaults.
        </section>
      ) : null}
      <EconomicsScheduleSelector
        study={study}
        canEdit={canEdit}
        onSaved={onConfigurationSaved}
      />
      <SetupForm
        study={study}
        assumptions={selectedProfile}
        indexSeries={indexSeries}
        defaultRates={defaultRatesQuery.data ?? []}
        defaultRatesLoading={defaultRatesQuery.isLoading}
        defaultRatesError={defaultRatesQuery.isError}
        loading={
          profilesLoading || indexSeriesLoading || defaultRatesQuery.isLoading
        }
        readOnly={!canEdit || !selectedProfile}
        onSave={saveProfile}
        onSaved={onConfigurationSaved}
      />
      <BaselineForm
        study={study}
        profile={selectedProfile}
        assumptions={selectedProfile}
        baseline={selectedProfile}
        indexSeries={indexSeries}
        loading={profilesLoading || indexSeriesLoading}
        readOnly={!canEdit || !selectedProfile}
        onSave={saveProfile}
        onSaved={onConfigurationSaved}
      />
    </div>
  );
}
 
function activeQueryLoading(query: {
  currentData?: unknown;
  isLoading: boolean;
  isFetching: boolean;
}) {
  return query.isLoading || (query.isFetching && query.currentData == null);
}
 
export function EconomicsSetupPanel(props: EconomicsSettingsPanelProps) {
  return <EconomicsSettingsPanel {...props} />;
}
 
export function EconomicsBaselinePanel(props: EconomicsSettingsPanelProps) {
  return <EconomicsSettingsPanel {...props} />;
}
 
function selectedSettingsProfile(
  profiles: EconomicsSettingsProfileRead[],
  selectedProfileId?: number | null,
) {
  return (
    profiles.find((profile) => profile.id === selectedProfileId) ??
    profiles.find((profile) => profile.is_default) ??
    profiles[0]
  );
}