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

fix(app): handle guest user state in SWR hooks for AI assistants and users list

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

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

+ 5 - 8
apps/app/src/stores/page.tsx

@@ -90,15 +90,11 @@ export const mutateAllPageInfo = (): Promise<void[]> => {
   return mutate((key) => Array.isArray(key) && key[0] === '/page/info');
   return mutate((key) => Array.isArray(key) && key[0] === '/page/info');
 };
 };
 
 
-/**
- * Build query params for /page/info endpoint.
- * Only includes shareLinkId when it is a non-empty string.
- */
 const buildPageInfoParams = (
 const buildPageInfoParams = (
   pageId: string,
   pageId: string,
   shareLinkId: string | null | undefined,
   shareLinkId: string | null | undefined,
 ): { pageId: string; shareLinkId?: string } => {
 ): { pageId: string; shareLinkId?: string } => {
-  if (shareLinkId != null && shareLinkId.trim().length > 0) {
+  if (shareLinkId != null) {
     return { pageId, shareLinkId };
     return { pageId, shareLinkId };
   }
   }
   return { pageId };
   return { pageId };
@@ -113,9 +109,10 @@ export const useSWRxPageInfo = (
   const isGuestUser = useIsGuestUser();
   const isGuestUser = useIsGuestUser();
 
 
   const key = useMemo(() => {
   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 && shareLinkId == null) return null;
+    return ['/page/info', pageId, shareLinkId, isGuestUser];
   }, [shareLinkId, isGuestUser, pageId]);
   }, [shareLinkId, isGuestUser, pageId]);
 
 
   const swrResult = useSWRImmutable(
   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 { apiv3Get } from '~/client/util/apiv3-client';
 import type { PopulatedGrantedGroup } from '~/interfaces/page-grant';
 import type { PopulatedGrantedGroup } from '~/interfaces/page-grant';
+import { useIsGuestUser } from '~/states/context';
 import { checkAndUpdateImageUrlCached } from '~/stores/middlewares/user';
 import { checkAndUpdateImageUrlCached } from '~/stores/middlewares/user';
 
 
 export const useSWRxUsersList = (
 export const useSWRxUsersList = (
   userIds: string[],
   userIds: string[],
 ): SWRResponse<IUserHasId[], Error> => {
 ): SWRResponse<IUserHasId[], Error> => {
+  const isGuestUser = useIsGuestUser();
   const distinctUserIds =
   const distinctUserIds =
     userIds.length > 0 ? Array.from(new Set(userIds)).sort() : [];
     userIds.length > 0 ? Array.from(new Set(userIds)).sort() : [];
+
+  const shouldFetch = !isGuestUser && distinctUserIds.length > 0;
+
   return useSWR(
   return useSWR(
-    distinctUserIds.length > 0 ? ['/users/list', distinctUserIds] : null,
+    shouldFetch ? ['/users/list', distinctUserIds] : null,
     ([endpoint, userIds]) =>
     ([endpoint, userIds]) =>
       apiv3Get(endpoint, { userIds: userIds.join(',') }).then((response) => {
       apiv3Get(endpoint, { userIds: userIds.join(',') }).then((response) => {
         return response.data.users;
         return response.data.users;