Skip to content

Commit bcc22be

Browse files
committed
Add cancel functionality for intelligent file updates in response preview
1 parent 521cea8 commit bcc22be

9 files changed

Lines changed: 97 additions & 8 deletions

File tree

‎packages/ui/src/components/editor/panel/ResponsePreview/ResponsePreview.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ type Props = {
1414
file_path: string
1515
workspace_name?: string
1616
}) => void
17+
on_cancel_intelligent_update: (file: {
18+
file_path: string
19+
workspace_name?: string
20+
}) => void
1721
on_toggle_file: (file: {
1822
file_path: string
1923
workspace_name?: string
@@ -223,6 +227,12 @@ export const ResponsePreview: FC<Props> = (props) => {
223227
workspace_name: file.workspace_name
224228
})
225229
}
230+
on_cancel_intelligent_update={() =>
231+
props.on_cancel_intelligent_update({
232+
file_path: file.file_path,
233+
workspace_name: file.workspace_name
234+
})
235+
}
226236
on_go_to_file={() =>
227237
props.on_go_to_file({
228238
file_path: file.file_path,

‎packages/ui/src/components/editor/panel/ResponsePreview/components/FileItem/FileItem.module.scss‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
color: var(--vscode-descriptionForeground);
1212
margin-right: 4px;
1313
white-space: nowrap;
14+
15+
&__cancel {
16+
display: flex;
17+
align-items: center;
18+
cursor: pointer;
19+
20+
&:hover {
21+
color: var(--vscode-textLink-foreground);
22+
}
23+
}
1424
}
1525

1626
.file {

‎packages/ui/src/components/editor/panel/ResponsePreview/components/FileItem/FileItem.tsx‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Props = {
1515
on_discard_user_changes: () => void
1616
on_preview_generated_code: () => void
1717
on_intelligent_update: () => void
18+
on_cancel_intelligent_update: () => void
1819
on_go_to_file: () => void
1920
}
2021

@@ -56,6 +57,7 @@ export const FileItem: FC<Props> = ({
5657
on_discard_user_changes,
5758
on_preview_generated_code,
5859
on_intelligent_update,
60+
on_cancel_intelligent_update,
5961
on_go_to_file
6062
}) => {
6163
const message_obj = get_file_message(file)
@@ -82,6 +84,18 @@ export const FileItem: FC<Props> = ({
8284
<div className={styles.progress}>
8385
<span>{status_text}</span>
8486
<span className="codicon codicon-loading codicon-modifier-spin" />
87+
{file.apply_status !== 'done' && (
88+
<div
89+
className={styles.progress__cancel}
90+
onClick={(e) => {
91+
e.stopPropagation()
92+
on_cancel_intelligent_update()
93+
}}
94+
title="Cancel"
95+
>
96+
<span className="codicon codicon-close" />
97+
</div>
98+
)}
8599
</div>
86100
)
87101
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { PanelProvider } from '@/views/panel/backend/panel-provider'
2+
3+
import { CancelIntelligentUpdateFileInPreviewMessage } from '@/views/panel/types/messages'
4+
5+
export const handle_cancel_intelligent_update_file_in_preview = (
6+
panel_provider: PanelProvider,
7+
8+
message: CancelIntelligentUpdateFileInPreviewMessage
9+
) => {
10+
const { file_path, workspace_name } = message
11+
12+
const source_index =
13+
panel_provider.intelligent_update_cancel_token_sources.findIndex(
14+
(s) => s.file_path === file_path && s.workspace_name === workspace_name
15+
)
16+
17+
if (source_index > -1) {
18+
const { source } =
19+
panel_provider.intelligent_update_cancel_token_sources[source_index]
20+
21+
source.cancel('User cancelled the operation')
22+
23+
panel_provider.intelligent_update_cancel_token_sources.splice(
24+
source_index,
25+
26+
1
27+
)
28+
}
29+
}

‎packages/vscode/src/views/panel/backend/message-handlers/handle-intelligent-update-file-in-preview.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,11 @@ export const handle_intelligent_update_file_in_preview = async (
127127
if (!safe_path) return
128128

129129
const cancel_token_source = axios.CancelToken.source()
130-
panel_provider.intelligent_update_cancel_token_sources.push(
131-
cancel_token_source
132-
)
130+
panel_provider.intelligent_update_cancel_token_sources.push({
131+
source: cancel_token_source,
132+
file_path,
133+
workspace_name
134+
})
133135

134136
panel_provider.send_message({
135137
command: 'UPDATE_FILE_PROGRESS',
@@ -265,8 +267,8 @@ export const handle_intelligent_update_file_in_preview = async (
265267
}
266268
} finally {
267269
const index =
268-
panel_provider.intelligent_update_cancel_token_sources.indexOf(
269-
cancel_token_source
270+
panel_provider.intelligent_update_cancel_token_sources.findIndex(
271+
(s) => s.source === cancel_token_source
270272
)
271273
if (index > -1) {
272274
panel_provider.intelligent_update_cancel_token_sources.splice(index, 1)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@ export * from './handle-delete-task'
6060
export * from './handle-preview-generated-code'
6161
export * from './handle-fix-all-failed-files'
6262
export * from './handle-prune-context'
63+
export * from './handle-cancel-intelligent-update-file-in-preview'

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ import {
7878
handle_save_tasks,
7979
handle_delete_task,
8080
handle_fix_all_failed_files,
81-
handle_prune_context
81+
handle_prune_context,
82+
handle_cancel_intelligent_update_file_in_preview
8283
} from './message-handlers'
8384
import {
8485
API_EDIT_FORMAT_STATE_KEY,
@@ -124,7 +125,11 @@ export class PanelProvider implements vscode.WebviewViewProvider {
124125
public api_edit_format: EditFormat
125126
public api_prompt_type: ApiPromptType
126127
public mode: Mode = MODE.WEB
127-
public intelligent_update_cancel_token_sources: CancelTokenSource[] = []
128+
public intelligent_update_cancel_token_sources: {
129+
source: CancelTokenSource
130+
file_path: string
131+
workspace_name?: string
132+
}[] = []
128133
public api_call_cancel_token_source: CancelTokenSource | null = null
129134
public api_manager!: ApiManager
130135
public commit_was_staged_by_script: boolean = false
@@ -406,7 +411,7 @@ export class PanelProvider implements vscode.WebviewViewProvider {
406411
public cancel_all_intelligent_updates() {
407412
const sources = [...this.intelligent_update_cancel_token_sources]
408413
this.intelligent_update_cancel_token_sources = []
409-
sources.forEach((source) => source.cancel('Preview finished.'))
414+
sources.forEach((item) => item.source.cancel('Preview finished.'))
410415
}
411416

412417
public send_context_files() {
@@ -637,6 +642,10 @@ export class PanelProvider implements vscode.WebviewViewProvider {
637642
await handle_discard_user_changes_in_preview(message)
638643
} else if (message.command == 'INTELLIGENT_UPDATE_FILE_IN_PREVIEW') {
639644
await handle_intelligent_update_file_in_preview(this, message)
645+
} else if (
646+
message.command == 'CANCEL_INTELLIGENT_UPDATE_FILE_IN_PREVIEW'
647+
) {
648+
handle_cancel_intelligent_update_file_in_preview(this, message)
640649
} else if (message.command == 'RESPONSE_PREVIEW') {
641650
await handle_response_preview(message)
642651
} else if (message.command == 'REMOVE_RESPONSE_HISTORY_ITEM') {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,13 @@ export const Panel = () => {
557557
workspace_name: file.workspace_name
558558
})
559559
}}
560+
on_cancel_intelligent_update={(file) => {
561+
post_message(vscode, {
562+
command: 'CANCEL_INTELLIGENT_UPDATE_FILE_IN_PREVIEW',
563+
file_path: file.file_path,
564+
workspace_name: file.workspace_name
565+
})
566+
}}
560567
on_preview_generated_code={(file) => {
561568
post_message(vscode, {
562569
command: 'PREVIEW_GENERATED_CODE',

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ export interface IntelligentUpdateFileInPreviewMessage extends BaseMessage {
342342
workspace_name?: string
343343
}
344344

345+
export interface CancelIntelligentUpdateFileInPreviewMessage extends BaseMessage {
346+
command: 'CANCEL_INTELLIGENT_UPDATE_FILE_IN_PREVIEW'
347+
file_path: string
348+
workspace_name?: string
349+
}
350+
345351
export interface FixAllFailedFilesMessage extends BaseMessage {
346352
command: 'FIX_ALL_FAILED_FILES'
347353
}
@@ -520,6 +526,7 @@ export type FrontendMessage =
520526
| GetWorkspaceStateMessage
521527
| RequestGitStateMessage
522528
| IntelligentUpdateFileInPreviewMessage
529+
| CancelIntelligentUpdateFileInPreviewMessage
523530
| UpdateLastUsedPresetMessage
524531
| FixAllFailedFilesMessage
525532
| CommitChangesMessage

0 commit comments

Comments
 (0)