Skip to content

Commit cd0dbd7

Browse files
committed
DRY up more stuff
1 parent ca36ab4 commit cd0dbd7

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

src/core/webview/webviewMessageHandler.ts

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,26 +1084,22 @@ export const webviewMessageHandler = async (provider: ClineProvider, message: We
10841084
}
10851085

10861086
// Load the old configuration to get its ID.
1087-
const oldConfig = await provider.providerSettingsManager.activateProfile({ name: oldName })
1087+
const { id } = await provider.providerSettingsManager.getProfile({ name: oldName })
10881088

1089-
// Create a new configuration with the same ID.
1090-
const newConfig = { ...message.apiConfiguration, id: oldConfig.id }
1089+
// Create a new configuration with the new name and old ID.
1090+
await provider.providerSettingsManager.saveConfig(newName, { ...message.apiConfiguration, id })
10911091

1092-
// Save with the new name but same ID.
1093-
await provider.providerSettingsManager.saveConfig(newName, newConfig)
1092+
// Delete the old configuration.
10941093
await provider.providerSettingsManager.deleteConfig(oldName)
10951094

1096-
const listApiConfig = await provider.providerSettingsManager.listConfig()
1097-
1098-
// Update listApiConfigMeta first to ensure UI has latest data.
1099-
await updateGlobalState("listApiConfigMeta", listApiConfig)
1100-
await updateGlobalState("currentApiConfigName", newName)
1101-
1102-
await provider.postStateToWebview()
1095+
// Re-activate to update the global settings related to the
1096+
// currently activated provider profile.
1097+
await provider.activateProviderProfile({ name: newName })
11031098
} catch (error) {
11041099
provider.log(
11051100
`Error rename api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
11061101
)
1102+
11071103
vscode.window.showErrorMessage(t("common:errors.rename_api_config"))
11081104
}
11091105
}
@@ -1144,32 +1140,25 @@ export const webviewMessageHandler = async (provider: ClineProvider, message: We
11441140
break
11451141
}
11461142

1147-
try {
1148-
await provider.providerSettingsManager.deleteConfig(message.text)
1149-
const listApiConfig = await provider.providerSettingsManager.listConfig()
1150-
1151-
// Update listApiConfigMeta first to ensure UI has latest data.
1152-
await updateGlobalState("listApiConfigMeta", listApiConfig)
1153-
1154-
// If this was the current config, switch to first available.
1155-
const currentApiConfigName = getGlobalState("currentApiConfigName")
1143+
const oldName = message.text
11561144

1157-
if (message.text === currentApiConfigName && listApiConfig?.[0]?.name) {
1158-
const { name: _, ...apiConfig } = await provider.providerSettingsManager.activateProfile({
1159-
name: listApiConfig[0].name,
1160-
})
1145+
const newName = (await provider.providerSettingsManager.listConfig()).filter(
1146+
(c) => c.name !== oldName,
1147+
)[0]?.name
11611148

1162-
await Promise.all([
1163-
updateGlobalState("currentApiConfigName", listApiConfig[0].name),
1164-
provider.updateApiConfiguration(apiConfig),
1165-
])
1166-
}
1149+
if (!newName) {
1150+
vscode.window.showErrorMessage(t("common:errors.delete_api_config"))
1151+
return
1152+
}
11671153

1168-
await provider.postStateToWebview()
1154+
try {
1155+
await provider.providerSettingsManager.deleteConfig(oldName)
1156+
await provider.activateProviderProfile({ name: newName })
11691157
} catch (error) {
11701158
provider.log(
11711159
`Error delete api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
11721160
)
1161+
11731162
vscode.window.showErrorMessage(t("common:errors.delete_api_config"))
11741163
}
11751164
}

0 commit comments

Comments
 (0)