8000 fix(browser): scope navigate events by surfaceId by schroldgames · Pull Request #25 · amirlehmam/wmux · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .claude/settings.local.json

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ release/

# Local Netlify folder
.netlify

# Local Claude Code settings
.claude/settings.local.json
4 changes: 2 additions & 2 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ contextBridge.exposeInMainWorld('wmux', {
},
},
browser: {
navigate: (_surfaceId: string, url: string) => {
navigate: (surfaceId: string, url: string) => {
// Dispatch a custom event that BrowserPane listens for
window.dispatchEvent(new CustomEvent('wmux:browser-navigate', { detail: { url } }));
window.dispatchEvent(new CustomEvent('wmux:browser-navigate', { detail: { url, surfaceId: surfaceId || undefined } }));
},
},
agent: {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export default function App() {
const prevPorts = ws?.ports || [];
// Only auto-navigate if this is a NEW port (not already known)
if (!prevPorts.includes(port)) {
window.wmux?.browser?.navigate?.('', `http://localhost:${port}`);
window.wmux?.browser?.navigate?.(`browser-${currentWs}`, `http://localhost:${port}`);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/renderer/components/Browser/BrowserPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface BrowserPaneProps {
onUrlChange?: (url: string) => void;
}

export default function BrowserPane({ initialUrl = 'https://github.com/amirlehmam/wmux', surfaceId: _surfaceId, onUrlChange }: BrowserPaneProps) {
export default function BrowserPane({ initialUrl = 'https://github.com/amirlehmam/wmux', surfaceId, onUrlChange }: BrowserPaneProps) {
const [url, setUrl] = useState(initialUrl);
const [currentUrl, setCurrentUrl] = useState(initialUrl);
const [isLoading, setIsLoading] = useState(false);
Expand Down Expand Up @@ -87,12 +87,13 @@ export default function BrowserPane({ initialUrl = 'https://github.com/amirlehma
// Listen for programmatic navigation (e.g. auto-navigate on dev server detection)
useEffect(() => {
const handler = (e: Event) => {
const url = (e as CustomEvent).detail?.url;
if (url) navigate(url);
const detail = (e as CustomEvent).detail;
const targetId = detail?.surfaceId;
if (detail?.url && (!targetId || targetId === surfaceId)) navigate(detail.url);
};
window.addEventListener('wmux:browser-navigate', handler);
return () => window.removeEventListener('wmux:browser-navigate', handler);
}, [navigate]);
}, [navigate, surfaceId]);

return (
<div className="browser-pane">
Expand Down
16 changes: 12 additions & 4 deletions src/renderer/utils/open-in-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { splitNode, getAllPaneIds } from '../store/split-utils';
import { PaneId, SplitNode, SurfaceRef, WorkspaceId } from '../../shared/types';
import { v4 as uuid } from 'uuid';

function findLeaf(tree: SplitNode, paneId: PaneId): (SplitNode & { type: 'leaf' }) | null {
if (tree.type === 'leaf') return tree.paneId === paneId ? tree : null;
return findLeaf(tree.children[0], paneId) ?? findLeaf(tree.children[1], paneId);
}

/** Recursively collect all surfaces from a split tree. */
function getAllSurfaces(node: SplitNode): SurfaceRef[] {
if (node.type === 'leaf') return node.surfaces;
Expand Down Expand Up @@ -36,11 +41,11 @@ export function openInWmuxBrowser(url: string, opts?: { forceExternal?: boolean

// Check if a browser surface already exists in this workspace
const allSurfaces = getAllSurfaces(ws.splitTree);
const hasBrowser = allSurfaces.some(s => s.type === 'browser');
const browserSurface = allSurfaces.find(s => s.type === 'browser');

if (hasBrowser) {
if (browserSurface) {
// Browser exists — just navigate
window.dispatchEvent(new CustomEvent('wmux:browser-navigate', { detail: { url } }));
window.dispatchEvent(new CustomEvent('wmux:browser-navigate', { detail: { url, surfaceId: browserSurface.id } }));
return;
}

Expand All @@ -56,9 +61,12 @@ export function openInWmuxBrowser(url: string, opts?: { forceExternal?: boolean
const newTree = splitNode(ws.splitTree, targetPaneId, newPaneId, 'browser', 'horizontal');
state.updateSplitTree(wsId, newTree);

// Resolve the surfaceId of the newly created browser pane from the updated tree
const newSurfaceId = findLeaf(newTree, newPaneId)?.surfaces[0]?.id;

// Wait for React to mount the BrowserPane + webview dom-ready, then navigate
// 600ms covers: React render (~16ms) + webview init (~200-500ms)
setTimeout(() => {
window.dispatchEvent(new CustomEvent('wmux:browser-navigate', { detail: { url } }));
window.dispatchEvent(new CustomEvent('wmux:browser-navigate', { detail: { url, surfaceId: newSurfaceId } }));
}, 600);
}
0