send/app/templates/file/index.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

const html = require('choo/html');
const number = require('../../utils').number;
2018-07-31 18:09:18 +00:00
const bytes = require('../../utils').bytes;
const fileIcon = require('../fileIcon');
2018-07-31 18:09:18 +00:00
module.exports = function(file, state) {
const ttl = file.expiresAt - Date.now();
2018-01-24 18:23:13 +00:00
const remainingTime =
timeLeft(ttl, state) || state.translate('linkExpiredAlt');
2018-01-23 02:11:48 +00:00
const downloadLimit = file.dlimit || 1;
const totalDownloads = file.dtotal || 0;
2018-07-31 18:09:18 +00: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 20:56:53 +00:00
return html`
2018-07-31 18:09:18 +00:00
<a href=${toastClick()}>
<li class="fileToast ${activeClass}" id="${file.id}">
<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)}`
2018-08-09 21:49:52 +00:00
})}</span> ·
2018-07-31 18:09:18 +00:00
<span>${remainingTime}</span>
</p>
</div>
</div>
</li>
</a>
`;
2018-07-31 18:09:18 +00:00
function toastClick() {
return isOnSharePage() ? '/' : `/share/${file.id}`;
}
2018-07-31 18:09:18 +00:00
function isOnSharePage() {
return state.href.includes('/share/') && state.params.id === file.id;
}
};
2018-02-16 20:56:53 +00:00
function timeLeft(milliseconds, state) {
const minutes = Math.floor(milliseconds / 1000 / 60);
const hours = Math.floor(minutes / 60);
2018-08-08 18:07:09 +00:00
const days = Math.floor(hours / 24);
if (days >= 1) {
return state.translate('expiresDaysHoursMinutes', {
days,
hours: hours % 24,
minutes: minutes % 60
});
}
2018-02-16 20:56:53 +00: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;
}