PageBulkExportJobModelNotification.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { type HasObjectId, isPopulated } from '@growi/core';
  2. import { useTranslation } from 'react-i18next';
  3. import type { IPageBulkExportJobHasId } from '~/features/page-bulk-export/interfaces/page-bulk-export';
  4. import { SupportedAction, SupportedTargetModel } from '~/interfaces/activity';
  5. import type { IInAppNotification } from '~/interfaces/in-app-notification';
  6. import * as pageBulkExportJobSerializers from '~/models/serializers/in-app-notification-snapshot/page-bulk-export-job-client';
  7. import type { ModelNotificationUtils } from '.';
  8. import { ModelNotification } from './ModelNotification';
  9. import { useActionMsgAndIconForModelNotification } from './useActionAndMsg';
  10. export const usePageBulkExportJobModelNotification = (
  11. notification: IInAppNotification & HasObjectId,
  12. ): ModelNotificationUtils | null => {
  13. const { t } = useTranslation();
  14. const { actionMsg, actionIcon } =
  15. useActionMsgAndIconForModelNotification(notification);
  16. const isPageBulkExportJobModelNotification = (
  17. notification: IInAppNotification & HasObjectId,
  18. ): notification is IInAppNotification<IPageBulkExportJobHasId> &
  19. HasObjectId => {
  20. return (
  21. notification.targetModel ===
  22. SupportedTargetModel.MODEL_PAGE_BULK_EXPORT_JOB
  23. );
  24. };
  25. if (!isPageBulkExportJobModelNotification(notification)) {
  26. return null;
  27. }
  28. const actionUsers = notification.user.username;
  29. notification.parsedSnapshot = pageBulkExportJobSerializers.parseSnapshot(
  30. notification.snapshot,
  31. );
  32. const getSubMsg = (): JSX.Element => {
  33. if (
  34. notification.action ===
  35. SupportedAction.ACTION_PAGE_BULK_EXPORT_COMPLETED &&
  36. notification.target == null
  37. ) {
  38. return (
  39. <div className="text-danger">
  40. <small>{t('page_export.bulk_export_download_expired')}</small>
  41. </div>
  42. );
  43. }
  44. if (
  45. notification.action ===
  46. SupportedAction.ACTION_PAGE_BULK_EXPORT_JOB_EXPIRED
  47. ) {
  48. return (
  49. <div className="text-danger">
  50. <small>{t('page_export.bulk_export_job_expired')}</small>
  51. </div>
  52. );
  53. }
  54. return <></>;
  55. };
  56. const Notification = () => {
  57. return (
  58. <ModelNotification
  59. notification={notification}
  60. actionMsg={actionMsg}
  61. actionIcon={actionIcon}
  62. actionUsers={actionUsers}
  63. hideActionUsers
  64. subMsg={getSubMsg()}
  65. />
  66. );
  67. };
  68. const clickLink =
  69. notification.action === SupportedAction.ACTION_PAGE_BULK_EXPORT_COMPLETED &&
  70. notification.target?.attachment != null &&
  71. isPopulated(notification.target?.attachment)
  72. ? notification.target.attachment.downloadPathProxied
  73. : undefined;
  74. return {
  75. Notification,
  76. clickLink,
  77. isDisabled: notification.target == null,
  78. };
  79. };