Open your view at a place
A link can take the person straight to a place in your view such as a room, a product or a step of a game. The address of the webapp carries a path of your site after ?gui= and the webapp loads your view at that path. Your page reads the place from its address at start and checks it against the world in case an old link names a place that no longer exists. The live example opens at the hall. To open it at the garden, press Open above it and add ?room=garden to its address.
import { useWorld } from './useWorld.js';
// The fake museum keeps its rooms in its data.
const museum = {
name: 'Museum',
data: {
rooms: {
hall: 'The hall: tickets, maps and the cloakroom.',
garden: 'The garden: statues around a fountain.',
library: 'The library: old books and globes.',
},
},
};
// The place to open, read from the address of this page. A webapp link ending with
// ?gui=/?room=garden loads this page at https://your.site/?room=garden.
const wanted = new URLSearchParams(location.search).get('room');
export default function App() {
// While building: the fake world. In the webapp: useWorld()
const { world, error } = useWorld({ fake: { world: museum } });
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
const rooms = world.data.rooms;
// Look for the room among those the world lists, because an old link can name one that is gone.
const isThere = wanted !== null && Object.hasOwn(rooms, wanted);
return (
<>
<h2>{world.name}</h2>
<p>{rooms[isThere ? wanted : 'hall']}</p>
{wanted !== null && !isThere && <p>There is no room "{wanted}": here is the hall.</p>}
</>
);
}import { connectToHost } from '@unaiverse/gui-kit';
// The fake museum keeps its rooms in its data.
const museum = {
name: 'Museum',
data: {
rooms: {
hall: 'The hall: tickets, maps and the cloakroom.',
garden: 'The garden: statues around a fountain.',
library: 'The library: old books and globes.',
},
},
};
// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: museum } });
// The place to open, read from the address of this page. A webapp link ending with
// ?gui=/?room=garden loads this page at https://your.site/?room=garden.
const wanted = new URLSearchParams(location.search).get('room');
const title = document.createElement('h2');
const room = document.createElement('p');
const note = document.createElement('p');
document.body.append(title, room, note);
// Called at once, then after every change of the world.
world.subscribe((node) => {
const rooms = node.data.rooms;
// Look for the room among those the world lists, because an old link can name one that is gone.
const isThere = wanted !== null && Object.hasOwn(rooms, wanted);
title.textContent = node.name;
room.textContent = rooms[isThere ? wanted : 'hall'];
note.textContent = wanted !== null && !isThere ? `There is no room "${wanted}": here is the hall.` : '';
});How it works
When the address of the webapp holds ?gui=/?room=garden, the webapp loads your view at https://your.site/?room=garden and your page reads location.search like any web page. The world lists the places that exist (here data.rooms) and the example checks the wanted place in subscribe once the world is there. For a place that is gone it opens one that always exists and tells the person why.
rooms[wanted]also matches inherited names such asconstructorwhileObject.hasOwn(rooms, wanted)looks only among the rooms.- A path such as
/rooms/gardenin place of a query works if your server answers every such path with the page of your view. - Calling
world.setPathwhenever the person moves keeps the place in the address and produces such links (Keep the open tab in the address).