Disable the message box when the person cannot send
The rules of a world decide when the person may send (not during the turn of someone else, not before the game starts) and an agent takes no message until it has connected. canSend combines both conditions and is updated at every change of the world. While it is false, disable the Send button and write the reason in a short line. Press Restart on the example to see the button stay disabled while the agent connects.
jsx
import { useState } from 'react';
import { useWorld } from './useWorld.js';
// A fake agent to build with. Like a real one, it needs a moment to connect.
const helper = {
name: 'Helper',
onMessage: (message) => `Got it: "${message.text}".`,
};
export default function App() {
// While building: the fake agent. In the webapp: useWorld()
const { world: agent, error } = useWorld({ fake: { agent: helper } });
const [text, setText] = useState('');
if (error) return <p>Could not connect ({error.code}).</p>;
if (!agent) return <p>Connecting…</p>;
function handleSubmit(event) {
event.preventDefault();
if (!agent.canSend || !text.trim()) return;
agent.send(text);
setText('');
}
const last = agent.messages.filter((message) => !message.fromMe).at(-1);
return (
<>
<form onSubmit={handleSubmit}>
{/* The field stays on, because the person can write the next message while they wait. */}
<input value={text} onChange={(event) => setText(event.target.value)} placeholder={`Write to ${agent.name}`} />
{/* canSend is true when the rules of the agent (or of the world) let the person send now. */}
<button disabled={!agent.canSend}>Send</button>
</form>
<p>{agent.canSend ? 'You can send now.' : `Wait: ${agent.name} cannot take a message now.`}</p>
<p>{last ? last.text : 'No answer yet.'}</p>
</>
);
}js
import { connectToHost } from '@unaiverse/gui-kit';
// A fake agent to build with. Like a real one, it needs a moment to connect.
const helper = {
name: 'Helper',
onMessage: (message) => `Got it: "${message.text}".`,
};
// While building: the fake agent. In the webapp: const agent = await connectToHost();
const agent = await connectToHost({ fake: { agent: helper } });
const form = document.createElement('form');
const input = document.createElement('input');
input.placeholder = `Write to ${agent.name}`;
const sendButton = document.createElement('button');
sendButton.textContent = 'Send';
form.append(input, sendButton);
const hint = document.createElement('p');
const answer = document.createElement('p');
document.body.append(form, hint, answer);
form.onsubmit = (event) => {
event.preventDefault();
if (!agent.get().canSend || !input.value.trim()) return;
agent.send(input.value);
input.value = '';
};
agent.subscribe((node) => {
// canSend is true when the rules of the agent (or of the world) let the person send now.
// The field stays on, because the person can write the next message while they wait.
sendButton.disabled = !node.canSend;
hint.textContent = node.canSend ? 'You can send now.' : `Wait: ${node.name} cannot take a message now.`;
const last = node.messages.filter((message) => !message.fromMe).at(-1);
answer.textContent = last ? last.text : 'No answer yet.';
});How it works
canSendistruewhen the rules of the world (or of the agent) accept a message from the person now. While a message of the person is pending it isfalseas well.- The view disables only the button and leaves the text field enabled for two reasons: (1) the person can write the next message while waiting and (2) on a phone a disabled field loses the keyboard.
- A
sendcalled anyway is harmless because it resolves with'not-allowed'without sending anything when the message is not accepted. With an agent that is still connecting it first waits up to 20 seconds for the connection. The check in the submit handler only avoids a useless call.
See also
- MessageBox: a message box ready to use.
send.- Messages and questions.