Просмотр исходного кода

Merge branch 'master' into support/use-jotai

Update socket behavior for guest users
Yuki Takei 6 месяцев назад
Родитель
Сommit
918c89ce10

+ 17 - 2
apps/app/src/features/collaborative-editor/side-effects/index.ts

@@ -2,6 +2,7 @@ import { useEffect } from 'react';
 
 import { useCurrentPageYjsDataActions } from '~/features/collaborative-editor/states';
 import { SocketEventName } from '~/interfaces/websocket';
+import { useIsGuestUser } from '~/states/context';
 import {
   useCurrentPageData,
   useCurrentPageId,
@@ -13,15 +14,27 @@ export const useCurrentPageYjsDataAutoLoadEffect = (): void => {
   const { fetchCurrentPageYjsData } = useCurrentPageYjsDataActions();
   const pageId = useCurrentPageId();
   const currentPage = useCurrentPageData();
+  const isGuestUser = useIsGuestUser();
   const isNotFound = usePageNotFound();
 
   // Optimized effects with minimal dependencies
   useEffect(() => {
     // Load YJS data only when revision changes and page exists
-    if (pageId && currentPage?.revision?._id && !isNotFound) {
+    if (
+      !isGuestUser &&
+      pageId != null &&
+      currentPage?.revision?._id != null &&
+      !isNotFound
+    ) {
       fetchCurrentPageYjsData();
     }
-  }, [currentPage?.revision?._id, fetchCurrentPageYjsData, isNotFound, pageId]);
+  }, [
+    isGuestUser,
+    currentPage?.revision?._id,
+    fetchCurrentPageYjsData,
+    isNotFound,
+    pageId,
+  ]);
 };
 
 export const useNewlyYjsDataSyncingEffect = (): void => {
@@ -31,6 +44,7 @@ export const useNewlyYjsDataSyncingEffect = (): void => {
 
   useEffect(() => {
     if (socket == null) {
+      // socket must be null if the user is a guest
       return;
     }
 
@@ -54,6 +68,7 @@ export const useAwarenessSyncingEffect = (): void => {
 
   useEffect(() => {
     if (socket == null) {
+      // socket must be null if the user is a guest
       return;
     }
 

+ 6 - 2
apps/app/src/states/socket-io/global-socket.ts

@@ -6,6 +6,8 @@ import { SocketEventName } from '~/interfaces/websocket';
 import { useCurrentPageId } from '~/states/page';
 import loggerFactory from '~/utils/logger';
 
+import { useIsGuestUser } from '../context';
+
 const logger = loggerFactory('growi:states:websocket');
 
 // Constants
@@ -28,6 +30,8 @@ export const useSetupGlobalSocket = (): void => {
   const setSocket = useSetAtom(globalSocketAtom);
   const socket = useAtomValue(globalSocketAtom);
 
+  const isGuestUser = useIsGuestUser();
+
   const initializeSocket = useCallback(async () => {
     try {
       // Dynamic import of socket.io-client
@@ -52,10 +56,10 @@ export const useSetupGlobalSocket = (): void => {
   }, [setSocket]);
 
   useEffect(() => {
-    if (socket == null) {
+    if (!isGuestUser && socket == null) {
       initializeSocket();
     }
-  }, [socket, initializeSocket]);
+  }, [isGuestUser, socket, initializeSocket]);
 };
 
 /**