Shun Miyazawa 1 год назад
Родитель
Сommit
74d4bbc398

+ 7 - 7
apps/app/src/client/services/side-effects/yjs-draft.ts

@@ -2,17 +2,17 @@ import { useCallback, useEffect } from 'react';
 
 import { useGlobalSocket } from '@growi/core/dist/swr';
 
+import type { CurrentPageYjsDraft } from '~/interfaces/page';
 import { SocketEventName } from '~/interfaces/websocket';
-import { useHasYjsDraft } from '~/stores/page';
+import { useCurrentPageYjsDraft } from '~/stores/page';
 
 export const useYjsDraftEffect = (): void => {
-
-  const { mutate: mutateHasYjsDraft } = useHasYjsDraft();
+  const { mutate: mutateeCurrentPageYjsDraft } = useCurrentPageYjsDraft();
   const { data: socket } = useGlobalSocket();
 
-  const yjsDraftUpdateHandler = useCallback(((hasYjsDraft: boolean) => {
-    mutateHasYjsDraft(hasYjsDraft);
-  }), [mutateHasYjsDraft]);
+  const yjsDraftUpdateHandler = useCallback(((currentPageYjsDraft: CurrentPageYjsDraft) => {
+    mutateeCurrentPageYjsDraft(currentPageYjsDraft);
+  }), [mutateeCurrentPageYjsDraft]);
 
   useEffect(() => {
 
@@ -24,5 +24,5 @@ export const useYjsDraftEffect = (): void => {
       socket.off(SocketEventName.YjsUpdated, yjsDraftUpdateHandler);
     };
 
-  }, [mutateHasYjsDraft, socket, yjsDraftUpdateHandler]);
+  }, [mutateeCurrentPageYjsDraft, socket, yjsDraftUpdateHandler]);
 };

+ 3 - 3
apps/app/src/components/Navbar/PageEditorModeManager.tsx

@@ -5,7 +5,7 @@ import { useTranslation } from 'next-i18next';
 
 import { useCreatePageAndTransit } from '~/client/services/create-page';
 import { toastError } from '~/client/util/toastr';
-import { useIsNotFound, useHasYjsDraft } from '~/stores/page';
+import { useIsNotFound, useCurrentPageYjsDraft } from '~/stores/page';
 import { EditorMode, useEditorMode, useIsDeviceLargerThanMd } from '~/stores/ui';
 
 import { shouldCreateWipPage } from '../../utils/should-create-wip-page';
@@ -64,7 +64,7 @@ export const PageEditorModeManager = (props: Props): JSX.Element => {
   const { data: isNotFound } = useIsNotFound();
   const { mutate: mutateEditorMode } = useEditorMode();
   const { data: isDeviceLargerThanMd } = useIsDeviceLargerThanMd();
-  const { data: hasYjsDraft } = useHasYjsDraft();
+  const { data: currentPageYjsDraft } = useCurrentPageYjsDraft();
 
   const { isCreating, createAndTransit } = useCreatePageAndTransit();
 
@@ -113,7 +113,7 @@ export const PageEditorModeManager = (props: Props): JSX.Element => {
             onClick={editButtonClickedHandler}
           >
             <span className="material-symbols-outlined me-1 fs-5">edit_square</span>{t('Edit')}
-            { hasYjsDraft && <span className="position-absolute top-0 start-100 translate-middle p-1 bg-primary rounded-circle" />}
+            { currentPageYjsDraft?.hasYjsDraft && <span className="position-absolute top-0 start-100 translate-middle p-1 bg-primary rounded-circle" />}
           </PageEditorModeButton>
         )}
       </div>

+ 8 - 0
apps/app/src/interfaces/page.ts

@@ -75,3 +75,11 @@ export type IOptionsForCreate = {
   origin?: Origin
   wip?: boolean,
 };
+
+export type CurrentPageYjsDraft = {
+  hasYjsDraft: boolean,
+}
+
+export const CurrentPageYjsDraftData = {
+  hasYjsDraft: { hasYjsDraft: true },
+};

+ 2 - 2
apps/app/src/pages/[[...path]].page.tsx

@@ -42,7 +42,7 @@ import {
 } from '~/stores/context';
 import { useEditingMarkdown } from '~/stores/editor';
 import {
-  useSWRxCurrentPage, useSWRMUTxCurrentPage, useCurrentPageId, useHasYjsDraft,
+  useSWRxCurrentPage, useSWRMUTxCurrentPage, useCurrentPageId, useCurrentPageYjsDraft,
   useIsNotFound, useIsLatestRevision, useTemplateTagData, useTemplateBodyData,
 } from '~/stores/page';
 import { useRedirectFrom } from '~/stores/page-redirect';
@@ -221,7 +221,7 @@ const Page: NextPageWithLayout<Props> = (props: Props) => {
   useIsUploadAllFileAllowed(props.isUploadAllFileAllowed);
   useIsUploadEnabled(props.isUploadEnabled);
 
-  useHasYjsDraft(props.hasYjsDraft);
+  useCurrentPageYjsDraft({ hasYjsDraft: props.hasYjsDraft });
 
   const { pageWithMeta } = props;
 

+ 3 - 1
apps/app/src/server/service/socket-io.js

@@ -1,6 +1,7 @@
 import { GlobalSocketEventName } from '@growi/core/dist/interfaces';
 import { Server } from 'socket.io';
 
+import { CurrentPageYjsDraftData } from '~/interfaces/page';
 import { SocketEventName } from '~/interfaces/websocket';
 import loggerFactory from '~/utils/logger';
 
@@ -8,6 +9,7 @@ import { RoomPrefix, getRoomNameWithId } from '../util/socket-io-helpers';
 
 import { getYjsConnectionManager } from './yjs-connection-manager';
 
+
 const expressSession = require('express-session');
 const passport = require('passport');
 
@@ -173,7 +175,7 @@ class SocketIoService {
         // Emit to the client in the room of the target pageId.
         this.io
           .in(getRoomNameWithId(RoomPrefix.PAGE, pageId))
-          .emit(SocketEventName.YjsUpdated, true);
+          .emit(SocketEventName.YjsUpdated, CurrentPageYjsDraftData.hasYjsDraft);
 
         try {
           await yjsConnectionManager.handleYDocSync(pageId, initialValue);

+ 4 - 2
apps/app/src/stores/page.tsx

@@ -18,11 +18,13 @@ import useSWRMutation, { type SWRMutationResponse } from 'swr/mutation';
 
 import { apiGet } from '~/client/util/apiv1-client';
 import { apiv3Get } from '~/client/util/apiv3-client';
+import type { CurrentPageYjsDraft } from '~/interfaces/page';
 import type { IRecordApplicableGrant, IResIsGrantNormalized } from '~/interfaces/page-grant';
 import type { AxiosResponse } from '~/utils/axios';
 
 import type { IPageTagsInfo } from '../interfaces/tag';
 
+
 import {
   useCurrentPathname, useShareLinkId, useIsGuestUser, useIsReadOnlyUser,
 } from './context';
@@ -52,8 +54,8 @@ export const useTemplateBodyData = (initialData?: string): SWRResponse<string, E
   return useSWRStatic<string, Error>('templateBodyData', initialData);
 };
 
-export const useHasYjsDraft = (initialData?: boolean): SWRResponse<boolean, Error> => {
-  return useSWRStatic<boolean, Error>('hasYjsDraft', initialData);
+export const useCurrentPageYjsDraft = (initialData?: CurrentPageYjsDraft): SWRResponse<CurrentPageYjsDraft, Error> => {
+  return useSWRStatic<CurrentPageYjsDraft, Error>('currentPageYjsDraft', initialData);
 };
 
 /** "useSWRxCurrentPage" is intended for initial data retrieval only. Use "useSWRMUTxCurrentPage" for revalidation */