ShareLinkAlert.jsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. const 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. function specifyColor() {
  19. let color;
  20. if (ratio >= 0.75) {
  21. color = 'success';
  22. }
  23. else if (ratio < 0.75 && ratio >= 0.5) {
  24. color = 'info';
  25. }
  26. else if (ratio < 0.5 && ratio >= 0.25) {
  27. color = 'warning';
  28. }
  29. else {
  30. color = 'danger';
  31. }
  32. return color;
  33. }
  34. return (
  35. <p className={`alert alert-${specifyColor()} py-3 px-4 d-edit-none`}>
  36. <i className="icon-fw icon-link"></i>
  37. {(expiredAt === '' ? <span>{t('page_page.notice.no_deadline')}</span>
  38. // eslint-disable-next-line react/no-danger
  39. : <span dangerouslySetInnerHTML={{ __html: t('page_page.notice.expiration', { expiredAt }) }} />
  40. )}
  41. </p>
  42. );
  43. };
  44. ShareLinkAlert.propTypes = {
  45. t: PropTypes.func.isRequired, // i18next
  46. };
  47. export default withTranslation()(ShareLinkAlert);