Skip to content

Commit 3798801

Browse files
committed
Refactor use compacting hook to automatically measure width and remove manual reporting from components
- To make the compacting logic much more robust, we can automate the measurement directly inside the use_compacting hook. Instead of manually adding up hardcoded padding, gap values, and span widths across various components (and reporting them manually), we can let the hook measure the real widths. - use_compacting should not need param [Fragment]
1 parent 35703ed commit 3798801

6 files changed

Lines changed: 51 additions & 111 deletions

File tree

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect, useRef, useLayoutEffect } from 'react'
1+
import { useState, useEffect } from 'react'
22
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'
@@ -63,23 +63,11 @@ export const Home: React.FC<Props> = (props) => {
6363
? active_workspace_root
6464
: roots[0]
6565

66-
const { container_ref, compact_step, report_width } = use_compacting(2)
67-
const left_ref = useRef<HTMLDivElement>(null)
68-
const right_ref = useRef<HTMLDivElement>(null)
66+
const { container_ref, compact_step } = use_compacting()
6967

7068
const discord_label = 'Discord'
7169
const coffee_label = t('header.buy-me-a-coffee')
7270

73-
useLayoutEffect(() => {
74-
if (left_ref.current && right_ref.current) {
75-
const width =
76-
left_ref.current.getBoundingClientRect().width +
77-
right_ref.current.getBoundingClientRect().width +
78-
8
79-
report_width(width, compact_step)
80-
}
81-
}, [compact_step, report_width])
82-
8371
useEffect(() => {
8472
const handle_message = (event: MessageEvent<BackendMessage>) => {
8573
const message = event.data
@@ -105,13 +93,13 @@ export const Home: React.FC<Props> = (props) => {
10593
return (
10694
<>
10795
<div className={styles.header} ref={container_ref}>
108-
<div className={styles['header__left']} ref={left_ref}>
96+
<div className={styles['header__left']}>
10997
<div className={styles['header__home']}>
11098
<span className="codicon codicon-home" />
11199
</div>
112100
<span className={styles['header__text']}>CWC</span>
113101
</div>
114-
<div className={styles['header__right']} ref={right_ref}>
102+
<div className={styles['header__right']}>
115103
<CompactableActionButton
116104
label={discord_label}
117105
href="https://discord.gg/KJySXsrSX5"

‎apps/editor/src/views/prompt/frontend/components/Layout/Footer/Footer.tsx‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect, useContext, useRef, useLayoutEffect } from 'react'
1+
import { useState, useEffect, useContext } from 'react'
22
import styles from './Footer.module.scss'
33
import { use_compacting } from '@shared/hooks'
44
import { LayoutContext } from '../../../contexts/LayoutContext'
@@ -26,19 +26,7 @@ export const Footer: React.FC<Props> = (props) => {
2626
const [is_apply_disabled_temporarily, set_is_apply_disabled_temporarily] =
2727
useState(false)
2828

29-
const { container_ref, compact_step, report_width } = use_compacting(3)
30-
const left_ref = useRef<HTMLDivElement>(null)
31-
const right_ref = useRef<HTMLDivElement>(null)
32-
33-
useLayoutEffect(() => {
34-
if (left_ref.current && right_ref.current) {
35-
const width =
36-
left_ref.current.getBoundingClientRect().width +
37-
right_ref.current.getBoundingClientRect().width +
38-
6
39-
report_width(width, compact_step)
40-
}
41-
}, [compact_step, report_width])
29+
const { container_ref, compact_step } = use_compacting()
4230

4331
useEffect(() => {
4432
set_is_apply_disabled_temporarily(false)
@@ -60,7 +48,7 @@ export const Footer: React.FC<Props> = (props) => {
6048
return (
6149
<>
6250
<div className={styles.footer} ref={container_ref}>
63-
<div ref={left_ref} className={styles.footer__left}>
51+
<div className={styles.footer__left}>
6452
<CompactableActionButton
6553
is_compact={compact_step >= 2}
6654
on_click={props.on_history_click}
@@ -78,7 +66,7 @@ export const Footer: React.FC<Props> = (props) => {
7866
/>
7967
</div>
8068

81-
<div ref={right_ref}>
69+
<div>
8270
<CompactableActionButton
8371
is_compact={compact_step >= 3}
8472
on_click={handle_apply_click}

‎apps/editor/src/views/prompt/frontend/components/recent-donations/RecentDonationsFooter/RecentDonationsFooter.tsx‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useRef, useLayoutEffect } from 'react'
21
import cn from 'classnames'
32
import { use_compacting } from '@shared/hooks'
43
import { Button as UiButton } from '@ui/components/editor/common/Button'
@@ -11,25 +10,12 @@ type Props = {
1110

1211
export const RecentDonationsFooter: React.FC<Props> = (props) => {
1312
const { t } = use_translation()
14-
const { container_ref, compact_step, report_width } = use_compacting(2)
15-
const close_ref = useRef<HTMLSpanElement>(null)
16-
const donation_ref = useRef<HTMLSpanElement>(null)
17-
18-
useLayoutEffect(() => {
19-
if (close_ref.current && donation_ref.current) {
20-
const padding_and_gap = 2 * 24 + 8
21-
const width =
22-
close_ref.current.getBoundingClientRect().width +
23-
donation_ref.current.getBoundingClientRect().width +
24-
padding_and_gap
25-
report_width(width, compact_step)
26-
}
27-
}, [compact_step, report_width])
13+
const { container_ref, compact_step } = use_compacting()
2814

2915
return (
3016
<div className={styles.container} ref={container_ref}>
3117
<UiButton on_click={props.on_close} is_secondary>
32-
<span className={styles['button-content']} ref={close_ref}>
18+
<span className={styles['button-content']}>
3319
{compact_step < 1 && t('recent-donations.close')}
3420
{compact_step >= 1 && (
3521
<span className={cn('codicon', 'codicon-chevron-left')} />
@@ -40,7 +26,7 @@ export const RecentDonationsFooter: React.FC<Props> = (props) => {
4026
url="https://buymeacoffee.com/robertpiosik"
4127
title="buymeacoffee.com/robertpiosik"
4228
>
43-
<span className={styles['button-content']} ref={donation_ref}>
29+
<span className={styles['button-content']}>
4430
{compact_step < 2 && `${t('recent-donations.buy-me-a-coffee')} ↗`}
4531
{compact_step >= 2 && (
4632
<span className={cn('codicon', 'codicon-coffee')} />
Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,72 @@
1-
import { useState, useEffect, useRef, useCallback } from 'react'
1+
import { useState, useEffect, useRef, useLayoutEffect } from 'react'
22

3-
export const use_compacting = (max_compact_step = 4) => {
3+
export const use_compacting = () => {
44
const container_ref = useRef<HTMLDivElement>(null)
55
const [compact_step, set_compact_step] = useState(0)
66
const [thresholds, set_thresholds] = useState<Record<number, number>>({})
77

8-
const report_width = useCallback((width: number, step: number) => {
8+
useLayoutEffect(() => {
9+
if (!container_ref.current) return
10+
const container = container_ref.current
11+
12+
const original_width = container.style.width
13+
const original_max_width = container.style.maxWidth
14+
const original_flex_shrink = container.style.flexShrink
15+
const original_transition = container.style.transition
16+
17+
container.style.transition = 'none'
18+
container.style.width = 'max-content'
19+
container.style.maxWidth = 'none'
20+
container.style.flexShrink = '0'
21+
22+
const width = container.getBoundingClientRect().width
23+
24+
container.style.width = original_width
25+
container.style.maxWidth = original_max_width
26+
container.style.flexShrink = original_flex_shrink
27+
container.style.transition = original_transition
28+
929
set_thresholds((prev) => {
1030
const threshold = Math.ceil(width)
11-
if (prev[step] == threshold) return prev
12-
return { ...prev, [step]: threshold }
31+
if (prev[compact_step] == threshold) return prev
32+
return { ...prev, [compact_step]: threshold }
1333
})
14-
}, [])
34+
}, [compact_step])
1535

1636
useEffect(() => {
1737
const observer = new ResizeObserver((entries) => {
18-
const width = entries[0].contentRect.width
38+
const container = entries[0].target as HTMLElement
39+
const width = container.getBoundingClientRect().width
1940

20-
// Try to expand (reduce compaction)
2141
for (let i = 0; i < compact_step; i++) {
2242
if (thresholds[i] !== undefined && width >= thresholds[i]) {
2343
set_compact_step(i)
2444
return
2545
}
2646
}
2747

28-
// Try to shrink (increase compaction)
2948
if (thresholds[0] !== undefined && width < thresholds[0]) {
3049
if (compact_step == 0) {
3150
set_compact_step(1)
32-
} else if (compact_step < max_compact_step) {
51+
} else {
3352
if (
3453
thresholds[compact_step] !== undefined &&
3554
width < thresholds[compact_step]
3655
) {
37-
set_compact_step(compact_step + 1)
56+
if (thresholds[compact_step] < thresholds[compact_step - 1]) {
57+
set_compact_step(compact_step + 1)
58+
}
3859
}
3960
}
4061
}
4162
})
4263

4364
if (container_ref.current) observer.observe(container_ref.current)
4465
return () => observer.disconnect()
45-
}, [thresholds, compact_step, max_compact_step])
66+
}, [thresholds, compact_step])
4667

4768
return {
4869
container_ref,
49-
compact_step,
50-
report_width
70+
compact_step
5171
}
5272
}

‎packages/ui/src/components/editor/prompt/BrowserExtensionMessage/BrowserExtensionMessage.tsx‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,13 @@
1-
import { useLayoutEffect, useRef } from 'react'
2-
31
import { use_compacting } from '@shared/hooks/use-compacting'
42
import styles from './BrowserExtensionMessage.module.scss'
53

64
export const BrowserExtensionMessage: React.FC = () => {
7-
const { container_ref, compact_step, report_width } = use_compacting(2)
8-
const links_ref = useRef<HTMLDivElement>(null)
9-
10-
useLayoutEffect(() => {
11-
if (links_ref.current) {
12-
let width = 0
13-
const children = links_ref.current.children
14-
for (let i = 0; i < children.length; i++) {
15-
const child = children[i] as HTMLElement
16-
const style = getComputedStyle(child)
17-
width +=
18-
child.getBoundingClientRect().width +
19-
parseFloat(style.marginLeft) +
20-
parseFloat(style.marginRight)
21-
}
22-
report_width(width, compact_step)
23-
}
24-
}, [compact_step, report_width])
5+
const { container_ref, compact_step } = use_compacting()
256

267
return (
278
<div ref={container_ref} className={styles.container}>
289
<div className={styles.heading}>Enable autofill in chatbots</div>
29-
<div ref={links_ref} className={styles.links}>
10+
<div className={styles.links}>
3011
<a href="https://chromewebstore.google.com/detail/autofill-for-code-web-chat/ljookipcanaglfaocjbgdicfbdhhjffp">
3112
{compact_step >= 2 ? 'Chrome' : 'Chrome Web Store'}
3213
</a>

‎packages/ui/src/components/editor/prompt/ResponsePreviewFooter/ResponsePreviewFooter.tsx‎

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useRef, useLayoutEffect } from 'react'
21
import { use_compacting } from '@shared/hooks'
32
import { Button } from '../../common/Button'
43
import styles from './ResponsePreviewFooter.module.scss'
@@ -16,23 +15,7 @@ export const ResponsePreviewFooter: React.FC<Props> = ({
1615
on_accept,
1716
is_accept_disabled
1817
}) => {
19-
const { container_ref, compact_step, report_width } = use_compacting(3)
20-
const back_ref = useRef<HTMLSpanElement>(null)
21-
const reject_ref = useRef<HTMLSpanElement>(null)
22-
const accept_ref = useRef<HTMLSpanElement>(null)
23-
24-
useLayoutEffect(() => {
25-
if (back_ref.current && reject_ref.current && accept_ref.current) {
26-
// Padding & icon (approx 44px per button) + gap (8px * 2)
27-
const padding_and_gap = 3 * 40 + 2 * 8
28-
const width =
29-
back_ref.current.getBoundingClientRect().width +
30-
reject_ref.current.getBoundingClientRect().width +
31-
accept_ref.current.getBoundingClientRect().width +
32-
padding_and_gap
33-
report_width(width, compact_step)
34-
}
35-
}, [compact_step, report_width])
18+
const { container_ref, compact_step } = use_compacting()
3619

3720
return (
3821
<div className={styles.container} ref={container_ref}>
@@ -42,24 +25,18 @@ export const ResponsePreviewFooter: React.FC<Props> = ({
4225
title="Back"
4326
codicon="chevron-left"
4427
>
45-
<span ref={back_ref}>
46-
{compact_step < 1 && <span className={styles.text}>Back</span>}
47-
</span>
28+
{compact_step < 1 && <span className={styles.text}>Back</span>}
4829
</Button>
4930
<Button on_click={on_reject} is_danger title="Reject" codicon="close">
50-
<span ref={reject_ref}>
51-
{compact_step < 2 && <span className={styles.text}>Reject</span>}
52-
</span>
31+
{compact_step < 2 && <span className={styles.text}>Reject</span>}
5332
</Button>
5433
<Button
5534
on_click={on_accept}
5635
disabled={is_accept_disabled}
5736
title="Accept"
5837
codicon="check"
5938
>
60-
<span ref={accept_ref}>
61-
{compact_step < 3 && <span className={styles.text}>Accept</span>}
62-
</span>
39+
{compact_step < 3 && <span className={styles.text}>Accept</span>}
6340
</Button>
6441
</div>
6542
)

0 commit comments

Comments
 (0)