PageBulkExportJobModelNotification.tsx 2.7 KB

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