Skip to content

Commit 4b8620f

Browse files
committed
Refactor checkpoint quick pick logic to separate view updates from data fetching and state management
1 parent fd9c7d1 commit 4b8620f

1 file changed

Lines changed: 59 additions & 41 deletions

File tree

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

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -86,38 +86,17 @@ export const checkpoints_command = (params: {
8686

8787
let notification_count = 0
8888
let checkpoints: Checkpoint[] = []
89+
let temp_checkpoint_is_valid = false
8990

90-
const refresh_items = async () => {
91-
quick_pick.busy = true
92-
checkpoints = await get_checkpoints(params.context)
93-
94-
const temp_checkpoint = params.context.workspaceState.get<Checkpoint>(
95-
TEMPORARY_CHECKPOINT_STATE_KEY
96-
)
91+
const update_view = () => {
9792
let revert_item:
9893
| (vscode.QuickPickItem & { id?: string; checkpoint?: Checkpoint })
9994
| undefined
10095

101-
if (temp_checkpoint) {
102-
const three_hours_in_ms = 3 * 60 * 60 * 1000
103-
if (Date.now() - temp_checkpoint.timestamp < three_hours_in_ms) {
104-
try {
105-
const checkpoint_path = get_checkpoint_path(
106-
temp_checkpoint.timestamp
107-
)
108-
await vscode.workspace.fs.stat(vscode.Uri.file(checkpoint_path))
109-
revert_item = {
110-
id: 'revert-last',
111-
label: '$(discard) Revert last restored checkpoint',
112-
alwaysShow: true
113-
}
114-
} catch {
115-
// file doesn't exist, so we can't revert. Clean up state.
116-
await params.context.workspaceState.update(
117-
TEMPORARY_CHECKPOINT_STATE_KEY,
118-
undefined
119-
)
120-
}
96+
if (temp_checkpoint_is_valid) {
97+
revert_item = {
98+
id: 'revert-last',
99+
label: '$(discard) Revert last restored checkpoint'
121100
}
122101
}
123102

@@ -133,13 +112,7 @@ export const checkpoints_command = (params: {
133112
return b.timestamp - a.timestamp
134113
})
135114

136-
quick_pick.items = [
137-
{
138-
id: 'add-new',
139-
label: '$(add) New checkpoint...',
140-
alwaysShow: true
141-
},
142-
...(revert_item ? [revert_item] : []),
115+
const checkpoint_items = [
143116
...(visible_checkpoints.length > 0
144117
? [
145118
{
@@ -179,9 +152,54 @@ export const checkpoints_command = (params: {
179152
}
180153
})
181154
]
155+
156+
if (quick_pick.value) {
157+
quick_pick.items = checkpoint_items
158+
} else {
159+
quick_pick.items = [
160+
{
161+
id: 'add-new',
162+
label: '$(add) New checkpoint...'
163+
},
164+
...(revert_item ? [revert_item] : []),
165+
...checkpoint_items
166+
]
167+
}
168+
}
169+
170+
const refresh_and_update_view = async () => {
171+
quick_pick.busy = true
172+
checkpoints = await get_checkpoints(params.context)
173+
174+
const temp_checkpoint = params.context.workspaceState.get<Checkpoint>(
175+
TEMPORARY_CHECKPOINT_STATE_KEY
176+
)
177+
temp_checkpoint_is_valid = false
178+
if (temp_checkpoint) {
179+
const three_hours_in_ms = 3 * 60 * 60 * 1000
180+
if (Date.now() - temp_checkpoint.timestamp < three_hours_in_ms) {
181+
try {
182+
const checkpoint_path = get_checkpoint_path(
183+
temp_checkpoint.timestamp
184+
)
185+
await vscode.workspace.fs.stat(vscode.Uri.file(checkpoint_path))
186+
temp_checkpoint_is_valid = true
187+
} catch {
188+
// file doesn't exist, so we can't revert. Clean up state.
189+
await params.context.workspaceState.update(
190+
TEMPORARY_CHECKPOINT_STATE_KEY,
191+
undefined
192+
)
193+
}
194+
}
195+
}
196+
197+
update_view()
182198
quick_pick.busy = false
183199
}
184200

201+
quick_pick.onDidChangeValue(update_view)
202+
185203
quick_pick.onDidAccept(async () => {
186204
const selected = quick_pick.selectedItems[0]
187205
if (!selected) return
@@ -194,7 +212,7 @@ export const checkpoints_command = (params: {
194212
params.panel_provider,
195213
params.websites_provider
196214
)
197-
await show_quick_pick()
215+
await refresh_and_update_view()
198216
} else if (selected.id == 'revert-last') {
199217
quick_pick.hide()
200218
const temp_checkpoint =
@@ -258,7 +276,7 @@ export const checkpoints_command = (params: {
258276
TEMPORARY_CHECKPOINT_STATE_KEY,
259277
undefined
260278
)
261-
await show_quick_pick()
279+
await refresh_and_update_view()
262280
}
263281
})
264282

@@ -281,7 +299,7 @@ export const checkpoints_command = (params: {
281299
)
282300
params.panel_provider.send_checkpoints()
283301
}
284-
await refresh_items()
302+
await refresh_and_update_view()
285303
const active_item = quick_pick.items.find(
286304
(i) =>
287305
(i as any).checkpoint?.timestamp === item.checkpoint?.timestamp
@@ -315,7 +333,7 @@ export const checkpoints_command = (params: {
315333
params.panel_provider.send_checkpoints()
316334
}
317335
}
318-
await refresh_items()
336+
await refresh_and_update_view()
319337
if (new_description !== undefined) {
320338
const active_item = quick_pick.items.find(
321339
(i) =>
@@ -354,7 +372,7 @@ export const checkpoints_command = (params: {
354372
)
355373
params.panel_provider.send_checkpoints()
356374
checkpoints = updated_checkpoints
357-
await refresh_items()
375+
update_view()
358376

359377
const currentOperation = {
360378
timestamp: deleted_checkpoint.timestamp,
@@ -407,7 +425,7 @@ export const checkpoints_command = (params: {
407425
.then(() => {
408426
notification_count--
409427
})
410-
await refresh_items()
428+
update_view()
411429
} else {
412430
await currentOperation.finalize()
413431
}
@@ -434,7 +452,7 @@ export const checkpoints_command = (params: {
434452
}
435453
quick_pick.dispose()
436454
})
437-
await refresh_items()
455+
await refresh_and_update_view()
438456
quick_pick.show()
439457
}
440458

0 commit comments

Comments
 (0)