File tree Expand file tree Collapse file tree 3 files changed +31
-12
lines changed Expand file tree Collapse file tree 3 files changed +31
-12
lines changed Original file line number Diff line number Diff line change 331331 onclick ={async () => {
332332 if (! stackId ) return ;
333333 laneState ?.selection .set ({ branchName , codegen: true , previewOpen: true });
334- setTimeout (() => {
335- focusClaudeInput (stackId );
336- }, 100 );
334+ focusClaudeInput (stackId );
337335 }}
338336 />
339337 {/if }
Original file line number Diff line number Diff line change 1- export function focusClaudeInput ( stackId : string ) {
2- // This is a hacky way, but we need the job done until we
3- // can figure out a good way of autofocusing text inputs,
4- // without too many of them firing at the wrong times.
5- const element = document . querySelector ( `[data-id="${ stackId } "] .ContentEditable__root` ) ;
1+ import { sleep } from '$lib/utils/sleep' ;
2+
3+ /**
4+ * Tries to focus the claude input.
5+ *
6+ * This currently operates via a retried selector.
7+ */
8+ export async function focusClaudeInput ( stackId : string ) {
9+ const element = await hackyRetriedSelector ( `[data-id="${ stackId } "] .ContentEditable__root` ) ;
610 if ( element instanceof HTMLElement ) {
711 element ?. focus ( ) ;
812 }
913}
14+
15+ /**
16+ * Tries to find the element at a given selector with a time limit and a
17+ * refreshInterval.
18+ *
19+ * Retried selectors should be the _last_ resort. Prefer anything else.
20+ */
21+ async function hackyRetriedSelector (
22+ selector : string ,
23+ timeLimit = 50 ,
24+ refreshInterval = 1
25+ ) : Promise < Element | undefined > {
26+ let tries = 0 ;
27+ while ( refreshInterval * tries < timeLimit ) {
28+ const element = document . querySelector ( selector ) ;
29+ console . log ( element ) ;
30+ if ( element ) return element ;
31+ await sleep ( refreshInterval ) ;
32+ tries += 1 ;
33+ }
34+ }
Original file line number Diff line number Diff line change 11import { focusClaudeInput } from '$lib/codegen/focusClaudeInput' ;
22import { STACK_SERVICE } from '$lib/stacks/stackService.svelte' ;
33import { UI_STATE } from '$lib/state/uiState.svelte' ;
4- import { sleep } from '$lib/utils/sleep' ;
54import { inject } from '@gitbutler/core/context' ;
65import { untrack } from 'svelte' ;
76import type { Reactive } from '@gitbutler/shared/storeUtils' ;
@@ -23,9 +22,6 @@ export function useCreateAiStack(projectId: Reactive<string>) {
2322 const lane = uiState . lane ( stack . id ) ;
2423 lane . selection . set ( { codegen : true , branchName : stack . heads [ 0 ] ?. name , previewOpen : true } ) ;
2524
26- // Because the ui state is updated asyncly, we need to let some time
27- // pass. This is far from a good solution to this problem.
28- await sleep ( 50 ) ;
2925 focusClaudeInput ( stack . id ) ;
3026 }
3127
You can’t perform that action at this time.
0 commit comments