Skip to content

Commit 4d76948

Browse files
committed
Merge individual preset, group, and separator delete/duplicate actions into unified handlers and update related message types, imports, and UI components accordingly
1 parent 8ca3028 commit 4d76948

11 files changed

Lines changed: 239 additions & 280 deletions

File tree

‎packages/ui/src/components/editor/panel/Presets/Presets.tsx‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ export namespace Presets {
5959
on_preset_copy: (name: string) => void
6060
on_presets_reorder: (reordered_presets: Preset[]) => void
6161
on_preset_edit: (name: string) => void
62-
on_preset_duplicate: (index: number) => void
63-
on_preset_delete: (index: number) => void
64-
on_group_delete: (index: number) => void
65-
on_separator_delete: (index: number) => void
62+
on_duplicate_preset_group_or_separator: (index: number) => void
63+
on_delete_preset_group_or_separator: (index: number) => void
6664
on_toggle_selected_preset: (name: string) => void
6765
on_toggle_preset_pinned: (name: string) => void
6866
on_toggle_group_collapsed: (name: string) => void
@@ -217,7 +215,7 @@ export const Presets: React.FC<Presets.Props> = (props) => {
217215
title="Duplicate"
218216
on_click={(e) => {
219217
e.stopPropagation()
220-
props.on_preset_duplicate(index)
218+
props.on_duplicate_preset_group_or_separator(index)
221219
}}
222220
/>
223221
<IconButton
@@ -233,7 +231,7 @@ export const Presets: React.FC<Presets.Props> = (props) => {
233231
title="Delete"
234232
on_click={(e) => {
235233
e.stopPropagation()
236-
props.on_preset_delete(index)
234+
props.on_delete_preset_group_or_separator(index)
237235
}}
238236
/>
239237
</div>
@@ -331,15 +329,19 @@ export const Presets: React.FC<Presets.Props> = (props) => {
331329
title="Duplicate"
332330
on_click={(e) => {
333331
e.stopPropagation()
334-
props.on_preset_duplicate(preset.original_index!)
332+
props.on_duplicate_preset_group_or_separator(
333+
preset.original_index!
334+
)
335335
}}
336336
/>
337337
<IconButton
338338
codicon_icon="trash"
339339
title="Delete"
340340
on_click={(e) => {
341341
e.stopPropagation()
342-
props.on_separator_delete(preset.original_index!)
342+
props.on_delete_preset_group_or_separator(
343+
preset.original_index!
344+
)
343345
}}
344346
/>
345347
</div>
@@ -536,7 +538,9 @@ export const Presets: React.FC<Presets.Props> = (props) => {
536538
title="Duplicate"
537539
on_click={(e) => {
538540
e.stopPropagation()
539-
props.on_preset_duplicate(preset.original_index!)
541+
props.on_duplicate_preset_group_or_separator(
542+
preset.original_index!
543+
)
540544
}}
541545
/>
542546
<IconButton
@@ -552,11 +556,9 @@ export const Presets: React.FC<Presets.Props> = (props) => {
552556
title="Delete"
553557
on_click={(e) => {
554558
e.stopPropagation()
555-
if (preset.chatbot) {
556-
props.on_preset_delete(preset.original_index!)
557-
} else {
558-
props.on_group_delete(preset.original_index!)
559-
}
559+
props.on_delete_preset_group_or_separator(
560+
preset.original_index!
561+
)
560562
}}
561563
/>
562564
</div>

‎packages/vscode/src/views/panel/backend/message-handlers/handle-delete-group.ts‎

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import * as vscode from 'vscode'
2+
import { PanelProvider } from '@/views/panel/backend/panel-provider'
3+
import { dictionary } from '@shared/constants/dictionary'
4+
import { DeletePresetGroupOrSeparatorMessage } from '@/views/panel/types/messages'
5+
import { ConfigPresetFormat } from '@/views/panel/backend/utils/preset-format-converters'
6+
7+
export const handle_delete_preset_group_or_separator = async (
8+
panel_provider: PanelProvider,
9+
message: DeletePresetGroupOrSeparatorMessage,
10+
webview_view: vscode.WebviewView
11+
): Promise<void> => {
12+
const { index } = message
13+
const config = vscode.workspace.getConfiguration('codeWebChat')
14+
const presets_config_key = panel_provider.get_presets_config_key()
15+
const current_presets =
16+
config.get<ConfigPresetFormat[]>(presets_config_key, []) || []
17+
18+
if (index < 0 || index >= current_presets.length) return
19+
20+
const item_to_delete = current_presets[index]
21+
22+
let item_type: 'preset' | 'group' | 'separator'
23+
let should_show_confirmation: boolean
24+
25+
if (item_to_delete.chatbot) {
26+
item_type = 'preset'
27+
should_show_confirmation = true
28+
} else if (item_to_delete.name) {
29+
item_type = 'group'
30+
const item_name = item_to_delete.name
31+
const is_unnamed = !item_name || /^\(\d+\)$/.test(item_name.trim())
32+
const has_affixes =
33+
!!item_to_delete.promptPrefix || !!item_to_delete.promptSuffix
34+
should_show_confirmation = !is_unnamed || has_affixes
35+
} else {
36+
// It's a separator
37+
item_type = 'separator'
38+
should_show_confirmation = false
39+
}
40+
41+
if (should_show_confirmation) {
42+
const item_name = item_to_delete.name
43+
const is_unnamed = !item_name || /^\(\d+\)$/.test(item_name?.trim() ?? '')
44+
const display_item_name = is_unnamed ? 'Unnamed' : item_name!
45+
46+
const delete_button = 'Delete'
47+
const result = await vscode.window.showInformationMessage(
48+
dictionary.information_message.PLEASE_CONFIRM,
49+
{
50+
modal: true,
51+
detail: is_unnamed
52+
? dictionary.warning_message.CONFIRM_DELETE_ITEM(
53+
item_type as 'preset' | 'group'
54+
)
55+
: dictionary.warning_message.CONFIRM_DELETE_NAMED_ITEM(
56+
item_type,
57+
display_item_name
58+
)
59+
},
60+
delete_button
61+
)
62+
63+
if (result != delete_button) {
64+
return
65+
}
66+
}
67+
68+
const updated_presets = [...current_presets]
69+
let delete_count = 1
70+
const is_group = !item_to_delete.chatbot && item_to_delete.name
71+
if (is_group) {
72+
for (let i = index + 1; i < current_presets.length; i++) {
73+
const next_item = current_presets[i]
74+
if (!next_item.chatbot && next_item.name) {
75+
break
76+
}
77+
delete_count++
78+
if (!next_item.chatbot && !next_item.name) {
79+
break
80+
}
81+
}
82+
}
83+
updated_presets.splice(index, delete_count)
84+
85+
try {
86+
await config.update(
87+
presets_config_key,
88+
updated_presets,
89+
vscode.ConfigurationTarget.Global
90+
)
91+
92+
panel_provider.send_presets_to_webview(webview_view.webview)
93+
} catch (error) {
94+
vscode.window.showErrorMessage(
95+
dictionary.error_message.FAILED_TO_DELETE_ITEM(item_type, error)
96+
)
97+
}
98+
}

‎packages/vscode/src/views/panel/backend/message-handlers/handle-delete-preset.ts‎

Lines changed: 0 additions & 61 deletions
This file was deleted.

‎packages/vscode/src/views/panel/backend/message-handlers/handle-delete-separator.ts‎

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)