Skip to content

MemberName ​

MemberName replaces the plain from of a message with a button that names the sender. The button shows the name and the role of a member. For a message of the world or a note of its rules it shows the name of the world. A click asks the webapp to show the profile card of that member or of the world over your view. When the world asks not to show senders, MemberName renders nothing.

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

// The component: copy this part.

/**
 * MemberName: who wrote a message, as a button that opens their profile card
 * in the webapp. A member shows with their role and a note of the world with
 * the name of the world. It shows nothing when the world asks not to show who
 * wrote each message.
 * @param {object} props
 * @param {object} props.world - The world, from useWorld.
 * @param {object} props.message - A message of `world.messages`.
 * @param {(isShown: boolean) => void} [props.onOpen] - Called after a click: true when the webapp showed the card.
 */
export function MemberName({ world, message, onOpen }) {
	// Show nothing when the world asks not to show who wrote each message, and show the names when an older webapp gives no `showsSenders`.
	if (world.showsSenders === false) return null;
	if (message.fromMe) return <strong>You</strong>;

	// Who wrote it: a member (`fromId` is an id of `members`), or the world itself, for its notes.
	const member = (world.members ?? []).find((one) => one.id === message.fromId);
	const isWorld = Boolean(message.fromId) && message.fromId === world.id;
	const name = member?.name ?? (isWorld ? world.name : message.from) ?? 'Someone';
	// No id to open (an older webapp gives no `fromId`): show the name only.
	if (!member && !isWorld) return <strong>{name}</strong>;

	const open = async () => {
		// The webapp shows its own card over the view and gives the view only true or false.
		const isShown = await world.openProfile(message.fromId).catch(() => false);
		onOpen?.(isShown);
	};
	return (
		<button type="button" onClick={open} title={`Show the profile of ${name}`}
			style={{ font: 'inherit', fontWeight: 600, padding: 0, border: 0, background: 'none', color: 'inherit', textDecoration: 'underline', cursor: 'pointer' }}>
			{name}
			{member?.role && (
				<span style={{ marginLeft: 6, fontSize: 12, fontWeight: 400, padding: '1px 8px', borderRadius: 9, border: '1px solid rgba(127, 127, 127, 0.4)', display: 'inline-block', textDecoration: 'none' }}>
					{member.role}
				</span>
			)}
		</button>
	);
}

// Using it, on a fake world.

// A fake world to build with: two members, and answers from Otto or from the world itself.
const bookClub = {
	name: 'Book club',
	members: [
		{ name: 'Otto', role: 'host', isMaster: true },
		{ name: 'Mia', role: 'reader' },
	],
	roles: [{ name: 'host' }, { name: 'reader' }],
	// `from` names the member who answers. Without it, the world answers with a note of its rules.
	onMessage: (message) => (message.text === 'hello'
		? { from: 'Otto', text: 'Welcome to the club!' }
		: 'We meet on Friday at six.'),
};

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: bookClub } });
	const [opened, setOpened] = useState('');

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;
	// While building, the fake world shows no card: write the result in the page instead.
	const onOpen = (isShown) => setOpened(isShown ? 'Profile shown: in the webapp, the card opens over the view.' : 'No profile to show.');
	return (
		<div style={{ display: 'grid', gap: 10, maxWidth: 480 }}>
			<div style={{ display: 'flex', gap: 8 }}>
				<button type="button" disabled={!world.canSend} onClick={() => world.send('hello')}>Say hello</button>
				<button type="button" disabled={!world.canSend} onClick={() => world.send('When do we meet?')}>Ask when we meet</button>
			</div>
			{world.messages.map((message) => (
				<p key={message.id} style={{ margin: 0 }}>
					<MemberName world={world} message={message} onOpen={onOpen} />: {message.text}
				</p>
			))}
			<p role="status" style={{ margin: 0, fontSize: 14, opacity: 0.8 }}>{opened}</p>
		</div>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// The component: copy this part.

/**
 * Makes the name of who wrote a message, as a button that opens their
 * profile card in the webapp. A member shows with their role and a note of
 * the world with the name of the world. It returns null when the world asks
 * not to show who wrote each message.
 * @param {object} message - A message of `world.messages`.
 * @param {object} world - The world: the newest one, from `subscribe`.
 * @param {{onOpen?: (isShown: boolean) => void}} [options] - Called after a click: true when the webapp showed the card.
 * @returns {HTMLElement|null} The element, or null when no name is shown.
 */
export function memberName(message, world, { onOpen } = {}) {
	// Show nothing when the world asks not to show who wrote each message, and show the names when an older webapp gives no `showsSenders`.
	if (world.showsSenders === false) return null;
	const plain = document.createElement('strong');
	if (message.fromMe) {
		plain.textContent = 'You';
		return plain;
	}

	// Who wrote it: a member (`fromId` is an id of `members`), or the world itself, for its notes.
	const member = (world.members ?? []).find((one) => one.id === message.fromId);
	const isWorld = Boolean(message.fromId) && message.fromId === world.id;
	const name = member?.name ?? (isWorld ? world.name : message.from) ?? 'Someone';
	// No id to open (an older webapp gives no `fromId`): show the name only.
	if (!member && !isWorld) {
		plain.textContent = name;
		return plain;
	}

	const button = document.createElement('button');
	button.type = 'button';
	button.title = `Show the profile of ${name}`;
	button.textContent = name;
	Object.assign(button.style, { font: 'inherit', fontWeight: '600', padding: '0', border: '0', background: 'none', color: 'inherit', textDecoration: 'underline', cursor: 'pointer' });
	if (member?.role) {
		const role = document.createElement('span');
		role.textContent = member.role;
		Object.assign(role.style, { marginLeft: '6px', fontSize: '12px', fontWeight: '400', padding: '1px 8px', borderRadius: '9px', border: '1px solid rgba(127, 127, 127, 0.4)', display: 'inline-block', textDecoration: 'none' });
		button.append(role);
	}
	button.onclick = async () => {
		// The webapp shows its own card over the view and gives the view only true or false.
		const isShown = await world.openProfile(message.fromId).catch(() => false);
		onOpen?.(isShown);
	};
	return button;
}

// Using it, on a fake world.

// A fake world to build with: two members, and answers from Otto or from the world itself.
const bookClub = {
	name: 'Book club',
	members: [
		{ name: 'Otto', role: 'host', isMaster: true },
		{ name: 'Mia', role: 'reader' },
	],
	roles: [{ name: 'host' }, { name: 'reader' }],
	// `from` names the member who answers. Without it, the world answers with a note of its rules.
	onMessage: (message) => (message.text === 'hello'
		? { from: 'Otto', text: 'Welcome to the club!' }
		: 'We meet on Friday at six.'),
};

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

const page = document.createElement('div');
Object.assign(page.style, { display: 'grid', gap: '10px', maxWidth: '480px' });
const buttons = document.createElement('div');
Object.assign(buttons.style, { display: 'flex', gap: '8px' });
const hello = document.createElement('button');
hello.textContent = 'Say hello';
hello.onclick = () => world.send('hello');
const when = document.createElement('button');
when.textContent = 'Ask when we meet';
when.onclick = () => world.send('When do we meet?');
buttons.append(hello, when);
const list = document.createElement('div');
Object.assign(list.style, { display: 'grid', gap: '10px' });
const opened = document.createElement('p');
opened.setAttribute('role', 'status');
Object.assign(opened.style, { margin: '0', fontSize: '14px', opacity: '0.8' });
page.append(buttons, list, opened);
document.body.append(page);

// While building, the fake world shows no card: write the result in the page instead.
const onOpen = (isShown) => {
	opened.textContent = isShown ? 'Profile shown: in the webapp, the card opens over the view.' : 'No profile to show.';
};

// Draws the messages again when they change.
let shownCount = -1;
world.subscribe((node) => {
	hello.disabled = !node.canSend;
	when.disabled = !node.canSend;
	if (node.messages.length === shownCount) return;
	shownCount = node.messages.length;
	list.replaceChildren(...node.messages.map((message) => {
		const line = document.createElement('p');
		line.style.margin = '0';
		const name = memberName(message, node, { onOpen });
		if (name) line.append(name, ': ');
		line.append(message.text ?? '');
		return line;
	}));
});
Live, on a fake world
Open

The fromId of a message identifies the sender and takes one of three values:

  • the id of a member that MemberName looks up in world.members for the name and the role
  • the id of the world (world.id) for its own messages and the notes of its rules (in the view of an agent the id of the agent)
  • null for the messages of the person (labelled "You" by MemberName).

When fromId is missing (in an older webapp) or matches neither a member nor the world, MemberName renders the name in from as plain text with nothing to click.

A click calls world.openProfile(id). The webapp shows its own card over your view and the Promise resolves true. It resolves false for an id that belongs neither to the world nor to a member and for a member that has no card yet because it is known only by its role (type: 'unknown'). Your view receives nothing from the card. A fake world shows no card even though its openProfile resolves true for the id of the world or of a member. To make the click visible during development the example writes Profile shown in the page.

The messages of the person carry no id on purpose. Your view knows the person only through world.me with the nickname and the fields that the person chose to share on accepting your view.

When the world hides senders. A world with a blind vote or an anonymous review can ask not to show who wrote each message. world.showsSenders is then false and MemberName returns nothing. Your own components should show no names or pictures next to messages either. A showsSenders missing in an older webapp counts as true.

Props ​

PropDefaultWhat it is
world(required)The world or agent returned by useWorld.
message(required)A message of world.messages.
onOpennoneA callback called after a click with true when the webapp showed the card and false otherwise.

Parameters ​

memberName(message, world, options) returns a new element or null when no name is shown. Pass it the node that the current subscribe callback received. An older snapshot of the world lacks the members who have just joined.

ParameterWhat it is
messageA message of world.messages.
worldThe world or agent as the node passed by subscribe.
options.onOpenA callback called after a click with true when the webapp showed the card and false otherwise.

Change it ​

  • Add a picture to the name with an Avatar of the member inside the button.
  • Leave out the role by removing its span.
  • Name the sender once per group of messages. Show the name only when the previous message has a different fromId (Group messages by who sent them).
  • Show more than the card of the webapp in a panel of your own built from the data of the member. The card of the webapp, however, stays the only place with the full profile.

See also ​

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