Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 7283390

Browse files
committed
chore: add progress bar
1 parent bd55179 commit 7283390

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

examples/exchange-files-in-browser/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"http-server": "^0.11.1"
1313
},
1414
"dependencies": {
15-
"ipfs": "file:../../",
16-
"stream-buffers": "^3.0.1"
15+
"stream-buffers": "^3.0.1",
16+
"ipfs": "file:../../"
1717
}
1818
}

examples/exchange-files-in-browser/public/app.css

+14
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,20 @@ table tbody tr:hover {
273273
margin-left: 1em;
274274
}
275275

276+
#progress-container {
277+
margin-top: 1em;
278+
border-radius: 0.5em;
279+
background-color: #0B3A53;
280+
overflow-x: hidden;
281+
}
282+
283+
#progress-bar {
284+
height: 1em;
285+
border-radius: 0.5em;
286+
background-color: #6ACAD1;
287+
transform: translateX(-100%);
288+
}
289+
276290
/* ===========================================================================
277291
Responsiveness
278292
=========================================================================== */

examples/exchange-files-in-browser/public/app.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const $connectButton = document.querySelector('#peer-btn')
1515
const $multihashInput = document.querySelector('#multihash-input')
1616
const $fetchButton = document.querySelector('#fetch-btn')
1717
const $dragContainer = document.querySelector('#drag-container')
18+
const $progressBar = document.querySelector('#progress-bar')
1819
const $fileHistory = document.querySelector('#file-history tbody')
1920
const $emptyRow = document.querySelector('.empty-row')
2021
// Misc
@@ -25,6 +26,8 @@ const $allDisabledElements = document.querySelectorAll('.disabled')
2526
const FILES = []
2627
const workspace = location.hash
2728

29+
let fileSize = 0
30+
2831
let node
2932
let info
3033
let Buffer
@@ -103,6 +106,14 @@ const isFileInList = (hash) => FILES.indexOf(hash) !== -1
103106

104107
const sendFileList = () => FILES.forEach((hash) => publishHash(hash))
105108

109+
const updateProgress = (bytesLoaded) => {
110+
let percent = 100 - ((bytesLoaded / fileSize) * 100)
111+
112+
$progressBar.style.transform = `translateX(${-percent}%)`
113+
}
114+
115+
const resetProgress = () => $progressBar.style.transform = 'translateX(-100%)'
116+
106117
function appendFile (name, hash, size, data) {
107118
const file = new window.Blob([data], { type: 'application/octet-binary' })
108119
const url = window.URL.createObjectURL(file)
@@ -190,10 +201,12 @@ function onDrop (event) {
190201
files.forEach((file) => {
191202
readFileContents(file)
192203
.then((buffer) => {
204+
fileSize = file.size
205+
193206
node.files.add({
194207
path: file.name,
195208
content: Buffer.from(buffer)
196-
}, { wrap: true }, (err, filesAdded) => {
209+
}, { wrap: true, progress: updateProgress }, (err, filesAdded) => {
197210
if (err) {
198211
return onError(err)
199212
}
@@ -202,6 +215,7 @@ function onDrop (event) {
202215
// the original file name when adding it to the table
203216
$multihashInput.value = filesAdded[1].hash
204217

218+
resetProgress()
205219
getFile()
206220
})
207221
})

examples/exchange-files-in-browser/public/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ <h2>Files</h2>
6666
<p><b>Drag &amp; drop</b> a file to upload it.</p>
6767
</div>
6868

69+
<div id="progress-container">
70+
<div id="progress-bar"></div>
71+
</div>
72+
6973
<table id="file-history">
7074
<thead>
7175
<tr>

0 commit comments

Comments
 (0)