Skip to content

Open a member's profile ​

The webapp already has a profile card for every member and for the world. world.openProfile(id) shows that card over your view and saves you from building one of your own. In the example a click on a member opens their card and About the club opens the card of the world. Outside the webapp (while you build, for example) no card appears and the example writes on the page what happened instead.

jsx
import { useState } from 'react';
import { useWorld } from './useWorld.js';

// A fake chess club with three members, to build with.
const club = {
	name: 'Chess club',
	members: [
		{ name: 'Ada', role: 'player' },
		{ name: 'Bob', role: 'player' },
		{ name: 'Cleo', role: 'referee' },
	],
};

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: club } });
	const [note, setNote] = useState('');

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;

	// Asks the webapp to show its own profile card of a member, or of the world (by the id of the world), over the view.
	async function showProfile(id, name) {
		try {
			// true when the card is shown, and false for an id of neither the world nor a member.
			// While you build, no card appears, and the page says what happened instead.
			const isShown = await world.openProfile(id);
			setNote(isShown ? `Profile shown: ${name}` : 'Not a member');
		} catch (error) {
			// The code is 'not-allowed-verb' with a webapp older than profile cards.
			setNote(`Could not open it (${error.code}).`);
		}
	}

	return (
		<>
			<button onClick={() => showProfile(world.id, world.name)}>About the club</button>
			<ul>
				{(world.members ?? []).map((member) => (
					<li key={member.id}>
						<button onClick={() => showProfile(member.id, member.name ?? 'Someone')}>{member.name ?? 'Someone'}</button>
						{' '}{member.role}
					</li>
				))}
			</ul>
			<p>{note}</p>
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// A fake chess club with three members, to build with.
const club = {
	name: 'Chess club',
	members: [
		{ name: 'Ada', role: 'player' },
		{ name: 'Bob', role: 'player' },
		{ name: 'Cleo', role: 'referee' },
	],
};

// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: club } });

const about = document.createElement('button');
about.textContent = 'About the club';
const list = document.createElement('ul');
const note = document.createElement('p');
document.body.append(about, list, note);

// Asks the webapp to show its own profile card of a member, or of the world (by the id of the world), over the view.
async function showProfile(id, name) {
	try {
		// true when the card is shown, and false for an id of neither the world nor a member.
		// While you build, no card appears, and the page says what happened instead.
		const isShown = await world.openProfile(id);
		note.textContent = isShown ? `Profile shown: ${name}` : 'Not a member';
	} catch (error) {
		// The code is 'not-allowed-verb' with a webapp older than profile cards.
		note.textContent = `Could not open it (${error.code}).`;
	}
}

about.onclick = () => showProfile(world.id, world.name);

world.subscribe((node) => {
	list.replaceChildren(...(node.members ?? []).map((member) => {
		const item = document.createElement('li');
		const button = document.createElement('button');
		button.textContent = member.name ?? 'Someone';
		button.onclick = () => showProfile(member.id, member.name ?? 'Someone');
		item.append(button, ` ${member.role ?? ''}`);
		return item;
	}));
});
Live, on a fake world
Open

How it works ​

  • openProfile(id) takes the id of the world itself or of a member (from members or from the fromId of a message). It resolves with true when the card is shown and with false for any other id. A member known only by its role (type: 'unknown') has no profile yet and the call resolves with false for its id.
  • The card belongs to the webapp and returns nothing to your view. The person closes it in the webapp.
  • openProfile changes nothing in the world. In a webapp that predates profile cards it rejects with the code 'not-allowed-verb'.
  • Your view cannot open the card of the person because the messages of the person have fromId: null.
  • In a fake world openProfile shows no card and resolves with true for the id of the world or of a member.

See also ​

The UNaIVERSE GUI kit, released under the Apache License 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.