Skip to content

Commit 969ab35

Browse files
committed
Extract browser connection status logic into a custom hook and share state with header components
- in landscape, should center 'edit' 'ask' vertically, as well as the middle-column elements - in landscape, in header, should keep the back button on top, just center edit&ask (held together) - in landscape, for the header left to have ideally centered edit/ask buttons, add a dummy IconButton below as a space-taking trick - keep the state of connection status by main view in a hook - header should accept props if browser connection status is closed, if so, in target.web, add hidden dummy `apps/editor/src/views/prompt/frontend/Main/MainView/components/BrowserConnectionStatus/BrowserConnectionStatus.tsx` here [Fragment] - instead of [Fragment], place statusbar - add identical below, and remove css-made padding bottom [Fragment] - rename 'is browser connection closed' to 'is browser connection status bar closed' ├── apps │ └── editor │ └── src │ └── views │ └── prompt │ └── frontend │ └── Main │ └── MainView │ ├── MainView.module.scss │ ├── MainView.tsx │ ├── components │ │ ├── BrowserConnectionStatus │ │ │ └── BrowserConnectionStatus.tsx │ │ └── Header │ │ ├── Header.module.scss │ │ ├── Header.tsx │ │ └── index.ts │ └── hooks │ ├── use-browser-connection-status.ts │ ├── use-invocation-counts.ts │ ├── use-keyboard-shortcuts.ts │ ├── use-last-choice-tooltip.ts │ └── use-re-render-on-interval.ts └── packages └── ui └── src └── components └── editor └── prompt └── StatusBar └── StatusBar.tsx
1 parent 0491a23 commit 969ab35

6 files changed

Lines changed: 150 additions & 88 deletions

File tree

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
}
2020

2121
@include landscape {
22-
height: auto;
22+
height: max-content;
23+
min-height: 100%;
2324
}
2425
}
2526

@@ -46,11 +47,7 @@
4647
}
4748

4849
.content {
49-
padding-bottom: 8px;
50-
51-
@include modern-ui {
52-
padding-bottom: 6px;
53-
}
50+
justify-content: center;
5451
}
5552
}
5653

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Scrollable as UiScrollable } from '@ui/components/editor/common/Scrolla
1414
import { BrowserConnectionStatus } from './components/BrowserConnectionStatus'
1515
import { ApiConfiguration, SetupProgress } from '@/views/prompt/types/messages'
1616
import { use_last_choice_tooltip } from './hooks/use-last-choice-tooltip'
17+
import { use_browser_connection_status } from './hooks/use-browser-connection-status'
1718
import { use_keyboard_shortcuts } from './hooks/use-keyboard-shortcuts'
1819
import { Header } from './components/Header'
1920
import { SelectionState } from '@/views/prompt/types/messages'
@@ -202,6 +203,8 @@ export const MainView: React.FC<Props> = (props) => {
202203
is_disabled: props.are_keyboard_shortcuts_disabled
203204
})
204205

206+
const browser_connection = use_browser_connection_status(props.is_connected)
207+
205208
const web_configurations: UiConfigurations.Configuration[] =
206209
props.web_configurations.map((web_configuration, index) => {
207210
const is_unnamed =
@@ -269,6 +272,7 @@ export const MainView: React.FC<Props> = (props) => {
269272
on_api_prompt_type_change={props.on_api_prompt_type_change}
270273
is_alt_pressed={is_alt_pressed}
271274
is_landscape={is_landscape}
275+
is_browser_connection_status_bar_closed={browser_connection.is_closed}
272276
/>
273277
)
274278

@@ -279,6 +283,8 @@ export const MainView: React.FC<Props> = (props) => {
279283
<BrowserConnectionStatus
280284
is_visible={props.target == TARGET.WEB}
281285
is_connected={props.is_connected}
286+
is_closed={browser_connection.is_closed}
287+
on_close={browser_connection.handle_close}
282288
on_install={props.on_install_browser_extension}
283289
translations={{
284290
connected: t('main.browser-connection.connected'),
@@ -482,6 +488,8 @@ export const MainView: React.FC<Props> = (props) => {
482488
}}
483489
/>
484490
)}
491+
492+
{is_landscape && <UiSeparator height={6} />}
485493
</>
486494
)
487495

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

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,23 @@
1-
import React, { useEffect, useState } from 'react'
1+
import React from 'react'
22
import { StatusBar } from '@ui/components/editor/prompt/StatusBar'
33
import { Separator as UiSeparator } from '@ui/components/editor/prompt/Separator'
44

55
type Props = {
66
is_connected: boolean
77
is_visible: boolean
8+
is_closed: boolean
89
translations: {
910
connected: string
1011
not_connected: string
1112
install: string
1213
hide: string
1314
}
1415
on_install: () => void
16+
on_close: () => void
1517
}
1618

1719
export const BrowserConnectionStatus: React.FC<Props> = (props) => {
18-
const [is_closed, setIsClosed] = useState(false)
19-
const [has_been_closed, setHasBeenClosed] = useState(false)
20-
21-
useEffect(() => {
22-
if (!props.is_connected) {
23-
setIsClosed(false)
24-
}
25-
}, [props.is_connected])
26-
27-
useEffect(() => {
28-
if (props.is_connected && !is_closed && has_been_closed) {
29-
const timer = setTimeout(() => {
30-
setIsClosed(true)
31-
}, 3000)
32-
return () => clearTimeout(timer)
33-
}
34-
}, [props.is_connected, is_closed, has_been_closed])
35-
36-
if (is_closed || !props.is_visible) {
20+
if (props.is_closed || !props.is_visible) {
3721
return null
3822
}
3923

@@ -52,10 +36,7 @@ export const BrowserConnectionStatus: React.FC<Props> = (props) => {
5236
icon: 'codicon-close-small',
5337
label: props.translations.hide,
5438
title: props.translations.hide,
55-
on_click: () => {
56-
setIsClosed(true)
57-
setHasBeenClosed(true)
58-
}
39+
on_click: props.on_close
5940
})
6041
}
6142

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
}
1313

1414
@include landscape {
15-
height: auto;
16-
align-items: flex-start;
15+
height: 100%;
16+
align-items: center;
1717
padding: unset;
18-
margin: 6px 0 0 12px;
18+
margin: 0 0 0 12px;
1919

2020
@include modern-ui {
2121
padding: unset;
22-
margin: 6px 0 0 10px;
22+
margin: 0 0 0 10px;
2323
}
2424
}
2525

@@ -28,15 +28,27 @@
2828
align-items: center;
2929
padding: 4px 0;
3030
gap: 4px;
31+
height: 100%;
3132

3233
@include landscape {
3334
flex-direction: column;
3435
align-items: flex-start;
35-
padding: 0;
36+
padding: 6px 0;
37+
}
38+
}
39+
40+
&__back {
41+
display: flex;
42+
}
3643

37-
> :first-child {
38-
order: 1;
39-
}
44+
&__types {
45+
display: flex;
46+
align-items: center;
47+
gap: 4px;
48+
49+
@include landscape {
50+
flex-direction: column;
51+
margin: auto 0;
4052
}
4153
}
4254
}

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

Lines changed: 83 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
web_prompt_type_labels
1111
} from '../../prompt-type-labels'
1212
import { use_translation } from '@/views/prompt/frontend/i18n/use-translation'
13+
import { StatusBar as UiStatusBar } from '@ui/components/editor/prompt/StatusBar'
1314

1415
type Props = {
1516
target: Target
@@ -20,6 +21,7 @@ type Props = {
2021
on_api_prompt_type_change: (prompt_type: ApiPromptType) => void
2122
is_alt_pressed: boolean
2223
is_landscape: boolean
24+
is_browser_connection_status_bar_closed: boolean
2325
}
2426

2527
export const Header: React.FC<Props> = (props) => {
@@ -31,57 +33,88 @@ export const Header: React.FC<Props> = (props) => {
3133
return (
3234
<div className={styles.header} ref={container_ref}>
3335
<div className={styles.header__left}>
34-
<UiIconButton
35-
codicon_icon="chevron-left"
36-
on_click={props.on_show_home}
37-
title={`${t('header.return')} (Esc)`}
38-
/>
39-
{props.target == TARGET.WEB && (
40-
<>
41-
<UiKeycapWrapper char={props.is_alt_pressed ? 'E' : undefined}>
42-
<UiIconAccentButton
43-
label={web_prompt_type_labels['edit-files']}
44-
icon="edit-sparkle"
45-
is_active={props.web_prompt_type == 'edit-files'}
46-
active_color="blue"
47-
is_compact={
48-
!props.is_landscape &&
49-
(props.web_prompt_type == 'edit-files'
50-
? compact_step >= 2
51-
: compact_step >= 1)
52-
}
53-
on_click={() => props.on_web_prompt_type_change('edit-files')}
54-
/>
55-
</UiKeycapWrapper>
56-
<UiKeycapWrapper char={props.is_alt_pressed ? 'A' : undefined}>
57-
<UiIconAccentButton
58-
label={web_prompt_type_labels['ask-about-files']}
59-
icon="chat-sparkle"
60-
is_active={props.web_prompt_type == 'ask-about-files'}
61-
active_color="purple"
62-
is_compact={
63-
!props.is_landscape &&
64-
(props.web_prompt_type == 'ask-about-files'
65-
? compact_step >= 2
66-
: compact_step >= 1)
67-
}
68-
on_click={() =>
69-
props.on_web_prompt_type_change('ask-about-files')
70-
}
71-
/>
72-
</UiKeycapWrapper>
73-
</>
74-
)}
75-
{props.target == TARGET.API && (
76-
<UiIconAccentButton
77-
label={api_prompt_type_labels['edit-files']}
78-
icon="edit-sparkle"
79-
is_active={props.api_prompt_type == 'edit-files'}
80-
active_color="blue"
81-
is_compact={!props.is_landscape && compact_step >= 1}
82-
on_click={() => props.on_api_prompt_type_change('edit-files')}
36+
<div className={styles.header__back}>
37+
<UiIconButton
38+
codicon_icon="chevron-left"
39+
on_click={props.on_show_home}
40+
title={`${t('header.return')} (Esc)`}
8341
/>
84-
)}
42+
</div>
43+
44+
<div className={styles.header__types}>
45+
{props.target == TARGET.WEB && (
46+
<>
47+
<UiKeycapWrapper char={props.is_alt_pressed ? 'E' : undefined}>
48+
<UiIconAccentButton
49+
label={web_prompt_type_labels['edit-files']}
50+
icon="edit-sparkle"
51+
is_active={props.web_prompt_type == 'edit-files'}
52+
active_color="blue"
53+
is_compact={
54+
!props.is_landscape &&
55+
(props.web_prompt_type == 'edit-files'
56+
? compact_step >= 2
57+
: compact_step >= 1)
58+
}
59+
on_click={() => props.on_web_prompt_type_change('edit-files')}
60+
/>
61+
</UiKeycapWrapper>
62+
<UiKeycapWrapper char={props.is_alt_pressed ? 'A' : undefined}>
63+
<UiIconAccentButton
64+
label={web_prompt_type_labels['ask-about-files']}
65+
icon="chat-sparkle"
66+
is_active={props.web_prompt_type == 'ask-about-files'}
67+
active_color="purple"
68+
is_compact={
69+
!props.is_landscape &&
70+
(props.web_prompt_type == 'ask-about-files'
71+
? compact_step >= 2
72+
: compact_step >= 1)
73+
}
74+
on_click={() =>
75+
props.on_web_prompt_type_change('ask-about-files')
76+
}
77+
/>
78+
</UiKeycapWrapper>
79+
{props.is_landscape && (
80+
<div
81+
style={{
82+
visibility: 'hidden',
83+
pointerEvents: 'none',
84+
width: 0
85+
}}
86+
>
87+
<UiIconButton codicon_icon="chevron-left" />
88+
{props.is_browser_connection_status_bar_closed && (
89+
<UiStatusBar
90+
icon="codicon-debug-disconnect"
91+
label=""
92+
actions={[
93+
{
94+
id: 'install',
95+
icon: 'codicon-add',
96+
label: '',
97+
title: '',
98+
on_click: () => {}
99+
}
100+
]}
101+
/>
102+
)}
103+
</div>
104+
)}
105+
</>
106+
)}
107+
{props.target == TARGET.API && (
108+
<UiIconAccentButton
109+
label={api_prompt_type_labels['edit-files']}
110+
icon="edit-sparkle"
111+
is_active={props.api_prompt_type == 'edit-files'}
112+
active_color="blue"
113+
is_compact={!props.is_landscape && compact_step >= 1}
114+
on_click={() => props.on_api_prompt_type_change('edit-files')}
115+
/>
116+
)}
117+
</div>
85118
</div>
86119
</div>
87120
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useEffect, useState } from 'react'
2+
3+
export const use_browser_connection_status = (is_connected: boolean) => {
4+
const [is_closed, set_is_closed] = useState(false)
5+
const [has_been_closed, set_has_been_closed] = useState(false)
6+
7+
useEffect(() => {
8+
if (!is_connected) {
9+
set_is_closed(false)
10+
}
11+
}, [is_connected])
12+
13+
useEffect(() => {
14+
if (is_connected && !is_closed && has_been_closed) {
15+
const timer = setTimeout(() => {
16+
set_is_closed(true)
17+
}, 2000)
18+
return () => clearTimeout(timer)
19+
}
20+
}, [is_connected, is_closed, has_been_closed])
21+
22+
const handle_close = () => {
23+
set_is_closed(true)
24+
set_has_been_closed(true)
25+
}
26+
27+
return {
28+
is_closed,
29+
handle_close
30+
}
31+
}

0 commit comments

Comments
 (0)