fix: errored requests

This commit is contained in:
Marc Seitz
2025-12-09 09:20:42 +01:00
parent 758b2fa000
commit 867684b374
5 changed files with 36 additions and 59 deletions

View File

@@ -159,7 +159,7 @@ export default function LinkSheet({
groupId?: string; groupId?: string;
}; };
const { domains } = useDomains(); const { domains } = useDomains({ enabled: isOpen });
const { const {
viewerGroups, viewerGroups,

View File

@@ -93,7 +93,7 @@ export function DataroomLinkSheet({
groupId?: string; groupId?: string;
}; };
const { domains } = useDomains(); const { domains } = useDomains({ enabled: isOpen });
const { const {
viewerGroups, viewerGroups,

View File

@@ -4,14 +4,17 @@ import useSWR from "swr";
import { fetcher } from "@/lib/utils"; import { fetcher } from "@/lib/utils";
export function useDomains() { export function useDomains({ enabled = true }: { enabled?: boolean } = {}) {
const teamInfo = useTeam(); const teamInfo = useTeam();
const { data: domains, error } = useSWR<Domain[]>( const { data: domains, error } = useSWR<Domain[]>(
teamInfo?.currentTeam?.id ? `/api/teams/${teamInfo.currentTeam.id}/domains` : null, enabled && teamInfo?.currentTeam?.id
? `/api/teams/${teamInfo.currentTeam.id}/domains`
: null,
fetcher, fetcher,
{ {
dedupingInterval: 60000, dedupingInterval: 60000,
revalidateOnFocus: false,
}, },
); );

View File

@@ -1,13 +1,13 @@
import { useTeam } from "@/context/team-context"; import { useTeam } from "@/context/team-context";
import { useSession } from "next-auth/react";
import useSWR from "swr"; import useSWR from "swr";
import { BetaFeatures } from "@/lib/featureFlags";
import { TeamDetail, CustomUser } from "@/lib/types";
import { fetcher } from "@/lib/utils"; import { fetcher } from "@/lib/utils";
interface TeamAISettings { interface TeamAISettings {
agentsEnabled: boolean; agentsEnabled: boolean;
vectorStoreId: string | null;
isAdmin: boolean;
isAIFeatureEnabled: boolean;
} }
/** /**
@@ -16,67 +16,35 @@ interface TeamAISettings {
* whether it's enabled, and if the current user is an admin * whether it's enabled, and if the current user is an admin
*/ */
export function useTeamAI() { export function useTeamAI() {
const { data: session } = useSession();
const teamInfo = useTeam(); const teamInfo = useTeam();
const teamId = teamInfo?.currentTeam?.id; const teamId = teamInfo?.currentTeam?.id;
// Fetch feature flags to check if AI is enabled for this team const { data, isLoading, mutate } = useSWR<TeamAISettings>(
const { data: features, isLoading: featuresLoading } = useSWR<
Record<BetaFeatures, boolean>
>(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<TeamDetail>(
teamId ? `/api/teams/${teamId}` : null,
fetcher,
{
dedupingInterval: 20000,
},
);
// Fetch team AI settings
const {
data: aiSettings,
isLoading: aiSettingsLoading,
mutate: mutateAISettings,
} = useSWR<TeamAISettings>(
teamId ? `/api/teams/${teamId}/ai-settings` : null, teamId ? `/api/teams/${teamId}/ai-settings` : null,
fetcher, 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 { return {
// Feature flag - is AI available for this team? // Feature flag - is AI available for this team?
isAIFeatureEnabled, isAIFeatureEnabled: data?.isAIFeatureEnabled ?? false,
// Team setting - is AI enabled for this team? // Team setting - is AI enabled for this team?
isAIEnabled, isAIEnabled: data?.agentsEnabled ?? false,
// Is the current user an admin? // Is the current user an admin?
isAdmin, isAdmin: data?.isAdmin ?? false,
// Can the user manage AI settings? (admin + feature enabled) // 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) // Is the feature ready to use? (feature enabled + team enabled)
canUseAI: isAIFeatureEnabled && isAIEnabled, canUseAI: (data?.isAIFeatureEnabled && data?.agentsEnabled) ?? false,
// Loading states // Vector store ID
isLoading: featuresLoading || teamLoading || aiSettingsLoading, vectorStoreId: data?.vectorStoreId ?? null,
// Loading state
isLoading,
// Mutate function to refresh AI settings // Mutate function to refresh AI settings
mutateAISettings, mutateAISettings: mutate,
}; };
} }

View File

@@ -42,11 +42,6 @@ export default async function handle(
// Check if AI feature is enabled for this team // Check if AI feature is enabled for this team
const features = await getFeatureFlags({ teamId }); 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") { if (req.method === "GET") {
// GET /api/teams/:teamId/ai-settings // GET /api/teams/:teamId/ai-settings
@@ -63,15 +58,26 @@ export default async function handle(
return res.status(404).json({ error: "Team not found" }); return res.status(404).json({ error: "Team not found" });
} }
const isAdmin = teamAccess.role === "ADMIN";
return res.status(200).json({ return res.status(200).json({
agentsEnabled: team.agentsEnabled, agentsEnabled: team.agentsEnabled,
vectorStoreId: team.vectorStoreId, vectorStoreId: team.vectorStoreId,
isAdmin,
isAIFeatureEnabled: features.ai,
}); });
} catch (error) { } catch (error) {
errorhandler(error, res); errorhandler(error, res);
} }
} else if (req.method === "PATCH") { } else if (req.method === "PATCH") {
// PATCH /api/teams/:teamId/ai-settings // 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 // Only admins can update AI settings
if (teamAccess.role !== "ADMIN") { if (teamAccess.role !== "ADMIN") {
return res.status(403).json({ return res.status(403).json({