ShareLinkAlert.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import React, { FC } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. const generateRatio = (expiredAt: Date, createdAt: Date): number => {
  4. const wholeTime = new Date(expiredAt).getTime() - new Date(createdAt).getTime();
  5. const remainingTime = new Date(expiredAt).getTime() - new Date().getTime();
  6. return remainingTime / wholeTime;
  7. };
  8. const getAlertColor = (ratio: number): string => {
  9. let color: string;
  10. if (ratio >= 0.75) {
  11. color = 'success';
  12. }
  13. else if (ratio < 0.75 && ratio >= 0.5) {
  14. color = 'info';
  15. }
  16. else if (ratio < 0.5 && ratio >= 0.25) {
  17. color = 'warning';
  18. }
  19. else {
  20. color = 'danger';
  21. }
  22. return color;
  23. };
  24. type Props = {
  25. createdAt: Date,
  26. expiredAt?: Date,
  27. }
  28. const ShareLinkAlert: FC<Props> = (props: Props) => {
  29. const { t } = useTranslation();
  30. const { expiredAt, createdAt } = props;
  31. const ratio = expiredAt != null ? generateRatio(expiredAt, createdAt) : 1;
  32. const alertColor = getAlertColor(ratio);
  33. return (
  34. <p className={`alert alert-${alertColor} px-4 d-edit-none`}>
  35. <i className="icon-fw icon-link"></i>
  36. {(expiredAt === null ? <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;