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

Merge pull request #11026 from growilabs/fix/181823-request-requiring-login-is-being-made-on-the-shared-link-page

fix: Request requiring login is being made on the shared link page
Shun Miyazawa 1 день назад
Родитель
Сommit
ddc10dd5f2

+ 3 - 4
apps/app/src/client/components/Common/Dropdown/PageItemControl.tsx

@@ -371,7 +371,7 @@ export const PageItemControlSubstance = (
 
   const {
     data: fetchedPageInfo,
-    error: fetchError,
+    isLoading: isFetchLoading,
     mutate: mutatePageInfo,
   } = useSWRxPageInfo(shouldFetch ? pageId : null);
 
@@ -399,9 +399,8 @@ export const PageItemControlSubstance = (
     [mutatePageInfo, onClickBookmarkMenuItem, shouldFetch],
   );
 
-  // isLoading should be true only when fetching is in progress (data and error are both undefined)
-  const isLoading =
-    shouldFetch && fetchedPageInfo == null && fetchError == null;
+  // Delegate to SWR's isLoading so that a skipped request (null key) is not treated as loading
+  const isLoading = shouldFetch && isFetchLoading;
   const isDataUnavailable =
     !isLoading && fetchedPageInfo == null && presetPageInfo == null;
 

+ 4 - 1
apps/app/src/features/openai/client/stores/ai-assistant.tsx

@@ -2,6 +2,7 @@ import type { SWRResponse } from 'swr';
 import useSWRImmutable from 'swr/immutable';
 
 import { apiv3Get } from '~/client/util/apiv3-client';
+import { useIsGuestUser } from '~/states/context';
 
 import type { AccessibleAiAssistantsHasId } from '../../interfaces/ai-assistant';
 
@@ -9,8 +10,10 @@ export const useSWRxAiAssistants = (): SWRResponse<
   AccessibleAiAssistantsHasId,
   Error
 > => {
+  const isGuestUser = useIsGuestUser();
+
   return useSWRImmutable<AccessibleAiAssistantsHasId>(
-    ['/openai/ai-assistants'],
+    !isGuestUser ? ['/openai/ai-assistants'] : null,
     ([endpoint]) =>
       apiv3Get(endpoint).then(
         (response) => response.data.accessibleAiAssistants,

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

@@ -90,6 +90,12 @@ export const mutateAllPageInfo = (): Promise<void[]> => {
   return mutate((key) => Array.isArray(key) && key[0] === '/page/info');
 };
 
+const hasShareLinkId = (
+  shareLinkId: string | null | undefined,
+): shareLinkId is string => {
+  return shareLinkId != null && shareLinkId.trim().length > 0;
+};
+
 /**
  * Build query params for /page/info endpoint.
  * Only includes shareLinkId when it is a non-empty string.
@@ -98,7 +104,7 @@ const buildPageInfoParams = (
   pageId: string,
   shareLinkId: string | null | undefined,
 ): { pageId: string; shareLinkId?: string } => {
-  if (shareLinkId != null && shareLinkId.trim().length > 0) {
+  if (hasShareLinkId(shareLinkId)) {
     return { pageId, shareLinkId };
   }
   return { pageId };
@@ -113,9 +119,10 @@ export const useSWRxPageInfo = (
   const isGuestUser = useIsGuestUser();
 
   const key = useMemo(() => {
-    return pageId != null
-      ? ['/page/info', pageId, shareLinkId, isGuestUser]
-      : null;
+    if (pageId == null) return null;
+    // Guests without a share link cannot access page info, so skip the request
+    if (isGuestUser && !hasShareLinkId(shareLinkId)) return null;
+    return ['/page/info', pageId, shareLinkId, isGuestUser];
   }, [shareLinkId, isGuestUser, pageId]);
 
   const swrResult = useSWRImmutable(

+ 6 - 1
apps/app/src/stores/user.tsx

@@ -5,15 +5,20 @@ import useSWRImmutable from 'swr/immutable';
 
 import { apiv3Get } from '~/client/util/apiv3-client';
 import type { PopulatedGrantedGroup } from '~/interfaces/page-grant';
+import { useIsGuestUser } from '~/states/context';
 import { checkAndUpdateImageUrlCached } from '~/stores/middlewares/user';
 
 export const useSWRxUsersList = (
   userIds: string[],
 ): SWRResponse<IUserHasId[], Error> => {
+  const isGuestUser = useIsGuestUser();
   const distinctUserIds =
     userIds.length > 0 ? Array.from(new Set(userIds)).sort() : [];
+
+  const shouldFetch = !isGuestUser && distinctUserIds.length > 0;
+
   return useSWR(
-    distinctUserIds.length > 0 ? ['/users/list', distinctUserIds] : null,
+    shouldFetch ? ['/users/list', distinctUserIds] : null,
     ([endpoint, userIds]) =>
       apiv3Get(endpoint, { userIds: userIds.join(',') }).then((response) => {
         return response.data.users;