Skip to content

Commit b0b0d12

Browse files
committed
Implement undo functionality for checkpoint deletions with simplified notification text
1 parent 38c8a69 commit b0b0d12

9 files changed

Lines changed: 204 additions & 117 deletions

File tree

‎packages/shared/src/constants/dictionary.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ export const dictionary = {
2222
'Clipboard content applied successfully.',
2323
COPIED_TO_CLIPBOARD: 'Message copied to the clipboard.',
2424
CHECKPOINTS_COPIED_TO_CLIPBOARD: 'Checkpoints copied to clipboard as logs.',
25-
CHECKPOINT_DELETED: (from_now: string) =>
26-
`Checkpoint from ${from_now} deleted.`,
25+
CHECKPOINT_DELETED: 'Checkpoint deleted.',
2726
CHECKPOINT_RESTORED: 'Checkpoint restored.',
2827
NO_SAVED_CONTEXTS_IN_WORKSPACE_STATE:
2928
'No saved contexts remaining in the Workspace State.',

‎packages/vscode/src/commands/checkpoints-command/actions/delete-checkpoint.ts‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Checkpoint } from '../types'
44
import { get_checkpoint_path } from '../utils'
55
import { Logger } from '@shared/utils/logger'
66
import { PanelProvider } from '@/views/panel/backend/panel-provider'
7+
import { dictionary } from '@shared/constants/dictionary'
78

89
export const delete_checkpoint = async (params: {
910
context: vscode.ExtensionContext
@@ -39,3 +40,119 @@ export const delete_checkpoint = async (params: {
3940
})
4041
}
4142
}
43+
44+
export type ActiveDeleteOperation = {
45+
finalize: () => Promise<void>
46+
timestamp: number
47+
}
48+
49+
export const delete_checkpoint_with_undo = async (params: {
50+
context: vscode.ExtensionContext
51+
checkpoint: Checkpoint
52+
panel_provider: PanelProvider
53+
get_active_operation: () => ActiveDeleteOperation | null
54+
set_active_operation: (op: ActiveDeleteOperation | null) => void
55+
on_did_update_checkpoints?: (checkpoints: Checkpoint[]) => void
56+
on_before_show_message?: () => void
57+
on_after_show_message?: () => void
58+
}): Promise<boolean> => {
59+
const {
60+
context,
61+
checkpoint,
62+
panel_provider,
63+
get_active_operation,
64+
set_active_operation,
65+
on_did_update_checkpoints,
66+
on_before_show_message,
67+
on_after_show_message
68+
} = params
69+
70+
if (get_active_operation()) {
71+
await get_active_operation()!.finalize()
72+
set_active_operation(null)
73+
}
74+
75+
const checkpoints =
76+
context.workspaceState.get<Checkpoint[]>(CHECKPOINTS_STATE_KEY, []) ?? []
77+
78+
// Optimistically remove from state
79+
const updated_checkpoints = checkpoints.filter(
80+
(c) => c.timestamp !== checkpoint.timestamp
81+
)
82+
await context.workspaceState.update(
83+
CHECKPOINTS_STATE_KEY,
84+
updated_checkpoints
85+
)
86+
await panel_provider.send_checkpoints()
87+
on_did_update_checkpoints?.(updated_checkpoints)
88+
89+
const finalize = async () => {
90+
try {
91+
const checkpoint_path = get_checkpoint_path(checkpoint.timestamp)
92+
await vscode.workspace.fs.delete(vscode.Uri.file(checkpoint_path), {
93+
recursive: true
94+
})
95+
} catch (error: any) {
96+
vscode.window.showWarningMessage(
97+
dictionary.warning_message.COULD_NOT_DELETE_CHECKPOINT_FILES(
98+
error.message
99+
)
100+
)
101+
}
102+
}
103+
104+
const operation = { timestamp: checkpoint.timestamp, finalize }
105+
set_active_operation(operation)
106+
107+
on_before_show_message?.()
108+
const choice = await vscode.window.showInformationMessage(
109+
dictionary.information_message.CHECKPOINT_DELETED,
110+
'Undo'
111+
)
112+
on_after_show_message?.()
113+
114+
const current_active_op = get_active_operation()
115+
116+
if (
117+
current_active_op &&
118+
current_active_op.timestamp === operation.timestamp
119+
) {
120+
if (choice === 'Undo') {
121+
const current_checkpoints =
122+
context.workspaceState.get<Checkpoint[]>(CHECKPOINTS_STATE_KEY, []) ??
123+
[]
124+
125+
current_checkpoints.push(checkpoint)
126+
current_checkpoints.sort((a, b) => b.timestamp - a.timestamp)
127+
128+
await context.workspaceState.update(
129+
CHECKPOINTS_STATE_KEY,
130+
current_checkpoints
131+
)
132+
await panel_provider.send_checkpoints()
133+
on_did_update_checkpoints?.(current_checkpoints)
134+
135+
on_before_show_message?.()
136+
await vscode.window.showInformationMessage(
137+
dictionary.information_message.CHECKPOINT_RESTORED
138+
)
139+
on_after_show_message?.()
140+
141+
set_active_operation(null)
142+
return true
143+
} else {
144+
await operation.finalize()
145+
set_active_operation(null)
146+
return false
147+
}
148+
} else if (choice === 'Undo') {
149+
on_before_show_message?.()
150+
await vscode.window.showInformationMessage(
151+
dictionary.information_message.COULD_NOT_UNDO_ANOTHER_CHECKPOINT_DELETED
152+
)
153+
on_after_show_message?.()
154+
return false
155+
}
156+
157+
return false
158+
}

‎packages/vscode/src/commands/checkpoints-command/checkpoints-command.ts‎

Lines changed: 31 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -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'
1820
import { PanelProvider } from '@/views/panel/backend/panel-provider'
1921
import { 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
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { PanelProvider } from '../panel-provider'
2+
import { DeleteCheckpointMessage } from '@/views/panel/types/messages'
3+
import {
4+
delete_checkpoint_with_undo,
5+
get_checkpoints
6+
} from '@/commands/checkpoints-command/actions'
7+
8+
export const handle_delete_checkpoint = async (
9+
panel_provider: PanelProvider,
10+
message: DeleteCheckpointMessage
11+
) => {
12+
const context = panel_provider.context
13+
14+
const checkpoints = await get_checkpoints(context)
15+
const checkpoint_to_delete = checkpoints.find(
16+
(c) => c.timestamp === message.timestamp
17+
)
18+
19+
if (!checkpoint_to_delete) {
20+
return
21+
}
22+
23+
await delete_checkpoint_with_undo({
24+
context,
25+
checkpoint: checkpoint_to_delete,
26+
panel_provider,
27+
get_active_operation: () =>
28+
panel_provider.active_checkpoint_delete_operation,
29+
set_active_operation: (op) =>
30+
(panel_provider.active_checkpoint_delete_operation = op)
31+
})
32+
}

‎packages/vscode/src/views/panel/backend/message-handlers/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ export * from './handle-manage-configurations'
5252
export * from './handle-save-component-collapsed-state'
5353
export * from './handle-undo'
5454
export * from './handle-show-diff'
55+
export * from './handle-delete-checkpoint'

‎packages/vscode/src/views/panel/backend/panel-provider.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ import {
6868
handle_get_collapsed_states,
6969
handle_manage_configurations,
7070
handle_save_component_collapsed_state,
71-
handle_undo
71+
handle_undo,
72+
handle_delete_checkpoint
7273
} from './message-handlers'
7374
import {
7475
API_EDIT_FORMAT_STATE_KEY,
@@ -119,6 +120,10 @@ export class PanelProvider implements vscode.WebviewViewProvider {
119120
public api_call_cancel_token_source: CancelTokenSource | null = null
120121
public commit_was_staged_by_script: boolean = false
121122
public response_history: ResponseHistoryItem[] = []
123+
public active_checkpoint_delete_operation: {
124+
finalize: () => Promise<void>
125+
timestamp: number
126+
} | null = null
122127

123128
public async send_checkpoints() {
124129
const checkpoints = await get_checkpoints(this.context)
@@ -457,6 +462,8 @@ export class PanelProvider implements vscode.WebviewViewProvider {
457462
description: message.description,
458463
panel_provider: this
459464
})
465+
} else if (message.command == 'DELETE_CHECKPOINT') {
466+
await handle_delete_checkpoint(this, message)
460467
} else if (message.command == 'GET_HISTORY') {
461468
handle_get_history(this)
462469
} else if (message.command == 'GET_RESPONSE_HISTORY') {

‎packages/vscode/src/views/panel/frontend/Home/Home.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Props = {
2828
on_selected_history_item_change: (created_at: number) => void
2929
on_response_history_item_remove: (created_at: number) => void
3030
on_edit_checkpoint_description: (timestamp: number) => void
31+
on_delete_checkpoint: (timestamp: number) => void
3132
}
3233

3334
export const Home: React.FC<Props> = (props) => {
@@ -136,6 +137,7 @@ export const Home: React.FC<Props> = (props) => {
136137
}
137138
on_item_click={(id) => props.on_restore_checkpoint(id)}
138139
on_edit={props.on_edit_checkpoint_description}
140+
on_delete={props.on_delete_checkpoint}
139141
/>
140142
)}
141143
</div>

0 commit comments

Comments
 (0)