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.
This commit is contained in:
Thomas Camlong committed 2026-08-07 18:18:53 +02:00
1 parent 09c56002d6
commit acc11e8d17
2 files changed
+30 -21

No files matched your search

+11 -8
View File
@@ -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) => <CustomMantineProvider {...innerProps} defaultColorScheme={colorScheme} />,
(innerProps) => <ModalProvider {...innerProps} />,
(innerProps) => <SpotlightProvider {...innerProps} />,
(innerProps) => <AssistantGate enabled={assistantEnabled} {...innerProps} />,
(innerProps) => <AssistantGate availability={assistantAvailability} {...innerProps} />,
]);
const { locale } = await props.params;
@@ -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 <DisabledAssistant description={t("unavailable.notConfigured")}>{children}</DisabledAssistant>;
if (availability === "enabled") {
return <EnabledAssistantRoot>{children}</EnabledAssistantRoot>;
}
// The instance has the assistant on, but it is still per-user gated behind a session.
if (session.status !== "authenticated") {
return <DisabledAssistant description={t("unavailable.signIn")}>{children}</DisabledAssistant>;
}
return <EnabledAssistantRoot>{children}</EnabledAssistantRoot>;
return <DisabledAssistant description={t(unavailableMessageKeys[availability])}>{children}</DisabledAssistant>;
};