Keep the open tab in the address
When the person reloads the page or sends its address to a friend, they expect the same tab to be open. Your view cannot change the address of the webapp itself. It can, however, ask the webapp with world.setPath(path) to write a path of your site after ?gui= in that address. Opening that address makes the webapp load your view at that path and your view opens the tab named in the path. The example shows what the address of the webapp would hold for the tab you choose.
import { useState } from 'react';
import { useWorld } from './useWorld.js';
// The fake world needs no account and no network.
const arena = {
name: 'Arena',
profile: { description: 'First to three points wins.' },
now: { text: 'Your move' },
};
const tabs = { board: 'Board', chat: 'Chat', rules: 'Rules' };
// The tab to open first is the one in the address of this page (https://your.site/arena/?tab=rules), or the board.
const asked = new URLSearchParams(location.search).get('tab');
const firstTab = Object.keys(tabs).includes(asked) ? asked : 'board';
export default function App() {
// While building: the fake world. In the webapp: useWorld()
const { world, error } = useWorld({ fake: { world: arena } });
const [tab, setTab] = useState(firstTab);
const [address, setAddress] = useState(null);
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
function choose(name) {
setTab(name);
// Write the tab into the address of the webapp (…?gui=/arena/?tab=chat). A reload, or a shared link, then opens it.
const next = `${location.pathname}?tab=${name}`;
world.setPath(next);
setAddress(next);
}
return (
<>
<nav>
{Object.entries(tabs).map(([name, label]) => (
<button key={name} onClick={() => choose(name)} style={{ fontWeight: name === tab ? 'bold' : 'normal' }}>
{label}
</button>
))}
</nav>
{tab === 'board' && <p>{world.now?.text}</p>}
{tab === 'chat' && <p>Messages: {world.messages.length}</p>}
{tab === 'rules' && <p>{world.profile.description}</p>}
{address && <p>The webapp's address now ends with ?gui={address}</p>}
</>
);
}import { connectToHost } from '@unaiverse/gui-kit';
// The fake world needs no account and no network.
const arena = {
name: 'Arena',
profile: { description: 'First to three points wins.' },
now: { text: 'Your move' },
};
// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: arena } });
const tabs = { board: 'Board', chat: 'Chat', rules: 'Rules' };
// The tab to open first is the one in the address of this page (https://your.site/arena/?tab=rules), or the board.
const asked = new URLSearchParams(location.search).get('tab');
let tab = Object.keys(tabs).includes(asked) ? asked : 'board';
const bar = document.createElement('nav');
const content = document.createElement('p');
const address = document.createElement('p');
document.body.append(bar, content, address);
function choose(name) {
tab = name;
// Write the tab into the address of the webapp (…?gui=/arena/?tab=chat). A reload, or a shared link, then opens it.
const path = `${location.pathname}?tab=${name}`;
world.setPath(path);
address.textContent = `The webapp's address now ends with ?gui=${path}`;
draw(world.get());
}
function draw(node) {
bar.replaceChildren(...Object.entries(tabs).map(([name, label]) => {
const button = document.createElement('button');
button.textContent = label;
button.style.fontWeight = name === tab ? 'bold' : 'normal';
button.onclick = () => choose(name);
return button;
}));
if (tab === 'board') content.textContent = node.now?.text ?? '';
if (tab === 'chat') content.textContent = `Messages: ${node.messages.length}`;
if (tab === 'rules') content.textContent = node.profile.description ?? '';
}
// Called at once, then after every change of the world.
world.subscribe(draw);How it works
world.setPath('/arena/?tab=chat') writes the path after ?gui= in the address of the webapp without adding a step to the history. The back button therefore does not walk back through the tabs. When the webapp later opens an address with ?gui=/arena/?tab=chat, it loads your view at https://your.site/arena/?tab=chat. There location.search holds ?tab=chat and the view opens that tab (or the first one when the tab is unknown).
- The path must be a path of your own site that starts at its root with a single leading
/.setPathignores anything else and resolves withfalse. To work wherever your view lives, the example keeps the path of your page (location.pathname) and adds the tab after?. - Call
setPathwhen the tab changes rather than on every scroll. - With a fake world
setPathresolves withtrueand changes nothing. For a test with the real address, enter the address of your view in the Own view field of a world of yours (Going live). Anhttp://localhostaddress is accepted there.