Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Home: a guest's page no longer asks for the studio's settings

Adarsh8e59d62f558b
diff --git a/src/components/connect-field.tsx b/src/components/connect-field.tsx
index 0390593f..76483135 100644
--- a/src/components/connect-field.tsx
+++ b/src/components/connect-field.tsx
@@ -7,7 +7,7 @@ import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
-import { saveSettings, testConnection, type ConnectionTest } from "@/lib/api";
+import { saveSettings, testConnection, useViewer, type ConnectionTest } from "@/lib/api";
import { settingProblem, type TestProvider } from "@/lib/connections";
import { addressHost, addressKind, refusedLine, refusedOnHosted } from "@/lib/page-place";
import { cn } from "@/lib/utils";
@@ -21,21 +21,29 @@ export interface PublicSetting {
isSet: boolean;
}
-/** GET /api/settings: every setting, and whether the server may test a private address for a hosted page. */
-const settingsQuery = (enabled: boolean) => ({
- queryKey: ["settings"],
- queryFn: async () => (await (await fetch("/api/settings")).json()) as { settings: PublicSetting[]; allowPrivate?: boolean },
- enabled,
-});
+/**
+ * GET /api/settings: every setting, and whether the server may test a private address for a hosted page. Only the
+ * studio's owner may read them, so it waits until the page knows who is looking and is never asked for anyone else
+ * (a guest's Home still mounts the guide's GPU card, hidden, on a phone).
+ */
+function useSettingsQuery<T>(enabled: boolean, select: (d: { settings: PublicSetting[]; allowPrivate?: boolean }) => T) {
+ const owner = !!useViewer().data?.owner;
+ return useQuery({
+ queryKey: ["settings"],
+ queryFn: async () => (await (await fetch("/api/settings")).json()) as { settings: PublicSetting[]; allowPrivate?: boolean },
+ enabled: enabled && owner,
+ select,
+ });
+}
/** Every setting as the browser may see it (secrets masked). `enabled: false` waits (Home's guide, until the status says it's wanted). */
export function useSettings(enabled = true) {
- return useQuery({ ...settingsQuery(enabled), select: (d) => d.settings });
+ return useSettingsQuery(enabled, (d) => d.settings);
}
/** The server may call a private address (FUOBE_ALLOW_PRIVATE_URLS: a VPN such as Tailscale) for a hosted page. */
export function useAllowPrivate(enabled = true): boolean {
- return useQuery({ ...settingsQuery(enabled), select: (d) => d.allowPrivate === true }).data ?? false;
+ return useSettingsQuery(enabled, (d) => d.allowPrivate === true).data ?? false;
}
interface Result extends ConnectionTest {
diff --git a/tests/settings-read.test.ts b/tests/settings-read.test.ts
new file mode 100644
index 00000000..ca1b8d54
--- /dev/null
+++ b/tests/settings-read.test.ts
@@ -0,0 +1,56 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+// GET /api/settings answers only the studio's owner, so the hooks that read it ask only once the page knows the
+// owner is looking: a guest's or an account's page (Home's guide, the connect dialog) never asks and never logs a 401.
+
+const state = vi.hoisted(() => ({ viewer: undefined as undefined | { owner: boolean; signedIn: boolean; publicSignup: boolean }, asked: [] as { enabled?: boolean }[] }));
+
+vi.mock("@tanstack/react-query", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("@tanstack/react-query")>()),
+ useQuery: (o: { enabled?: boolean }) => {
+ state.asked.push(o);
+ return { data: undefined };
+ },
+}));
+vi.mock("@/lib/api", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("@/lib/api")>()),
+ useViewer: () => ({ data: state.viewer }),
+}));
+
+const { useAllowPrivate, useSettings } = await import("@/components/connect-field");
+
+const enabled = (hook: () => unknown) => {
+ state.asked.length = 0;
+ hook();
+ return state.asked.map((o) => o.enabled);
+};
+
+describe("the settings hooks", () => {
+ beforeEach(() => {
+ state.viewer = undefined;
+ });
+
+ it("wait while the page doesn't yet know who is looking", () => {
+ expect(enabled(() => useSettings())).toEqual([false]);
+ expect(enabled(() => useAllowPrivate())).toEqual([false]);
+ });
+
+ it("never ask for a guest or an account", () => {
+ for (const viewer of [
+ { owner: false, signedIn: false, publicSignup: true },
+ { owner: false, signedIn: true, publicSignup: true },
+ ]) {
+ state.viewer = viewer;
+ expect(enabled(() => useSettings())).toEqual([false]);
+ expect(enabled(() => useAllowPrivate())).toEqual([false]);
+ }
+ });
+
+ it("ask for the owner, unless the caller waits", () => {
+ state.viewer = { owner: true, signedIn: true, publicSignup: true };
+ expect(enabled(() => useSettings())).toEqual([true]);
+ expect(enabled(() => useAllowPrivate())).toEqual([true]);
+ expect(enabled(() => useSettings(false))).toEqual([false]);
+ expect(enabled(() => useAllowPrivate(false))).toEqual([false]);
+ });
+});