Show the files of a message
A message can carry files such as a report, a table or a recording. message.files lists them as { name, type, url } objects (the same shape as a file in data) whose url holds the content of the file as a data URI. Render each file as a link that saves it. Files sent by an agent have no name and your view must give them one. The accountant in the example sends you your reports when you ask for them.
jsx
import { useWorld } from './useWorld.js';
// Two small files: a table and a note.
const table = 'month,paid\nJanuary,1200\nFebruary,950\n';
const note = 'Paid: 3 of 4 invoices.';
// A fake accountant answers with the two files, as base64 data URIs.
// As from a real agent, they arrive with no name.
const accountant = {
name: 'Accountant',
onMessage: () => ({
text: 'Here are your reports.',
files: [
{ type: 'text/csv', url: `data:text/csv;base64,${btoa(table)}` },
{ type: 'text/plain', url: `data:text/plain;base64,${btoa(note)}` },
],
}),
};
// On the page, a file shows its name (or words that say it has none) and its type.
function labelOf(file) {
return `${file.name ?? 'A file with no name'} (${file.type ?? 'unknown type'})`;
}
// Each file of a message becomes a link that saves it, or only its name when its content was not kept.
function FileView({ file }) {
// Once the view is loaded again, only the name of a file the person sent is kept.
if (!file.url) return <p>{labelOf(file)}, not kept on this device</p>;
// `download` saves the file under this name, instead of opening it.
return <p><a href={file.url} download={file.name ?? 'file'}>{labelOf(file)}</a></p>;
}
export default function App() {
// While building: the fake agent. In the webapp: useWorld()
const { world: agent, error } = useWorld({ fake: { agent: accountant } });
if (error) return <p>Could not connect ({error.code}).</p>;
if (!agent) return <p>Connecting…</p>;
return (
<>
{agent.messages.map((message) => (
<div key={message.id}>
<p>{message.fromMe ? 'You' : message.from}: {message.text}</p>
{/* An older webapp gives no `files`, and the view then counts on an empty list. */}
{(message.files ?? []).map((file, index) => <FileView key={index} file={file} />)}
</div>
))}
<button onClick={() => agent.send('My reports, please')} disabled={!agent.canSend}>Ask for the reports</button>
</>
);
}js
import { connectToHost } from '@unaiverse/gui-kit';
// Two small files: a table and a note.
const table = 'month,paid\nJanuary,1200\nFebruary,950\n';
const note = 'Paid: 3 of 4 invoices.';
// A fake accountant answers with the two files, as base64 data URIs.
// As from a real agent, they arrive with no name.
const accountant = {
name: 'Accountant',
onMessage: () => ({
text: 'Here are your reports.',
files: [
{ type: 'text/csv', url: `data:text/csv;base64,${btoa(table)}` },
{ type: 'text/plain', url: `data:text/plain;base64,${btoa(note)}` },
],
}),
};
// On the page, a file shows its name (or words that say it has none) and its type.
function labelOf(file) {
return `${file.name ?? 'A file with no name'} (${file.type ?? 'unknown type'})`;
}
// Each file of a message becomes a link that saves it, or only its name when its content was not kept.
function fileView(file) {
const paragraph = document.createElement('p');
if (!file.url) {
// Once the view is loaded again, only the name of a file the person sent is kept.
paragraph.textContent = `${labelOf(file)}, not kept on this device`;
return paragraph;
}
const link = document.createElement('a');
link.href = file.url;
// `download` saves the file under this name, instead of opening it.
link.download = file.name ?? 'file';
link.textContent = labelOf(file);
paragraph.append(link);
return paragraph;
}
// While building: the fake agent. In the webapp: const agent = await connectToHost();
const agent = await connectToHost({ fake: { agent: accountant } });
const list = document.createElement('div');
const ask = document.createElement('button');
ask.textContent = 'Ask for the reports';
ask.onclick = () => agent.send('My reports, please');
document.body.append(list, ask);
agent.subscribe((node) => {
ask.disabled = !node.canSend;
list.replaceChildren(...node.messages.map((message) => {
const item = document.createElement('div');
const line = document.createElement('p');
line.textContent = `${message.fromMe ? 'You' : message.from}: ${message.text ?? ''}`;
item.append(line);
// An older webapp gives no `files`, and the view then counts on an empty list.
for (const file of message.files ?? []) item.append(fileView(file));
return item;
}));
});Live, on a fake worldOpen
How it works
- The link
<a href={file.url} download={file.name ?? 'file'}>saves the file under that name instead of opening it and asks the person for no permission (Downloads). - An agent sends its files with
name: null(GuiFile). For such a filelabelOfrenders "A file with no name" followed by thetype. When you know what the world sends, pick a better name yourself ('report.csv'for'text/csv'). - After the view is loaded again, a file sent by the person keeps only its name. Its
urlis thennulland the example renders the name without a link. - Files come from the person, from an agent or from a member of a world and never from the notes of the rules of a world.
message.files ?? []also covers an older webapp that has nofilesfield.- The answer of a fake agent can carry
files: [{ type, url }]with a data URI as in the example. These files arrive without a name like the files of a real agent.
See also
- FileButton: a button that picks a file and sends it.
- Send a file
- Download a file from data
GuiMessage