expiryOptions.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const html = require('choo/html');
  2. const raw = require('choo/html/raw');
  3. const { secondsToL10nId } = require('../utils');
  4. const selectbox = require('./selectbox');
  5. module.exports = function(state, emit) {
  6. const el = html`
  7. <div class="px-1">
  8. ${raw(
  9. state.translate('archiveExpiryInfo', {
  10. downloadCount:
  11. '<span class="lg:inline-block md:block sm:inline-block block"></span><select id="dlCount"></select>',
  12. timespan: '<select id="timespan"></select>'
  13. })
  14. )}
  15. </div>
  16. `;
  17. if (el.__encoded) {
  18. // we're rendering on the server
  19. return el;
  20. }
  21. const counts = state.DEFAULTS.DOWNLOAD_COUNTS.filter(
  22. i => state.capabilities.account || i <= state.user.maxDownloads
  23. );
  24. const dlCountSelect = el.querySelector('#dlCount');
  25. el.replaceChild(
  26. selectbox(
  27. state.archive.dlimit,
  28. counts,
  29. num => state.translate('downloadCount', { num }),
  30. value => {
  31. const max = state.user.maxDownloads;
  32. state.archive.dlimit = Math.min(value, max);
  33. if (value > max) {
  34. emit('signup-cta', 'count');
  35. } else {
  36. emit('render');
  37. }
  38. },
  39. 'expire-after-dl-count-select'
  40. ),
  41. dlCountSelect
  42. );
  43. const expires = state.DEFAULTS.EXPIRE_TIMES_SECONDS.filter(
  44. i => state.capabilities.account || i <= state.user.maxExpireSeconds
  45. );
  46. const timeSelect = el.querySelector('#timespan');
  47. el.replaceChild(
  48. selectbox(
  49. state.archive.timeLimit,
  50. expires,
  51. num => {
  52. const l10n = secondsToL10nId(num);
  53. return state.translate(l10n.id, l10n);
  54. },
  55. value => {
  56. const max = state.user.maxExpireSeconds;
  57. state.archive.timeLimit = Math.min(value, max);
  58. if (value > max) {
  59. emit('signup-cta', 'time');
  60. } else {
  61. emit('render');
  62. }
  63. },
  64. 'expire-after-time-select'
  65. ),
  66. timeSelect
  67. );
  68. return el;
  69. };