2017-08-24 23:54:02 +02:00
|
|
|
const html = require('choo/html');
|
2018-02-13 20:32:59 +01:00
|
|
|
const number = require('../../utils').number;
|
2018-07-31 20:09:18 +02:00
|
|
|
const bytes = require('../../utils').bytes;
|
|
|
|
const fileIcon = require('../fileIcon');
|
2017-08-24 23:54:02 +02:00
|
|
|
|
2018-07-31 20:09:18 +02:00
|
|
|
module.exports = function(file, state) {
|
2017-08-24 23:54:02 +02:00
|
|
|
const ttl = file.expiresAt - Date.now();
|
2018-01-24 19:23:13 +01:00
|
|
|
const remainingTime =
|
|
|
|
timeLeft(ttl, state) || state.translate('linkExpiredAlt');
|
2018-01-23 03:11:48 +01:00
|
|
|
const downloadLimit = file.dlimit || 1;
|
|
|
|
const totalDownloads = file.dtotal || 0;
|
2018-07-31 20:09:18 +02:00
|
|
|
|
|
|
|
const multiFiles = file.manifest.files;
|
|
|
|
const fileName =
|
|
|
|
multiFiles.length > 1
|
|
|
|
? `${multiFiles[0].name} + ${state.translate('fileCount', {
|
|
|
|
num: multiFiles.length - 1
|
|
|
|
})}`
|
|
|
|
: file.name;
|
|
|
|
|
|
|
|
const activeClass = isOnSharePage() ? 'fileToast--active' : '';
|
2018-02-16 21:56:53 +01:00
|
|
|
return html`
|
2018-08-09 02:24:58 +02:00
|
|
|
<li id="${file.id}">
|
|
|
|
<a class="fileToast ${activeClass}" href=${toastClick()}>
|
|
|
|
<div class="fileToast__content">
|
|
|
|
${fileIcon(file.name, file._hasPassword)}
|
|
|
|
<div class="fileData">
|
|
|
|
<p class="fileName">${fileName}</p>
|
|
|
|
<p class="fileInfo">
|
|
|
|
<span>${bytes(file.size)}</span> ·
|
|
|
|
<span>${state.translate('downloadCount', {
|
|
|
|
num: `${number(totalDownloads)} / ${number(downloadLimit)}`
|
|
|
|
})}</span>
|
|
|
|
<span>${remainingTime}</span>
|
|
|
|
</p>
|
|
|
|
</div>
|
2018-07-31 20:09:18 +02:00
|
|
|
</div>
|
2018-08-09 02:24:58 +02:00
|
|
|
</a>
|
2018-07-31 20:09:18 +02:00
|
|
|
</li>
|
2017-08-24 23:54:02 +02:00
|
|
|
`;
|
|
|
|
|
2018-07-31 20:09:18 +02:00
|
|
|
function toastClick() {
|
|
|
|
return isOnSharePage() ? '/' : `/share/${file.id}`;
|
2017-08-24 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
2018-07-31 20:09:18 +02:00
|
|
|
function isOnSharePage() {
|
|
|
|
return state.href.includes('/share/') && state.params.id === file.id;
|
2017-08-24 23:54:02 +02:00
|
|
|
}
|
|
|
|
};
|
2018-02-16 21:56:53 +01:00
|
|
|
|
|
|
|
function timeLeft(milliseconds, state) {
|
|
|
|
const minutes = Math.floor(milliseconds / 1000 / 60);
|
|
|
|
const hours = Math.floor(minutes / 60);
|
2018-08-08 20:07:09 +02:00
|
|
|
const days = Math.floor(hours / 24);
|
|
|
|
if (days >= 1) {
|
|
|
|
return state.translate('expiresDaysHoursMinutes', {
|
|
|
|
days,
|
|
|
|
hours: hours % 24,
|
|
|
|
minutes: minutes % 60
|
|
|
|
});
|
|
|
|
}
|
2018-02-16 21:56:53 +01:00
|
|
|
if (hours >= 1) {
|
|
|
|
return state.translate('expiresHoursMinutes', {
|
|
|
|
hours,
|
|
|
|
minutes: minutes % 60
|
|
|
|
});
|
|
|
|
} else if (hours === 0) {
|
|
|
|
if (minutes === 0) {
|
|
|
|
return state.translate('expiresMinutes', { minutes: '< 1' });
|
|
|
|
}
|
|
|
|
return state.translate('expiresMinutes', { minutes });
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|