From 867684b3744a20296052d8262ccdd88b7bd00629 Mon Sep 17 00:00:00 2001 From: Marc Seitz Date: Tue, 9 Dec 2025 09:20:42 +0100 Subject: [PATCH] fix: errored requests --- components/links/link-sheet/index.tsx | 2 +- .../components/dataroom-link-sheet.tsx | 2 +- lib/swr/use-domains.ts | 7 +- lib/swr/use-team-ai.ts | 68 +++++-------------- pages/api/teams/[teamId]/ai-settings.ts | 16 +++-- 5 files changed, 36 insertions(+), 59 deletions(-) diff --git a/components/links/link-sheet/index.tsx b/components/links/link-sheet/index.tsx index 6e6e5f5b..cc2305c1 100644 --- a/components/links/link-sheet/index.tsx +++ b/components/links/link-sheet/index.tsx @@ -159,7 +159,7 @@ export default function LinkSheet({ groupId?: string; }; - const { domains } = useDomains(); + const { domains } = useDomains({ enabled: isOpen }); const { viewerGroups, diff --git a/ee/features/permissions/components/dataroom-link-sheet.tsx b/ee/features/permissions/components/dataroom-link-sheet.tsx index 24fabcf6..7aebc9d2 100644 --- a/ee/features/permissions/components/dataroom-link-sheet.tsx +++ b/ee/features/permissions/components/dataroom-link-sheet.tsx @@ -93,7 +93,7 @@ export function DataroomLinkSheet({ groupId?: string; }; - const { domains } = useDomains(); + const { domains } = useDomains({ enabled: isOpen }); const { viewerGroups, diff --git a/lib/swr/use-domains.ts b/lib/swr/use-domains.ts index 9ef00004..0c2a76f0 100644 --- a/lib/swr/use-domains.ts +++ b/lib/swr/use-domains.ts @@ -4,14 +4,17 @@ import useSWR from "swr"; import { fetcher } from "@/lib/utils"; -export function useDomains() { +export function useDomains({ enabled = true }: { enabled?: boolean } = {}) { const teamInfo = useTeam(); const { data: domains, error } = useSWR( - teamInfo?.currentTeam?.id ? `/api/teams/${teamInfo.currentTeam.id}/domains` : null, + enabled && teamInfo?.currentTeam?.id + ? `/api/teams/${teamInfo.currentTeam.id}/domains` + : null, fetcher, { dedupingInterval: 60000, + revalidateOnFocus: false, }, ); diff --git a/lib/swr/use-team-ai.ts b/lib/swr/use-team-ai.ts index 77dedc8b..0e564c48 100644 --- a/lib/swr/use-team-ai.ts +++ b/lib/swr/use-team-ai.ts @@ -1,82 +1,50 @@ import { useTeam } from "@/context/team-context"; -import { useSession } from "next-auth/react"; import useSWR from "swr"; -import { BetaFeatures } from "@/lib/featureFlags"; -import { TeamDetail, CustomUser } from "@/lib/types"; import { fetcher } from "@/lib/utils"; interface TeamAISettings { agentsEnabled: boolean; + vectorStoreId: string | null; + isAdmin: boolean; + isAIFeatureEnabled: boolean; } /** * Hook to check team AI settings and user permissions - * Returns whether AI is feature-flagged for the team, + * Returns whether AI is feature-flagged for the team, * whether it's enabled, and if the current user is an admin */ export function useTeamAI() { - const { data: session } = useSession(); const teamInfo = useTeam(); const teamId = teamInfo?.currentTeam?.id; - // Fetch feature flags to check if AI is enabled for this team - const { data: features, isLoading: featuresLoading } = useSWR< - Record - >(teamId ? `/api/feature-flags?teamId=${teamId}` : null, fetcher, { - dedupingInterval: 60000, - }); - - // Fetch team details to get agentsEnabled and user role - const { data: team, isLoading: teamLoading } = useSWR( - teamId ? `/api/teams/${teamId}` : null, - fetcher, - { - dedupingInterval: 20000, - }, - ); - - // Fetch team AI settings - const { - data: aiSettings, - isLoading: aiSettingsLoading, - mutate: mutateAISettings, - } = useSWR( + const { data, isLoading, mutate } = useSWR( teamId ? `/api/teams/${teamId}/ai-settings` : null, fetcher, { - dedupingInterval: 10000, + dedupingInterval: 30000, + revalidateOnFocus: false, + revalidateOnReconnect: false, }, ); - const userId = (session?.user as CustomUser)?.id; - - // Check if current user is admin - const isAdmin = team?.users.some( - (user) => user.role === "ADMIN" && user.userId === userId, - ); - - // Check if AI feature is available for this team (via feature flags) - const isAIFeatureEnabled = features?.ai ?? false; - - // Check if AI is enabled for this team (team setting) - const isAIEnabled = aiSettings?.agentsEnabled ?? false; - return { // Feature flag - is AI available for this team? - isAIFeatureEnabled, + isAIFeatureEnabled: data?.isAIFeatureEnabled ?? false, // Team setting - is AI enabled for this team? - isAIEnabled, + isAIEnabled: data?.agentsEnabled ?? false, // Is the current user an admin? - isAdmin, + isAdmin: data?.isAdmin ?? false, // Can the user manage AI settings? (admin + feature enabled) - canManageAI: isAdmin && isAIFeatureEnabled, + canManageAI: (data?.isAdmin && data?.isAIFeatureEnabled) ?? false, // Is the feature ready to use? (feature enabled + team enabled) - canUseAI: isAIFeatureEnabled && isAIEnabled, - // Loading states - isLoading: featuresLoading || teamLoading || aiSettingsLoading, + canUseAI: (data?.isAIFeatureEnabled && data?.agentsEnabled) ?? false, + // Vector store ID + vectorStoreId: data?.vectorStoreId ?? null, + // Loading state + isLoading, // Mutate function to refresh AI settings - mutateAISettings, + mutateAISettings: mutate, }; } - diff --git a/pages/api/teams/[teamId]/ai-settings.ts b/pages/api/teams/[teamId]/ai-settings.ts index 35bc53c0..77ae5703 100644 --- a/pages/api/teams/[teamId]/ai-settings.ts +++ b/pages/api/teams/[teamId]/ai-settings.ts @@ -42,11 +42,6 @@ export default async function handle( // Check if AI feature is enabled for this team const features = await getFeatureFlags({ teamId }); - if (!features.ai) { - return res - .status(403) - .json({ error: "AI feature is not available for this team" }); - } if (req.method === "GET") { // GET /api/teams/:teamId/ai-settings @@ -63,15 +58,26 @@ export default async function handle( return res.status(404).json({ error: "Team not found" }); } + const isAdmin = teamAccess.role === "ADMIN"; + return res.status(200).json({ agentsEnabled: team.agentsEnabled, vectorStoreId: team.vectorStoreId, + isAdmin, + isAIFeatureEnabled: features.ai, }); } catch (error) { errorhandler(error, res); } } else if (req.method === "PATCH") { // PATCH /api/teams/:teamId/ai-settings + // AI feature must be enabled for this team + if (!features.ai) { + return res + .status(403) + .json({ error: "AI feature is not available for this team" }); + } + // Only admins can update AI settings if (teamAccess.role !== "ADMIN") { return res.status(403).json({