Skip to content

Commit bcb54c7

Browse files
committed
Refactor undo state management by extracting can-undo logic into a dedicated message handler
1 parent fba1076 commit bcb54c7

5 files changed

Lines changed: 27 additions & 20 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { PanelProvider } from '../panel-provider'
2+
import { LAST_APPLIED_CHANGES_STATE_KEY } from '@/constants/state-keys'
3+
import { OriginalFileState } from '@/commands/apply-chat-response-command/types/original-file-state'
4+
5+
export const handle_request_can_undo = (panel_provider: PanelProvider) => {
6+
const original_states = panel_provider.context.workspaceState.get<
7+
OriginalFileState[]
8+
>(LAST_APPLIED_CHANGES_STATE_KEY)
9+
const can_undo = !!original_states && original_states.length > 0
10+
11+
panel_provider.send_message({
12+
command: 'CAN_UNDO_CHANGED',
13+
can_undo
14+
})
15+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ export * from './handle-show-diff'
5353
export * from './handle-delete-checkpoint'
5454
export * from './handle-clear-all-checkpoints'
5555
export * from './handle-discard-user-changes-in-preview'
56+
export * from './handle-request-can-undo'

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ import {
7070
handle_manage_configurations,
7171
handle_save_component_collapsed_state,
7272
handle_undo,
73-
handle_delete_checkpoint
73+
handle_delete_checkpoint,
74+
handle_request_can_undo
7475
} from './message-handlers'
7576
import {
7677
API_EDIT_FORMAT_STATE_KEY,
@@ -81,7 +82,6 @@ import {
8182
INSTRUCTIONS_EDIT_CONTEXT_STATE_KEY,
8283
INSTRUCTIONS_NO_CONTEXT_STATE_KEY,
8384
get_configurations_collapsed_state_key,
84-
LAST_APPLIED_CHANGES_STATE_KEY,
8585
LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY,
8686
LAST_SELECTED_EDIT_CONTEXT_CONFIG_ID_STATE_KEY,
8787
get_presets_collapsed_state_key,
@@ -92,7 +92,6 @@ import {
9292
config_preset_to_ui_format,
9393
ConfigPresetFormat
9494
} from '@/views/panel/backend/utils/preset-format-converters'
95-
import { OriginalFileState } from '@/commands/apply-chat-response-command/types/original-file-state'
9695
import { CHATBOTS } from '@shared/constants/chatbots'
9796
import { MODE, Mode } from '../types/main-view-mode'
9897
import { ApiPromptType, WebPromptType } from '@shared/types/prompt-types'
@@ -648,6 +647,8 @@ export class PanelProvider implements vscode.WebviewViewProvider {
648647
if (this.preview_switch_choice_resolver) {
649648
this.preview_switch_choice_resolver(message.choice)
650649
}
650+
} else if (message.command == 'REQUEST_CAN_UNDO') {
651+
handle_request_can_undo(this)
651652
}
652653
} catch (error: any) {
653654
Logger.error({
@@ -661,15 +662,6 @@ export class PanelProvider implements vscode.WebviewViewProvider {
661662
}
662663
}
663664
)
664-
665-
this.send_presets_to_webview(webview_view.webview)
666-
// We need to wait until the webview fully initialized
667-
setTimeout(() => {
668-
this.send_message({
669-
command: 'CAN_UNDO_CHANGED',
670-
can_undo: this._can_undo()
671-
})
672-
}, 1000)
673665
}
674666

675667
public calculate_token_count() {
@@ -907,13 +899,6 @@ export class PanelProvider implements vscode.WebviewViewProvider {
907899
})
908900
}
909901

910-
private _can_undo(): boolean {
911-
const original_states = this.context.workspaceState.get<
912-
OriginalFileState[]
913-
>(LAST_APPLIED_CHANGES_STATE_KEY)
914-
return !!original_states && original_states.length > 0
915-
}
916-
917902
private _get_html_for_webview(webview: vscode.Webview) {
918903
const resources_uri = webview.asWebviewUri(
919904
vscode.Uri.joinPath(this.extension_uri, 'resources')

‎packages/vscode/src/views/panel/frontend/hooks/use-panel.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ export const use_panel = (vscode: any) => {
296296
{ command: 'REQUEST_GIT_STATE' },
297297
{ command: 'GET_COLLAPSED_STATES' },
298298
{ command: 'GET_CHECKPOINTS' },
299-
{ command: 'REQUEST_CURRENTLY_OPEN_FILE_TEXT' }
299+
{ command: 'REQUEST_CURRENTLY_OPEN_FILE_TEXT' },
300+
{ command: 'REQUEST_CAN_UNDO' }
300301
]
301302
initial_messages.forEach((message) => post_message(vscode, message))
302303

‎packages/vscode/src/views/panel/types/messages.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@ export interface ClearAllCheckpointsMessage extends BaseMessage {
397397
command: 'CLEAR_ALL_CHECKPOINTS'
398398
}
399399

400+
export interface RequestCanUndoMessage extends BaseMessage {
401+
command: 'REQUEST_CAN_UNDO'
402+
}
403+
400404
export interface PreviewSwitchChoiceMessage extends BaseMessage {
401405
command: 'PREVIEW_SWITCH_CHOICE'
402406
choice?: 'Switch'
@@ -475,6 +479,7 @@ export type FrontendMessage =
475479
| UpdateCheckpointDescriptionMessage
476480
| DeleteCheckpointMessage
477481
| ClearAllCheckpointsMessage
482+
| RequestCanUndoMessage
478483
| PreviewSwitchChoiceMessage
479484

480485
// === FROM BACKEND TO FRONTEND ===

0 commit comments

Comments
 (0)