Skip to content

Commit c0ae636

Browse files
committed
Refactor web configuration update logic into a shared action and update all relevant message handlers and UI components to use it
- in settings, when clicked 'cancel' on web configuration editing, if some changes were made, ask for confirmation - like you do in panel. - when cancelling modified web configuration, the popup should be a warning and don't have 'discard changes' as the default placed 'cancel' is enough. - extract common web configuration updating logic to an action and make necessary edits to message types - simplify origin in UpdateWebConfigurationMessage
1 parent 47d6b78 commit c0ae636

7 files changed

Lines changed: 156 additions & 162 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import * as vscode from 'vscode'
2+
import { dictionary } from '@shared/constants/dictionary'
3+
import { WebConfiguration } from '@shared/types/web-configuration'
4+
import {
5+
ConfigWebConfigurationFormat,
6+
ui_web_configuration_to_config_format
7+
} from '../utils/web-configuration-format-converters'
8+
import { generate_unique_name } from '../utils/generate-unique-name'
9+
import { are_web_configurations_equal } from '../utils/are-web-configurations-equal'
10+
11+
export const update_web_configuration = async (params: {
12+
updating_web_configuration: WebConfiguration
13+
updated_web_configuration: WebConfiguration
14+
origin?: 'cancel' | 'save'
15+
}): Promise<{ success: boolean; has_changes: boolean; new_name?: string }> => {
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 == params.updating_web_configuration.name
22+
)
23+
24+
if (web_configuration_index == -1) {
25+
console.error(
26+
`web configuration with original name "${params.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+
params.updating_web_configuration.name!
32+
)
33+
)
34+
return { success: false, has_changes: false }
35+
}
36+
37+
const final_updated_web_configuration = { ...params.updated_web_configuration }
38+
39+
const has_changes = !are_web_configurations_equal(
40+
params.updating_web_configuration,
41+
final_updated_web_configuration
42+
)
43+
44+
if (!has_changes) {
45+
return { success: true, has_changes: false }
46+
}
47+
48+
if (params.origin == 'cancel') {
49+
const save_changes_button = 'Save'
50+
const result = await vscode.window.showWarningMessage(
51+
dictionary.information_message.CONFIRM_SAVE_CHANGES_TO_ITEM('web configuration'),
52+
{
53+
modal: true,
54+
detail: dictionary.information_message.UNSAVED_CHANGES_TO_ITEM_WILL_BE_LOST(
55+
'web configuration'
56+
)
57+
},
58+
save_changes_button
59+
)
60+
61+
if (result != save_changes_button) {
62+
return { success: true, has_changes: false }
63+
}
64+
}
65+
66+
const updated_ui_web_configuration = { ...final_updated_web_configuration }
67+
68+
const other_names = current_web_configurations
69+
.filter((_, index) => index != web_configuration_index)
70+
.map((c) => c.name)
71+
72+
updated_ui_web_configuration.name = generate_unique_name(
73+
updated_ui_web_configuration.name,
74+
other_names
75+
)
76+
77+
const updated_web_configurations = [...current_web_configurations]
78+
updated_web_configurations[web_configuration_index] = ui_web_configuration_to_config_format(
79+
updated_ui_web_configuration
80+
)
81+
82+
await config.update(
83+
'webConfigurations',
84+
updated_web_configurations,
85+
vscode.ConfigurationTarget.Global
86+
)
87+
88+
return {
89+
success: true,
90+
has_changes: true,
91+
new_name: updated_ui_web_configuration.name
92+
}
93+
}
Lines changed: 12 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,28 @@
1-
import * as vscode from 'vscode'
2-
import { dictionary } from '@shared/constants/dictionary'
31
import { PanelProvider } from '@/views/panel/backend/panel-provider'
42
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'
114

125
export const handle_update_web_configuration = async (
136
panel_provider: PanelProvider,
147
message: UpdateWebConfigurationMessage
158
): 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+
})
4314

44-
if (!has_changes) {
15+
if (result.has_changes && result.new_name) {
4516
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
4720
})
48-
return
4921
}
5022

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) {
10124
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'
10526
})
10627
}
107-
108-
panel_provider.send_message({
109-
command: 'WEB_CONFIGURATION_UPDATED'
110-
})
11128
}

‎apps/editor/src/views/panel/frontend/hooks/panel/use-web-configuration-editing.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const use_web_configuration_editing = (vscode: any) => {
1212
command: 'UPDATE_WEB_CONFIGURATION',
1313
updating_web_configuration: updating_web_configuration!,
1414
updated_web_configuration: updated_web_configuration!,
15-
origin: 'back_button'
15+
origin: 'cancel'
1616
})
1717
}
1818

@@ -21,7 +21,7 @@ export const use_web_configuration_editing = (vscode: any) => {
2121
command: 'UPDATE_WEB_CONFIGURATION',
2222
updating_web_configuration: updating_web_configuration!,
2323
updated_web_configuration: updated_web_configuration!,
24-
origin: 'save_button'
24+
origin: 'save'
2525
})
2626
}
2727

‎apps/editor/src/views/panel/types/messages.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export interface UpdateWebConfigurationMessage extends BaseMessage {
170170
command: 'UPDATE_WEB_CONFIGURATION'
171171
updating_web_configuration: WebConfiguration
172172
updated_web_configuration: WebConfiguration
173-
origin?: 'back_button' | 'save_button'
173+
origin?: 'cancel' | 'save'
174174
}
175175

176176
export interface DeleteWebConfigurationMessage extends BaseMessage {
Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,20 @@
1-
import * as vscode from 'vscode'
2-
import { dictionary } from '@shared/constants/dictionary'
31
import { SettingsProvider } from '@/views/settings/backend/settings-provider'
42
import { UpdateWebConfigurationMessage } from '@/views/settings/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'
114

125
export const handle_update_web_configuration = async (
136
settings_provider: SettingsProvider,
147
message: UpdateWebConfigurationMessage
158
): 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-
)
43-
44-
if (!has_changes) {
45-
return
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+
})
14+
15+
if (result.success) {
16+
settings_provider.postMessage({
17+
command: 'WEB_CONFIGURATION_UPDATED'
18+
})
4619
}
47-
48-
const updated_ui_web_configuration = { ...final_updated_web_configuration }
49-
50-
const other_names = current_web_configurations
51-
.filter((_, index) => index != web_configuration_index)
52-
.map((c) => c.name)
53-
54-
updated_ui_web_configuration.name = generate_unique_name(
55-
updated_ui_web_configuration.name,
56-
other_names
57-
)
58-
59-
const updated_web_configurations = [...current_web_configurations]
60-
updated_web_configurations[web_configuration_index] = ui_web_configuration_to_config_format(
61-
updated_ui_web_configuration
62-
)
63-
64-
await config.update(
65-
'webConfigurations',
66-
updated_web_configurations,
67-
vscode.ConfigurationTarget.Global
68-
)
6920
}

‎apps/editor/src/views/settings/frontend/Settings.tsx‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export const Settings = () => {
5656
const handle_message = (event: MessageEvent<BackendMessage>) => {
5757
if (event.data.command == 'SHOW_SECTION') {
5858
set_scroll_to_section_on_load(event.data.section as NavItem)
59+
} else if (event.data.command == 'WEB_CONFIGURATION_UPDATED') {
60+
set_updating_web_configuration(undefined)
5961
}
6062
}
6163
window.addEventListener('message', handle_message)
@@ -199,20 +201,44 @@ export const Settings = () => {
199201
scroll_to_section_on_load={scroll_to_section_on_load}
200202
/>
201203
{updating_web_configuration && (
202-
<UiModal on_close={() => set_updating_web_configuration(undefined)}>
204+
<UiModal
205+
on_close={() => {
206+
if (updated_web_configuration) {
207+
post_message(vscode, {
208+
command: 'UPDATE_WEB_CONFIGURATION',
209+
updating_web_configuration,
210+
updated_web_configuration,
211+
origin: 'cancel'
212+
})
213+
} else {
214+
set_updating_web_configuration(undefined)
215+
}
216+
}}
217+
>
203218
<UiModal.Form
204219
title="Edit Configuration"
205220
on_save={() => {
206221
if (updated_web_configuration) {
207222
post_message(vscode, {
208223
command: 'UPDATE_WEB_CONFIGURATION',
209224
updating_web_configuration,
210-
updated_web_configuration
225+
updated_web_configuration,
226+
origin: 'save'
211227
})
212228
}
213-
set_updating_web_configuration(undefined)
214229
}}
215-
on_cancel={() => set_updating_web_configuration(undefined)}
230+
on_cancel={() => {
231+
if (updated_web_configuration) {
232+
post_message(vscode, {
233+
command: 'UPDATE_WEB_CONFIGURATION',
234+
updating_web_configuration,
235+
updated_web_configuration,
236+
origin: 'cancel'
237+
})
238+
} else {
239+
set_updating_web_configuration(undefined)
240+
}
241+
}}
216242
>
217243
<EditWebConfigurationForm
218244
web_configuration={updating_web_configuration}

0 commit comments

Comments
 (0)