TrashPageAlert.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import React from 'react';
  2. import { UserPicture } from '@growi/ui';
  3. import { format } from 'date-fns';
  4. import { useTranslation } from 'react-i18next';
  5. import {
  6. useIsTrashPage, useShareLinkId,
  7. } from '~/stores/context';
  8. import { usePageDeleteModal, usePutBackPageModal } from '~/stores/modal';
  9. import { useSWRxPageInfo, useSWRxCurrentPage } from '~/stores/page';
  10. import { useIsAbleToShowTrashPageManagementButtons } from '~/stores/ui';
  11. const onDeletedHandler = (pathOrPathsToDelete) => {
  12. if (typeof pathOrPathsToDelete !== 'string') {
  13. return;
  14. }
  15. window.location.href = '/';
  16. };
  17. export const TrashPageAlert = (): JSX.Element => {
  18. const { t } = useTranslation();
  19. const { data: isAbleToShowTrashPageManagementButtons } = useIsAbleToShowTrashPageManagementButtons();
  20. const { data: shareLinkId } = useShareLinkId();
  21. const { data: pageData } = useSWRxCurrentPage();
  22. const { data: isTrashPage } = useIsTrashPage();
  23. const pageId = pageData?._id;
  24. const pagePath = pageData?.path;
  25. const { data: pageInfo } = useSWRxPageInfo(pageId ?? null, shareLinkId);
  26. const { open: openDeleteModal } = usePageDeleteModal();
  27. const { open: openPutBackPageModal } = usePutBackPageModal();
  28. const lastUpdateUserName = pageData?.lastUpdateUser?.name;
  29. const deletedAt = pageData?.deletedAt ? format(new Date(pageData?.deletedAt), 'yyyy/MM/dd HH:mm') : '';
  30. const revisionId = pageData?.revision?._id;
  31. if (!isTrashPage) {
  32. return <></>;
  33. }
  34. function openPutbackPageModalHandler() {
  35. if (pageId === undefined || pagePath === undefined) {
  36. return;
  37. }
  38. const putBackedHandler = () => {
  39. window.location.reload();
  40. };
  41. openPutBackPageModal({ pageId, path: pagePath }, { onPutBacked: putBackedHandler });
  42. }
  43. function openPageDeleteModalHandler() {
  44. if (pageId === undefined || revisionId === undefined || pagePath === undefined) {
  45. return;
  46. }
  47. const pageToDelete = {
  48. data: {
  49. _id: pageId,
  50. revision: revisionId,
  51. path: pagePath,
  52. },
  53. meta: pageInfo,
  54. };
  55. openDeleteModal([pageToDelete], { onDeleted: onDeletedHandler });
  56. }
  57. function renderTrashPageManagementButtons() {
  58. return (
  59. <>
  60. <button
  61. type="button"
  62. className="btn btn-info rounded-pill btn-sm ml-auto mr-2"
  63. onClick={openPutbackPageModalHandler}
  64. data-toggle="modal"
  65. >
  66. <i className="icon-action-undo" aria-hidden="true"></i> { t('Put Back') }
  67. </button>
  68. <button
  69. type="button"
  70. className="btn btn-danger rounded-pill btn-sm"
  71. disabled={!(pageInfo?.isAbleToDeleteCompletely ?? false)}
  72. onClick={openPageDeleteModalHandler}
  73. >
  74. <i className="icon-fire" aria-hidden="true"></i> { t('Delete Completely') }
  75. </button>
  76. </>
  77. );
  78. }
  79. return (
  80. <>
  81. <div className="alert alert-warning py-3 pl-4 d-flex flex-column flex-lg-row">
  82. <div className="flex-grow-1">
  83. This page is in the trash <i className="icon-trash" aria-hidden="true"></i>.
  84. <br />
  85. <UserPicture user={{ username: lastUpdateUserName }} />
  86. <span className="ml-2">
  87. Deleted by { lastUpdateUserName } at {deletedAt || pageData?.updatedAt}
  88. </span>
  89. </div>
  90. <div className="pt-1 d-flex align-items-end align-items-lg-center">
  91. { isAbleToShowTrashPageManagementButtons && renderTrashPageManagementButtons()}
  92. </div>
  93. </div>
  94. </>
  95. );
  96. };