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;
}));
});How it works
openProfile(id)takes theidof the world itself or of a member (frommembersor from thefromIdof a message). It resolves withtruewhen the card is shown and withfalsefor any other id. A member known only by its role (type: 'unknown') has no profile yet and the call resolves withfalsefor its id.- The card belongs to the webapp and returns nothing to your view. The person closes it in the webapp.
openProfilechanges 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
openProfileshows no card and resolves withtruefor the id of the world or of a member.
See also
openProfile- MemberName: a button with the name of the sender that opens their profile card.
- Show who wrote each message
- Show who is online