Skip to content

Commit 881712e

Browse files
committed
Refactor settings components to trigger configuration updates on input blur instead of relying on debounced effects
1 parent b5fa3f0 commit 881712e

5 files changed

Lines changed: 72 additions & 138 deletions

File tree

‎packages/ui/src/components/editor/common/Input/Input.tsx‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type Props = {
88
max_width?: number
99
width?: number
1010
on_change: (value: string) => void
11+
on_blur?: () => void
1112
on_key_down?: (e: React.KeyboardEvent<HTMLInputElement>) => void
1213
min?: number
1314
max?: number
@@ -34,13 +35,22 @@ export const Input: React.FC<Props> = (props) => {
3435
class_names.push(styles.invalid)
3536
}
3637

38+
const handle_key_down = (e: React.KeyboardEvent<HTMLInputElement>) => {
39+
if (e.key == 'Enter') {
40+
e.preventDefault()
41+
e.currentTarget.blur()
42+
}
43+
props.on_key_down?.(e)
44+
}
45+
3746
return (
3847
<input
3948
id={props.id}
4049
type={props.type || 'text'}
4150
value={props.value}
4251
onChange={(e) => props.on_change(e.target.value)}
43-
onKeyDown={props.on_key_down}
52+
onBlur={props.on_blur}
53+
onKeyDown={handle_key_down}
4454
className={class_names.join(' ')}
4555
placeholder={props.placeholder}
4656
style={Object.keys(style).length > 0 ? style : undefined}

‎packages/ui/src/components/editor/common/Textarea/Textarea.tsx‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,28 @@ type Props = {
99
min_rows?: number
1010
max_rows?: number
1111
on_change: (value: string) => void
12+
on_blur?: () => void
1213
on_key_down?: (e: React.KeyboardEvent<HTMLTextAreaElement>) => void
1314
onFocus?: (e: React.FocusEvent<HTMLTextAreaElement>) => void
1415
}
1516

1617
export const Textarea = React.forwardRef<HTMLTextAreaElement, Props>(
1718
(props, ref) => {
19+
const handle_key_down = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
20+
if (e.key == 'Enter' && !e.shiftKey) {
21+
e.preventDefault()
22+
e.currentTarget.blur()
23+
}
24+
props.on_key_down?.(e)
25+
}
26+
1827
return (
1928
<TextareaAutosize
2029
id={props.id}
2130
value={props.value}
2231
onChange={(e) => props.on_change(e.target.value)}
23-
onKeyDown={props.on_key_down}
32+
onBlur={props.on_blur}
33+
onKeyDown={handle_key_down}
2434
className={styles.textarea}
2535
placeholder={props.placeholder}
2636
minRows={props.min_rows}

‎packages/vscode/src/views/settings/frontend/Home/Home.tsx‎

Lines changed: 19 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -194,65 +194,16 @@ export const Home: React.FC<Props> = (props) => {
194194
set_edit_context_instructions(props.edit_context_system_instructions || '')
195195
}, [props.edit_context_system_instructions])
196196

197-
useEffect(() => {
198-
const handler = setTimeout(() => {
199-
if (
200-
props.commit_message_instructions !== undefined &&
201-
commit_instructions !== props.commit_message_instructions
202-
) {
203-
props.on_commit_instructions_change(commit_instructions)
204-
}
205-
}, 500)
206-
return () => clearTimeout(handler)
207-
}, [
208-
commit_instructions,
209-
props.on_commit_instructions_change,
210-
props.commit_message_instructions
211-
])
212-
213-
useEffect(() => {
214-
const handler = setTimeout(() => {
215-
if (commit_message_auto_accept_after_str == '') {
216-
if (props.commit_message_auto_accept_after !== null) {
217-
props.on_commit_message_auto_accept_after_change(null)
218-
}
219-
return
220-
}
221-
222-
const num_seconds = parseInt(commit_message_auto_accept_after_str, 10)
223-
if (
224-
!isNaN(num_seconds) &&
225-
num_seconds >= 0 &&
226-
props.commit_message_auto_accept_after !== undefined &&
227-
num_seconds != props.commit_message_auto_accept_after
228-
) {
229-
props.on_commit_message_auto_accept_after_change(num_seconds)
230-
}
231-
}, 500)
232-
return () => clearTimeout(handler)
233-
}, [
234-
commit_message_auto_accept_after_str,
235-
props.on_commit_message_auto_accept_after_change,
236-
props.commit_message_auto_accept_after
237-
])
238-
239-
useEffect(() => {
240-
const handler = setTimeout(() => {
241-
if (
242-
props.edit_context_system_instructions !== undefined &&
243-
edit_context_instructions !== props.edit_context_system_instructions
244-
) {
245-
props.on_edit_context_system_instructions_change(
246-
edit_context_instructions
247-
)
248-
}
249-
}, 500)
250-
return () => clearTimeout(handler)
251-
}, [
252-
edit_context_instructions,
253-
props.on_edit_context_system_instructions_change,
254-
props.edit_context_system_instructions
255-
])
197+
const handle_commit_message_auto_accept_after_blur = () => {
198+
if (commit_message_auto_accept_after_str === '') {
199+
props.on_commit_message_auto_accept_after_change(null)
200+
return
201+
}
202+
const num_seconds = parseInt(commit_message_auto_accept_after_str, 10)
203+
if (!isNaN(num_seconds) && num_seconds >= 0) {
204+
props.on_commit_message_auto_accept_after_change(num_seconds)
205+
}
206+
}
256207

257208
const handle_scroll_to_section = (item_id: NavItem) => {
258209
const section = section_refs.current[item_id]
@@ -372,6 +323,11 @@ export const Home: React.FC<Props> = (props) => {
372323
<Textarea
373324
value={edit_context_instructions}
374325
on_change={set_edit_context_instructions}
326+
on_blur={() =>
327+
props.on_edit_context_system_instructions_change(
328+
edit_context_instructions
329+
)
330+
}
375331
/>
376332
}
377333
/>
@@ -463,6 +419,9 @@ export const Home: React.FC<Props> = (props) => {
463419
<Textarea
464420
value={commit_instructions}
465421
on_change={set_commit_instructions}
422+
on_blur={() =>
423+
props.on_commit_instructions_change(commit_instructions)
424+
}
466425
/>
467426
}
468427
/>
@@ -474,6 +433,7 @@ export const Home: React.FC<Props> = (props) => {
474433
type="number"
475434
value={commit_message_auto_accept_after_str}
476435
on_change={set_commit_message_auto_accept_after_str}
436+
on_blur={handle_commit_message_auto_accept_after_blur}
477437
max_width={60}
478438
/>
479439
}

‎packages/vscode/src/views/settings/frontend/Home/sections/GeneralSection.tsx‎

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -59,60 +59,23 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
5959
}
6060
}, [props.edit_format_instructions])
6161

62-
useEffect(() => {
63-
const handler = setTimeout(() => {
64-
const num_threshold = parseInt(context_size_warning_threshold, 10)
65-
if (
66-
!isNaN(num_threshold) &&
67-
num_threshold >= 0 &&
68-
props.context_size_warning_threshold !== undefined &&
69-
num_threshold != props.context_size_warning_threshold
70-
) {
71-
props.on_context_size_warning_threshold_change(num_threshold)
72-
}
73-
}, 500)
74-
return () => clearTimeout(handler)
75-
}, [
76-
context_size_warning_threshold,
77-
props.on_context_size_warning_threshold_change,
78-
props.context_size_warning_threshold
79-
])
62+
const handle_context_size_warning_threshold_blur = () => {
63+
const num_threshold = parseInt(context_size_warning_threshold, 10)
64+
if (!isNaN(num_threshold) && num_threshold >= 0) {
65+
props.on_context_size_warning_threshold_change(num_threshold)
66+
}
67+
}
8068

81-
useEffect(() => {
82-
const handler = setTimeout(() => {
83-
const num_hours = parseInt(checkpoint_lifespan_str, 10)
84-
if (
85-
!isNaN(num_hours) &&
86-
num_hours > 0 &&
87-
props.checkpoint_lifespan !== undefined &&
88-
num_hours != props.checkpoint_lifespan
89-
) {
90-
props.on_checkpoint_lifespan_change(num_hours)
91-
}
92-
}, 500)
93-
return () => clearTimeout(handler)
94-
}, [
95-
checkpoint_lifespan_str,
96-
props.on_checkpoint_lifespan_change,
97-
props.checkpoint_lifespan
98-
])
69+
const handle_checkpoint_lifespan_blur = () => {
70+
const num_hours = parseInt(checkpoint_lifespan_str, 10)
71+
if (!isNaN(num_hours) && num_hours > 0) {
72+
props.on_checkpoint_lifespan_change(num_hours)
73+
}
74+
}
9975

100-
useEffect(() => {
101-
const handler = setTimeout(() => {
102-
if (
103-
props.edit_format_instructions &&
104-
JSON.stringify(instructions) !==
105-
JSON.stringify(props.edit_format_instructions)
106-
) {
107-
props.on_edit_format_instructions_change(instructions)
108-
}
109-
}, 500)
110-
return () => clearTimeout(handler)
111-
}, [
112-
instructions,
113-
props.on_edit_format_instructions_change,
114-
props.edit_format_instructions
115-
])
76+
const handle_instructions_blur = () => {
77+
props.on_edit_format_instructions_change(instructions)
78+
}
11679

11780
return (
11881
<Section
@@ -139,6 +102,7 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
139102
type="number"
140103
value={context_size_warning_threshold}
141104
on_change={set_context_size_warning_threshold}
105+
on_blur={handle_context_size_warning_threshold_blur}
142106
max_width={100}
143107
/>
144108
}
@@ -151,6 +115,7 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
151115
type="number"
152116
value={checkpoint_lifespan_str}
153117
on_change={set_checkpoint_lifespan_str}
118+
on_blur={handle_checkpoint_lifespan_blur}
154119
max_width={100}
155120
min={1}
156121
/>
@@ -179,6 +144,7 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
179144
on_change={(value) =>
180145
set_instructions((prev) => ({ ...prev, whole: value }))
181146
}
147+
on_blur={handle_instructions_blur}
182148
/>
183149
}
184150
/>
@@ -192,6 +158,7 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
192158
on_change={(value) =>
193159
set_instructions((prev) => ({ ...prev, truncated: value }))
194160
}
161+
on_blur={handle_instructions_blur}
195162
/>
196163
}
197164
/>
@@ -205,6 +172,7 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
205172
on_change={(value) =>
206173
set_instructions((prev) => ({ ...prev, diff: value }))
207174
}
175+
on_blur={handle_instructions_blur}
208176
/>
209177
}
210178
/>

‎packages/vscode/src/views/settings/frontend/Home/sections/PresetsSection.tsx‎

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,16 @@ export const PresetsSection = forwardRef<HTMLDivElement, Props>(
2222
)
2323
}, [props.gemini_user_id])
2424

25-
useEffect(() => {
26-
const handler = setTimeout(() => {
27-
if (gemini_user_id_str == '') {
28-
if (props.gemini_user_id !== null) {
29-
props.on_gemini_user_id_change(null)
30-
}
31-
return
32-
}
33-
34-
const num_id = parseInt(gemini_user_id_str, 10)
35-
if (
36-
!isNaN(num_id) &&
37-
num_id >= 0 &&
38-
props.gemini_user_id !== undefined &&
39-
num_id != props.gemini_user_id
40-
) {
41-
props.on_gemini_user_id_change(num_id)
42-
}
43-
}, 500)
44-
return () => clearTimeout(handler)
45-
}, [
46-
gemini_user_id_str,
47-
props.on_gemini_user_id_change,
48-
props.gemini_user_id
49-
])
25+
const handle_gemini_user_id_blur = () => {
26+
if (gemini_user_id_str === '') {
27+
props.on_gemini_user_id_change(null)
28+
return
29+
}
30+
const num_id = parseInt(gemini_user_id_str, 10)
31+
if (!isNaN(num_id) && num_id >= 0) {
32+
props.on_gemini_user_id_change(num_id)
33+
}
34+
}
5035

5136
return (
5237
<Section
@@ -64,6 +49,7 @@ export const PresetsSection = forwardRef<HTMLDivElement, Props>(
6449
type="number"
6550
value={gemini_user_id_str}
6651
on_change={set_gemini_user_id_str}
52+
on_blur={handle_gemini_user_id_blur}
6753
max_width={60}
6854
/>
6955
}

0 commit comments

Comments
 (0)