Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

Pick a repo: focus Repo URL once its tab has rendered (21f) (#695)

Oleksandr Ivanchenko8a794e487d24
diff --git a/frontend/src/components/ManageTeamModal.tsx b/frontend/src/components/ManageTeamModal.tsx
index 19f420a1..da215bd6 100644
--- a/frontend/src/components/ManageTeamModal.tsx
+++ b/frontend/src/components/ManageTeamModal.tsx
@@ -11,6 +11,17 @@ import { TeamRepoDevLine } from './TeamRepoDevLine';
import { useEscapeLayer } from '../escape';
import { SuggestMembersButton } from './ReadyTeamsEntry';
+/** The workspace tab a team opens on: its own folder or repo, else (A8, no workspace yet) Git Repo for "Pick a repo",
+ * Local Folder for "Pick a folder", or Default. */
+export function seededWorkspaceType(
+ team: Pick<Team, 'workspace_path' | 'repo_url'>,
+ focusWorkspace: boolean | 'folder',
+): 'local' | 'repo' | 'default' {
+ if (team.workspace_path) return 'local';
+ if (team.repo_url) return 'repo';
+ return focusWorkspace === 'folder' ? 'local' : focusWorkspace ? 'repo' : 'default';
+}
+
interface ManageTeamModalProps {
isOpen: boolean;
team: Team | null;
@@ -137,20 +148,9 @@ export const ManageTeamModal: React.FC<ManageTeamModalProps> = ({
setInstructions(team.instructions || '');
setLeadKwantId(team.lead_kwant_id || (kwants[0]?.id ?? ''));
- if (team.workspace_path) {
- setWorkspaceType('local');
- setWorkspacePath(team.workspace_path);
- setRepoUrl('');
- } else if (team.repo_url) {
- setWorkspaceType('repo');
- setWorkspacePath('');
- setRepoUrl(team.repo_url);
- } else {
- // A8: no workspace yet, so "Pick a repo" lands on Git Repo.
- setWorkspaceType(focusWorkspace === 'folder' ? 'local' : focusWorkspace ? 'repo' : 'default');
- setWorkspacePath('');
- setRepoUrl('');
- }
+ setWorkspaceType(seededWorkspaceType(team, focusWorkspace));
+ setWorkspacePath(team.workspace_path || '');
+ setRepoUrl(team.workspace_path ? '' : team.repo_url || '');
const parsedMembers: string[] = Array.isArray(team.member_kwants)
? team.member_kwants
@@ -164,16 +164,23 @@ export const ManageTeamModal: React.FC<ManageTeamModalProps> = ({
}
}, [team, kwants, isOpen, focusWorkspace]);
- // A8: after the fields are seeded, bring Team Workspace into view and focus the selected tab's input.
+ // A8/21f: bring Team Workspace into view and focus the seeded tab's input, once per opening. This waits for the
+ // render where the seeded tab is showing (its input is in the DOM), instead of a frame that could fire before the
+ // seed's state change rendered. Switching tabs by hand afterwards doesn't pull focus back.
const wsRef = useRef<HTMLDivElement>(null);
+ const wsFocusedFor = useRef<string | null>(null);
useEffect(() => {
- if (!isOpen || !team || !focusWorkspace) return;
- const id = requestAnimationFrame(() => {
- wsRef.current?.scrollIntoView?.({ block: 'center' });
- wsRef.current?.querySelector<HTMLInputElement>('input[data-ws-input]')?.focus({ preventScroll: true });
- });
- return () => cancelAnimationFrame(id);
- }, [isOpen, team, focusWorkspace]);
+ if (!isOpen || !team || !focusWorkspace) {
+ wsFocusedFor.current = null;
+ return;
+ }
+ if (wsFocusedFor.current === team.id || workspaceType !== seededWorkspaceType(team, focusWorkspace)) return;
+ const input = wsRef.current?.querySelector<HTMLInputElement>('input[data-ws-input]');
+ if (!input) return;
+ wsFocusedFor.current = team.id;
+ wsRef.current?.scrollIntoView?.({ block: 'center' });
+ input.focus({ preventScroll: true });
+ }, [isOpen, team, focusWorkspace, workspaceType]);
if (!isOpen || !team) return null;
diff --git a/frontend/tests/teamTemplates.test.mjs b/frontend/tests/teamTemplates.test.mjs
index dd80abf1..a49d140d 100644
--- a/frontend/tests/teamTemplates.test.mjs
+++ b/frontend/tests/teamTemplates.test.mjs
@@ -76,6 +76,14 @@ const mount = async (el) => {
await settle();
return { host, root, rerender: async (e) => (root.render(e), await settle()), unmount: () => (root.unmount(), host.remove()) };
};
+// Waits (polling, with a deadline) for focus to land, instead of a fixed number of ticks that load can outrun.
+const focusOn = async (ok, what, ms = 2000) => {
+ const end = Date.now() + ms;
+ while (!ok()) {
+ if (Date.now() > end) assert.fail(`focus never reached ${what}; it is on ${document.activeElement?.outerHTML?.slice(0, 120)}`);
+ await new Promise((r) => setTimeout(r, 5));
+ }
+};
const byText = (root, sel, re) => [...root.querySelectorAll(sel)].find((el) => re.test(el.textContent ?? ''));
const primary = (root) => byText(root, 'button', /^(Create team|Add \d|Add bots|Try again|Pick another folder|Creating)/);
const use = (opts) => {
@@ -510,16 +518,33 @@ test('A8 "Pick a repo": Manage team opens on Team Workspace; Git Repo + repo URL
};
const kwants = [{ id: 'kw_a', name: 'Ada', avatar_sprite: 'architect' }];
const base = { isOpen: true, kwants, onClose() {}, onUpdateTeam: async () => {}, onDeleteTeam: async () => {}, focusWorkspace: true };
- let v = await mount(h(m.ManageTeamModal, { ...base, team: { id: 'team_x', name: 'x', lead_kwant_id: 'kw_a', member_kwants: ['kw_a'] } }));
- await settle(30);
- assert.deepEqual(scrolled.at(-1), [true, 'center']);
- assert.equal(document.activeElement?.getAttribute('aria-label'), 'Repo URL');
- v.unmount();
- v = await mount(h(m.ManageTeamModal, { ...base, team: { id: 'team_y', name: 'y', workspace_path: '~/projects/y', lead_kwant_id: 'kw_a', member_kwants: ['kw_a'] } }));
- await settle(30);
- assert.equal(document.activeElement?.value, '~/projects/y');
- v.unmount();
- window.HTMLElement.prototype.scrollIntoView = orig;
+ const origRaf = [window.requestAnimationFrame, globalThis.requestAnimationFrame];
+ const label = () => document.activeElement?.getAttribute('aria-label');
+ try {
+ const teamX = { id: 'team_x', name: 'x', lead_kwant_id: 'kw_a', member_kwants: ['kw_a'] };
+ let v = await mount(h(m.ManageTeamModal, { ...base, team: teamX }));
+ await focusOn(() => label() === 'Repo URL', 'Repo URL');
+ assert.deepEqual(scrolled.at(-1), [true, 'center']);
+ // Once per opening: picking Local Folder by hand, then Git Repo again, doesn't pull focus back to the input.
+ await click(byText(document.body, 'button', /Local Folder/));
+ await click(byText(document.body, 'button', /Git Repo/));
+ assert.notEqual(label(), 'Repo URL');
+ const scrolls = scrolled.length;
+ await v.rerender(h(m.ManageTeamModal, { ...base, team: { ...teamX } }));
+ assert.equal(scrolled.length, scrolls, 'a refreshed team object is the same opening');
+ v.unmount();
+ v = await mount(h(m.ManageTeamModal, { ...base, team: { id: 'team_y', name: 'y', workspace_path: '~/projects/y', lead_kwant_id: 'kw_a', member_kwants: ['kw_a'] } }));
+ await focusOn(() => document.activeElement?.value === '~/projects/y', 'the folder input');
+ v.unmount();
+ // 21f: a frame that fires before the seeded tab renders (what load did in CI) still ends in Repo URL.
+ window.requestAnimationFrame = globalThis.requestAnimationFrame = (cb) => (cb(0), 0);
+ v = await mount(h(m.ManageTeamModal, { ...base, team: { id: 'team_z', name: 'z', lead_kwant_id: 'kw_a', member_kwants: ['kw_a'] } }));
+ await focusOn(() => label() === 'Repo URL', 'Repo URL with an early frame');
+ v.unmount();
+ } finally {
+ [window.requestAnimationFrame, globalThis.requestAnimationFrame] = origRaf;
+ window.HTMLElement.prototype.scrollIntoView = orig;
+ }
});
test('the sheet closes on Esc and from Close, and not while creating', async () => {