ShareLinkAlert.tsx 1.4 KB

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