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.

Phone layout: keep the browser pane's tab; load the sandbox only when the pane opens

Lin (kwant bot)6aabaf7744f7
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index 83e20273..c0bd83cd 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -1972,15 +1972,21 @@ export const SpaceApp: React.FC<SpaceAppProps> = ({
? `Laya picks who answers, from the ${kwants.length} ${kwants.length === 1 ? 'bot' : 'bots'} in ${space.name}`
: currentTeam?.description || currentChannel?.description || `A team in ${space.name}`;
- // Load container status and screenshot for active team or bot
+ // A different team or bot: forget the last one's page
useEffect(() => {
- const sid = activeSandboxId;
- let cancelled = false;
setScreenshotBase64(null);
setActiveUrl('');
setPageTitle('');
setBrowserError(null);
- if (!sid || sid === 'default') return;
+ }, [activeSandboxId]);
+
+ // Load container status and screenshot once the pane is open. Both start the bot's browser
+ // container, which can take half a minute, and two such requests on every chat opened tie up
+ // the browser's few connections to this server, so the rest of the page loads late.
+ useEffect(() => {
+ const sid = activeSandboxId;
+ let cancelled = false;
+ if (!isVmPaneOpen || !sid || sid === 'default') return;
fetch(`/api/sandbox/${sid}/status`)
.then((r) => (r.ok ? r.json() : null))
@@ -2016,7 +2022,7 @@ export const SpaceApp: React.FC<SpaceAppProps> = ({
}
});
return () => { cancelled = true; };
- }, [activeSandboxId]);
+ }, [activeSandboxId, isVmPaneOpen]);
const handleRefreshVm = async () => {
const sid = activeSandboxId;
diff --git a/frontend/src/components/VirtualMachinePane.tsx b/frontend/src/components/VirtualMachinePane.tsx
index 1cb6f113..ab7736c7 100644
--- a/frontend/src/components/VirtualMachinePane.tsx
+++ b/frontend/src/components/VirtualMachinePane.tsx
@@ -45,6 +45,21 @@ export interface VirtualMachinePaneProps {
approvalsOff?: boolean;
}
+type PaneTab = 'browser' | 'terminal' | 'vault';
+
+// On a phone, going back to the list unmounts the chat while the pane stays open, so the
+// chosen tab lives here, per bot, or it would fall back to Browser on the way back.
+const chosenTabs = new Map<string, PaneTab>();
+
+/** The tab the pane last showed for this bot or team, for when it opens again. */
+export function chosenPaneTab(teamId: string | undefined): PaneTab {
+ return (teamId && chosenTabs.get(teamId)) || 'browser';
+}
+
+export function rememberPaneTab(teamId: string | undefined, tab: PaneTab): void {
+ if (teamId) chosenTabs.set(teamId, tab);
+}
+
export const VirtualMachinePane: React.FC<VirtualMachinePaneProps> = ({
isOpen,
onClose,
@@ -61,7 +76,8 @@ export const VirtualMachinePane: React.FC<VirtualMachinePaneProps> = ({
onNavigate,
approvalsOff = false,
}) => {
- const [activeTab, setActiveTab] = useState<'browser' | 'terminal' | 'vault'>('browser');
+ const [activeTab, setActiveTab] = useState<PaneTab>(() => chosenPaneTab(teamId));
+ useEffect(() => rememberPaneTab(teamId, activeTab), [teamId, activeTab]);
const [inputUrl, setInputUrl] = useState(activeUrl);
// No event fires when the browser catches a login, so a new screenshot or page loads the list again.
const passwords = useSavedPasswords(teamId, `${activeUrl}|${screenshotBase64?.length ?? 0}|${screenshotBase64?.slice(-32) ?? ''}`);
@@ -289,6 +305,7 @@ export const VirtualMachinePane: React.FC<VirtualMachinePaneProps> = ({
playSfx('click');
setActiveTab('browser');
}}
+ aria-pressed={activeTab === 'browser'}
className={`flex-1 py-2 flex items-center justify-center gap-1.5 transition-colors border-b-2 cursor-pointer ${
activeTab === 'browser'
? 'border-sky-500 text-sky-400 bg-sky-500/10'
@@ -304,6 +321,7 @@ export const VirtualMachinePane: React.FC<VirtualMachinePaneProps> = ({
playSfx('click');
setActiveTab('terminal');
}}
+ aria-pressed={activeTab === 'terminal'}
className={`flex-1 py-2 flex items-center justify-center gap-1.5 transition-colors border-b-2 cursor-pointer ${
activeTab === 'terminal'
? 'border-sky-500 text-sky-400 bg-sky-500/10'
@@ -319,6 +337,7 @@ export const VirtualMachinePane: React.FC<VirtualMachinePaneProps> = ({
playSfx('click');
setActiveTab('vault');
}}
+ aria-pressed={activeTab === 'vault'}
className={`flex-1 py-2 flex items-center justify-center gap-1.5 transition-colors border-b-2 cursor-pointer relative ${
activeTab === 'vault'
? 'border-sky-500 text-sky-400 bg-sky-500/10'
diff --git a/frontend/tests/browserPanel.test.mjs b/frontend/tests/browserPanel.test.mjs
index 411bddee..e77d2f87 100644
--- a/frontend/tests/browserPanel.test.mjs
+++ b/frontend/tests/browserPanel.test.mjs
@@ -7,7 +7,7 @@ globalThis.localStorage ??= { getItem: () => null, setItem: () => {} };
const m = await loadBundled(`
export { isBlankPage, browserFooterLabel } from './src/computerMode';
- export { VirtualMachinePane } from './src/components/VirtualMachinePane';
+ export { VirtualMachinePane, chosenPaneTab, rememberPaneTab } from './src/components/VirtualMachinePane';
export { renderToStaticMarkup } from 'react-dom/server.browser';
export { createElement } from 'react';
`);
@@ -51,3 +51,22 @@ test('the footer says where Chrome runs and where you see it', () => {
// Settings not loaded yet: no claim about the user's computer.
assert.equal(m.browserFooterLabel(null, true), 'Browser Ready');
});
+
+// On a phone, Back to the list unmounts the chat while the pane stays open; it must come back on
+// the tab the person chose, not on Browser.
+const pressedTab = (html) => [...html.matchAll(/aria-pressed="true"[^>]*>(?:<svg[\s\S]*?<\/svg>)?<span>([^<]+)<\/span>/g)].map((x) => x[1]);
+
+test('the pane opens on Browser for a bot it has not shown before', () => {
+ assert.equal(m.chosenPaneTab('fresh-bot'), 'browser');
+ assert.equal(m.chosenPaneTab(undefined), 'browser');
+ assert.deepEqual(pressedTab(pane({ teamId: 'fresh-bot' })), ['Browser']);
+});
+
+test('a remounted pane comes back on the tab chosen for that bot', () => {
+ m.rememberPaneTab('bot-a', 'vault');
+ assert.deepEqual(pressedTab(pane({ teamId: 'bot-a' })), ['Logins']);
+ m.rememberPaneTab('bot-a', 'terminal');
+ assert.deepEqual(pressedTab(pane({ teamId: 'bot-a' })), ['Terminal']);
+ // Another bot keeps its own tab
+ assert.deepEqual(pressedTab(pane({ teamId: 'bot-b' })), ['Browser']);
+});