Skip to content

Commit 2be8bd8

Browse files
committed
Refactor name generation and web configuration duplication into shared utilities and improve web configuration update message handling
- place the core logic of web configuration duplication in `apps/editor/src/views/utils` - extract unique name generation to a separate util - in the web configuration edit form, clicking back or save do nothing - when having `apps/editor/src/views/panel/frontend/components/edit-web-configuration-form/EditWebConfigurationForm/EditWebConfigurationForm.tsx` open, clicking save or back does nothing if the configuration hasn't been edited.
1 parent 3861a59 commit 2be8bd8

7 files changed

Lines changed: 108 additions & 94 deletions

File tree

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,12 @@
11
import * as vscode from 'vscode'
22
import { PanelProvider } from '@/views/panel/backend/panel-provider'
3-
import { dictionary } from '@shared/constants/dictionary'
43
import { DuplicateWebConfigurationMessage } from '@/views/panel/types/messages'
5-
import { ConfigWebConfigurationFormat } from '@/views/utils/web-configuration-format-converters'
6-
7-
const generate_unique_name = (
8-
base_name: string | undefined,
9-
all_web_configurations: ConfigWebConfigurationFormat[]
10-
): string => {
11-
const web_configuration_name = base_name ?? ''
12-
13-
const parenthetical_match = web_configuration_name.match(/^(.*?)(?:\s*\((\d+)\))?$/)
14-
const original_base_name = parenthetical_match?.[1]?.trim()
15-
const existing_number_str = parenthetical_match?.[2]
16-
17-
const base_for_duplication =
18-
existing_number_str !== undefined && original_base_name !== undefined
19-
? original_base_name
20-
: web_configuration_name.trim()
21-
22-
let copy_number =
23-
existing_number_str !== undefined
24-
? parseInt(existing_number_str, 10) + 1
25-
: 1
26-
27-
let new_name = base_for_duplication
28-
? `${base_for_duplication} (${copy_number})`
29-
: `(${copy_number})`
30-
31-
while (all_web_configurations.some((p) => p.name == new_name)) {
32-
copy_number++
33-
new_name = base_for_duplication
34-
? `${base_for_duplication} (${copy_number})`
35-
: `(${copy_number})`
36-
}
37-
return new_name
38-
}
4+
import { duplicate_web_configuration } from '@/views/utils/duplicate-web-configuration'
395
export const handle_duplicate_web_configuration = async (
406
panel_provider: PanelProvider,
417
message: DuplicateWebConfigurationMessage,
428
webview_view: vscode.WebviewView
439
): Promise<void> => {
44-
const original_index = message.index
45-
const config = vscode.workspace.getConfiguration('codeWebChat')
46-
const current_web_configurations =
47-
config.get<ConfigWebConfigurationFormat[]>('webConfigurations', []) || []
48-
49-
if (original_index < 0 || original_index >= current_web_configurations.length) {
50-
vscode.window.showErrorMessage(
51-
dictionary.error_message.PRESET_NOT_FOUND(`at index ${original_index}`)
52-
)
53-
return
54-
}
55-
56-
const web_configuration_to_duplicate = current_web_configurations[original_index]
57-
58-
const new_name = generate_unique_name(
59-
web_configuration_to_duplicate.name,
60-
current_web_configurations
61-
)
62-
63-
const duplicated_web_configuration = {
64-
...web_configuration_to_duplicate,
65-
name: new_name
66-
}
67-
68-
const updated_web_configurations = [...current_web_configurations]
69-
updated_web_configurations.splice(original_index + 1, 0, duplicated_web_configuration)
70-
71-
try {
72-
await config.update('webConfigurations', updated_web_configurations, true)
73-
panel_provider.send_web_configurations_to_webview(webview_view.webview)
74-
} catch (error) {
75-
vscode.window.showErrorMessage(
76-
dictionary.error_message.FAILED_TO_DUPLICATE_PRESET(error)
77-
)
78-
}
10+
await duplicate_web_configuration({ index: message.index })
11+
panel_provider.send_web_configurations_to_webview(webview_view.webview)
7912
}

‎apps/editor/src/views/panel/backend/message-handlers/handle-update-web-configuration.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export const handle_update_web_configuration = async (
5858
)
5959

6060
if (!has_changes) {
61+
panel_provider.send_message({
62+
command: 'WEB_CONFIGURATION_UPDATED'
63+
})
6164
return
6265
}
6366

@@ -77,6 +80,9 @@ export const handle_update_web_configuration = async (
7780
)
7881

7982
if (result == discard_changes) {
83+
panel_provider.send_message({
84+
command: 'WEB_CONFIGURATION_UPDATED'
85+
})
8086
return
8187
}
8288

‎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
@@ -9,7 +9,7 @@ export const use_web_configuration_editing = (vscode: any) => {
99

1010
const edit_web_configuration_back_click_handler = () => {
1111
post_message(vscode, {
12-
command: 'UPSERT_WEB_CONFIGURATION',
12+
command: 'UPDATE_WEB_CONFIGURATION',
1313
updating_web_configuration: updating_web_configuration!,
1414
updated_web_configuration: updated_web_configuration!,
1515
origin: 'back_button'
@@ -18,7 +18,7 @@ export const use_web_configuration_editing = (vscode: any) => {
1818

1919
const edit_web_configuration_save_handler = () => {
2020
post_message(vscode, {
21-
command: 'UPSERT_WEB_CONFIGURATION',
21+
command: 'UPDATE_WEB_CONFIGURATION',
2222
updating_web_configuration: updating_web_configuration!,
2323
updated_web_configuration: updated_web_configuration!,
2424
origin: 'save_button'

‎apps/editor/src/views/utils/create-web-configuration.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CHATBOTS } from '@shared/constants/chatbots'
44
import {
55
ConfigWebConfigurationFormat
66
} from './web-configuration-format-converters'
7+
import { generate_unique_name } from './generate-unique-name'
78

89
export const create_web_configuration = async (params: {
910
placement?: 'top' | 'bottom'
@@ -107,11 +108,10 @@ export const create_web_configuration = async (params: {
107108

108109
if (!selected_chatbot) return undefined
109110

110-
let copy_number = 0
111-
let new_name: string
112-
do {
113-
new_name = `(${copy_number++})`
114-
} while (current_web_configurations.some((p) => p.name == new_name))
111+
const new_name = generate_unique_name(
112+
undefined,
113+
current_web_configurations.map(c => c.name)
114+
)
115115

116116
const new_web_configuration: ConfigWebConfigurationFormat = {
117117
name: new_name,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as vscode from 'vscode'
2+
import { dictionary } from '@shared/constants/dictionary'
3+
import { ConfigWebConfigurationFormat } from '@/views/utils/web-configuration-format-converters'
4+
import { generate_unique_name } from './generate-unique-name'
5+
6+
7+
export const duplicate_web_configuration = async (params: {
8+
index: number
9+
}): Promise<void> => {
10+
const original_index = params.index
11+
const config = vscode.workspace.getConfiguration('codeWebChat')
12+
const current_web_configurations =
13+
config.get<ConfigWebConfigurationFormat[]>('webConfigurations', []) || []
14+
15+
if (original_index < 0 || original_index >= current_web_configurations.length) {
16+
vscode.window.showErrorMessage(
17+
dictionary.error_message.PRESET_NOT_FOUND(`at index ${original_index}`)
18+
)
19+
return
20+
}
21+
22+
const web_configuration_to_duplicate = current_web_configurations[original_index]
23+
24+
const new_name = generate_unique_name(
25+
web_configuration_to_duplicate.name,
26+
current_web_configurations.map(c => c.name)
27+
)
28+
29+
const duplicated_web_configuration = {
30+
...web_configuration_to_duplicate,
31+
name: new_name
32+
}
33+
34+
const updated_web_configurations = [...current_web_configurations]
35+
updated_web_configurations.splice(original_index + 1, 0, duplicated_web_configuration)
36+
37+
try {
38+
await config.update('webConfigurations', updated_web_configurations, true)
39+
} catch (error) {
40+
vscode.window.showErrorMessage(
41+
dictionary.error_message.FAILED_TO_DUPLICATE_PRESET(error)
42+
)
43+
}
44+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export const generate_unique_name = (
2+
base_name: string | undefined,
3+
existing_names: (string | undefined)[]
4+
): string => {
5+
const name_to_check = base_name?.trim()
6+
7+
if (name_to_check && !existing_names.includes(name_to_check)) {
8+
return name_to_check
9+
}
10+
11+
const match = (name_to_check || '').match(/^(.*?)(?:\s*\((\d+)\))?$/)
12+
const original_base_name = match?.[1]?.trim()
13+
const existing_number_str = match?.[2]
14+
15+
const base_for_duplication =
16+
existing_number_str !== undefined && original_base_name !== undefined
17+
? original_base_name
18+
: (name_to_check || '')
19+
20+
let copy_number =
21+
existing_number_str !== undefined
22+
? parseInt(existing_number_str, 10) + 1
23+
: 1
24+
25+
let new_name = base_for_duplication
26+
? `${base_for_duplication} (${copy_number})`
27+
: `(${copy_number})`
28+
29+
while (existing_names.includes(new_name)) {
30+
copy_number++
31+
new_name = base_for_duplication
32+
? `${base_for_duplication} (${copy_number})`
33+
: `(${copy_number})`
34+
}
35+
36+
return new_name
37+
}

‎apps/editor/src/views/utils/upsert-model-provider.ts‎

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from '@/services/model-providers-manager'
77
import { dictionary } from '@shared/constants/dictionary'
88
import { PROVIDERS } from '@/constants/providers'
9+
import { generate_unique_name } from './generate-unique-name'
910

1011
const normalize_base_url = (url: string): string => {
1112
return url.trim().replace(/\/+$/, '')
@@ -465,12 +466,10 @@ export const upsert_model_provider = async (params: {
465466

466467
const model_providers = await providers_manager.get_model_providers()
467468

468-
let final_name = new_name
469-
let counter = 1
470-
while (model_providers.some((p) => p.name == final_name)) {
471-
final_name = `${new_name} (${counter})`
472-
counter++
473-
}
469+
const final_name = generate_unique_name(
470+
new_name,
471+
model_providers.map((p) => p.name)
472+
)
474473

475474
const new_model_provider: ModelProvider = {
476475
name: final_name,
@@ -566,17 +565,12 @@ export const upsert_model_provider = async (params: {
566565
const model_providers = await providers_manager.get_model_providers()
567566
let updated_model_providers = [...model_providers]
568567

569-
let final_name = working_model_provider.name
570-
let counter = 1
571-
while (
572-
updated_model_providers.some(
573-
(p) => p.name == final_name && p.name != original_name
574-
)
575-
) {
576-
final_name = `${working_model_provider.name} (${counter})`
577-
counter++
578-
}
579-
working_model_provider.name = final_name
568+
working_model_provider.name = generate_unique_name(
569+
working_model_provider.name,
570+
updated_model_providers
571+
.filter((p) => p.name !== original_name)
572+
.map((p) => p.name)
573+
)
580574

581575
if (original_name) {
582576
updated_model_providers = updated_model_providers.map((p) =>

0 commit comments

Comments
 (0)