Skip to content

Commit 6aab185

Browse files
authored
improve demo file UX (#1390)
1 parent ecf37c4 commit 6aab185

File tree

1 file changed

+60
-9
lines changed

1 file changed

+60
-9
lines changed

examples/demo/demo.ts

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const appActions = {
8181
const file = ($('file') as HTMLInputElement).files?.[0]!;
8282
currentRoom?.localParticipant.sendFile(file, {
8383
mimeType: file.type,
84-
topic: 'welcome',
84+
topic: 'files',
8585
onProgress: (progress) => console.log('sending file, progress', Math.ceil(progress * 100)),
8686
});
8787
},
@@ -278,18 +278,69 @@ const appActions = {
278278
const info = reader.info;
279279

280280
appendLog(`started to receive a file called "${info.name}" from ${participant?.identity}`);
281+
282+
const progressContainer = document.createElement('div');
283+
progressContainer.style.margin = '10px 0';
284+
const progressLabel = document.createElement('div');
285+
progressLabel.innerText = `Receiving "${info.name}" from ${participant?.identity}...`;
286+
const progressBar = document.createElement('progress');
287+
progressBar.max = 100;
288+
progressBar.value = 0;
289+
progressBar.style.width = '100%';
290+
291+
progressContainer.appendChild(progressLabel);
292+
progressContainer.appendChild(progressBar);
293+
$('chat-area').after(progressContainer);
294+
295+
appendLog(`Started receiving file "${info.name}" from ${participant?.identity}`);
296+
281297
reader.onProgress = (progress) => {
282298
console.log(`"progress ${progress ? (progress * 100).toFixed(0) : 'undefined'}%`);
299+
300+
if (progress) {
301+
progressBar.value = progress * 100;
302+
progressLabel.innerText = `Receiving "${info.name}" from ${participant?.identity} (${(progress * 100).toFixed(0)}%)`;
303+
}
283304
};
305+
284306
const result = new Blob(await reader.readAll(), { type: info.mimeType });
285-
appendLog(`completely received file called "${info.name}" from ${participant?.identity}`);
286-
const downloadLink = URL.createObjectURL(result);
287-
const linkEl = document.createElement('a');
288-
linkEl.href = downloadLink;
289-
linkEl.innerText = info.name;
290-
linkEl.setAttribute('download', info.name);
291-
document.body.append(linkEl);
292-
}, 'welcome');
307+
appendLog(`Completely received file "${info.name}" from ${participant?.identity}`);
308+
309+
progressContainer.remove();
310+
311+
if (info.mimeType.startsWith('image/')) {
312+
// Embed images directly in HTML
313+
const imgContainer = document.createElement('div');
314+
imgContainer.style.margin = '10px 0';
315+
imgContainer.style.padding = '10px';
316+
317+
const img = document.createElement('img');
318+
img.style.maxWidth = '300px';
319+
img.style.maxHeight = '300px';
320+
img.src = URL.createObjectURL(result);
321+
322+
const downloadLink = document.createElement('a');
323+
downloadLink.href = img.src;
324+
downloadLink.innerText = `Download ${info.name}`;
325+
downloadLink.setAttribute('download', info.name);
326+
downloadLink.style.display = 'block';
327+
downloadLink.style.marginTop = '5px';
328+
329+
imgContainer.appendChild(img);
330+
imgContainer.appendChild(downloadLink);
331+
$('chat-area').after(imgContainer);
332+
} else {
333+
// Non-images get a text download link instead
334+
const downloadLink = document.createElement('a');
335+
downloadLink.href = URL.createObjectURL(result);
336+
downloadLink.innerText = `Download ${info.name}`;
337+
downloadLink.setAttribute('download', info.name);
338+
downloadLink.style.margin = '10px';
339+
downloadLink.style.padding = '5px';
340+
downloadLink.style.display = 'block';
341+
$('chat-area').after(downloadLink);
342+
}
343+
}, 'files');
293344

294345
try {
295346
// read and set current key from input

0 commit comments

Comments
 (0)