ShareLinkAlert.jsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. const ShareLinkAlert = (props) => {
  5. const { t } = props;
  6. const shareContent = document.getElementById('is-shared-page');
  7. let expiredAt = shareContent.getAttribute('data-share-link-expired-at');
  8. const createdAt = shareContent.getAttribute('data-share-link-created-at');
  9. function generateRatio() {
  10. const wholeTime = new Date(expiredAt).getTime() - new Date(createdAt).getTime();
  11. const remainingTime = new Date(expiredAt).getTime() - new Date().getTime();
  12. return remainingTime / wholeTime;
  13. }
  14. let ratio = 1;
  15. if (expiredAt !== '') {
  16. ratio = generateRatio();
  17. }
  18. else {
  19. expiredAt = t('share_links.Unlimited');
  20. }
  21. function specifyColor() {
  22. let color;
  23. if (ratio >= 0.75) {
  24. color = 'success';
  25. }
  26. else if (ratio < 0.75 && ratio >= 0.5) {
  27. color = 'info';
  28. }
  29. else if (ratio < 0.5 && ratio >= 0.25) {
  30. color = 'warning';
  31. }
  32. else {
  33. color = 'danger';
  34. }
  35. return color;
  36. }
  37. return (
  38. <p className={`alert alert-${specifyColor()} py-3 px-4`}>
  39. <i className="icon-fw icon-link"></i>
  40. {/* eslint-disable-next-line react/no-danger */}
  41. <span dangerouslySetInnerHTML={{ __html: t('page_page.notice.expiration', { expiredAt }) }} />
  42. </p>
  43. );
  44. };
  45. ShareLinkAlert.propTypes = {
  46. t: PropTypes.func.isRequired, // i18next
  47. };
  48. export default withTranslation()(ShareLinkAlert);