@@ -13,7 +13,9 @@ import {
1313 delete_checkpoint ,
1414 get_checkpoints ,
1515 restore_checkpoint ,
16- toggle_checkpoint_star
16+ toggle_checkpoint_star ,
17+ ActiveDeleteOperation ,
18+ delete_checkpoint_with_undo
1719} from './actions'
1820import { PanelProvider } from '@/views/panel/backend/panel-provider'
1921import { get_checkpoint_path } from './utils'
@@ -30,10 +32,7 @@ export const checkpoints_command = (params: {
3032 websites_provider : WebsitesProvider
3133 panel_provider : PanelProvider
3234} ) : vscode . Disposable [ ] => {
33- let active_delete_operation : {
34- finalize : ( ) => Promise < void >
35- timestamp : number
36- } | null = null
35+ let active_delete_operation : ActiveDeleteOperation | null = null
3736
3837 const create_new_checkpoint_command = vscode . commands . registerCommand (
3938 'codeWebChat.createNewCheckpoint' ,
@@ -367,120 +366,38 @@ export const checkpoints_command = (params: {
367366 }
368367
369368 if ( e . button . tooltip == 'Delete' ) {
370- if ( active_delete_operation ) {
371- await active_delete_operation . finalize ( )
372- }
373-
374- const deleted_checkpoint = item . checkpoint
375- const real_index_in_state = checkpoints . findIndex (
376- ( c ) => c . timestamp == deleted_checkpoint . timestamp
377- )
378- if ( real_index_in_state == - 1 ) return
379-
380- const original_checkpoint_from_state =
381- checkpoints [ real_index_in_state ]
382-
383- // Optimistically remove from state and update UI
384- const updated_checkpoints = checkpoints . filter (
385- ( c ) => c . timestamp !== deleted_checkpoint . timestamp
386- )
387- await params . context . workspaceState . update (
388- CHECKPOINTS_STATE_KEY ,
389- updated_checkpoints
390- )
391- params . panel_provider . send_checkpoints ( )
392- checkpoints = updated_checkpoints
393- update_view ( )
394-
395- const currentOperation = {
396- timestamp : deleted_checkpoint . timestamp ,
397- finalize : async ( ) => {
398- try {
399- const checkpoint_path = get_checkpoint_path (
400- deleted_checkpoint . timestamp
401- )
402- await vscode . workspace . fs . delete (
403- vscode . Uri . file ( checkpoint_path ) ,
404- { recursive : true }
405- )
406- } catch ( error : any ) {
407- vscode . window . showWarningMessage (
408- dictionary . warning_message . COULD_NOT_DELETE_CHECKPOINT_FILES (
409- error . message
410- )
411- )
412- }
413- }
414- }
415- active_delete_operation = currentOperation
416-
417- notification_count ++
418- const choice = await vscode . window . showInformationMessage (
419- dictionary . information_message . CHECKPOINT_DELETED (
420- dayjs ( deleted_checkpoint . timestamp ) . fromNow ( )
421- ) ,
422- 'Undo'
423- )
424- notification_count --
425-
426- if (
427- active_delete_operation &&
428- active_delete_operation . timestamp === currentOperation . timestamp
429- ) {
430- if ( choice == 'Undo' ) {
431- // Restore to state and UI
432- const current_checkpoints =
433- params . context . workspaceState . get < Checkpoint [ ] > (
434- CHECKPOINTS_STATE_KEY ,
435- [ ]
436- ) ?? [ ]
437-
438- current_checkpoints . push ( original_checkpoint_from_state )
439- current_checkpoints . sort ( ( a , b ) => b . timestamp - a . timestamp )
440-
441- checkpoints = current_checkpoints
442-
443- await params . context . workspaceState . update (
444- CHECKPOINTS_STATE_KEY ,
445- checkpoints
446- )
447- params . panel_provider . send_checkpoints ( )
448- notification_count ++
449- vscode . window
450- . showInformationMessage (
451- dictionary . information_message . CHECKPOINT_RESTORED
452- )
453- . then ( ( ) => {
454- notification_count --
455- } )
369+ const was_restored = await delete_checkpoint_with_undo ( {
370+ context : params . context ,
371+ checkpoint : item . checkpoint ,
372+ panel_provider : params . panel_provider ,
373+ get_active_operation : ( ) => active_delete_operation ,
374+ set_active_operation : ( op ) => ( active_delete_operation = op ) ,
375+ on_did_update_checkpoints : ( updated ) => {
376+ checkpoints = updated
456377 update_view ( )
457- const restored_item = quick_pick . items . find (
458- ( i ) =>
459- ( i as any ) . checkpoint ?. timestamp ==
460- original_checkpoint_from_state . timestamp
461- )
462- if ( restored_item ) {
463- quick_pick . activeItems = [ restored_item ]
378+ // If restoring, we might want to highlight the restored item
379+ if ( updated . length > checkpoints . length ) {
380+ // Heuristic: if count increased, likely restored
381+ const restored_item = quick_pick . items . find (
382+ ( i ) =>
383+ ( i as any ) . checkpoint ?. timestamp ==
384+ item . checkpoint ?. timestamp
385+ )
386+ if ( restored_item ) {
387+ quick_pick . activeItems = [ restored_item ]
388+ }
464389 }
465390 quick_pick . show ( )
466- } else {
467- await currentOperation . finalize ( )
468- quick_pick . dispose ( )
469- }
470- active_delete_operation = null
471- } else if ( choice === 'Undo' ) {
472- notification_count ++
473- vscode . window
474- . showInformationMessage (
475- dictionary . information_message
476- . COULD_NOT_UNDO_ANOTHER_CHECKPOINT_DELETED
477- )
478- . then ( ( ) => {
479- notification_count --
480- } )
391+ } ,
392+ on_before_show_message : ( ) => notification_count ++ ,
393+ on_after_show_message : ( ) => notification_count --
394+ } )
395+
396+ if ( ! was_restored ) {
397+ quick_pick . dispose ( )
398+ } else {
481399 quick_pick . show ( )
482400 }
483-
484401 return
485402 }
486403 } )
0 commit comments