Skip to content

Commit 910c800

Browse files
committed
Remove redundant useEffect hook controlling history enablement to rely solely on the selectionchange listener
1 parent 075d867 commit 910c800

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

‎packages/ui/src/components/editor/panel/PromptField/hooks/use-handlers.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,24 +197,26 @@ export const use_handlers = (
197197
useEffect(() => {
198198
const on_selection_change = () => {
199199
if (document.activeElement === input_ref.current && input_ref.current) {
200+
const selection = window.getSelection()
200201
const pos = get_caret_position_from_div(input_ref.current)
201202
const raw_pos = map_display_pos_to_raw_pos(
202203
pos,
203204
props.value,
204205
props.context_file_paths ?? []
205206
)
206207
raw_caret_pos_ref.current = raw_pos
208+
209+
const is_at_end =
210+
raw_pos == props.value.length && !!selection?.isCollapsed
211+
set_is_history_enabled(is_at_end)
207212
}
208213
}
209214
document.addEventListener('selectionchange', on_selection_change)
215+
on_selection_change()
210216
return () =>
211217
document.removeEventListener('selectionchange', on_selection_change)
212218
}, [props.value, props.context_file_paths, input_ref])
213219

214-
useEffect(() => {
215-
set_is_history_enabled(history_index >= 0 || !props.value)
216-
}, [history_index, props.value])
217-
218220
const update_value = (new_value: string, caret_pos?: number) => {
219221
if (new_value === props.value) return
220222
set_undo_stack((prev) => [

0 commit comments

Comments
 (0)