Skip to content

Commit 54459a7

Browse files
committed
Refactor file preview logic to explicitly check for type: 'file' instead of relying on the presence of file_path
1 parent 49857dc commit 54459a7

7 files changed

Lines changed: 22 additions & 11 deletions

File tree

‎packages/shared/src/types/file-in-preview.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type FileInPreview = {
2+
type: 'file'
23
file_path: string
34
is_checked: boolean
45
workspace_name?: string

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ export default {
88

99
const base_files = [
1010
{
11+
type: 'file',
1112
file_path: 'src/components/Button/Button.tsx',
1213
lines_added: 10,
1314
lines_removed: 2
1415
},
1516
{
17+
type: 'file',
1618
file_path: 'src/components/Input/Input.module.scss',
1719
lines_added: 5,
1820
lines_removed: 5
1921
},
2022
{
23+
type: 'file',
2124
file_path: 'src/utils/helpers.ts',
2225
lines_added: 20,
2326
lines_removed: 0
@@ -26,25 +29,29 @@ const base_files = [
2629

2730
const files_using_fallbacks = [
2831
{
32+
type: 'file',
2933
file_path: 'src/new-feature.ts',
3034
is_new: true,
3135
lines_added: 50,
3236
lines_removed: 0
3337
},
3438
{
39+
type: 'file',
3540
file_path: 'src/old-component.tsx',
3641
is_deleted: true,
3742
lines_added: 0,
3843
lines_removed: 100
3944
},
4045
{
46+
type: 'file',
4147
file_path: 'src/complex-logic.js',
4248
is_fallback: true,
4349
diff_fallback_method: 'recount',
4450
lines_added: 15,
4551
lines_removed: 12
4652
},
4753
{
54+
type: 'file',
4855
file_path: 'src/aggressive-fallback.css',
4956
is_fallback: true,
5057
diff_fallback_method: 'search_and_replace',
@@ -62,7 +69,7 @@ const log_action =
6269
const InteractiveResponsePreview = (props: any) => {
6370
const [items, set_items] = useState(
6471
props.items.map((f: any) =>
65-
'file_path' in f ? { ...f, is_checked: true } : f
72+
f.type == 'file' ? { ...f, is_checked: true } : f
6673
)
6774
)
6875

@@ -77,9 +84,9 @@ const InteractiveResponsePreview = (props: any) => {
7784
}) => {
7885
set_items(
7986
items.map((f: any) =>
80-
'file_path' in f &&
81-
f.file_path === file_path &&
82-
f.workspace_name === workspace_name
87+
f.type == 'file' &&
88+
f.file_path == file_path &&
89+
f.workspace_name == workspace_name
8390
? { ...f, is_checked }
8491
: f
8592
)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const ResponsePreview: FC<Props> = (props) => {
6868
}
6969

7070
const files_in_preview = props.items.filter(
71-
(i) => 'file_path' in i
71+
(i) => i.type == 'file'
7272
) as FileInPreview[]
7373
const fallback_count = files_in_preview.filter((f) => f.is_fallback).length
7474

@@ -118,7 +118,7 @@ export const ResponsePreview: FC<Props> = (props) => {
118118
)}
119119
<div className={styles.list}>
120120
{props.items.map((item, index) => {
121-
if ('file_path' in item) {
121+
if (item.type === 'file') {
122122
const file = item
123123
const last_slash_index = file.file_path.lastIndexOf('/')
124124
const file_name = file.file_path.substring(last_slash_index + 1)

‎packages/vscode/src/commands/apply-chat-response-command/apply-chat-response-command.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export const apply_chat_response_command = (
154154
!state.is_new && current_content === '' && state.content !== ''
155155

156156
files_for_history.push({
157+
type: 'file',
157158
file_path: state.file_path,
158159
workspace_name: state.workspace_name,
159160
is_new: state.is_new,

‎packages/vscode/src/commands/apply-chat-response-command/utils/preview/file-preparer.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const prepare_files_from_original_states = async (params: {
6262
(!state.is_new && current_content == '' && state.content != '')
6363

6464
const reviewable_file: ReviewableFile = {
65+
type: 'file',
6566
file_path: state.file_path,
6667
content: current_content,
6768
workspace_name: state.workspace_name,
@@ -110,6 +111,7 @@ export const prepare_files_from_original_states = async (params: {
110111
})
111112

112113
const restored_reviewable_file: ReviewableFile = {
114+
type: 'file',
113115
file_path: state.file_path_to_restore,
114116
content: restored_current_content,
115117
workspace_name: state.workspace_name,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ export const Panel = () => {
332332
}}
333333
on_accept={() => {
334334
const accepted_files = items_to_review.filter(
335-
(f) => 'file_path' in f && f.is_checked
335+
(f) => f.type === 'file' && f.is_checked
336336
) as FileInPreview[]
337337
set_response_history([])
338338
set_selected_history_item_created_at(undefined)
@@ -343,7 +343,7 @@ export const Panel = () => {
343343
}}
344344
is_accept_disabled={
345345
items_to_review.filter(
346-
(f) => 'file_path' in f && f.is_checked
346+
(f) => f.type === 'file' && f.is_checked
347347
).length == 0
348348
}
349349
/>
@@ -370,7 +370,7 @@ export const Panel = () => {
370370
on_toggle_file={(file) => {
371371
set_items_to_review((current_items) =>
372372
current_items?.map((f) =>
373-
'file_path' in f &&
373+
f.type === 'file' &&
374374
f.file_path == file.file_path &&
375375
f.workspace_name == file.workspace_name
376376
? { ...f, is_checked: file.is_checked }

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,15 @@ export const use_panel = (vscode: any) => {
148148
const items = current_items ?? []
149149
const existing_file_index = items.findIndex(
150150
(f) =>
151-
'file_path' in f &&
151+
f.type === 'file' &&
152152
f.file_path == message.file.file_path &&
153153
f.workspace_name == message.file.workspace_name
154154
)
155155

156156
if (existing_file_index != -1) {
157157
const new_items = [...items]
158158
const existing_item = items[existing_file_index]
159-
if ('file_path' in existing_item) {
159+
if (existing_item.type === 'file') {
160160
new_items[existing_file_index] = {
161161
...message.file,
162162
is_checked: existing_item.is_checked

0 commit comments

Comments
 (0)