Skip to content

Commit 03d4d25

Browse files
committed
Replace ModeToggler and custom keycap implementations with a reusable KeycapWrapper component
- the border should have the same color as active tab background (update code) - while in home, pressing alt+1 should open 'web', pressing alt+2 should open 'api'. - in `apps/editor/src/views/prompt/frontend/Home/hooks` create appropriate 'use keyboard shortcuts' hook - when main is active, replace the 'alt+esc' shortcut with: 'alt+1' activating 'web', when 'api' is active, and 'alt+2' activating 'api', when 'web' is active - should use 'small wave' when copacted - on 'mode button' display keycaps [1] [2], when appropriate - extract keycap to a separate component that accept children and renders the keycap over it (as a wrapper) - use keycap wrapper over 'back' button, use '1' when in 'api' (change to 'web'), and '2', when in 'web' (change to 'api') ├── apps │ └── editor │ └── src │ └── views │ └── prompt │ └── frontend │ ├── Home │ │ ├── Home.module.scss │ │ ├── Home.tsx │ │ ├── hooks │ │ │ ├── use-has-scrolled-past-mode-button.ts │ │ │ ├── use-keyboard-shortcuts.ts │ │ │ └── use-tasks.ts │ │ └── index.ts │ ├── Main │ │ ├── Main.tsx │ │ └── MainView │ │ ├── MainView.module.scss │ │ ├── MainView.tsx │ │ ├── components │ │ │ └── Header │ │ │ ├── Header.module.scss │ │ │ ├── Header.tsx │ │ │ ├── hooks │ │ │ │ └── use-keyboard-shortcuts.ts │ │ │ └── index.ts │ │ └── hooks │ │ ├── use-invocation-counts.ts │ │ ├── use-last-choice-tooltip.ts │ │ └── use-re-render-on-interval.ts │ └── Prompt.tsx └── packages └── ui └── src └── components └── editor └── prompt ├── KeycapWrapper │ ├── KeycapWrapper.module.scss │ ├── KeycapWrapper.stories.tsx │ ├── KeycapWrapper.tsx │ └── index.ts ├── ModeButton │ ├── ModeButton.module.scss │ ├── ModeButton.stories.tsx │ ├── ModeButton.tsx │ └── index.ts ├── PromptTypeButton │ ├── PromptTypeButton.module.scss │ ├── PromptTypeButton.stories.tsx │ ├── PromptTypeButton.tsx │ └── index.ts └── Tabs ├── Tabs.module.scss ├── Tabs.tsx └── index.ts
1 parent fb4de9f commit 03d4d25

17 files changed

Lines changed: 282 additions & 277 deletions

File tree

‎apps/editor/src/views/prompt/frontend/Home/Home.tsx‎

Lines changed: 85 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import styles from './Home.module.scss'
33
import { Scrollable as UiScrollable } from '@ui/components/editor/common/Scrollable'
44
import { Tabs as UiTabs } from '@ui/components/editor/prompt/Tabs'
55
import { ModeButton as UiModeButton } from '@ui/components/editor/prompt/ModeButton'
6+
import { KeycapWrapper as UiKeycapWrapper } from '@ui/components/editor/prompt/KeycapWrapper'
67
import cn from 'classnames'
78
import { post_message } from '../utils/post-message'
89
import { BackendMessage } from '@/views/prompt/types/messages'
@@ -17,6 +18,7 @@ import { use_tasks } from './hooks/use-tasks'
1718
import { use_has_scrolled_past_mode_button } from './hooks/use-has-scrolled-past-mode-button'
1819
import { use_compacting } from '@shared/hooks'
1920
import { MODE } from '@/views/prompt/types/main-view-mode'
21+
import { use_keyboard_shortcuts } from './hooks/use-keyboard-shortcuts'
2022

2123
type Props = {
2224
vscode: any
@@ -65,6 +67,9 @@ export const Home: React.FC<Props> = (props) => {
6567

6668
const header_modes_ref = useRef<HTMLDivElement>(null)
6769

70+
const [is_alt_pressed, set_is_alt_pressed] = useState(false)
71+
const alt_interrupted_ref = useRef(false)
72+
6873
const discord_label = 'Discord'
6974
const coffee_label = t('header.buy-me-a-coffee')
7075

@@ -80,15 +85,61 @@ export const Home: React.FC<Props> = (props) => {
8085
}, [])
8186

8287
useEffect(() => {
83-
const handle_mouse_up = (event: MouseEvent) => {
84-
if (props.is_active && event.button == 4) {
85-
props.on_go_forward()
88+
const handle_key_down = (event: KeyboardEvent) => {
89+
if (
90+
event.key == 'Alt' &&
91+
!event.shiftKey &&
92+
!event.ctrlKey &&
93+
!event.metaKey
94+
) {
95+
if (!alt_interrupted_ref.current) {
96+
set_is_alt_pressed(true)
97+
}
98+
} else {
99+
if (event.altKey) {
100+
alt_interrupted_ref.current = true
101+
}
102+
set_is_alt_pressed(false)
103+
}
104+
}
105+
106+
const handle_key_up = (event: KeyboardEvent) => {
107+
if (!event.altKey) {
108+
alt_interrupted_ref.current = false
109+
} else if (event.key != 'Alt') {
110+
alt_interrupted_ref.current = true
86111
}
112+
set_is_alt_pressed(
113+
event.altKey &&
114+
!alt_interrupted_ref.current &&
115+
!event.shiftKey &&
116+
!event.ctrlKey &&
117+
!event.metaKey
118+
)
119+
}
120+
121+
const handle_blur = () => {
122+
set_is_alt_pressed(false)
123+
alt_interrupted_ref.current = false
124+
}
125+
126+
window.addEventListener('keydown', handle_key_down)
127+
window.addEventListener('keyup', handle_key_up)
128+
window.addEventListener('blur', handle_blur)
129+
130+
return () => {
131+
window.removeEventListener('keydown', handle_key_down)
132+
window.removeEventListener('keyup', handle_key_up)
133+
window.removeEventListener('blur', handle_blur)
87134
}
135+
}, [])
88136

89-
window.addEventListener('mouseup', handle_mouse_up)
90-
return () => window.removeEventListener('mouseup', handle_mouse_up)
91-
}, [props.is_active, props.on_go_forward])
137+
use_keyboard_shortcuts({
138+
is_active: props.is_active,
139+
on_go_forward: props.on_go_forward,
140+
on_chatbots_click: props.on_chatbots_click,
141+
on_api_calls_click: props.on_api_calls_click
142+
})
92143

93144
return (
94145
<>
@@ -127,17 +178,21 @@ export const Home: React.FC<Props> = (props) => {
127178
[styles['header__modes--visible']]: has_scrolled_past_mode_button
128179
})}
129180
>
130-
<UiModeButton
131-
label={MODE.WEB}
132-
on_click={props.on_chatbots_click}
133-
is_compact
134-
/>
181+
<UiKeycapWrapper char={is_alt_pressed ? '1' : undefined} full_width>
182+
<UiModeButton
183+
label={MODE.WEB}
184+
on_click={props.on_chatbots_click}
185+
is_compact
186+
/>
187+
</UiKeycapWrapper>
135188
<div className={styles['header__modes-divider']} />
136-
<UiModeButton
137-
label={MODE.API}
138-
on_click={props.on_api_calls_click}
139-
is_compact
140-
/>
189+
<UiKeycapWrapper char={is_alt_pressed ? '2' : undefined} full_width>
190+
<UiModeButton
191+
label={MODE.API}
192+
on_click={props.on_api_calls_click}
193+
is_compact
194+
/>
195+
</UiKeycapWrapper>
141196
</div>
142197
</div>
143198

@@ -166,11 +221,21 @@ export const Home: React.FC<Props> = (props) => {
166221

167222
<div className={styles.inner__mode} ref={mode_ref}>
168223
<AsciiArtEffect />
169-
<UiModeButton label={MODE.WEB} on_click={props.on_chatbots_click} />
170-
<UiModeButton label={MODE.API} on_click={props.on_api_calls_click} />
224+
<UiKeycapWrapper char={is_alt_pressed ? '1' : undefined} full_width>
225+
<UiModeButton
226+
label={MODE.WEB}
227+
on_click={props.on_chatbots_click}
228+
/>
229+
</UiKeycapWrapper>
230+
<UiKeycapWrapper char={is_alt_pressed ? '2' : undefined} full_width>
231+
<UiModeButton
232+
label={MODE.API}
233+
on_click={props.on_api_calls_click}
234+
/>
235+
</UiKeycapWrapper>
171236
</div>
172237

173-
<UiSeparator height={8} />
238+
<UiSeparator height={12} />
174239

175240
<UiTabs
176241
tabs={[{ id: 'tasks', label: t('home.tasks') }]}
@@ -210,7 +275,7 @@ export const Home: React.FC<Props> = (props) => {
210275
}
211276
/>
212277

213-
<UiSeparator height={6} />
278+
<UiSeparator height={4} />
214279

215280
{roots.length == 0 && (
216281
<div className={styles.inner__empty}>{t('home.tasks.empty')}</div>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { useEffect } from 'react'
2+
3+
type Params = {
4+
is_active: boolean
5+
on_go_forward: () => void
6+
on_chatbots_click: () => void
7+
on_api_calls_click: () => void
8+
}
9+
10+
export const use_keyboard_shortcuts = (params: Params) => {
11+
useEffect(() => {
12+
const handle_mouse_up = (event: MouseEvent) => {
13+
if (params.is_active && event.button == 4) {
14+
params.on_go_forward()
15+
}
16+
}
17+
18+
const handle_key_down = (event: KeyboardEvent) => {
19+
if (!params.is_active) return
20+
21+
if (event.altKey && !event.shiftKey && !event.metaKey && !event.ctrlKey) {
22+
if (event.code == 'Digit1') {
23+
event.preventDefault()
24+
params.on_chatbots_click()
25+
} else if (event.code == 'Digit2') {
26+
event.preventDefault()
27+
params.on_api_calls_click()
28+
}
29+
}
30+
}
31+
32+
window.addEventListener('mouseup', handle_mouse_up)
33+
window.addEventListener('keydown', handle_key_down)
34+
return () => {
35+
window.removeEventListener('mouseup', handle_mouse_up)
36+
window.removeEventListener('keydown', handle_key_down)
37+
}
38+
}, [
39+
params.is_active,
40+
params.on_go_forward,
41+
params.on_chatbots_click,
42+
params.on_api_calls_click
43+
])
44+
}

‎apps/editor/src/views/prompt/frontend/Main/MainView/components/Header/Header.module.scss‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515
display: flex;
1616
align-items: center;
1717
padding: 4px 0;
18-
gap: 6px;
19-
}
20-
21-
&__prompt-types {
22-
display: flex;
23-
align-items: center;
24-
gap: 2px;
18+
gap: 4px;
2519
}
2620
}

‎apps/editor/src/views/prompt/frontend/Main/MainView/components/Header/Header.tsx‎

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { MODE, Mode } from '@/views/prompt/types/main-view-mode'
22
import { use_compacting } from '@shared/hooks'
33
import { ApiPromptType, WebPromptType } from '@shared/types/prompt-types'
44
import { PromptTypeButton as UiPromptTypeButton } from '@ui/components/editor/prompt/PromptTypeButton'
5+
import { KeycapWrapper as UiKeycapWrapper } from '@ui/components/editor/prompt/KeycapWrapper'
56
import { IconButton as UiIconButton } from '@ui/components/editor/common/IconButton'
6-
import { ModeToggler as UiModeToggler } from '@ui/components/editor/prompt/ModeToggler'
77
import styles from './Header.module.scss'
88
import {
99
api_prompt_type_labels,
@@ -39,52 +39,50 @@ export const Header: React.FC<Props> = (props) => {
3939
return (
4040
<div className={styles.header} ref={container_ref}>
4141
<div className={styles.header__left}>
42-
<UiIconButton
43-
codicon_icon="chevron-left"
44-
on_click={props.on_show_home}
45-
title={`${t('header.return')} (Esc)`}
46-
/>
47-
<UiModeToggler
48-
mode={props.mode == MODE.WEB ? MODE.WEB : MODE.API}
49-
alt_mode={props.mode == MODE.WEB ? MODE.API : MODE.WEB}
50-
is_alt_pressed={is_alt_pressed}
51-
on_toggle={() =>
52-
props.on_mode_change(
53-
props.mode == MODE.WEB ? MODE.API : MODE.WEB
54-
)
42+
<UiKeycapWrapper
43+
char={
44+
is_alt_pressed ? (props.mode == MODE.API ? '1' : '2') : undefined
5545
}
56-
/>
46+
>
47+
<UiIconButton
48+
codicon_icon="chevron-left"
49+
on_click={props.on_show_home}
50+
title={`${t('header.return')} (Esc)`}
51+
/>
52+
</UiKeycapWrapper>
5753
{props.mode == MODE.WEB && (
58-
<div className={styles['header__prompt-types']}>
59-
<UiPromptTypeButton
60-
label={web_prompt_type_labels['edit-files']}
61-
icon="edit-sparkle"
62-
is_active={props.web_prompt_type == 'edit-files'}
63-
active_color="blue"
64-
is_compact={
65-
props.web_prompt_type == 'edit-files'
66-
? compact_step >= 2
67-
: compact_step >= 1
68-
}
69-
keycap_char={is_alt_pressed ? 'E' : undefined}
70-
on_click={() => props.on_web_prompt_type_change('edit-files')}
71-
/>
72-
<UiPromptTypeButton
73-
label={web_prompt_type_labels['ask-about-files']}
74-
icon="chat-sparkle"
75-
is_active={props.web_prompt_type == 'ask-about-files'}
76-
active_color="orange"
77-
is_compact={
78-
props.web_prompt_type == 'ask-about-files'
79-
? compact_step >= 2
80-
: compact_step >= 1
81-
}
82-
keycap_char={is_alt_pressed ? 'A' : undefined}
83-
on_click={() =>
84-
props.on_web_prompt_type_change('ask-about-files')
85-
}
86-
/>
87-
</div>
54+
<>
55+
<UiKeycapWrapper char={is_alt_pressed ? 'E' : undefined}>
56+
<UiPromptTypeButton
57+
label={web_prompt_type_labels['edit-files']}
58+
icon="edit-sparkle"
59+
is_active={props.web_prompt_type == 'edit-files'}
60+
active_color="blue"
61+
is_compact={
62+
props.web_prompt_type == 'edit-files'
63+
? compact_step >= 2
64+
: compact_step >= 1
65+
}
66+
on_click={() => props.on_web_prompt_type_change('edit-files')}
67+
/>
68+
</UiKeycapWrapper>
69+
<UiKeycapWrapper char={is_alt_pressed ? 'A' : undefined}>
70+
<UiPromptTypeButton
71+
label={web_prompt_type_labels['ask-about-files']}
72+
icon="chat-sparkle"
73+
is_active={props.web_prompt_type == 'ask-about-files'}
74+
active_color="orange"
75+
is_compact={
76+
props.web_prompt_type == 'ask-about-files'
77+
? compact_step >= 2
78+
: compact_step >= 1
79+
}
80+
on_click={() =>
81+
props.on_web_prompt_type_change('ask-about-files')
82+
}
83+
/>
84+
</UiKeycapWrapper>
85+
</>
8886
)}
8987
{props.mode == MODE.API && (
9088
<UiPromptTypeButton

‎apps/editor/src/views/prompt/frontend/Main/MainView/components/Header/hooks/use-keyboard-shortcuts.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,14 @@ export const use_keyboard_shortcuts = (params: {
117117
return
118118
}
119119

120-
if (event.code == 'Escape') {
120+
if (event.code == 'Digit1' && params.mode == MODE.API) {
121121
event.preventDefault()
122-
params.on_mode_change(
123-
params.mode == MODE.WEB ? MODE.API : MODE.WEB
124-
)
122+
params.on_mode_change(MODE.WEB)
123+
}
124+
125+
if (event.code == 'Digit2' && params.mode == MODE.WEB) {
126+
event.preventDefault()
127+
params.on_mode_change(MODE.API)
125128
}
126129
}
127130

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.wrapper {
2+
position: relative;
3+
display: flex;
4+
5+
&--full-width {
6+
flex: 1;
7+
width: 100%;
8+
}
9+
}
10+
11+
.keycap {
12+
position: absolute;
13+
top: 50%;
14+
left: 50%;
15+
transform: translate(-50%, -50%);
16+
background-color: var(--vscode-sideBar-background);
17+
color: var(--vscode-foreground);
18+
border: 1px solid var(--cwc-border-color-dimmed);
19+
padding: 2px 4px 3px 4px;
20+
border-radius: 4px;
21+
font-size: 11px;
22+
font-family: var(--vscode-font-family);
23+
line-height: 1;
24+
pointer-events: none;
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { KeycapWrapper } from './KeycapWrapper'
2+
3+
export default {
4+
component: KeycapWrapper
5+
}
6+
7+
export const Default = () => (
8+
<KeycapWrapper char="A">
9+
<button style={{ padding: '10px 20px' }}>Button with Keycap</button>
10+
</KeycapWrapper>
11+
)
12+
13+
export const WithoutChar = () => (
14+
<KeycapWrapper>
15+
<button style={{ padding: '10px 20px' }}>Normal Button</button>
16+
</KeycapWrapper>
17+
)
18+
19+
export const FullWidth = () => (
20+
<div style={{ display: 'flex', width: '300px', gap: '10px' }}>
21+
<KeycapWrapper char="1" full_width>
22+
<button style={{ width: '100%', padding: '10px' }}>Stretch</button>
23+
</KeycapWrapper>
24+
<KeycapWrapper char="2" full_width>
25+
<button style={{ width: '100%', padding: '10px' }}>Stretch</button>
26+
</KeycapWrapper>
27+
</div>
28+
)

0 commit comments

Comments
 (0)