Skip to content

world.openProfile(id) ​

Chiede alla webapp di mostrare sopra la tua vista la scheda del profilo di un membro o del mondo stesso.

openProfile(id: string): Promise<boolean>
js
const isShown = await world.openProfile(member.id);

Parametri ​

ParametroTipoChe cos'è
idstringL'id di un membro (da world.members o dal fromId di un messaggio) oppure il world.id del mondo stesso. La vista di un agente passa l'id dell'agente.

Valore restituito ​

Una Promise che si risolve con un boolean:

RisultatoChe cosa vuol dire
trueLa webapp mostra la scheda.
falseNon viene mostrato niente. L'id non appartiene né al mondo né a un membro oppure il membro è noto solo per il suo ruolo (type 'unknown') e non ha ancora un profilo (quindi nemmeno una scheda).

La webapp disegna la scheda sopra la tua vista con il proprio aspetto (un pannello dal basso su un telefono) e la persona la chiude lì. La tua vista non riceve né un evento alla chiusura della scheda né niente del suo contenuto.

La Promise viene rifiutata quando la webapp non può mostrare una scheda (Risoluzione dei problemi).

Uso ​

Aprire la scheda di un membro ​

L'esempio ha un pulsante per il mondo, uno per ogni membro e un ultimo il cui id non appartiene a nessuno e dà false. Come la webapp, un mondo finto si risolve con true per l'id del mondo e per gli id dei membri anche se non mostra nessuna scheda.

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

// A fake world with two members.
const arena = {
	name: 'Arena',
	members: [{ name: 'Ada', role: 'player' }, { name: 'Bo', role: 'player' }],
};

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: arena } });
	const [result, setResult] = useState('Click a name.');

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

	// Asks the webapp to show its profile card for the world or a member over the view.
	// A fake world shows nothing, but it resolves true for the id of the world or of a member.
	async function showProfile(id, name) {
		try {
			const isShown = await world.openProfile(id);
			setResult(isShown ? `The card of ${name} is shown.` : `No card for ${name}.`);
		} catch (profileError) {
			setResult(`Error: ${profileError.code}`);
		}
	}

	// A button for the world, one per member, and one with an id nobody has.
	const everyone = [{ id: world.id, name: world.name }, ...(world.members ?? []), { id: 'nobody', name: 'Someone else' }];
	return (
		<>
			<div>
				{everyone.map((someone) => (
					<button key={someone.id} onClick={() => showProfile(someone.id, someone.name)}>{someone.name ?? 'Someone'}</button>
				))}
			</div>
			<p>{result}</p>
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// A fake world with two members.
const arena = {
	name: 'Arena',
	members: [{ name: 'Ada', role: 'player' }, { name: 'Bo', role: 'player' }],
};

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

const buttons = document.createElement('div');
const result = document.createElement('p');
result.textContent = 'Click a name.';
document.body.append(buttons, result);

// Asks the webapp to show its profile card for the world or a member over the view.
// A fake world shows nothing, but it resolves true for the id of the world or of a member.
async function showProfile(id, name) {
	try {
		const isShown = await world.openProfile(id);
		result.textContent = isShown ? `The card of ${name} is shown.` : `No card for ${name}.`;
	} catch (error) {
		result.textContent = `Error: ${error.code}`;
	}
}

// A button for the world, one per member, and one with an id nobody has.
world.subscribe((node) => {
	const everyone = [{ id: node.id, name: node.name }, ...(node.members ?? []), { id: 'nobody', name: 'Someone else' }];
	buttons.replaceChildren(...everyone.map((someone) => {
		const button = document.createElement('button');
		button.textContent = someone.name ?? 'Someone';
		button.onclick = () => showProfile(someone.id, someone.name);
		return button;
	}));
});
Live, on a fake world
Open

Aprire la scheda di chi ha scritto un messaggio ​

Dato che il fromId di un messaggio è l'id del suo autore, un pulsante con il nome del mittente può aprirne la scheda:

js
function senderButton(message) {
  const button = document.createElement('button');
  button.textContent = message.from;
  // The person's own messages have no id: nothing to open.
  button.disabled = !message.fromId;
  button.onclick = () => world.openProfile(message.fromId);
  return button;
}

Quando showsSenders è false il mondo chiede alle viste di nascondere chi ha scritto ogni messaggio. In quel caso non mostrare nessun nome cliccabile.

Aprire la scheda del mondo ​

L'id del mondo apre la scheda del mondo stesso:

js
aboutButton.onclick = () => world.openProfile(world.id);

Avvertenze ​

  • Aprila solo su richiesta della persona (per esempio con un clic su un nome o su un'immagine). Una scheda che la persona non ha chiesto le è d'intralcio. Non aprirne mai una di tua iniziativa, in un ciclo o al passaggio del puntatore.
  • Non agisce nel mondo. La scheda mostra soltanto il profilo finché la persona non la chiude nella webapp.
  • Non per la persona. La tua vista non può aprire la scheda della persona perché non riceve mai il suo id (i suoi messaggi hanno fromId: null).
  • Un membro noto solo per il suo ruolo non ha una scheda. Finché un membro non condivide il suo profilo il suo type è 'unknown' e il suo nome è il suo ruolo. Per un membro così openProfile si risolve con false esattamente come la vista UNaIVERSE del mondo non apre nessuna scheda per lui. Offri il pulsante solo quando member.type !== 'unknown'.
  • Un mondo finto non mostra nessuna scheda. Il suo openProfile si risolve con true per l'id del mondo o di un membro e con false per qualsiasi altro id.

Risoluzione dei problemi ​

CodiceCausaChe cosa fare
'not-allowed-verb'Questa webapp è più vecchia del tuo kit e non conosce openProfile.Nascondi i pulsanti che aprono le schede o mostra tu quello che sai del membro.
'unavailable'Questa webapp qui non può mostrare una scheda.Prosegui senza la scheda come nella riga precedente.
'failed'La webapp non è riuscita a eseguire la chiamata (raro).Lascia riprovare la persona.
js
async function showCard(id) {
  try {
    await world.openProfile(id);
  } catch (error) {
    if (error.code === 'not-allowed-verb' || error.code === 'unavailable') hideCardButtons();
  }
}

Vedi anche ​

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