Skip to content

Commit bc28cfa

Browse files
committed
Replace switch statements with if-else chains for keyword handling in PromptField handlers
1 parent ef62e65 commit bc28cfa

1 file changed

Lines changed: 119 additions & 163 deletions

File tree

‎packages/ui/src/components/editor/panel/PromptField/hooks/use-handlers.ts‎

Lines changed: 119 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -53,91 +53,78 @@ const reconstruct_raw_value_from_node = (node: Node): string => {
5353
inner_content += reconstruct_raw_value_from_node(child)
5454
}
5555

56-
switch (el.dataset.type) {
57-
case 'file-keyword': {
58-
const path = el.dataset.path
59-
if (!path) return ''
60-
const filename = path.split('/').pop() || path
61-
const index = inner_content.indexOf(filename)
62-
if (index !== -1) {
63-
const prefix = inner_content.substring(0, index)
64-
const suffix = inner_content.substring(index + filename.length)
65-
return `${prefix}\`${path}\`${suffix}`
66-
}
67-
break
68-
}
69-
case 'changes-keyword': {
70-
const branchName = el.dataset.branchName
71-
if (!branchName) return ''
72-
const expected_text = `Diff with ${branchName}`
73-
const index = inner_content.indexOf(expected_text)
74-
if (index !== -1) {
75-
const prefix = inner_content.substring(0, index)
76-
const suffix = inner_content.substring(index + expected_text.length)
77-
return `${prefix}#Changes:${branchName}${suffix}`
78-
}
79-
break
80-
}
81-
case 'saved-context-keyword': {
82-
const contextType = el.dataset.contextType
83-
const contextName = el.dataset.contextName
84-
if (!contextType || !contextName) return ''
85-
const expected_text = `Context "${contextName}"`
86-
const index = inner_content.indexOf(expected_text)
87-
if (index !== -1) {
88-
const prefix = inner_content.substring(0, index)
89-
const suffix = inner_content.substring(index + expected_text.length)
90-
return `${prefix}#SavedContext:${contextType} "${contextName}"${suffix}`
91-
}
92-
break
93-
}
94-
case 'selection-keyword': {
95-
const expected_text = 'Selection'
96-
const index = inner_content.indexOf(expected_text)
97-
if (index !== -1) {
98-
const prefix = inner_content.substring(0, index)
99-
const suffix = inner_content.substring(index + expected_text.length)
100-
return `${prefix}#Selection${suffix}`
101-
}
102-
break
103-
}
104-
case 'commit-keyword': {
105-
const repo_name = el.dataset.repoName
106-
const commit_hash = el.dataset.commitHash
107-
const commit_message = el.dataset.commitMessage
108-
if (!repo_name || !commit_hash || commit_message === undefined) {
109-
return ''
110-
}
111-
const short_hash = commit_hash.substring(0, 7)
112-
const index = inner_content.indexOf(short_hash)
113-
if (index !== -1) {
114-
const prefix = inner_content.substring(0, index)
115-
const suffix = inner_content.substring(index + short_hash.length)
116-
return `${prefix}#Commit:${repo_name}:${commit_hash} "${commit_message.replace(
117-
/"/g,
118-
'\\"'
119-
)}"${suffix}`
120-
}
121-
break
122-
}
123-
case 'contextatcommit-keyword': {
124-
const repo_name = el.dataset.repoName
125-
const commit_hash = el.dataset.commitHash
126-
const commit_message = el.dataset.commitMessage
127-
if (!repo_name || !commit_hash || commit_message === undefined) {
128-
return ''
129-
}
130-
const short_hash = commit_hash.substring(0, 7)
131-
const index = inner_content.indexOf(short_hash)
132-
if (index !== -1) {
133-
const prefix = inner_content.substring(0, index)
134-
const suffix = inner_content.substring(index + short_hash.length)
135-
return `${prefix}#ContextAtCommit:${repo_name}:${commit_hash} "${commit_message.replace(
136-
/"/g,
137-
'\\"'
138-
)}"${suffix}`
139-
}
140-
break
56+
if (el.dataset.type == 'file-keyword') {
57+
const path = el.dataset.path
58+
if (!path) return ''
59+
const filename = path.split('/').pop() || path
60+
const index = inner_content.indexOf(filename)
61+
if (index !== -1) {
62+
const prefix = inner_content.substring(0, index)
63+
const suffix = inner_content.substring(index + filename.length)
64+
return `${prefix}\`${path}\`${suffix}`
65+
}
66+
} else if (el.dataset.type == 'changes-keyword') {
67+
const branchName = el.dataset.branchName
68+
if (!branchName) return ''
69+
const expected_text = `Diff with ${branchName}`
70+
const index = inner_content.indexOf(expected_text)
71+
if (index !== -1) {
72+
const prefix = inner_content.substring(0, index)
73+
const suffix = inner_content.substring(index + expected_text.length)
74+
return `${prefix}#Changes:${branchName}${suffix}`
75+
}
76+
} else if (el.dataset.type == 'saved-context-keyword') {
77+
const contextType = el.dataset.contextType
78+
const contextName = el.dataset.contextName
79+
if (!contextType || !contextName) return ''
80+
const expected_text = `Context "${contextName}"`
81+
const index = inner_content.indexOf(expected_text)
82+
if (index !== -1) {
83+
const prefix = inner_content.substring(0, index)
84+
const suffix = inner_content.substring(index + expected_text.length)
85+
return `${prefix}#SavedContext:${contextType} "${contextName}"${suffix}`
86+
}
87+
} else if (el.dataset.type == 'selection-keyword') {
88+
const expected_text = 'Selection'
89+
const index = inner_content.indexOf(expected_text)
90+
if (index !== -1) {
91+
const prefix = inner_content.substring(0, index)
92+
const suffix = inner_content.substring(index + expected_text.length)
93+
return `${prefix}#Selection${suffix}`
94+
}
95+
} else if (el.dataset.type == 'commit-keyword') {
96+
const repo_name = el.dataset.repoName
97+
const commit_hash = el.dataset.commitHash
98+
const commit_message = el.dataset.commitMessage
99+
if (!repo_name || !commit_hash || commit_message === undefined) {
100+
return ''
101+
}
102+
const short_hash = commit_hash.substring(0, 7)
103+
const index = inner_content.indexOf(short_hash)
104+
if (index !== -1) {
105+
const prefix = inner_content.substring(0, index)
106+
const suffix = inner_content.substring(index + short_hash.length)
107+
return `${prefix}#Commit:${repo_name}:${commit_hash} "${commit_message.replace(
108+
/"/g,
109+
'\\"'
110+
)}"${suffix}`
111+
}
112+
} else if (el.dataset.type == 'contextatcommit-keyword') {
113+
const repo_name = el.dataset.repoName
114+
const commit_hash = el.dataset.commitHash
115+
const commit_message = el.dataset.commitMessage
116+
if (!repo_name || !commit_hash || commit_message === undefined) {
117+
return ''
118+
}
119+
const short_hash = commit_hash.substring(0, 7)
120+
const index = inner_content.indexOf(short_hash)
121+
if (index !== -1) {
122+
const prefix = inner_content.substring(0, index)
123+
const suffix = inner_content.substring(index + short_hash.length)
124+
return `${prefix}#ContextAtCommit:${repo_name}:${commit_hash} "${commit_message.replace(
125+
/"/g,
126+
'\\"'
127+
)}"${suffix}`
141128
}
142129
}
143130

@@ -241,98 +228,67 @@ export const use_handlers = (
241228

242229
const handle_keyword_deletion_by_click = (keyword_element: HTMLElement) => {
243230
const keyword_type = keyword_element.dataset.type
244-
switch (keyword_type) {
245-
case 'file-keyword': {
246-
const file_path = keyword_element.dataset.path
247-
if (!file_path || !props.context_file_paths?.includes(file_path)) return
248-
249-
const search_pattern = `\`${file_path}\``
250-
const start_index = props.value.indexOf(search_pattern)
251-
252-
if (start_index !== -1) {
253-
apply_keyword_deletion(
254-
start_index,
255-
start_index + search_pattern.length
256-
)
257-
}
258-
break
231+
if (keyword_type === 'file-keyword') {
232+
const file_path = keyword_element.dataset.path
233+
if (!file_path || !props.context_file_paths?.includes(file_path)) return
234+
235+
const search_pattern = `\`${file_path}\``
236+
const start_index = props.value.indexOf(search_pattern)
237+
238+
if (start_index !== -1) {
239+
apply_keyword_deletion(start_index, start_index + search_pattern.length)
259240
}
260-
case 'changes-keyword': {
261-
const branch_name = keyword_element.dataset.branchName
262-
if (!branch_name) return
241+
} else if (keyword_type === 'changes-keyword') {
242+
const branch_name = keyword_element.dataset.branchName
243+
if (!branch_name) return
263244

264-
const search_pattern = `#Changes:${branch_name}`
265-
const start_index = props.value.indexOf(search_pattern)
245+
const search_pattern = `#Changes:${branch_name}`
246+
const start_index = props.value.indexOf(search_pattern)
266247

267-
if (start_index !== -1) {
268-
apply_keyword_deletion(
269-
start_index,
270-
start_index + search_pattern.length
271-
)
272-
}
273-
break
248+
if (start_index !== -1) {
249+
apply_keyword_deletion(start_index, start_index + search_pattern.length)
274250
}
275-
case 'saved-context-keyword': {
276-
const context_type = keyword_element.dataset.contextType
277-
const context_name = keyword_element.dataset.contextName
278-
if (!context_type || !context_name) return
251+
} else if (keyword_type === 'saved-context-keyword') {
252+
const context_type = keyword_element.dataset.contextType
253+
const context_name = keyword_element.dataset.contextName
254+
if (!context_type || !context_name) return
279255

280-
const search_pattern = `#SavedContext:${context_type} "${context_name}"`
281-
const start_index = props.value.indexOf(search_pattern)
256+
const search_pattern = `#SavedContext:${context_type} "${context_name}"`
257+
const start_index = props.value.indexOf(search_pattern)
282258

283-
if (start_index !== -1) {
284-
apply_keyword_deletion(
285-
start_index,
286-
start_index + search_pattern.length
287-
)
288-
}
289-
break
259+
if (start_index !== -1) {
260+
apply_keyword_deletion(start_index, start_index + search_pattern.length)
290261
}
291-
case 'selection-keyword': {
292-
const search_pattern = '#Selection'
293-
const start_index = props.value.indexOf(search_pattern)
262+
} else if (keyword_type === 'selection-keyword') {
263+
const search_pattern = '#Selection'
264+
const start_index = props.value.indexOf(search_pattern)
294265

295-
if (start_index !== -1) {
296-
apply_keyword_deletion(
297-
start_index,
298-
start_index + search_pattern.length
299-
)
300-
}
301-
break
266+
if (start_index !== -1) {
267+
apply_keyword_deletion(start_index, start_index + search_pattern.length)
302268
}
303-
case 'commit-keyword': {
304-
const repo_name = keyword_element.dataset.repoName
305-
const commit_hash = keyword_element.dataset.commitHash
306-
const commit_message = keyword_element.dataset.commitMessage
307-
if (!repo_name || !commit_hash || commit_message === undefined) return
269+
} else if (keyword_type === 'commit-keyword') {
270+
const repo_name = keyword_element.dataset.repoName
271+
const commit_hash = keyword_element.dataset.commitHash
272+
const commit_message = keyword_element.dataset.commitMessage
273+
if (!repo_name || !commit_hash || commit_message === undefined) return
308274

309-
const search_pattern = `#Commit:${repo_name}:${commit_hash} "${commit_message.replace(/"/g, '\\"')}"`
310-
const start_index = props.value.indexOf(search_pattern)
275+
const search_pattern = `#Commit:${repo_name}:${commit_hash} "${commit_message.replace(/"/g, '\\"')}"`
276+
const start_index = props.value.indexOf(search_pattern)
311277

312-
if (start_index !== -1) {
313-
apply_keyword_deletion(
314-
start_index,
315-
start_index + search_pattern.length
316-
)
317-
}
318-
break
278+
if (start_index !== -1) {
279+
apply_keyword_deletion(start_index, start_index + search_pattern.length)
319280
}
320-
case 'contextatcommit-keyword': {
321-
const repo_name = keyword_element.dataset.repoName
322-
const commit_hash = keyword_element.dataset.commitHash
323-
const commit_message = keyword_element.dataset.commitMessage
324-
if (!repo_name || !commit_hash || commit_message === undefined) return
281+
} else if (keyword_type === 'contextatcommit-keyword') {
282+
const repo_name = keyword_element.dataset.repoName
283+
const commit_hash = keyword_element.dataset.commitHash
284+
const commit_message = keyword_element.dataset.commitMessage
285+
if (!repo_name || !commit_hash || commit_message === undefined) return
325286

326-
const search_pattern = `#ContextAtCommit:${repo_name}:${commit_hash} "${commit_message.replace(/"/g, '\\"')}"`
327-
const start_index = props.value.indexOf(search_pattern)
287+
const search_pattern = `#ContextAtCommit:${repo_name}:${commit_hash} "${commit_message.replace(/"/g, '\\"')}"`
288+
const start_index = props.value.indexOf(search_pattern)
328289

329-
if (start_index !== -1) {
330-
apply_keyword_deletion(
331-
start_index,
332-
start_index + search_pattern.length
333-
)
334-
}
335-
break
290+
if (start_index !== -1) {
291+
apply_keyword_deletion(start_index, start_index + search_pattern.length)
336292
}
337293
}
338294
}

0 commit comments

Comments
 (0)