Mostra se un messaggio è stato consegnato
La consegna di un messaggio richiede un momento e può fallire. Ogni messaggio della persona ha uno status che segue la sua consegna e che la tua vista può mostrare come una parola accanto al messaggio. Nell'esempio l'agente finto fa fallire apposta il primo messaggio e consegna il secondo.
jsx
import { useState } from 'react';
import { useWorld } from './useWorld.js';
// The fake agent fails the first message sent to it, because of `failNext`.
const courier = {
name: 'Courier',
onMessage: () => 'Received.',
failNext: true,
};
// The `status` of a message from the person is 'sending', 'sent' or 'failed'.
const DELIVERY = { sending: 'Sending…', sent: 'Delivered', failed: 'Not delivered' };
export default function App() {
// While building: the fake agent. In the webapp: useWorld()
const { world: agent, error } = useWorld({ fake: { agent: courier } });
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 (!text.trim()) return;
agent.send(text);
setText('');
}
return (
<>
{agent.messages.map((message) => (
<p key={message.id}>
<b>{message.fromMe ? 'You' : message.from}:</b> {message.text}
{message.fromMe && <small> ({DELIVERY[message.status]})</small>}
</p>
))}
<form onSubmit={handleSubmit}>
<input value={text} onChange={(event) => setText(event.target.value)} placeholder="Message" />
<button disabled={!agent.canSend}>Send</button>
</form>
</>
);
}js
import { connectToHost } from '@unaiverse/gui-kit';
// The fake agent fails the first message sent to it, because of `failNext`.
const courier = {
name: 'Courier',
onMessage: () => 'Received.',
failNext: true,
};
// The `status` of a message from the person is 'sending', 'sent' or 'failed'.
const DELIVERY = { sending: 'Sending…', sent: 'Delivered', failed: 'Not delivered' };
// While building: the fake agent. In the webapp: const agent = await connectToHost();
const agent = await connectToHost({ fake: { agent: courier } });
const list = document.createElement('div');
const form = document.createElement('form');
const input = document.createElement('input');
input.placeholder = 'Message';
const sendButton = document.createElement('button');
sendButton.textContent = 'Send';
form.append(input, sendButton);
document.body.append(list, form);
form.onsubmit = (event) => {
event.preventDefault();
if (!input.value.trim()) return;
agent.send(input.value);
input.value = '';
};
agent.subscribe((node) => {
sendButton.disabled = !node.canSend;
list.replaceChildren(...node.messages.map((message) => {
const line = document.createElement('p');
const sender = document.createElement('b');
sender.textContent = `${message.fromMe ? 'You' : message.from}:`;
line.append(sender, ` ${message.text ?? ''}`);
if (message.fromMe) {
const delivery = document.createElement('small');
delivery.textContent = ` (${DELIVERY[message.status]})`;
line.append(delivery);
}
return line;
}));
});Come funziona
- Solo i messaggi della persona (
fromMe) hanno unostatus. Vale'sending'mentre il messaggio è in corso,'sent'quando è stato consegnato e'failed'quando la consegna non è riuscita. DELIVERYè una tabella che associa una parola a ogni stato. Cambia le parole o mostra un'icona al loro posto.failNext: truefa fallire a un agente o a un mondo finto solo il prossimo messaggio che riceve e ti permette di vedere lo stato di errore mentre sviluppi.message.retry()invia di nuovo un messaggio fallito (Riprova un messaggio fallito).
Vedi anche
GuiMessage: tutti i campi di un messaggio.- Riprova un messaggio fallito.
- Prova gli errori con un mondo finto.