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;
};
const { domains } = useDomains();
const { domains } = useDomains({ enabled: isOpen });
const {
viewerGroups,

View File

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

View File

@@ -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<Domain[]>(
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,
},
);

View File

@@ -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<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>(
const { data, isLoading, mutate } = useSWR<TeamAISettings>(
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,
};
}

View File

@@ -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({