|
| 1 | +/*! |
| 2 | + * Copyright 2026 - Swiss Data Science Center (SDSC) |
| 3 | + * A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and |
| 4 | + * Eidgenössische Technische Hochschule Zürich (ETHZ). |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import cx from "classnames"; |
| 20 | +import { useEffect, useMemo, useRef, useState } from "react"; |
| 21 | + |
| 22 | +import RtkOrDataServicesError from "~/components/errors/RtkOrDataServicesError"; |
| 23 | +import ProgressStepsIndicator, { |
| 24 | + ProgressStyle, |
| 25 | + ProgressType, |
| 26 | + StatusStepProgressBar, |
| 27 | + StepsProgressBar, |
| 28 | +} from "~/components/progress/ProgressSteps"; |
| 29 | +import { storageDefinitionAfterSavingCredentialsFromConfig } from "../cloudStorage/projectCloudStorage.utils"; |
| 30 | +import { usePatchDataConnectorsByDataConnectorIdSecretsMutation } from "../dataConnectorsV2/api/data-connectors.enhanced-api"; |
| 31 | +import { shouldCloudStorageSaveCredentials } from "./sessionLaunchValidation.utils"; |
| 32 | +import type { SessionStartDataConnectorConfiguration } from "./startSessionOptionsV2.types"; |
| 33 | + |
| 34 | +import progressBoxStyles from "~/components/progress/ProgressBox.module.scss"; |
| 35 | + |
| 36 | +interface SaveCloudStorageCredentialsProps { |
| 37 | + dataConnectors: SessionStartDataConnectorConfiguration[]; |
| 38 | + onComplete: (configs: SessionStartDataConnectorConfiguration[]) => void; |
| 39 | + title?: string; |
| 40 | +} |
| 41 | + |
| 42 | +export default function SaveCloudStorageCredentials({ |
| 43 | + dataConnectors, |
| 44 | + onComplete, |
| 45 | + title = "Saving credentials", |
| 46 | +}: SaveCloudStorageCredentialsProps) { |
| 47 | + const [steps, setSteps] = useState<StepsProgressBar[]>([]); |
| 48 | + const [saveCredentials, saveCredentialsResult] = |
| 49 | + usePatchDataConnectorsByDataConnectorIdSecretsMutation(); |
| 50 | + |
| 51 | + const credentialsToSave = useMemo(() => { |
| 52 | + return dataConnectors |
| 53 | + .filter(shouldCloudStorageSaveCredentials) |
| 54 | + .map((cs) => ({ |
| 55 | + storageName: cs.dataConnector.name, |
| 56 | + storageId: cs.dataConnector.id, |
| 57 | + secrets: cs.sensitiveFieldValues, |
| 58 | + })); |
| 59 | + }, [dataConnectors]); |
| 60 | + |
| 61 | + const [results, setResults] = useState<StatusStepProgressBar[]>( |
| 62 | + credentialsToSave.map(() => StatusStepProgressBar.WAITING), |
| 63 | + ); |
| 64 | + |
| 65 | + const [index, setIndex] = useState(0); |
| 66 | + const [hasFailed, setHasFailed] = useState(false); |
| 67 | + const [failedError, setFailedError] = |
| 68 | + useState<typeof saveCredentialsResult.error>(undefined); |
| 69 | + const hasCompletedRef = useRef(false); |
| 70 | + |
| 71 | + useEffect(() => { |
| 72 | + const theSteps = credentialsToSave.map((cs, i) => ({ |
| 73 | + id: i, |
| 74 | + status: results[i], |
| 75 | + step: `Saving credentials for ${cs.storageName}`, |
| 76 | + })); |
| 77 | + // TODO: fix react-hooks/set-state-in-effect |
| 78 | + // eslint-disable-next-line react-hooks/set-state-in-effect |
| 79 | + setSteps(theSteps); |
| 80 | + }, [credentialsToSave, results]); |
| 81 | + |
| 82 | + useEffect(() => { |
| 83 | + if ( |
| 84 | + hasFailed || |
| 85 | + credentialsToSave.length < 1 || |
| 86 | + index >= credentialsToSave.length |
| 87 | + ) |
| 88 | + return; |
| 89 | + // TODO: fix react-hooks/set-state-in-effect |
| 90 | + // eslint-disable-next-line react-hooks/set-state-in-effect |
| 91 | + setResults((prev) => { |
| 92 | + const newResults = [...prev]; |
| 93 | + newResults[index] = StatusStepProgressBar.EXECUTING; |
| 94 | + return newResults; |
| 95 | + }); |
| 96 | + const storage = credentialsToSave[index]; |
| 97 | + saveCredentials({ |
| 98 | + dataConnectorId: storage.storageId, |
| 99 | + dataConnectorSecretPatchList: Object.entries(storage.secrets).map( |
| 100 | + ([key, value]) => ({ |
| 101 | + name: key, |
| 102 | + value, |
| 103 | + }), |
| 104 | + ), |
| 105 | + }); |
| 106 | + }, [credentialsToSave, hasFailed, index, saveCredentials]); |
| 107 | + |
| 108 | + useEffect(() => { |
| 109 | + if ( |
| 110 | + saveCredentialsResult.isUninitialized || |
| 111 | + saveCredentialsResult.isLoading |
| 112 | + ) |
| 113 | + return; |
| 114 | + if (saveCredentialsResult.data != null) { |
| 115 | + // TODO: fix react-hooks/set-state-in-effect |
| 116 | + // eslint-disable-next-line react-hooks/set-state-in-effect |
| 117 | + setResults((prev) => { |
| 118 | + const newResults = [...prev]; |
| 119 | + newResults[index] = StatusStepProgressBar.READY; |
| 120 | + return newResults; |
| 121 | + }); |
| 122 | + saveCredentialsResult.reset(); |
| 123 | + setIndex((prev) => prev + 1); |
| 124 | + return; |
| 125 | + } |
| 126 | + if (saveCredentialsResult.error != null) { |
| 127 | + setResults((prev) => { |
| 128 | + const newResults = [...prev]; |
| 129 | + newResults[index] = StatusStepProgressBar.FAILED; |
| 130 | + return newResults; |
| 131 | + }); |
| 132 | + setFailedError(saveCredentialsResult.error); |
| 133 | + setHasFailed(true); |
| 134 | + saveCredentialsResult.reset(); |
| 135 | + } |
| 136 | + }, [index, saveCredentialsResult]); |
| 137 | + |
| 138 | + useEffect(() => { |
| 139 | + if ( |
| 140 | + hasFailed || |
| 141 | + saveCredentialsResult.isLoading || |
| 142 | + hasCompletedRef.current |
| 143 | + ) { |
| 144 | + return; |
| 145 | + } |
| 146 | + if (index >= credentialsToSave.length) { |
| 147 | + hasCompletedRef.current = true; |
| 148 | + const cloudStorageConfigs = dataConnectors.map((cs) => |
| 149 | + storageDefinitionAfterSavingCredentialsFromConfig(cs), |
| 150 | + ); |
| 151 | + onComplete(cloudStorageConfigs); |
| 152 | + } |
| 153 | + }, [ |
| 154 | + credentialsToSave.length, |
| 155 | + dataConnectors, |
| 156 | + hasFailed, |
| 157 | + index, |
| 158 | + onComplete, |
| 159 | + saveCredentialsResult.isLoading, |
| 160 | + ]); |
| 161 | + |
| 162 | + return ( |
| 163 | + <div |
| 164 | + className={cx( |
| 165 | + progressBoxStyles.progressBoxSmall, |
| 166 | + progressBoxStyles.progressBoxSmallSteps, |
| 167 | + )} |
| 168 | + > |
| 169 | + {hasFailed && failedError != null && ( |
| 170 | + <RtkOrDataServicesError |
| 171 | + dismissible={false} |
| 172 | + error={failedError as never} |
| 173 | + /> |
| 174 | + )} |
| 175 | + <ProgressStepsIndicator |
| 176 | + description="Saving credentials..." |
| 177 | + type={ProgressType.Determinate} |
| 178 | + style={ProgressStyle.Light} |
| 179 | + title={title} |
| 180 | + status={steps} |
| 181 | + /> |
| 182 | + </div> |
| 183 | + ); |
| 184 | +} |
0 commit comments