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

64.64% Statements 64/99
77% Branches 77/100
40% Functions 2/5
78.68% Lines 48/61

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                                53x         53x   56x 53x   56x 53x 53x 53x   53x   53x   53x 64x 59x 53x     77x 72x 62x   53x 1x       1x 152x       44x                     1x       1x       1x       1x 223x             152x     1x       1x 170x         182x 625x 11x       75x 19x       58x       53x   53x   224x 14x         53x 53x   123x 249x                 28x                             12x 13x          
import type { EconomicsSettingsProfileRead } from "@/api/apiStore.gen";
import {
  useEconomicsDefaultRatesListQuery,
  useEconomicsIndexSeriesListQuery,
  useEconomicsSettingsProfilesCopyCreateMutation,
  useEconomicsSettingsProfilesCreateMutation,
  useEconomicsSettingsProfilesDestroyMutation,
  useEconomicsSettingsProfilesListQuery,
  useEconomicsSettingsProfilesPartialUpdateMutation,
  useEconomicsStudiesPartialUpdateMutation,
} from "@/api/apiStore.gen";
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({
            id: selectedProfile.id,
            patchedEconomicsSettingsProfile: { name },
          }).unwrap();
          onConfigurationSaved?.();
        }}
        onSetDefaultProfile={async () => {
          Iif (!selectedProfile) return;
          await saveProfile({
            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({
            id: selectedProfile.id,
            settingsProfileCopyRequest: { name },
          }).unwrap();
          await selectProfile(profile.id);
        }}
        onDeleteProfile={async (profileId) => {
          await deleteProfile({ 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}
      <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}
      />
      <BaselineForm
        profile={selectedProfile}
        assumptions={selectedProfile}
        baseline={selectedProfile}
        indexSeries={indexSeries}
        loading={profilesLoading || indexSeriesLoading}
        readOnly={!canEdit || !selectedProfile}
        onSave={saveProfile}
      />
    </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]
  );
}