PageBulkExportJobModelNotification.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import React from 'react';
  2. import { isPopulated, type HasObjectId } 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 { ModelNotification } from './ModelNotification';
  9. import { useActionMsgAndIconForModelNotification } from './useActionAndMsg';
  10. import type { ModelNotificationUtils } from '.';
  11. export const usePageBulkExportJobModelNotification = (notification: IInAppNotification & HasObjectId): ModelNotificationUtils | null => {
  12. const { t } = useTranslation();
  13. const { actionMsg, actionIcon } = useActionMsgAndIconForModelNotification(notification);
  14. const isPageBulkExportJobModelNotification = (
  15. notification: IInAppNotification & HasObjectId,
  16. ): notification is IInAppNotification<IPageBulkExportJobHasId> & HasObjectId => {
  17. return notification.targetModel === SupportedTargetModel.MODEL_PAGE_BULK_EXPORT_JOB;
  18. };
  19. if (!isPageBulkExportJobModelNotification(notification)) {
  20. return null;
  21. }
  22. const actionUsers = notification.user.username;
  23. notification.parsedSnapshot = pageBulkExportJobSerializers.parseSnapshot(notification.snapshot);
  24. const getSubMsg = (): React.ReactElement => {
  25. if (notification.action === SupportedAction.ACTION_PAGE_BULK_EXPORT_COMPLETED && notification.target == null) {
  26. return <div className="text-danger"><small>{t('page_export.bulk_export_download_expired')}</small></div>;
  27. }
  28. if (notification.action === SupportedAction.ACTION_PAGE_BULK_EXPORT_JOB_EXPIRED) {
  29. return <div className="text-danger"><small>{t('page_export.bulk_export_job_expired')}</small></div>;
  30. }
  31. return <></>;
  32. };
  33. const Notification = () => {
  34. return (
  35. <ModelNotification
  36. notification={notification}
  37. actionMsg={actionMsg}
  38. actionIcon={actionIcon}
  39. actionUsers={actionUsers}
  40. hideActionUsers
  41. subMsg={getSubMsg()}
  42. />
  43. );
  44. };
  45. const clickLink = (notification.action === SupportedAction.ACTION_PAGE_BULK_EXPORT_COMPLETED
  46. && notification.target?.attachment != null && isPopulated(notification.target?.attachment))
  47. ? notification.target.attachment.downloadPathProxied : undefined;
  48. return {
  49. Notification,
  50. clickLink,
  51. isDisabled: notification.target == null,
  52. };
  53. };