|
1 | | -import * as vscode from 'vscode' |
2 | | -import { dictionary } from '@shared/constants/dictionary' |
3 | 1 | import { PanelProvider } from '@/views/panel/backend/panel-provider' |
4 | 2 | import { UpdateWebConfigurationMessage } from '@/views/panel/types/messages' |
5 | | -import { |
6 | | - ConfigWebConfigurationFormat, |
7 | | - ui_web_configuration_to_config_format |
8 | | -} from '@/views/utils/web-configuration-format-converters' |
9 | | -import { generate_unique_name } from '@/views/utils/generate-unique-name' |
10 | | -import { are_web_configurations_equal } from '@/views/utils/are-web-configurations-equal' |
| 3 | +import { update_web_configuration } from '@/views/actions/update-web-configuration' |
11 | 4 |
|
12 | 5 | export const handle_update_web_configuration = async ( |
13 | 6 | panel_provider: PanelProvider, |
14 | 7 | message: UpdateWebConfigurationMessage |
15 | 8 | ): Promise<void> => { |
16 | | - const config = vscode.workspace.getConfiguration('codeWebChat') |
17 | | - const current_web_configurations = |
18 | | - config.get<ConfigWebConfigurationFormat[]>('webConfigurations', []) || [] |
19 | | - |
20 | | - const web_configuration_index = current_web_configurations.findIndex( |
21 | | - (p) => p.name == message.updating_web_configuration.name |
22 | | - ) |
23 | | - |
24 | | - if (web_configuration_index == -1) { |
25 | | - console.error( |
26 | | - `web configuration with original name "${message.updating_web_configuration.name}" not found.` |
27 | | - ) |
28 | | - vscode.window.showErrorMessage( |
29 | | - dictionary.error_message.COULD_NOT_UPDATE_ITEM_NOT_FOUND( |
30 | | - 'web configuration', |
31 | | - message.updating_web_configuration.name! |
32 | | - ) |
33 | | - ) |
34 | | - return |
35 | | - } |
36 | | - |
37 | | - const final_updated_web_configuration = { ...message.updated_web_configuration } |
38 | | - |
39 | | - const has_changes = !are_web_configurations_equal( |
40 | | - message.updating_web_configuration, |
41 | | - final_updated_web_configuration |
42 | | - ) |
| 9 | + const result = await update_web_configuration({ |
| 10 | + updating_web_configuration: message.updating_web_configuration, |
| 11 | + updated_web_configuration: message.updated_web_configuration, |
| 12 | + origin: message.origin |
| 13 | + }) |
43 | 14 |
|
44 | | - if (!has_changes) { |
| 15 | + if (result.has_changes && result.new_name) { |
45 | 16 | panel_provider.send_message({ |
46 | | - command: 'WEB_CONFIGURATION_UPDATED' |
| 17 | + command: 'SELECTED_WEB_CONFIGURATION_CHANGED', |
| 18 | + prompt_type: panel_provider.web_prompt_type, |
| 19 | + name: result.new_name |
47 | 20 | }) |
48 | | - return |
49 | 21 | } |
50 | 22 |
|
51 | | - if (message.origin == 'back_button') { |
52 | | - const save_changes_button = 'Save' |
53 | | - const discard_changes = 'Discard changes' |
54 | | - const result = await vscode.window.showInformationMessage( |
55 | | - dictionary.information_message.CONFIRM_SAVE_CHANGES_TO_ITEM('web configuration'), |
56 | | - { |
57 | | - modal: true, |
58 | | - detail: dictionary.information_message.UNSAVED_CHANGES_TO_ITEM_WILL_BE_LOST( |
59 | | - 'web configuration' |
60 | | - ) |
61 | | - }, |
62 | | - save_changes_button, |
63 | | - discard_changes |
64 | | - ) |
65 | | - |
66 | | - if (result == discard_changes) { |
67 | | - panel_provider.send_message({ |
68 | | - command: 'WEB_CONFIGURATION_UPDATED' |
69 | | - }) |
70 | | - return |
71 | | - } |
72 | | - |
73 | | - if (result != save_changes_button) { |
74 | | - return |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - const updated_ui_web_configuration = { ...final_updated_web_configuration } |
79 | | - |
80 | | - const other_names = current_web_configurations |
81 | | - .filter((_, index) => index != web_configuration_index) |
82 | | - .map((c) => c.name) |
83 | | - |
84 | | - updated_ui_web_configuration.name = generate_unique_name( |
85 | | - updated_ui_web_configuration.name, |
86 | | - other_names |
87 | | - ) |
88 | | - |
89 | | - const updated_web_configurations = [...current_web_configurations] |
90 | | - updated_web_configurations[web_configuration_index] = ui_web_configuration_to_config_format( |
91 | | - updated_ui_web_configuration |
92 | | - ) |
93 | | - |
94 | | - await config.update( |
95 | | - 'webConfigurations', |
96 | | - updated_web_configurations, |
97 | | - vscode.ConfigurationTarget.Global |
98 | | - ) |
99 | | - |
100 | | - if (updated_ui_web_configuration && updated_ui_web_configuration.name) { |
| 23 | + if (result.success) { |
101 | 24 | panel_provider.send_message({ |
102 | | - command: 'SELECTED_WEB_CONFIGURATION_CHANGED', |
103 | | - prompt_type: panel_provider.web_prompt_type, |
104 | | - name: updated_ui_web_configuration.name |
| 25 | + command: 'WEB_CONFIGURATION_UPDATED' |
105 | 26 | }) |
106 | 27 | } |
107 | | - |
108 | | - panel_provider.send_message({ |
109 | | - command: 'WEB_CONFIGURATION_UPDATED' |
110 | | - }) |
111 | 28 | } |
0 commit comments