ShareLinkAlert.jsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. const wholeTime = new Date(expiredAt).getTime() - new Date(createdAt).getTime();
  10. const remainingTime = new Date(expiredAt).getTime() - new Date().getTime();
  11. const ratio = remainingTime / wholeTime;
  12. function specifyColor() {
  13. let color;
  14. if (ratio >= 0.75) {
  15. color = 'success';
  16. }
  17. else if (ratio < 0.75 && ratio >= 0.5) {
  18. color = 'info';
  19. }
  20. else if (ratio < 0.5 && ratio >= 0.25) {
  21. color = 'warning';
  22. }
  23. else {
  24. color = 'danger';
  25. }
  26. return color;
  27. }
  28. return (
  29. <p className={`alert alert-${specifyColor()} py-3 px-4`}>
  30. <i className="icon-fw icon-link"></i>
  31. {/* eslint-disable-next-line react/no-danger */}
  32. <span dangerouslySetInnerHTML={{ __html: t('page_page.notice.expiration', { expiredAt }) }} />
  33. </p>
  34. );
  35. };
  36. ShareLinkAlert.propTypes = {
  37. t: PropTypes.func.isRequired, // i18next
  38. };
  39. export default withTranslation()(ShareLinkAlert);