Skip to content

Members ​

Members renders the name, the online status and the role of each member of a world in a list for a side panel. Masters come first and carry a Master label. A line above the list shows the role of the person (world.myRole) when the world gives them one.

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

// The component: copy this part.

/** The tint behind a member's initial, by its type: agents, worlds, and people (no colour). */
const TYPE_TINT = { agent: 'rgba(159, 70, 107, 0.2)', world: 'rgba(42, 84, 196, 0.2)' };

/** Masters first, then by name. */
function byMasterThenName(a, b) {
	if (a.isMaster !== b.isMaster) return a.isMaster ? -1 : 1;
	return (a.name ?? '').localeCompare(b.name ?? '');
}

/**
 * Members: who is in the world, masters first, with their role and whether
 * they are online. It also says the person's own role.
 * @param {object} props
 * @param {object} props.world - The world, from useWorld.
 * @param {string} [props.title='Members'] - The heading of the list.
 */
export function Members({ world, title = 'Members' }) {
	// The view of an agent has no members, because only worlds have them.
	const members = [...(world.members ?? [])].sort(byMasterThenName);
	const roleText = (name) => world.roles?.find((role) => role.name === name)?.description ?? undefined;

	return (
		<section aria-label={title} style={{ display: 'grid', gap: 8 }}>
			<h2 style={{ margin: 0, fontSize: 16 }}>{title} · {members.length}</h2>
			{world.myRole && <p style={{ margin: 0, fontSize: 14, opacity: 0.8 }}>Your role: {world.myRole}</p>}
			{members.length === 0 && <p style={{ margin: 0, opacity: 0.6 }}>No one else is here yet.</p>}
			<ul style={{ listStyle: 'none', margin: 0, padding: 0, display: 'grid', gap: 4 }}>
				{members.map((member) => {
					const isOnline = member.status === 'online';
					return (
						<li key={member.id} style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '6px 4px' }}>
							<span aria-hidden="true" style={{ position: 'relative', flex: 'none', width: 32, height: 32, borderRadius: '50%', display: 'grid', placeItems: 'center', fontWeight: 600, background: TYPE_TINT[member.type] ?? 'rgba(127, 127, 127, 0.2)' }}>
								{(member.name ?? '?').slice(0, 1).toUpperCase()}
								<span style={{ position: 'absolute', right: -1, bottom: -1, width: 10, height: 10, borderRadius: '50%', border: '2px solid Canvas', background: isOnline ? '#399D57' : '#9D9C9A' }} />
							</span>
							<span style={{ flex: 1, minWidth: 0 }}>
								<span style={{ fontWeight: 600 }}>{member.name ?? 'Someone'}</span>
								<span style={{ display: 'block', fontSize: 13, opacity: 0.7 }}>{isOnline ? 'Online' : 'Away'}</span>
							</span>
							{member.isMaster && <span style={{ fontSize: 12, fontWeight: 600, padding: '1px 8px', borderRadius: 9, background: 'rgba(240, 165, 78, 0.25)' }}>Master</span>}
							{member.role && (
								<span title={roleText(member.role)} style={{ fontSize: 12, padding: '1px 8px', borderRadius: 9, border: '1px solid rgba(127, 127, 127, 0.4)' }}>
									{member.role}
								</span>
							)}
						</li>
					);
				})}
			</ul>
		</section>
	);
}

// Using it, on a fake world.

// A fake world to build with: three members, two roles, and the person's role.
const club = {
	name: 'Chess club',
	members: [
		{ name: 'Bob', role: 'player' },
		{ name: 'Cleo', role: 'referee' },
		{ name: 'Ann', role: 'player', isMaster: true },
	],
	roles: [
		{ name: 'player', description: 'Plays the games' },
		{ name: 'referee', description: 'Checks the moves' },
	],
	myRole: 'player',
};

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

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

// The component: copy this part.

/** The tint behind a member's initial, by its type: agents, worlds, and people (no colour). */
const TYPE_TINT = { agent: 'rgba(159, 70, 107, 0.2)', world: 'rgba(42, 84, 196, 0.2)' };

/** Masters first, then by name. */
function byMasterThenName(a, b) {
	if (a.isMaster !== b.isMaster) return a.isMaster ? -1 : 1;
	return (a.name ?? '').localeCompare(b.name ?? '');
}

/**
 * Makes a small rounded label (a role, "Master").
 * @param {string} text - Its words.
 * @param {object} style - Its look.
 * @returns {HTMLElement} The label.
 */
function chip(text, style) {
	const element = document.createElement('span');
	element.textContent = text;
	Object.assign(element.style, { fontSize: '12px', padding: '1px 8px', borderRadius: '9px' }, style);
	return element;
}

/**
 * Makes the row of one member: its initial, name, whether it is online, and its role.
 * @param {object} member - A member of `world.members`.
 * @param {Array<object>} roles - The roles of the world, for the description of the role.
 * @returns {HTMLLIElement} The row.
 */
function memberRow(member, roles) {
	const isOnline = member.status === 'online';
	const row = document.createElement('li');
	Object.assign(row.style, { display: 'flex', alignItems: 'center', gap: '10px', padding: '6px 4px' });

	const initial = document.createElement('span');
	initial.setAttribute('aria-hidden', 'true');
	initial.textContent = (member.name ?? '?').slice(0, 1).toUpperCase();
	Object.assign(initial.style, { position: 'relative', flex: 'none', width: '32px', height: '32px', borderRadius: '50%', display: 'grid', placeItems: 'center', fontWeight: '600', background: TYPE_TINT[member.type] ?? 'rgba(127, 127, 127, 0.2)' });
	const dot = document.createElement('span');
	Object.assign(dot.style, { position: 'absolute', right: '-1px', bottom: '-1px', width: '10px', height: '10px', borderRadius: '50%', border: '2px solid Canvas', background: isOnline ? '#399D57' : '#9D9C9A' });
	initial.append(dot);

	const words = document.createElement('span');
	Object.assign(words.style, { flex: '1', minWidth: '0' });
	const name = document.createElement('span');
	name.textContent = member.name ?? 'Someone';
	name.style.fontWeight = '600';
	const presence = document.createElement('span');
	presence.textContent = isOnline ? 'Online' : 'Away';
	Object.assign(presence.style, { display: 'block', fontSize: '13px', opacity: '0.7' });
	words.append(name, presence);

	row.append(initial, words);
	if (member.isMaster) row.append(chip('Master', { fontWeight: '600', background: 'rgba(240, 165, 78, 0.25)' }));
	if (member.role) {
		const role = chip(member.role, { border: '1px solid rgba(127, 127, 127, 0.4)' });
		role.title = roles.find((item) => item.name === member.role)?.description ?? '';
		row.append(role);
	}
	return row;
}

/**
 * Draws who is in the world into `container`, masters first, with the role
 * of each member and whether they are online, and above them the person's
 * own role. It draws again at every change of the members.
 * @param {HTMLElement} container - Where to draw it.
 * @param {object} world - The world, from connectToHost.
 * @param {{title?: string}} [options] - The heading of the list.
 * @returns {() => void} Stops following the world and removes what it drew.
 */
export function members(container, world, { title = 'Members' } = {}) {
	const section = document.createElement('section');
	section.setAttribute('aria-label', title);
	Object.assign(section.style, { display: 'grid', gap: '8px' });
	const heading = document.createElement('h2');
	Object.assign(heading.style, { margin: '0', fontSize: '16px' });
	const myRole = document.createElement('p');
	Object.assign(myRole.style, { margin: '0', fontSize: '14px', opacity: '0.8' });
	const empty = document.createElement('p');
	empty.textContent = 'No one else is here yet.';
	Object.assign(empty.style, { margin: '0', opacity: '0.6' });
	const list = document.createElement('ul');
	Object.assign(list.style, { listStyle: 'none', margin: '0', padding: '0', display: 'grid', gap: '4px' });
	section.append(heading, myRole, empty, list);
	container.append(section);

	// What the list shows now. The rows are made again only when it changes.
	let drawn = '';
	const stop = world.subscribe((node) => {
		// The view of an agent has no members, because only worlds have them.
		const sorted = [...(node.members ?? [])].sort(byMasterThenName);
		heading.textContent = `${title} · ${sorted.length}`;
		myRole.textContent = node.myRole ? `Your role: ${node.myRole}` : '';
		myRole.hidden = !node.myRole;
		empty.hidden = sorted.length > 0;
		const shown = JSON.stringify(sorted.map((member) => [member.id, member.name, member.status, member.role, member.isMaster]));
		if (shown === drawn) return;
		drawn = shown;
		list.replaceChildren(...sorted.map((member) => memberRow(member, node.roles ?? [])));
	});

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

// Using it, on a fake world.

// A fake world to build with: three members, two roles, and the person's role.
const club = {
	name: 'Chess club',
	members: [
		{ name: 'Bob', role: 'player' },
		{ name: 'Cleo', role: 'referee' },
		{ name: 'Ann', role: 'player', isMaster: true },
	],
	roles: [
		{ name: 'player', description: 'Plays the games' },
		{ name: 'referee', description: 'Checks the moves' },
	],
	myRole: 'player',
};

// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: club } });
const place = document.createElement('div');
place.style.maxWidth = '360px';
document.body.append(place);
members(place, world);
Live, on a fake world
Open

Each entry of world.members has id, name, type, role, isMaster, status and its own data. The roles of the world are in world.roles as { name, description } objects whose description becomes the tooltip of the role label. The view of an agent always gets an empty members array. With an empty array the component shows the text "No one else is here yet." in place of the list (People and roles).

As in Avatar and RoleBadge, the circle of each member shows its initial on the colour that the webapp uses for its type (one colour for agents and one for worlds). People and members of type 'unknown' get a neutral grey. A dot on the circle is green when status is 'online' and grey for 'connecting' and 'offline'.

Props ​

PropDefaultWhat it is
world(required)The world, from useWorld.
title'Members'The heading of the list. The number of members follows it (Members · 3).

Parameters ​

members(container, world, options) draws the list into container and subscribes to the world. The rows are rebuilt only when the id, name, status, role or master flag of a member changes.

ParameterWhat it is
containerThe element to draw into.
worldThe world, from connectToHost.
options.titleThe heading of the list. Default 'Members'.

It returns a function that unsubscribes from the world and removes the list.

Change it ​

  • Group by role: render one list per entry of world.roles with the members whose role equals its name.
  • Only who is online: keep the members whose status is 'online' (Show who is online).
  • The data of each member: the score that Scoreboard ranks is one of the values published by the member in member.data.
  • Pictures and role badges: render each row with Avatar and RoleBadge.

See also ​

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