Skip to content

Commit 5c4eb3c

Browse files
committed
Use repeated query selector
1 parent cf03f37 commit 5c4eb3c

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

apps/desktop/src/components/BranchList.svelte

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,7 @@
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}
Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
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+
}

apps/desktop/src/lib/stacks/createAiStack.svelte.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { focusClaudeInput } from '$lib/codegen/focusClaudeInput';
22
import { STACK_SERVICE } from '$lib/stacks/stackService.svelte';
33
import { UI_STATE } from '$lib/state/uiState.svelte';
4-
import { sleep } from '$lib/utils/sleep';
54
import { inject } from '@gitbutler/core/context';
65
import { untrack } from 'svelte';
76
import 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

0 commit comments

Comments
 (0)