page-operation.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { useCallback } from 'react';
  2. import type { IPageHasId } from '@growi/core';
  3. import { SubscriptionStatusType } from '@growi/core';
  4. import urljoin from 'url-join';
  5. import type { SyncLatestRevisionBody } from '~/interfaces/yjs';
  6. import { useIsGuestUser } from '~/stores-universal/context';
  7. import { useEditingMarkdown, usePageTagsForEditors } from '~/stores/editor';
  8. import {
  9. useCurrentPageId, useSWRMUTxCurrentPage, useSWRxApplicableGrant, useSWRxTagsInfo,
  10. useSWRxCurrentGrantData,
  11. } from '~/stores/page';
  12. import { useSetRemoteLatestPageData } from '~/stores/remote-latest-page';
  13. import loggerFactory from '~/utils/logger';
  14. import { apiPost } from '../util/apiv1-client';
  15. import { apiv3Get, apiv3Post, apiv3Put } from '../util/apiv3-client';
  16. import { toastError } from '../util/toastr';
  17. const logger = loggerFactory('growi:services:page-operation');
  18. export const toggleSubscribe = async(pageId: string, currentStatus: SubscriptionStatusType | undefined): Promise<void> => {
  19. try {
  20. const newStatus = currentStatus === SubscriptionStatusType.SUBSCRIBE
  21. ? SubscriptionStatusType.UNSUBSCRIBE
  22. : SubscriptionStatusType.SUBSCRIBE;
  23. await apiv3Put('/page/subscribe', { pageId, status: newStatus });
  24. }
  25. catch (err) {
  26. toastError(err);
  27. }
  28. };
  29. export const toggleLike = async(pageId: string, currentValue?: boolean): Promise<void> => {
  30. try {
  31. await apiv3Put('/page/likes', { pageId, bool: !currentValue });
  32. }
  33. catch (err) {
  34. toastError(err);
  35. }
  36. };
  37. export const toggleBookmark = async(pageId: string, currentValue?: boolean): Promise<void> => {
  38. try {
  39. await apiv3Put('/bookmarks', { pageId, bool: !currentValue });
  40. }
  41. catch (err) {
  42. toastError(err);
  43. }
  44. };
  45. export const updateContentWidth = async(pageId: string, newValue: boolean): Promise<void> => {
  46. try {
  47. await apiv3Put(`/page/${pageId}/content-width`, { expandContentWidth: newValue });
  48. }
  49. catch (err) {
  50. toastError(err);
  51. }
  52. };
  53. export const bookmark = async(pageId: string): Promise<void> => {
  54. try {
  55. await apiv3Put('/bookmarks', { pageId, bool: true });
  56. }
  57. catch (err) {
  58. toastError(err);
  59. }
  60. };
  61. export const unbookmark = async(pageId: string): Promise<void> => {
  62. try {
  63. await apiv3Put('/bookmarks', { pageId, bool: false });
  64. }
  65. catch (err) {
  66. toastError(err);
  67. }
  68. };
  69. export const exportAsMarkdown = (pageId: string, revisionId: string, format: string): void => {
  70. const url = new URL(urljoin(window.location.origin, '_api/v3/page/export', pageId));
  71. url.searchParams.append('format', format);
  72. url.searchParams.append('revisionId', revisionId);
  73. window.location.href = url.href;
  74. };
  75. /**
  76. * send request to fix broken paths caused by unexpected events such as server shutdown while renaming page paths
  77. */
  78. export const resumeRenameOperation = async(pageId: string): Promise<void> => {
  79. await apiv3Post('/pages/resume-rename', { pageId });
  80. };
  81. export type UpdateStateAfterSaveOption = {
  82. supressEditingMarkdownMutation: boolean,
  83. }
  84. export const useUpdateStateAfterSave = (pageId: string|undefined|null, opts?: UpdateStateAfterSaveOption): (() => Promise<void>) | undefined => {
  85. const { mutate: mutateCurrentPageId } = useCurrentPageId();
  86. const { trigger: mutateCurrentPage } = useSWRMUTxCurrentPage();
  87. const { setRemoteLatestPageData } = useSetRemoteLatestPageData();
  88. const { mutate: mutateTagsInfo } = useSWRxTagsInfo(pageId);
  89. const { sync: syncTagsInfoForEditor } = usePageTagsForEditors(pageId);
  90. const { mutate: mutateEditingMarkdown } = useEditingMarkdown();
  91. const { data: isGuestUser } = useIsGuestUser();
  92. const { mutate: mutateCurrentGrantData } = useSWRxCurrentGrantData(isGuestUser ? null : pageId);
  93. const { mutate: mutateApplicableGrant } = useSWRxApplicableGrant(isGuestUser ? null : pageId);
  94. // update swr 'currentPageId', 'currentPage', remote states
  95. return useCallback(async() => {
  96. if (pageId == null) { return }
  97. // update tag before page: https://github.com/weseek/growi/pull/7158
  98. // !! DO NOT CHANGE THE ORDERS OF THE MUTATIONS !! -- 12.26 yuken-t
  99. await mutateTagsInfo(); // get from DB
  100. syncTagsInfoForEditor(); // sync global state for client
  101. await mutateCurrentPageId(pageId);
  102. const updatedPage = await mutateCurrentPage();
  103. if (updatedPage == null || updatedPage.revision == null) { return }
  104. // supress to mutate only when updated from built-in editor
  105. // and see: https://github.com/weseek/growi/pull/7118
  106. const supressEditingMarkdownMutation = opts?.supressEditingMarkdownMutation ?? false;
  107. if (!supressEditingMarkdownMutation) {
  108. mutateEditingMarkdown(updatedPage.revision.body);
  109. }
  110. mutateCurrentGrantData();
  111. mutateApplicableGrant();
  112. const remoterevisionData = {
  113. remoteRevisionId: updatedPage.revision._id,
  114. remoteRevisionBody: updatedPage.revision.body,
  115. remoteRevisionLastUpdateUser: updatedPage.lastUpdateUser,
  116. remoteRevisionLastUpdatedAt: updatedPage.updatedAt,
  117. };
  118. setRemoteLatestPageData(remoterevisionData);
  119. },
  120. // eslint-disable-next-line max-len
  121. [pageId, mutateTagsInfo, syncTagsInfoForEditor, mutateCurrentPageId, mutateCurrentPage, opts?.supressEditingMarkdownMutation, mutateCurrentGrantData, mutateApplicableGrant, setRemoteLatestPageData, mutateEditingMarkdown]);
  122. };
  123. export const unlink = async(path: string): Promise<void> => {
  124. await apiPost('/pages.unlink', { path });
  125. };
  126. interface PageExistResponse {
  127. isExist: boolean,
  128. }
  129. export const exist = async(path: string): Promise<PageExistResponse> => {
  130. const res = await apiv3Get<PageExistResponse>('/page/exist', { path });
  131. return res.data;
  132. };
  133. interface NonUserRelatedGroupsGrantedResponse {
  134. isNonUserRelatedGroupsGranted: boolean,
  135. }
  136. export const getIsNonUserRelatedGroupsGranted = async(path: string): Promise<NonUserRelatedGroupsGrantedResponse> => {
  137. const res = await apiv3Get<NonUserRelatedGroupsGrantedResponse>('/page/non-user-related-groups-granted', { path });
  138. return res.data;
  139. };
  140. export const publish = async(pageId: string): Promise<IPageHasId> => {
  141. const res = await apiv3Put(`/page/${pageId}/publish`);
  142. return res.data;
  143. };
  144. export const unpublish = async(pageId: string): Promise<IPageHasId> => {
  145. const res = await apiv3Put(`/page/${pageId}/unpublish`);
  146. return res.data;
  147. };
  148. export const syncLatestRevisionBody = async(pageId: string, editingMarkdownLength?: number): Promise<SyncLatestRevisionBody> => {
  149. const res = await apiv3Put(`/page/${pageId}/sync-latest-revision-body-to-yjs-draft`, { editingMarkdownLength });
  150. return res.data;
  151. };