11import { useState , useEffect , useMemo } from 'react'
22import { use_settings } from './hooks/use-settings'
33import { post_message } from './utils/post-message'
4- import { BackendMessage , Template } from '../types/messages'
4+ import { BackendMessage } from '../types/messages'
55import { Home , NavItem } from './Home/Home'
66import { use_web_configuration_editing } from './hooks/use-web-configuration-editing'
77import { use_cli_configuration_editing } from './hooks/use-cli-configuration-editing'
@@ -13,6 +13,7 @@ import { EditWebConfigurationForm } from '@/views/shared/forms/EditWebConfigurat
1313import { EditApiConfigurationForm } from '@/views/shared/forms/EditApiConfigurationForm'
1414import { EditProviderForm } from './forms/EditProviderForm'
1515import { EditTemplateForm } from './forms/EditTemplateForm'
16+ import { use_template_editing } from './hooks/use-template-editing'
1617
1718const 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 }
0 commit comments