Skip to content

Commit 5d57c4a

Browse files
committed
Add confirmation for discarding unsaved changes when cancelling template edits
- in settings, when making changes to configurations and trying to close them, we see 'discard' dialog. Something similar should be implemented when trying to close modified template. - in settings, extract templates to forms ├── apps │ └── editor │ └── src │ ├── i18n │ │ └── translations │ │ ├── common.ts │ │ └── views │ │ └── common │ │ └── handlers │ │ └── common.ts │ └── views │ ├── settings │ │ ├── backend │ │ │ ├── message-handlers │ │ │ │ ├── handle-cancel-template-edit.ts │ │ │ │ ├── handle-update-api-configuration.ts │ │ │ │ ├── handle-update-cli-configuration.ts │ │ │ │ ├── handle-update-provider.ts │ │ │ │ ├── handle-update-templates.ts │ │ │ │ ├── handle-update-web-configuration.ts │ │ │ │ └── index.ts │ │ │ └── settings-view-provider.ts │ │ ├── frontend │ │ │ ├── App.tsx │ │ │ ├── Home │ │ │ │ ├── Home.tsx │ │ │ │ ├── hooks │ │ │ │ │ └── use-scroll-to.ts │ │ │ │ ├── index.ts │ │ │ │ └── sections │ │ │ │ ├── ApiSection.tsx │ │ │ │ ├── CliSection.tsx │ │ │ │ ├── GeneralSection.tsx │ │ │ │ └── WebSection.tsx │ │ │ ├── Settings.tsx │ │ │ ├── forms │ │ │ │ ├── EditProviderForm │ │ │ │ │ ├── EditProviderForm.module.scss │ │ │ │ │ ├── EditProviderForm.tsx │ │ │ │ │ └── index.ts │ │ │ │ ├── EditTemplateForm │ │ │ │ │ ├── EditTemplateForm.module.scss │ │ │ │ │ ├── EditTemplateForm.tsx │ │ │ │ │ └── index.ts │ │ │ │ └── TemplatesForm │ │ │ │ ├── TemplatesForm.tsx │ │ │ │ └── index.ts │ │ │ ├── hooks │ │ │ │ ├── use-api-configuration-editing.ts │ │ │ │ ├── use-cli-configuration-editing.ts │ │ │ │ ├── use-provider-editing.ts │ │ │ │ ├── use-settings.ts │ │ │ │ ├── use-template-editing.ts │ │ │ │ └── use-web-configuration-editing.ts │ │ │ ├── i18n │ │ │ │ ├── translations │ │ │ │ │ ├── actions.ts │ │ │ │ │ ├── api.ts │ │ │ │ │ ├── cli.ts │ │ │ │ │ ├── common.ts │ │ │ │ │ ├── edit-provider-form.ts │ │ │ │ │ ├── edit-template-form.ts │ │ │ │ │ ├── general.ts │ │ │ │ │ ├── index.ts │ │ │ │ │ ├── sections.ts │ │ │ │ │ └── web.ts │ │ │ │ └── use-translation.tsx │ │ │ └── utils │ │ │ └── post-message.ts │ │ └── types │ │ └── messages.ts │ └── shared │ └── actions │ ├── agent │ │ └── update.ts │ ├── api │ │ └── update │ │ └── update.ts │ └── web │ └── update.ts └── packages └── ui └── src └── components └── editor └── settings-view └── Modal ├── Form │ └── Form.tsx └── Modal.tsx
1 parent 88c3489 commit 5d57c4a

9 files changed

Lines changed: 208 additions & 77 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as vscode from 'vscode'
2+
import { SettingsViewProvider } from '../settings-view-provider'
3+
import { CancelTemplateEditMessage } from '@/views/settings/types/messages'
4+
import { t } from '@/i18n'
5+
6+
export const handle_cancel_template_edit = async (
7+
provider: SettingsViewProvider,
8+
_message: CancelTemplateEditMessage
9+
): Promise<void> => {
10+
const discard_button = t('common.action.discard')
11+
const result = await vscode.window.showWarningMessage(
12+
t('views.common.handlers.common.confirm-discard-unsaved-changes', {
13+
item_type: 'template'
14+
}),
15+
{
16+
modal: true,
17+
detail: t('views.common.handlers.common.unsaved-changes-will-be-lost', {
18+
item_type: 'template'
19+
})
20+
},
21+
discard_button
22+
)
23+
24+
if (result != discard_button) {
25+
return
26+
}
27+
28+
provider.postMessage({ command: 'TEMPLATE_EDIT_CANCELLED' })
29+
}

‎apps/editor/src/views/settings/backend/message-handlers/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from './handle-get-templates'
33
export * from './handle-update-templates'
44
export * from './handle-create-template'
55
export * from './handle-delete-template'
6+
export * from './handle-cancel-template-edit'
67
export * from './add-provider/handle-add-provider'
78
export * from './handle-update-provider'
89
export * from './handle-get-providers'

‎apps/editor/src/views/settings/backend/settings-view-provider.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ import {
7171
handle_set_default_cli_configuration,
7272
handle_select_default_cli_configuration,
7373
handle_open_editor_settings,
74-
handle_hash_sign_quick_pick
74+
handle_hash_sign_quick_pick,
75+
handle_cancel_template_edit
7576
} from './message-handlers'
7677
import { webview_html } from '@/views/shared/utils/webview-html'
7778

@@ -155,6 +156,8 @@ export class SettingsViewProvider {
155156
await handle_create_template(this, message)
156157
} else if (message.command == 'DELETE_TEMPLATE') {
157158
await handle_delete_template(this, message)
159+
} else if (message.command == 'CANCEL_TEMPLATE_EDIT') {
160+
await handle_cancel_template_edit(this, message)
158161
} else if (message.command == 'GET_API_CONFIGURATIONS') {
159162
await handle_get_api_configurations(this)
160163
} else if (message.command == 'SET_DEFAULT_API_CONFIGURATION') {

‎apps/editor/src/views/settings/frontend/Home/sections/GeneralSection.tsx‎

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { TextButton as UiTextButton } from '@ui/components/editor/common/TextBut
88
import { Textarea as UiTextarea } from '@ui/components/editor/common/Textarea'
99
import { use_translation } from '../../i18n/use-translation'
1010
import { NavItem } from '../Home'
11-
import { Templates } from '@ui/components/editor/settings-view/Templates'
1211
import { Template } from '@/views/settings/types/messages'
12+
import { TemplatesForm } from '../../forms/TemplatesForm'
1313

1414
type Props = {
1515
send_with_shift_enter: boolean
@@ -139,37 +139,12 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
139139
title={t('general.prompt.templates.title')}
140140
description={t('general.prompt.templates.description')}
141141
>
142-
<Templates
143-
templates={[
144-
{
145-
key: 'templatesForEditFiles',
146-
label: 'Edit',
147-
icon: 'edit-sparkle',
148-
accent_color: 'blue',
149-
items: props.templates.templatesForEditFiles || []
150-
},
151-
{
152-
key: 'templatesForAskAboutFiles',
153-
label: 'Ask',
154-
icon: 'chat-sparkle',
155-
accent_color: 'purple',
156-
items: props.templates.templatesForAskAboutFiles || []
157-
}
158-
]}
159-
on_reorder={(key, templates) =>
160-
props.on_update_templates(key, templates)
161-
}
142+
<TemplatesForm
143+
templates={props.templates}
144+
on_reorder={props.on_update_templates}
162145
on_delete={props.on_delete_template}
163146
on_edit={props.on_edit_template}
164147
on_add={props.on_add_template}
165-
translations={{
166-
item_text: t('general.prompt.templates.item'),
167-
items_text: t('general.prompt.templates.items'),
168-
items_text_many: t('general.prompt.templates.items-many'),
169-
expand: t('common.expand'),
170-
collapse: t('common.collapse'),
171-
add_new: t('action.add-new')
172-
}}
173148
/>
174149
</UiItem>
175150
<UiItem

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

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useState, useEffect, useMemo } from 'react'
22
import { use_settings } from './hooks/use-settings'
33
import { post_message } from './utils/post-message'
4-
import { BackendMessage, Template } from '../types/messages'
4+
import { BackendMessage } from '../types/messages'
55
import { Home, NavItem } from './Home/Home'
66
import { use_web_configuration_editing } from './hooks/use-web-configuration-editing'
77
import { use_cli_configuration_editing } from './hooks/use-cli-configuration-editing'
@@ -13,6 +13,7 @@ import { EditWebConfigurationForm } from '@/views/shared/forms/EditWebConfigurat
1313
import { EditApiConfigurationForm } from '@/views/shared/forms/EditApiConfigurationForm'
1414
import { EditProviderForm } from './forms/EditProviderForm'
1515
import { EditTemplateForm } from './forms/EditTemplateForm'
16+
import { use_template_editing } from './hooks/use-template-editing'
1617

1718
const vscode = acquireVsCodeApi()
1819

@@ -58,43 +59,21 @@ export const Settings = () => {
5859
const [scroll_to_section_on_load, set_scroll_to_section_on_load] =
5960
useState<NavItem>()
6061

61-
const [updating_template, set_updating_template] = useState<{
62-
key: string
63-
index?: number
64-
insertion_index?: number
65-
template: Template
66-
} | null>(null)
67-
const [updated_template, set_updated_template] = useState<Template | null>(
68-
null
62+
const {
63+
updating_template,
64+
set_updated_template,
65+
edit_template_cancel_handler,
66+
edit_template_save_handler,
67+
start_editing_template
68+
} = use_template_editing(
69+
vscode,
70+
settings_hook.templates,
71+
settings_hook.handle_update_templates
6972
)
7073

7174
const [default_option_for_voice_input, set_default_option_for_voice_input] =
7275
useState<'ask' | 'send-request' | 'invoke-agent' | undefined>(undefined)
7376

74-
const edit_template_cancel_handler = () => {
75-
set_updating_template(null)
76-
set_updated_template(null)
77-
}
78-
79-
const edit_template_save_handler = () => {
80-
if (updating_template && updated_template) {
81-
const { key, index, insertion_index } = updating_template
82-
const templates = [...(settings_hook.templates?.[key] || [])]
83-
84-
if (index !== undefined) {
85-
templates[index] = updated_template
86-
} else if (insertion_index !== undefined) {
87-
templates.splice(insertion_index, 0, updated_template)
88-
} else {
89-
templates.push(updated_template)
90-
}
91-
92-
settings_hook.handle_update_templates(key, templates)
93-
}
94-
set_updating_template(null)
95-
set_updated_template(null)
96-
}
97-
9877
const all_data_loaded = useMemo(() => {
9978
return (
10079
settings_hook.providers !== undefined &&
@@ -139,13 +118,6 @@ export const Settings = () => {
139118
const handle_message = (event: MessageEvent<BackendMessage>) => {
140119
if (event.data.command == 'SHOW_SECTION') {
141120
set_scroll_to_section_on_load(event.data.section as NavItem)
142-
} else if (event.data.command == 'START_TEMPLATE_CREATION') {
143-
set_updating_template({
144-
key: event.data.templates_key,
145-
insertion_index: event.data.insertion_index,
146-
template: event.data.template
147-
})
148-
set_updated_template(event.data.template)
149121
} else if (event.data.command === 'DEFAULT_OPTION_FOR_VOICE_INPUT') {
150122
set_default_option_for_voice_input(event.data.value)
151123
}
@@ -214,13 +186,7 @@ export const Settings = () => {
214186
send_with_shift_enter={settings_hook.send_with_shift_enter!}
215187
templates={settings_hook.templates!}
216188
on_update_templates={settings_hook.handle_update_templates}
217-
on_edit_template={(key, index) => {
218-
const template = settings_hook.templates?.[key]?.[index]
219-
if (template) {
220-
set_updating_template({ key, index, template })
221-
set_updated_template(template)
222-
}
223-
}}
189+
on_edit_template={start_editing_template}
224190
on_add_template={settings_hook.handle_add_template}
225191
on_delete_template={settings_hook.handle_delete_template}
226192
set_providers={settings_hook.set_providers}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React from 'react'
2+
import { Templates as UiTemplates } from '@ui/components/editor/settings-view/Templates'
3+
import { Template } from '@/views/settings/types/messages'
4+
import { use_translation } from '../../i18n/use-translation'
5+
6+
type Props = {
7+
templates: Record<string, Template[]>
8+
on_reorder: (key: string, templates: Template[]) => void
9+
on_delete: (key: string, index: number) => void
10+
on_edit: (key: string, index: number) => void
11+
on_add: (
12+
key: string,
13+
params?: { insertion_index?: number; exact_insertion?: boolean }
14+
) => void
15+
}
16+
17+
export const TemplatesForm: React.FC<Props> = (props) => {
18+
const { t } = use_translation()
19+
20+
return (
21+
<UiTemplates
22+
templates={[
23+
{
24+
key: 'templatesForEditFiles',
25+
label: 'Edit',
26+
icon: 'edit-sparkle',
27+
accent_color: 'blue',
28+
items: props.templates.templatesForEditFiles || []
29+
},
30+
{
31+
key: 'templatesForAskAboutFiles',
32+
label: 'Ask',
33+
icon: 'chat-sparkle',
34+
accent_color: 'purple',
35+
items: props.templates.templatesForAskAboutFiles || []
36+
}
37+
]}
38+
on_reorder={props.on_reorder}
39+
on_delete={props.on_delete}
40+
on_edit={props.on_edit}
41+
on_add={props.on_add}
42+
translations={{
43+
item_text: t('general.prompt.templates.item'),
44+
items_text: t('general.prompt.templates.items'),
45+
items_text_many: t('general.prompt.templates.items-many'),
46+
expand: t('common.expand'),
47+
collapse: t('common.collapse'),
48+
add_new: t('action.add-new')
49+
}}
50+
/>
51+
)
52+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './TemplatesForm'
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { useState, useEffect, useCallback } from 'react'
2+
import { Template, BackendMessage } from '../../types/messages'
3+
import { post_message } from '../utils/post-message'
4+
5+
export const use_template_editing = (
6+
vscode: any,
7+
templates: Record<string, Template[]> | undefined,
8+
on_update_templates: (key: string, templates: Template[]) => void
9+
) => {
10+
const [updating_template, set_updating_template] = useState<{
11+
key: string
12+
index?: number
13+
insertion_index?: number
14+
template: Template
15+
} | null>(null)
16+
const [updated_template, set_updated_template] = useState<Template | null>(
17+
null
18+
)
19+
20+
useEffect(() => {
21+
const handle_message = (event: MessageEvent<BackendMessage>) => {
22+
if (event.data.command == 'START_TEMPLATE_CREATION') {
23+
set_updating_template({
24+
key: event.data.templates_key,
25+
insertion_index: event.data.insertion_index,
26+
template: event.data.template
27+
})
28+
set_updated_template(event.data.template)
29+
} else if (event.data.command == 'TEMPLATE_EDIT_CANCELLED') {
30+
set_updating_template(null)
31+
set_updated_template(null)
32+
}
33+
}
34+
window.addEventListener('message', handle_message)
35+
return () => window.removeEventListener('message', handle_message)
36+
}, [])
37+
38+
const edit_template_cancel_handler = useCallback(() => {
39+
if (updating_template && updated_template) {
40+
const a = updating_template.template
41+
const b = updated_template
42+
const a_name = (a.name || '').trim()
43+
const b_name = (b.name || '').trim()
44+
const a_template = a.template.trim()
45+
const b_template = b.template.trim()
46+
const has_changes = a_name != b_name || a_template !== b_template
47+
48+
if (has_changes) {
49+
post_message(vscode, { command: 'CANCEL_TEMPLATE_EDIT' })
50+
return
51+
}
52+
}
53+
set_updating_template(null)
54+
set_updated_template(null)
55+
}, [vscode, updating_template, updated_template])
56+
57+
const edit_template_save_handler = useCallback(() => {
58+
if (updating_template && updated_template) {
59+
const { key, index, insertion_index } = updating_template
60+
const updated_templates = [...(templates?.[key] || [])]
61+
62+
if (index !== undefined) {
63+
updated_templates[index] = updated_template
64+
} else if (insertion_index !== undefined) {
65+
updated_templates.splice(insertion_index, 0, updated_template)
66+
} else {
67+
updated_templates.push(updated_template)
68+
}
69+
70+
on_update_templates(key, updated_templates)
71+
}
72+
set_updating_template(null)
73+
set_updated_template(null)
74+
}, [updating_template, updated_template, templates, on_update_templates])
75+
76+
const start_editing_template = useCallback(
77+
(key: string, index: number) => {
78+
const template = templates?.[key]?.[index]
79+
if (template) {
80+
set_updating_template({ key, index, template })
81+
set_updated_template(template)
82+
}
83+
},
84+
[templates]
85+
)
86+
87+
return {
88+
updating_template,
89+
set_updated_template,
90+
edit_template_cancel_handler,
91+
edit_template_save_handler,
92+
start_editing_template
93+
}
94+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ export interface GetIsModernUiMessage {
369369
command: 'GET_IS_MODERN_UI'
370370
}
371371

372+
export interface CancelTemplateEditMessage {
373+
command: 'CANCEL_TEMPLATE_EDIT'
374+
}
375+
372376
export type FrontendMessage =
373377
| GetProvidersMessage
374378
| ReorderProvidersMessage
@@ -439,6 +443,7 @@ export type FrontendMessage =
439443
| PickExampleFlagsMessage
440444
| SetDefaultAgentConfigurationMessage
441445
| SelectDefaultAgentConfigurationMessage
446+
| CancelTemplateEditMessage
442447

443448
// === FROM BACKEND TO FRONTEND ===
444449
export interface ProvidersMessage {
@@ -644,6 +649,10 @@ export interface InsertSymbolMessage {
644649
text: string
645650
}
646651

652+
export interface TemplateEditCancelledMessage {
653+
command: 'TEMPLATE_EDIT_CANCELLED'
654+
}
655+
647656
export type BackendMessage =
648657
| ProvidersMessage
649658
| ApiConfigurationsMessage
@@ -682,3 +691,4 @@ export type BackendMessage =
682691
| StartAgentConfigurationCreationMessage
683692
| NewlyPickedAgentMessage
684693
| NewlyPickedExampleFlagMessage
694+
| TemplateEditCancelledMessage

0 commit comments

Comments
 (0)