Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useFocusComposerOnThreadOpen.ts

21 lines827 Bytes

1import { useEffect } from "react";
2​
3/** Lands the caret in the composer a frame after a thread opens. */
4export function useFocusComposerOnThreadOpen(
5 threadId: string | undefined,
6 enabled: boolean,
7 focusComposer: () => void,
8) {
9 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- schedules a frame to focus the composer DOM element when a thread opens with the terminal closed
10 useEffect(() => {
11 if (!threadId || !enabled) return;
12 const frame = window.requestAnimationFrame(() => {
13 // Double-clicking a sidebar row opens its thread and its rename field in one gesture.
14 if (document.activeElement?.matches("[data-sidebar-row] input")) return;
15 focusComposer();
16 });
17 return () => {
18 window.cancelAnimationFrame(frame);
19 };
20 }, [threadId, enabled, focusComposer]);