Skip to content

Commit 3664599

Browse files
committed
Refactor the UI Header component to remove complex dropdown width calculations and move the Escape key handler into the dedicated keyboard shortcuts hook
1 parent 4b8620f commit 3664599

6 files changed

Lines changed: 62 additions & 73 deletions

File tree

‎packages/ui/src/components/editor/panel/Dropdown/Dropdown.module.scss‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.container {
2-
position: relative;
2+
position: absolute;
3+
top: 0;
4+
width: 100%;
35
}
46

57
.button {

‎packages/vscode/src/views/panel/backend/message-handlers/handle-show-prompt-template-quick-pick.ts‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,28 @@ export const handle_show_prompt_template_quick_pick = async (
8888
tooltip: 'Delete template'
8989
}
9090

91-
const create_template_items = (templates: PromptTemplate[]) => {
91+
const create_template_items = (
92+
templates: PromptTemplate[],
93+
search_value?: string
94+
) => {
9295
const items: (vscode.QuickPickItem & {
9396
template?: PromptTemplate
9497
index?: number
95-
})[] = [
96-
{
97-
label: ADD_NEW_TEMPLATE_LABEL
98-
}
99-
]
100-
if (templates.length > 0) {
98+
})[] = []
99+
100+
if (!search_value) {
101101
items.push({
102-
label: '',
103-
kind: vscode.QuickPickItemKind.Separator
102+
label: ADD_NEW_TEMPLATE_LABEL
104103
})
104+
}
105+
106+
if (templates.length > 0) {
107+
if (!search_value) {
108+
items.push({
109+
label: 'Recently used',
110+
kind: vscode.QuickPickItemKind.Separator
111+
})
112+
}
105113
items.push(
106114
...templates.map((template, index) => {
107115
const buttons = [edit_button, delete_button]
@@ -277,7 +285,10 @@ export const handle_show_prompt_template_quick_pick = async (
277285
vscode.ConfigurationTarget.Global
278286
)
279287
}
280-
templates_quick_pick.items = create_template_items(prompt_templates)
288+
templates_quick_pick.items = create_template_items(
289+
prompt_templates,
290+
templates_quick_pick.value
291+
)
281292
is_editing_template = false
282293
if (!is_disposed) {
283294
templates_quick_pick.show()
@@ -354,8 +365,8 @@ export const handle_show_prompt_template_quick_pick = async (
354365
})
355366
}
356367
}),
357-
templates_quick_pick.onDidChangeValue(() => {
358-
templates_quick_pick.items = create_template_items(prompt_templates)
368+
templates_quick_pick.onDidChangeValue((value) => {
369+
templates_quick_pick.items = create_template_items(prompt_templates, value)
359370
}),
360371
templates_quick_pick.onDidTriggerItemButton(async (event) => {
361372
const item = event.item as vscode.QuickPickItem & {
@@ -377,7 +388,10 @@ export const handle_show_prompt_template_quick_pick = async (
377388
} else {
378389
// User clicked 'Back' from the edit quick pick, or successfully edited and returned to it.
379390
// We need to re-show the main templates_quick_pick.
380-
templates_quick_pick.items = create_template_items(prompt_templates)
391+
templates_quick_pick.items = create_template_items(
392+
prompt_templates,
393+
templates_quick_pick.value
394+
)
381395
// Highlight the item that was just edited
382396
const edited_item = templates_quick_pick.items.find(
383397
(i) => i.index === item.index
@@ -407,7 +421,10 @@ export const handle_show_prompt_template_quick_pick = async (
407421
vscode.ConfigurationTarget.Global
408422
)
409423
prompt_templates = updated_templates
410-
templates_quick_pick.items = create_template_items(prompt_templates)
424+
templates_quick_pick.items = create_template_items(
425+
prompt_templates,
426+
templates_quick_pick.value
427+
)
411428

412429
// Handle undo asynchronously without blocking the UI
413430
const undo_button_text = 'Undo'
@@ -428,8 +445,10 @@ export const handle_show_prompt_template_quick_pick = async (
428445
prompt_templates,
429446
vscode.ConfigurationTarget.Global
430447
)
431-
templates_quick_pick.items =
432-
create_template_items(prompt_templates)
448+
templates_quick_pick.items = create_template_items(
449+
prompt_templates,
450+
templates_quick_pick.value
451+
)
433452

434453
notification_count++
435454
vscode.window

‎packages/vscode/src/views/panel/frontend/Main/MainView/MainView.tsx‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,6 @@ type Props = {
9797
}
9898

9999
export const MainView: React.FC<Props> = (props) => {
100-
useEffect(() => {
101-
const handle_key_down = (e: KeyboardEvent) => {
102-
if (
103-
e.key == 'Escape' &&
104-
!e.metaKey &&
105-
!e.ctrlKey &&
106-
!e.altKey &&
107-
!e.shiftKey
108-
) {
109-
props.on_show_home()
110-
}
111-
}
112-
113-
window.addEventListener('keydown', handle_key_down)
114-
return () => {
115-
window.removeEventListener('keydown', handle_key_down)
116-
}
117-
}, [])
118-
119100
const is_in_code_completions_prompt_type =
120101
(props.mode == MODE.WEB && props.web_prompt_type == 'code-completions') ||
121102
(props.mode == MODE.API && props.api_prompt_type == 'code-completions')

‎packages/vscode/src/views/panel/frontend/Main/MainView/components/Header/Header.module.scss‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@
4444
display: flex;
4545
align-items: center;
4646
gap: 8px;
47+
4748

4849
&__dropdown {
50+
position: relative;
51+
flex: 1;
4952
width: 100%;
53+
height: 24px;
5054
}
5155

5256
&__settings {

‎packages/vscode/src/views/panel/frontend/Main/MainView/components/Header/Header.tsx‎

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect, useRef, useCallback } from 'react'
1+
import { useRef, useCallback } from 'react'
22
import cn from 'classnames'
33
import { MODE, Mode } from '@/views/panel/types/main-view-mode'
44
import { use_is_narrow_viewport, use_is_mac } from '@shared/hooks'
@@ -21,13 +21,11 @@ type Props = {
2121
}
2222

2323
export const Header: React.FC<Props> = (props) => {
24-
const [dropdown_max_width, set_dropdown_max_width] = useState<
25-
number | undefined
26-
>(undefined)
2724
const header_ref = useRef<HTMLDivElement>(null)
2825
const is_narrow_viewport = use_is_narrow_viewport(294)
2926
const is_mac = use_is_mac()
3027
const header_left_ref = useRef<HTMLDivElement>(null)
28+
const dropdown_container_ref = useRef<HTMLDivElement>(null)
3129

3230
const handle_heading_click = useCallback(() => {
3331
if (props.mode == MODE.WEB) {
@@ -41,36 +39,10 @@ export const Header: React.FC<Props> = (props) => {
4139
mode: props.mode,
4240
handle_heading_click,
4341
on_web_prompt_type_change: props.on_web_prompt_type_change,
44-
on_api_prompt_type_change: props.on_api_prompt_type_change
42+
on_api_prompt_type_change: props.on_api_prompt_type_change,
43+
on_show_home: props.on_show_home
4544
})
4645

47-
const calculate_dropdown_max_width = () => {
48-
if (!header_ref.current || !header_left_ref.current) return
49-
50-
const header_width = header_ref.current.offsetWidth
51-
const header_left_width = header_left_ref.current.offsetWidth
52-
// header_width - left_width - (header padding) 24 - (gap between left/right) 12 - (gap in right) 8 - (settings button) 20
53-
const calculated_width = header_width - header_left_width - 64
54-
set_dropdown_max_width(calculated_width)
55-
}
56-
57-
useEffect(() => {
58-
if (!header_ref.current || !header_left_ref.current) return
59-
60-
const resize_observer = new ResizeObserver(() => {
61-
calculate_dropdown_max_width()
62-
})
63-
64-
resize_observer.observe(header_ref.current)
65-
resize_observer.observe(header_left_ref.current)
66-
67-
calculate_dropdown_max_width()
68-
69-
return () => {
70-
resize_observer.disconnect()
71-
}
72-
}, [])
73-
7446
return (
7547
<div className={styles.header} ref={header_ref}>
7648
<div className={styles.header__left} ref={header_left_ref}>
@@ -82,14 +54,17 @@ export const Header: React.FC<Props> = (props) => {
8254
<button
8355
className={styles['header__left__toggler']}
8456
onClick={handle_heading_click}
85-
title={`Change mode (${is_mac ? '⌘ Esc' : 'Ctrl+Esc'})`}
57+
title={`Change mode (${is_mac ? '⌘Esc' : 'Ctrl+Esc'})`}
8658
>
8759
{props.mode == MODE.WEB ? MODE.WEB : MODE.API}
8860
</button>
8961
</div>
9062

9163
<div className={styles.header__right}>
92-
<div className={styles.header__right__dropdown}>
64+
<div
65+
className={styles.header__right__dropdown}
66+
ref={dropdown_container_ref}
67+
>
9368
{props.mode == MODE.WEB && (
9469
<UiDropdown
9570
options={Object.entries(web_mode_labels).map(
@@ -101,7 +76,6 @@ export const Header: React.FC<Props> = (props) => {
10176
)}
10277
selected_value={props.web_prompt_type}
10378
on_change={props.on_web_prompt_type_change}
104-
max_width={dropdown_max_width}
10579
menu_max_width="calc(100vw - 52px)"
10680
info={is_narrow_viewport ? undefined : 'prompt type'}
10781
title={
@@ -122,7 +96,6 @@ export const Header: React.FC<Props> = (props) => {
12296
)}
12397
selected_value={props.api_prompt_type}
12498
on_change={props.on_api_prompt_type_change}
125-
max_width={dropdown_max_width}
12699
menu_max_width="calc(100vw - 60px)"
127100
info={is_narrow_viewport ? undefined : 'prompt type'}
128101
title={

‎packages/vscode/src/views/panel/frontend/Main/MainView/components/Header/hooks/use-keyboard-shortcuts.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,29 @@ type UseKeyboardShortcutsParams = {
99
handle_heading_click: () => void
1010
on_web_prompt_type_change: (mode: WebPromptType) => void
1111
on_api_prompt_type_change: (mode: ApiPromptType) => void
12+
on_show_home: () => void
1213
}
1314

1415
export const use_keyboard_shortcuts = ({
1516
mode,
1617
handle_heading_click,
1718
on_web_prompt_type_change,
1819
on_api_prompt_type_change,
20+
on_show_home
1921
}: UseKeyboardShortcutsParams) => {
2022
const is_mac = use_is_mac()
2123

2224
useEffect(() => {
2325
const handle_key_down = (event: KeyboardEvent) => {
2426
if (
27+
event.key == 'Escape' &&
28+
!event.metaKey &&
29+
!event.ctrlKey &&
30+
!event.altKey &&
31+
!event.shiftKey
32+
) {
33+
on_show_home()
34+
} else if (
2535
event.key == 'Escape' &&
2636
((is_mac && event.metaKey) || (!is_mac && event.ctrlKey))
2737
) {
@@ -35,7 +45,7 @@ export const use_keyboard_shortcuts = ({
3545
return () => {
3646
window.removeEventListener('keydown', handle_key_down)
3747
}
38-
}, [is_mac, handle_heading_click])
48+
}, [is_mac, handle_heading_click, on_show_home])
3949

4050
useEffect(() => {
4151
const handle_key_down = (event: KeyboardEvent) => {

0 commit comments

Comments
 (0)