ShareLinkAlert.jsx 1.4 KB

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