Skip to content

Commit c71e844

Browse files
committed
Localize header tooltips and improve keyboard shortcut state handling for mode changes
- upon making action either changing mode or edit format, the hint dissapears faster in Header than in Prompt Field - don't close after 100ms, only after state change - add missing translations
1 parent 115b601 commit c71e844

4 files changed

Lines changed: 105 additions & 30 deletions

File tree

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ export const Header: React.FC<Props> = (props) => {
6565
<UiIconButton
6666
codicon_icon="chevron-left"
6767
on_click={props.on_show_home}
68-
title="Return (Esc)"
68+
title={`${t('header.return')} (Esc)`}
6969
/>
7070
<button
7171
className={styles['header__left__toggler']}
7272
onClick={handle_heading_click}
73-
title={`Change mode (${is_mac ? '⌥Esc' : 'Alt+Esc'})`}
73+
title={`${t('header.change-mode')} (${is_mac ? '⌥Esc' : 'Alt+Esc'})`}
7474
>
7575
<span>{props.mode == MODE.WEB ? MODE.WEB : MODE.API}</span>
7676
{is_alt_pressed && (
@@ -100,11 +100,9 @@ export const Header: React.FC<Props> = (props) => {
100100
}
101101
menu_max_height={MENU_MAX_HEIGHT}
102102
info={t('header.prompt-type')}
103-
title={
104-
is_mac
105-
? 'Change prompt type (⇧⌥)'
106-
: 'Change prompt type (Shift+Alt)'
107-
}
103+
title={`${t('header.change-prompt-type')} (${
104+
is_mac ? '⇧⌥' : 'Shift+Alt'
105+
})`}
108106
match_button_width
109107
/>
110108
)}
@@ -124,11 +122,9 @@ export const Header: React.FC<Props> = (props) => {
124122
}
125123
menu_max_height={MENU_MAX_HEIGHT}
126124
info={is_narrow_viewport ? undefined : t('header.prompt-type')}
127-
title={
128-
is_mac
129-
? 'Change prompt type (⇧⌥)'
130-
: 'Change prompt type (Shift+Alt)'
131-
}
125+
title={`${t('header.change-prompt-type')} (${
126+
is_mac ? '⇧⌥' : 'Shift+Alt'
127+
})`}
132128
match_button_width
133129
/>
134130
)}

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

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,59 @@ export const use_keyboard_shortcuts = (params: {
1818
const is_mac = use_is_mac()
1919
const [is_alt_pressed, set_is_alt_pressed] = useState(false)
2020
const alt_interrupted_ref = useRef(false)
21+
const is_alt_pressed_raw_ref = useRef(false)
22+
const pending_mode_ref = useRef<Mode | null>(null)
23+
24+
const update_alt_pressed = (val: boolean) => {
25+
is_alt_pressed_raw_ref.current = val
26+
if (pending_mode_ref.current) {
27+
if (val) set_is_alt_pressed(true)
28+
} else {
29+
set_is_alt_pressed(val)
30+
}
31+
}
32+
33+
useEffect(() => {
34+
if (pending_mode_ref.current === params.mode) {
35+
pending_mode_ref.current = null
36+
set_is_alt_pressed(is_alt_pressed_raw_ref.current)
37+
}
38+
}, [params.mode])
2139

2240
useEffect(() => {
2341
const handle_key_down = (event: KeyboardEvent) => {
42+
let next_mode: Mode | undefined
43+
44+
if (
45+
event.key == 'Escape' &&
46+
event.altKey &&
47+
!event.metaKey &&
48+
!event.ctrlKey &&
49+
!event.shiftKey
50+
) {
51+
next_mode = params.mode === MODE.WEB ? MODE.API : MODE.WEB
52+
}
53+
2454
if (
2555
event.key == 'Alt' &&
2656
!event.shiftKey &&
2757
!event.ctrlKey &&
2858
!event.metaKey
2959
) {
3060
if (!alt_interrupted_ref.current) {
31-
set_is_alt_pressed(true)
61+
update_alt_pressed(true)
3262
}
3363
} else {
3464
if (event.altKey) {
3565
alt_interrupted_ref.current = true
3666
}
37-
set_is_alt_pressed(false)
67+
68+
if (next_mode && params.mode !== next_mode) {
69+
pending_mode_ref.current = next_mode
70+
update_alt_pressed(false)
71+
} else {
72+
update_alt_pressed(false)
73+
}
3874
}
3975

4076
if (params.is_disabled) return
@@ -47,13 +83,7 @@ export const use_keyboard_shortcuts = (params: {
4783
!event.shiftKey
4884
) {
4985
params.on_show_home()
50-
} else if (
51-
event.key == 'Escape' &&
52-
event.altKey &&
53-
!event.metaKey &&
54-
!event.ctrlKey &&
55-
!event.shiftKey
56-
) {
86+
} else if (next_mode) {
5787
event.preventDefault()
5888
params.handle_heading_click()
5989
}
@@ -65,7 +95,7 @@ export const use_keyboard_shortcuts = (params: {
6595
} else if (event.key != 'Alt') {
6696
alt_interrupted_ref.current = true
6797
}
68-
set_is_alt_pressed(
98+
update_alt_pressed(
6999
event.altKey &&
70100
!alt_interrupted_ref.current &&
71101
!event.shiftKey &&
@@ -75,7 +105,7 @@ export const use_keyboard_shortcuts = (params: {
75105
}
76106

77107
const handle_blur = () => {
78-
set_is_alt_pressed(false)
108+
update_alt_pressed(false)
79109
alt_interrupted_ref.current = false
80110
}
81111

@@ -102,7 +132,8 @@ export const use_keyboard_shortcuts = (params: {
102132
params.is_disabled,
103133
params.on_show_home,
104134
is_mac,
105-
params.handle_heading_click
135+
params.handle_heading_click,
136+
params.mode
106137
])
107138

108139
useEffect(() => {

‎apps/editor/src/views/prompt/frontend/i18n/translations/header.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,60 @@ export const header = {
1717
hu: 'Prompt típusok',
1818
bg: 'Типове промптове'
1919
},
20+
'header.return': {
21+
en: 'Return',
22+
pl: 'Powrót',
23+
'zh-cn': '返回',
24+
ja: '戻る',
25+
'zh-tw': '返回',
26+
de: 'Zurück',
27+
es: 'Volver',
28+
fr: 'Retour',
29+
'pt-br': 'Retornar',
30+
ru: 'Назад',
31+
ko: '돌아가기',
32+
it: 'Ritorno',
33+
tr: 'Geri Dön',
34+
cs: 'Návrat',
35+
hu: 'Vissza',
36+
bg: 'Връщане'
37+
},
38+
'header.change-mode': {
39+
en: 'Change mode',
40+
pl: 'Zmień tryb',
41+
'zh-cn': '更改模式',
42+
ja: 'モード変更',
43+
'zh-tw': '更改模式',
44+
de: 'Modus ändern',
45+
es: 'Cambiar modo',
46+
fr: 'Changer de mode',
47+
'pt-br': 'Alterar modo',
48+
ru: 'Изменить режим',
49+
ko: '모드 변경',
50+
it: 'Cambia modalità',
51+
tr: 'Modu değiştir',
52+
cs: 'Změnit režim',
53+
hu: 'Mód megváltoztatása',
54+
bg: 'Промяна на режима'
55+
},
56+
'header.change-prompt-type': {
57+
en: 'Change prompt type',
58+
pl: 'Zmień typ promptu',
59+
'zh-cn': '更改提示类型',
60+
ja: 'プロンプトの種類を変更',
61+
'zh-tw': '更改提示類型',
62+
de: 'Prompt-Typ ändern',
63+
es: 'Cambiar tipo de prompt',
64+
fr: 'Changer le type de prompt',
65+
'pt-br': 'Alterar tipo de prompt',
66+
ru: 'Изменить тип промпта',
67+
ko: '프롬프트 유형 변경',
68+
it: 'Cambia tipo di prompt',
69+
tr: 'Prompt türünü değiştir',
70+
cs: 'Změnit typ promptu',
71+
hu: 'Prompt típusának megváltoztatása',
72+
bg: 'Промяна на типа промпт'
73+
},
2074
'header.buy-me-a-coffee': {
2175
en: 'Buy Me A Coffee',
2276
pl: 'Postaw autorowi kawę',

‎packages/ui/src/components/editor/prompt/prompts/PromptField/hooks/use-keyboard-shortcuts.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@ export const use_keyboard_shortcuts = (
6666
if (format && props.edit_format !== format) {
6767
pending_edit_format_ref.current = format
6868
update_alt_pressed(false)
69-
setTimeout(() => {
70-
if (pending_edit_format_ref.current === format) {
71-
pending_edit_format_ref.current = null
72-
set_is_alt_pressed(is_alt_pressed_raw_ref.current)
73-
}
74-
}, 100)
7569
} else {
7670
update_alt_pressed(false)
7771
}

0 commit comments

Comments
 (0)