1- import { useState , useEffect } from 'react'
1+ import { useState , useEffect , useRef } from 'react'
22import { BackendMessage , InstructionsState } from '../../../../types/messages'
33import { post_message } from '../../../utils/post-message'
44import { Target } from '@shared/types/target'
@@ -21,6 +21,9 @@ export const use_instructions = (
2121 InstructionsState | undefined
2222 > ( )
2323
24+ const closed_ask_tabs = useRef < string [ ] > ( [ ] )
25+ const closed_edit_tabs = useRef < string [ ] > ( [ ] )
26+
2427 const handle_instructions_change = (
2528 value : string ,
2629 prompt_type : 'ask' | 'edit'
@@ -133,10 +136,14 @@ export const use_instructions = (
133136 state : InstructionsState | undefined ,
134137 setter : React . Dispatch <
135138 React . SetStateAction < InstructionsState | undefined >
136- >
139+ > ,
140+ closed_tabs_ref : React . MutableRefObject < string [ ] >
137141 ) => {
138142 if ( ! state || state . instructions . length <= 1 ) return
139143
144+ const deleted_instruction = state . instructions [ index ]
145+ closed_tabs_ref . current . push ( deleted_instruction )
146+
140147 const new_instructions = state . instructions . filter ( ( _ , i ) => i !== index )
141148 let new_active_index = state . active_index
142149
@@ -157,9 +164,64 @@ export const use_instructions = (
157164 }
158165
159166 if ( prompt_type === 'ask' ) {
160- update ( ask_about_context_instructions , set_ask_about_context_instructions )
167+ update (
168+ ask_about_context_instructions ,
169+ set_ask_about_context_instructions ,
170+ closed_ask_tabs
171+ )
161172 } else if ( prompt_type === 'edit' ) {
162- update ( edit_files_instructions , set_edit_files_instructions )
173+ update (
174+ edit_files_instructions ,
175+ set_edit_files_instructions ,
176+ closed_edit_tabs
177+ )
178+ }
179+ }
180+
181+ const handle_tab_restore = ( ) => {
182+ const prompt_type = (
183+ target === 'WEB'
184+ ? web_prompt_type
185+ : target === 'API'
186+ ? api_prompt_type
187+ : cli_prompt_type
188+ ) as any
189+
190+ const update = (
191+ state : InstructionsState | undefined ,
192+ setter : React . Dispatch <
193+ React . SetStateAction < InstructionsState | undefined >
194+ > ,
195+ closed_tabs_ref : React . MutableRefObject < string [ ] >
196+ ) => {
197+ if ( closed_tabs_ref . current . length === 0 ) return
198+
199+ const restored_instruction = closed_tabs_ref . current . pop ( ) !
200+ const current_instructions = state ?. instructions ?? [ '' ]
201+ const new_state = {
202+ instructions : [ ...current_instructions , restored_instruction ] ,
203+ active_index : current_instructions . length
204+ }
205+ setter ( new_state )
206+ post_message ( vscode , {
207+ command : 'SAVE_INSTRUCTIONS' ,
208+ instruction : new_state ,
209+ prompt_type
210+ } )
211+ }
212+
213+ if ( prompt_type === 'ask' ) {
214+ update (
215+ ask_about_context_instructions ,
216+ set_ask_about_context_instructions ,
217+ closed_ask_tabs
218+ )
219+ } else if ( prompt_type === 'edit' ) {
220+ update (
221+ edit_files_instructions ,
222+ set_edit_files_instructions ,
223+ closed_edit_tabs
224+ )
163225 }
164226 }
165227
@@ -222,6 +284,7 @@ export const use_instructions = (
222284 handle_tab_change,
223285 handle_new_tab,
224286 handle_tab_delete,
225- handle_tabs_reorder
287+ handle_tabs_reorder,
288+ handle_tab_restore
226289 }
227290}
0 commit comments