mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-30 21:59:46 +02:00
fix(web):refresh dashboard session queries
This commit is contained in:
parent
be95f65c6b
commit
b37114f193
5 changed files with 25 additions and 20 deletions
|
|
@ -13,13 +13,15 @@ import { Logo } from "@/components/Logo";
|
||||||
import { ModelProviderConnectionsPanel } from "@/components/settings/model-connections/model-provider-connections-panel";
|
import { ModelProviderConnectionsPanel } from "@/components/settings/model-connections/model-provider-connections-panel";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { useGlobalLoadingEffect } from "@/hooks/use-global-loading";
|
import { useGlobalLoadingEffect } from "@/hooks/use-global-loading";
|
||||||
import { getBearerToken, redirectToLogin } from "@/lib/auth-utils";
|
import { useSession } from "@/hooks/use-session";
|
||||||
|
import { redirectToLogin } from "@/lib/auth-utils";
|
||||||
import { hasEnabledChatModel, isLlmOnboardingComplete } from "@/lib/onboarding";
|
import { hasEnabledChatModel, isLlmOnboardingComplete } from "@/lib/onboarding";
|
||||||
|
|
||||||
export default function OnboardPage() {
|
export default function OnboardPage() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const params = useParams();
|
const params = useParams();
|
||||||
const searchSpaceId = Number(params.search_space_id);
|
const searchSpaceId = Number(params.search_space_id);
|
||||||
|
const session = useSession();
|
||||||
const { data: globalConnections = [], isLoading: globalLoading } = useAtomValue(
|
const { data: globalConnections = [], isLoading: globalLoading } = useAtomValue(
|
||||||
globalModelConnectionsAtom
|
globalModelConnectionsAtom
|
||||||
);
|
);
|
||||||
|
|
@ -29,8 +31,8 @@ export default function OnboardPage() {
|
||||||
useAtomValue(globalLlmConfigStatusAtom);
|
useAtomValue(globalLlmConfigStatusAtom);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!getBearerToken()) redirectToLogin();
|
if (session.status === "unauthenticated") redirectToLogin();
|
||||||
}, []);
|
}, [session.status]);
|
||||||
|
|
||||||
const hasUsableChatModel = useMemo(
|
const hasUsableChatModel = useMemo(
|
||||||
() => hasEnabledChatModel([...globalConnections, ...connections]),
|
() => hasEnabledChatModel([...globalConnections, ...connections]),
|
||||||
|
|
@ -43,7 +45,8 @@ export default function OnboardPage() {
|
||||||
connections
|
connections
|
||||||
);
|
);
|
||||||
|
|
||||||
const isLoading = globalLoading || rolesLoading || globalConfigStatusLoading;
|
const isLoading =
|
||||||
|
session.status === "loading" || globalLoading || rolesLoading || globalConfigStatusLoading;
|
||||||
|
|
||||||
// Onboarding only applies when no global_llm_config.yaml exists. If a global
|
// Onboarding only applies when no global_llm_config.yaml exists. If a global
|
||||||
// config is present (or onboarding is already complete), leave this page.
|
// config is present (or onboarding is already complete), leave this page.
|
||||||
|
|
|
||||||
|
|
@ -3,31 +3,32 @@
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { USER_QUERY_KEY } from "@/atoms/user/user-query.atoms";
|
import { USER_QUERY_KEY } from "@/atoms/user/user-query.atoms";
|
||||||
import { useGlobalLoadingEffect } from "@/hooks/use-global-loading";
|
import { useGlobalLoadingEffect } from "@/hooks/use-global-loading";
|
||||||
import { ensureTokensFromElectron, getBearerToken, redirectToLogin } from "@/lib/auth-utils";
|
import { useSession } from "@/hooks/use-session";
|
||||||
|
import { ensureTokensFromElectron, redirectToLogin } from "@/lib/auth-utils";
|
||||||
import { queryClient } from "@/lib/query-client/client";
|
import { queryClient } from "@/lib/query-client/client";
|
||||||
|
|
||||||
export function DashboardShell({ children }: { children: React.ReactNode }) {
|
export function DashboardShell({ children }: { children: React.ReactNode }) {
|
||||||
const [isCheckingAuth, setIsCheckingAuth] = useState(true);
|
const [isCheckingAuth, setIsCheckingAuth] = useState(true);
|
||||||
|
const session = useSession();
|
||||||
|
|
||||||
// Use the global loading screen - spinner animation won't reset
|
// Use the global loading screen - spinner animation won't reset
|
||||||
useGlobalLoadingEffect(isCheckingAuth);
|
useGlobalLoadingEffect(isCheckingAuth);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function checkAuth() {
|
async function checkAuth() {
|
||||||
let token = getBearerToken();
|
if (typeof window !== "undefined" && window.electronAPI) {
|
||||||
if (!token) {
|
await ensureTokensFromElectron();
|
||||||
const synced = await ensureTokensFromElectron();
|
|
||||||
if (synced) token = getBearerToken();
|
|
||||||
}
|
}
|
||||||
if (!token) {
|
if (session.status === "loading") return;
|
||||||
|
if (session.status === "unauthenticated") {
|
||||||
redirectToLogin();
|
redirectToLogin();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
queryClient.invalidateQueries({ queryKey: [...USER_QUERY_KEY] });
|
queryClient.invalidateQueries({ queryKey: [...USER_QUERY_KEY] });
|
||||||
setIsCheckingAuth(false);
|
setIsCheckingAuth(false);
|
||||||
}
|
}
|
||||||
checkAuth();
|
void checkAuth();
|
||||||
}, []);
|
}, [session.status]);
|
||||||
|
|
||||||
// Return null while loading - the global provider handles the loading UI
|
// Return null while loading - the global provider handles the loading UI
|
||||||
if (isCheckingAuth) {
|
if (isCheckingAuth) {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { atomWithQuery } from "jotai-tanstack-query";
|
import { atomWithQuery } from "jotai-tanstack-query";
|
||||||
import { agentFlagsApiService } from "@/lib/apis/agent-flags-api.service";
|
import { agentFlagsApiService } from "@/lib/apis/agent-flags-api.service";
|
||||||
import { getBearerToken } from "@/lib/auth-utils";
|
import { isAuthenticated } from "@/lib/auth-utils";
|
||||||
|
|
||||||
export const AGENT_FLAGS_QUERY_KEY = ["agent", "flags"] as const;
|
export const AGENT_FLAGS_QUERY_KEY = ["agent", "flags"] as const;
|
||||||
|
|
||||||
|
|
@ -12,6 +12,6 @@ export const AGENT_FLAGS_QUERY_KEY = ["agent", "flags"] as const;
|
||||||
export const agentFlagsAtom = atomWithQuery(() => ({
|
export const agentFlagsAtom = atomWithQuery(() => ({
|
||||||
queryKey: AGENT_FLAGS_QUERY_KEY,
|
queryKey: AGENT_FLAGS_QUERY_KEY,
|
||||||
staleTime: 10 * 60 * 1000,
|
staleTime: 10 * 60 * 1000,
|
||||||
enabled: !!getBearerToken(),
|
enabled: isAuthenticated(),
|
||||||
queryFn: () => agentFlagsApiService.get(),
|
queryFn: () => agentFlagsApiService.get(),
|
||||||
}));
|
}));
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,26 @@
|
||||||
import { atomWithQuery } from "jotai-tanstack-query";
|
import { atomWithQuery } from "jotai-tanstack-query";
|
||||||
import { modelConnectionsApiService } from "@/lib/apis/model-connections-api.service";
|
import { modelConnectionsApiService } from "@/lib/apis/model-connections-api.service";
|
||||||
import { getBearerToken } from "@/lib/auth-utils";
|
import { isAuthenticated } from "@/lib/auth-utils";
|
||||||
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
||||||
import { activeSearchSpaceIdAtom } from "../search-spaces/search-space-query.atoms";
|
import { activeSearchSpaceIdAtom } from "../search-spaces/search-space-query.atoms";
|
||||||
|
|
||||||
export const globalModelConnectionsAtom = atomWithQuery(() => ({
|
export const globalModelConnectionsAtom = atomWithQuery(() => ({
|
||||||
queryKey: cacheKeys.modelConnections.global(),
|
queryKey: cacheKeys.modelConnections.global(),
|
||||||
enabled: !!getBearerToken(),
|
enabled: isAuthenticated(),
|
||||||
staleTime: 10 * 60 * 1000,
|
staleTime: 10 * 60 * 1000,
|
||||||
queryFn: () => modelConnectionsApiService.getGlobalConnections(),
|
queryFn: () => modelConnectionsApiService.getGlobalConnections(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
export const globalLlmConfigStatusAtom = atomWithQuery(() => ({
|
export const globalLlmConfigStatusAtom = atomWithQuery(() => ({
|
||||||
queryKey: cacheKeys.modelConnections.globalConfigStatus(),
|
queryKey: cacheKeys.modelConnections.globalConfigStatus(),
|
||||||
enabled: !!getBearerToken(),
|
enabled: isAuthenticated(),
|
||||||
staleTime: 60 * 60 * 1000,
|
staleTime: 60 * 60 * 1000,
|
||||||
queryFn: () => modelConnectionsApiService.getGlobalLlmConfigStatus(),
|
queryFn: () => modelConnectionsApiService.getGlobalLlmConfigStatus(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
export const modelProvidersAtom = atomWithQuery(() => ({
|
export const modelProvidersAtom = atomWithQuery(() => ({
|
||||||
queryKey: cacheKeys.modelConnections.providers(),
|
queryKey: cacheKeys.modelConnections.providers(),
|
||||||
enabled: !!getBearerToken(),
|
enabled: isAuthenticated(),
|
||||||
staleTime: 60 * 60 * 1000,
|
staleTime: 60 * 60 * 1000,
|
||||||
queryFn: () => modelConnectionsApiService.getModelProviders(),
|
queryFn: () => modelConnectionsApiService.getModelProviders(),
|
||||||
}));
|
}));
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { atomWithQuery } from "jotai-tanstack-query";
|
import { atomWithQuery } from "jotai-tanstack-query";
|
||||||
import { userApiService } from "@/lib/apis/user-api.service";
|
import { userApiService } from "@/lib/apis/user-api.service";
|
||||||
import { getBearerToken } from "@/lib/auth-utils";
|
import { isAuthenticated } from "@/lib/auth-utils";
|
||||||
|
|
||||||
export const USER_QUERY_KEY = ["user", "me"] as const;
|
export const USER_QUERY_KEY = ["user", "me"] as const;
|
||||||
const userQueryFn = () => userApiService.getMe();
|
const userQueryFn = () => userApiService.getMe();
|
||||||
|
|
@ -12,7 +12,8 @@ export const currentUserAtom = atomWithQuery(() => {
|
||||||
// are now pushed via Zero (queries.user.me()), so /users/me only
|
// are now pushed via Zero (queries.user.me()), so /users/me only
|
||||||
// needs to fire once per session for the static profile fields.
|
// needs to fire once per session for the static profile fields.
|
||||||
staleTime: Infinity,
|
staleTime: Infinity,
|
||||||
enabled: !!getBearerToken(),
|
enabled: isAuthenticated(),
|
||||||
|
retry: false,
|
||||||
queryFn: userQueryFn,
|
queryFn: userQueryFn,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue