Skip to content

Apri il profilo di un membro ​

La webapp ha già una scheda del profilo per ogni membro e per il mondo. world.openProfile(id) mostra quella scheda sopra la tua vista e ti risparmia di costruirne una tua. Nell'esempio un clic su un membro apre la sua scheda e About the club apre la scheda del mondo. Mentre sviluppi fuori dalla webapp non compare nessuna scheda e l'esempio scrive invece sulla pagina che cosa è successo.

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

Come funziona ​

  • openProfile(id) prende l'id del mondo stesso o di un membro (da members o dal fromId di un messaggio). Si risolve con true quando la scheda viene mostrata e con false per qualsiasi altro id. Un membro conosciuto solo per il suo ruolo (type: 'unknown') non ha ancora un profilo e per il suo id la chiamata si risolve con false.
  • La scheda appartiene alla webapp e non restituisce niente alla tua vista. La persona la chiude nella webapp.
  • openProfile non cambia niente nel mondo. In una webapp precedente alle schede del profilo viene rifiutata con il codice 'not-allowed-verb'.
  • La tua vista non può aprire la scheda della persona perché i messaggi della persona hanno fromId: null.
  • In un mondo finto openProfile non mostra nessuna scheda e si risolve con true per l'id del mondo o di un membro.

Vedi anche ​

Il GUI kit di UNaIVERSE, rilasciato con licenza Apache 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.