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}; };