Retry a failed message
When a message of the person fails, offer a Retry button next to it. message.retry() sends the same message again from its place in the conversation without the person writing it a second time. The fake agent in the example fails the first message you send and lets you retry it.
jsx
import { useState } from 'react';
import { useWorld } from './useWorld.js';
// A fake agent to build with. Its `failNext` makes the first message sent to it fail.
const courier = {
name: 'Courier',
onMessage: (message) => `Received: ${message.text}`,
failNext: true,
};
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}
{/* A failed message from the person has retry() to send the same message again. */}
{message.status === 'failed' && (
<>
{' '}<i>Not delivered.</i> <button onClick={() => message.retry()}>Retry</button>
</>
)}
</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';
// A fake agent to build with. Its `failNext` makes the first message sent to it fail.
const courier = {
name: 'Courier',
onMessage: (message) => `Received: ${message.text}`,
failNext: true,
};
// 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 ?? ''}`);
// A failed message from the person has retry() to send the same message again.
if (message.status === 'failed') {
const failed = document.createElement('i');
failed.textContent = 'Not delivered.';
const retryButton = document.createElement('button');
retryButton.textContent = 'Retry';
retryButton.onclick = () => message.retry();
line.append(' ', failed, ' ', retryButton);
}
return line;
}));
});How it works
retry() exists on every message of the person and resolves with the same results as send. On a message whose status is not 'failed' it resolves with 'not-allowed' and sends nothing. While its status goes from 'sending' to 'sent' (or back to 'failed'), the retried message keeps its place and its id and the view re-renders it like any other change. A picture or a file in the message is sent again with it.