From acc11e8d1717535ec18af1557318c667de8f1d8c Mon Sep 17 00:00:00 2001 From: Thomas Camlong Date: Fri, 7 Aug 2026 18:18:53 +0200 Subject: [PATCH] fix(assistant): keep availability and authentication as separate states The gate took a single boolean, so a signed-out visitor on an instance that does have the assistant configured was told an administrator still needs to configure it. The server cannot check availability without a session, so collapsing the two states made it state something it could not know. The layout now resolves one of enabled, unconfigured, unauthenticated or error, which maps onto the four messages that already exist in the translations. Dropping the client-side session check from the branch also removes a possible remount once the session settles. --- apps/nextjs/src/app/[locale]/layout.tsx | 19 ++++++----- .../components/assistant/assistant-gate.tsx | 32 +++++++++++-------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/apps/nextjs/src/app/[locale]/layout.tsx b/apps/nextjs/src/app/[locale]/layout.tsx index 493ccdc9c..e665db9e7 100644 --- a/apps/nextjs/src/app/[locale]/layout.tsx +++ b/apps/nextjs/src/app/[locale]/layout.tsx @@ -26,6 +26,7 @@ import type { SupportedLanguage } from "@homarr/translation"; import { isLocaleRTL, isLocaleSupported } from "@homarr/translation"; import { Analytics } from "~/components/layout/analytics"; +import type { AssistantAvailability } from "~/components/assistant/assistant-gate"; import { AssistantGate } from "~/components/assistant/assistant-gate"; import { CrowdinLiveTranslation } from "~/components/layout/crowdin-live-translation"; @@ -86,14 +87,16 @@ export default async function Layout(props: { const session = await auth(); const user = session ? await api.user.getById({ userId: session.user.id }).catch(() => null) : null; const serverSettings = await getServerSettingsAsync(db); - // Resolved on the server so the assistant runtime chunk is never referenced by a page when the - // feature is off. Reuses the same availability check the assistant management page invalidates. - const assistantEnabled = session - ? await api.assistant + // Resolved on the server so the assistant runtime chunk is never referenced by a page unless the + // assistant is actually usable. Reuses the availability check the management page invalidates. + // The states stay distinct so a signed-out visitor is told to sign in rather than that the + // instance is unconfigured, which the server cannot know for them. + const assistantAvailability: AssistantAvailability = !session + ? "unauthenticated" + : await api.assistant .getAvailability() - .then((availability) => availability.enabled) - .catch(() => false) - : false; + .then((availability) => (availability.enabled ? ("enabled" as const) : ("unconfigured" as const))) + .catch(() => "error" as const); const colorScheme = await getCurrentColorSchemeAsync(); const direction = isLocaleRTL((await props.params).locale) ? "rtl" : "ltr"; @@ -132,7 +135,7 @@ export default async function Layout(props: { (innerProps) => , (innerProps) => , (innerProps) => , - (innerProps) => , + (innerProps) => , ]); const { locale } = await props.params; diff --git a/apps/nextjs/src/components/assistant/assistant-gate.tsx b/apps/nextjs/src/components/assistant/assistant-gate.tsx index dbcc42841..b55e232f6 100644 --- a/apps/nextjs/src/components/assistant/assistant-gate.tsx +++ b/apps/nextjs/src/components/assistant/assistant-gate.tsx @@ -4,7 +4,6 @@ import type { PropsWithChildren } from "react"; import { useMemo } from "react"; import dynamic from "next/dynamic"; -import { useSession } from "@homarr/auth/client"; import { useScopedI18n } from "@homarr/translation/client"; import { AssistantWidgetRendererProvider } from "@homarr/widgets"; @@ -51,23 +50,30 @@ const DisabledAssistant = ({ children, description }: PropsWithChildren<{ descri ); }; +/** + * Why the assistant is or is not usable, resolved on the server. + * + * `unauthenticated` is kept apart from `unconfigured` because the server cannot check availability + * for a signed-out visitor, and telling them the instance is unconfigured would be a guess. + */ +export type AssistantAvailability = "enabled" | "unconfigured" | "unauthenticated" | "error"; + interface AssistantGateProps extends PropsWithChildren { - /** Resolved on the server from the instance configuration, not per user. */ - enabled: boolean; + availability: AssistantAvailability; } -export const AssistantGate = ({ enabled, children }: AssistantGateProps) => { +const unavailableMessageKeys = { + unauthenticated: "unavailable.signIn", + unconfigured: "unavailable.notConfigured", + error: "unavailable.error", +} as const; + +export const AssistantGate = ({ availability, children }: AssistantGateProps) => { const t = useScopedI18n("common.assistant"); - const session = useSession(); - if (!enabled) { - return {children}; + if (availability === "enabled") { + return {children}; } - // The instance has the assistant on, but it is still per-user gated behind a session. - if (session.status !== "authenticated") { - return {children}; - } - - return {children}; + return {children}; };