Skip to content

WorldStatus ​

WorldStatus è un'intestazione per la parte alta della vista con il nome del mondo, lo stato della connessione e il testo che descrive il passo attuale delle regole (now.text). Dice alla persona dove si trova e se quello che vede è in diretta.

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

// The component: copy this part.

/** The words and the colour of the dot for each status of the world. */
const STATUS_LOOK = {
	connecting: { label: 'Connecting…', color: '#F0A54E' },
	online: { label: 'Online', color: '#399D57' },
	offline: { label: 'Offline', color: '#9D9C9A' },
	unreachable: { label: 'Cannot be reached', color: '#E5453A' },
	'not-found': { label: 'Not found', color: '#E5453A' },
	ended: { label: 'Ended', color: '#9D9C9A' },
};

/**
 * WorldStatus: the name of the world, whether it is online, and what is
 * happening now by its rules. Put it at the top of your view.
 * @param {object} props
 * @param {object} props.world - The world, from useWorld.
 * @param {boolean} [props.showNow=true] - Show what is happening now (`world.now.text`).
 */
export function WorldStatus({ world, showNow = true }) {
	const look = STATUS_LOOK[world.status] || { label: world.status, color: '#9D9C9A' };
	const nowText = world.status === 'ended' ? 'This world has ended.' : world.now?.text;

	return (
		<header style={{ display: 'grid', gap: 4, padding: '12px 16px', borderBottom: '1px solid rgba(127, 127, 127, 0.3)' }}>
			<div style={{ display: 'flex', alignItems: 'center', gap: 8, flexWrap: 'wrap' }}>
				<h1 style={{ margin: 0, fontSize: 20, lineHeight: 1.25 }}>{world.name}</h1>
				{world.profile?.isVerified && (
					<span title="Verified by UNaIVERSE" style={{ fontSize: 12, fontWeight: 600, padding: '1px 8px', borderRadius: 9, background: 'rgba(42, 84, 196, 0.15)' }}>
						Verified
					</span>
				)}
				{/* A status region: screen readers say it again when it changes. */}
				<span role="status" style={{ display: 'inline-flex', alignItems: 'center', gap: 6, marginLeft: 'auto', fontSize: 13 }}>
					<span aria-hidden="true" style={{ width: 8, height: 8, borderRadius: '50%', background: look.color }} />
					{look.label}
				</span>
			</div>
			{showNow && nowText && <p style={{ margin: 0, opacity: 0.75 }}>{nowText}</p>}
		</header>
	);
}

// Using it, on a fake world.

// A fake world to build with: no account, no network.
const arena = {
	name: 'Arena',
	profile: { description: 'A card game for four', isVerified: true },
	now: { text: 'Round 2: pick a card' },
};

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: arena } });

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;
	return <WorldStatus world={world} />;
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// The component: copy this part.

/** The words and the colour of the dot for each status of the world. */
const STATUS_LOOK = {
	connecting: { label: 'Connecting…', color: '#F0A54E' },
	online: { label: 'Online', color: '#399D57' },
	offline: { label: 'Offline', color: '#9D9C9A' },
	unreachable: { label: 'Cannot be reached', color: '#E5453A' },
	'not-found': { label: 'Not found', color: '#E5453A' },
	ended: { label: 'Ended', color: '#9D9C9A' },
};

/**
 * Draws into `container` the name of the world, whether it is online and what
 * is happening now by its rules, then again at every change of the world.
 * @param {HTMLElement} container - Where to draw it.
 * @param {object} world - The world, from connectToHost.
 * @param {{showNow?: boolean}} [options] - `showNow: false` hides what is happening now.
 * @returns {() => void} Stops following the world and removes what it drew.
 */
export function worldStatus(container, world, { showNow = true } = {}) {
	// The elements, made once.
	const header = document.createElement('header');
	Object.assign(header.style, { display: 'grid', gap: '4px', padding: '12px 16px', borderBottom: '1px solid rgba(127, 127, 127, 0.3)' });
	const top = document.createElement('div');
	Object.assign(top.style, { display: 'flex', alignItems: 'center', gap: '8px', flexWrap: 'wrap' });
	const title = document.createElement('h1');
	Object.assign(title.style, { margin: '0', fontSize: '20px', lineHeight: '1.25' });
	const verified = document.createElement('span');
	verified.textContent = 'Verified';
	verified.title = 'Verified by UNaIVERSE';
	Object.assign(verified.style, { fontSize: '12px', fontWeight: '600', padding: '1px 8px', borderRadius: '9px', background: 'rgba(42, 84, 196, 0.15)' });
	// A status region: screen readers say it again when it changes.
	const status = document.createElement('span');
	status.setAttribute('role', 'status');
	Object.assign(status.style, { display: 'inline-flex', alignItems: 'center', gap: '6px', marginLeft: 'auto', fontSize: '13px' });
	const dot = document.createElement('span');
	dot.setAttribute('aria-hidden', 'true');
	Object.assign(dot.style, { width: '8px', height: '8px', borderRadius: '50%' });
	const label = document.createElement('span');
	status.append(dot, label);
	const now = document.createElement('p');
	Object.assign(now.style, { margin: '0', opacity: '0.75' });
	top.append(title, verified, status);
	header.append(top, now);
	container.append(header);

	// Called at once, then after every change of the world.
	const stop = world.subscribe((node) => {
		const look = STATUS_LOOK[node.status] || { label: node.status, color: '#9D9C9A' };
		const nowText = node.status === 'ended' ? 'This world has ended.' : node.now?.text;
		title.textContent = node.name ?? '';
		verified.hidden = !node.profile?.isVerified;
		dot.style.background = look.color;
		label.textContent = look.label;
		now.textContent = nowText ?? '';
		now.hidden = !showNow || !nowText;
	});

	return () => {
		stop();
		header.remove();
	};
}

// Using it, on a fake world.

// A fake world to build with: no account, no network.
const arena = {
	name: 'Arena',
	profile: { description: 'A card game for four', isVerified: true },
	now: { text: 'Round 2: pick a card' },
};

// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: arena } });
const place = document.createElement('div');
document.body.append(place);
worldStatus(place, world);
Live, on a fake world
Open

L'intestazione legge tre campi del mondo: (1) name in un h1, (2) status come pallino colorato con un'etichetta e (3) now.text sotto il nome. L'etichetta dice Online mentre la persona è connessa al mondo e Ended dopo che il mondo è finito. Connecting…, Offline, Cannot be reached e Not found corrispondono agli altri valori di status. Quando il mondo è finito, al posto di now.text la riga sotto il nome dice "This world has ended.".

Un mondo verificato da UNaIVERSE (profile.isVerified) ha un badge Verified accanto al nome. Gli screen reader annunciano ogni cambiamento dello stato perché la sua etichetta sta in una regione role="status".

Props ​

PropPredefinitoChe cos'è
world(obbligatorio)Il mondo, da useWorld.
showNowtrueSe mostrare la riga con world.now.text. Con false l'intestazione mostra solo il nome e lo stato.

Parametri ​

worldStatus(container, world, options) disegna l'intestazione dentro container e la ridisegna a ogni cambiamento del mondo.

ParametroChe cos'è
containerL'elemento in cui disegnare (per esempio un div in cima alla pagina).
worldIl mondo, da connectToHost.
options.showNowCome per la prop, false nasconde la riga con world.now.text. Predefinito true.

Restituisce una funzione che annulla l'iscrizione al mondo e rimuove l'intestazione.

Modificalo ​

  • Etichette tue per ogni stato: modifica STATUS_LOOK. Per usare la lingua della persona, scegli le etichette in base a world.webapp.language (Usa la lingua della webapp).
  • La descrizione del mondo: aggiungi world.profile.description sotto il nome. Sono disponibili anche profile.place e profile.owner.
  • Un'intestazione più piccola: usa un h2 al posto dell'h1 oppure passa showNow={false} quando un'altra parte della tua vista mostra già che cosa sta succedendo.

Vedi anche ​

  • LoadingAndErrors: che cosa mostrare prima che il mondo arrivi e quando non può arrivare.
  • TurnIndicator: di chi è il turno in un gioco.
  • Il mondo: tutti i campi che l'intestazione potrebbe mostrare.

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