From 0257370d0e5187243e97bd2a6b8bf3c2dce8102f Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sat, 10 Oct 2026 22:41:20 +0000 Subject: [PATCH 1/3] feat(sources): watch a local folder in four clicks Watch a local folder on the Sensors page opens this machine's allowed folders straight away, on its connection (made on the way when there is none). Each allowed folder has a Watch button, so it no longer has to be opened first. Adding This machine as a connection skips the test-and-name step unless its test fails. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01EH4TJFTrrDKy4pfzvbbPyT --- CHANGELOG.md | 4 + docs/sensors.md | 6 +- .../__tests__/add-connection.test.tsx | 45 +++++++-- nextjs-frontend/__tests__/sensors-ui.test.tsx | 2 + .../__tests__/watch-folder.test.tsx | 99 +++++++++++++++++++ .../components/sensors/sensors-page.tsx | 10 +- .../components/sources/connection-dialog.tsx | 51 +++++++--- .../components/sources/folder-browser.tsx | 46 +++++++-- .../components/sources/sources-page.tsx | 33 ++++++- 9 files changed, 261 insertions(+), 35 deletions(-) create mode 100644 nextjs-frontend/__tests__/watch-folder.test.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index 84f6d93f4..6411a9213 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version ## Unreleased +- **Watch a folder in four clicks.** **Watch a local folder** on the Sensors page opens this machine's allowed + folders, and each has a **Watch** button, so it no longer has to be opened first. Adding **This machine** as a + connection skips the test-and-name step unless its test fails ([Sensors](docs/sensors.md#in-the-web-app)). + - **The graph as files.** Graph nodes are folders, nested along its links: collections inside collections, topics inside their broader topics, entities by type. Each folder holds its assets' files, with a `.json` sidecar of metadata and a `.txt` of the text; everyone sees what the web app shows them. Browse and read it through the API diff --git a/docs/sensors.md b/docs/sensors.md index b44269038..edce29e9d 100644 --- a/docs/sensors.md +++ b/docs/sensors.md @@ -117,7 +117,11 @@ folders feeding their namespaces): - A sensor's page: each stream with its last value and a chart of hourly averages (a day, a week or a month), the latest readings, its kinds of log line to label or drop, and its settings: name, namespace, what it keeps and for how long (empty boxes follow the settings, 0 days keeps them for good), a webhook's new token, a bridge's connection. -- **Files, email and calendars**: the Sources page, as before. +- **Files, email and calendars**: the Sources page, as before. **Watch a local folder**, on the Sensors tab, opens the + folders this machine allows (`sources.local_roots`, set at startup) straight away, each with a **Watch** button, so a + folder is watched in four clicks: Sensors, Watch a local folder, Watch, Start watching. The connection to this + machine is made on the way when there's none yet; it has nothing to fill in, so adding **This machine** in Add + connection doesn't ask for a test or a name either (the test step only shows if the test fails). - **Hub logins**: the usernames devices sign in to the MQTT hub with, with a password made for you. - **Settings → Sensors**: the hub, its ports and networks, and the defaults for what's kept. diff --git a/nextjs-frontend/__tests__/add-connection.test.tsx b/nextjs-frontend/__tests__/add-connection.test.tsx index ccef19baf..4d0a479b1 100644 --- a/nextjs-frontend/__tests__/add-connection.test.tsx +++ b/nextjs-frontend/__tests__/add-connection.test.tsx @@ -13,7 +13,10 @@ jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: jest.mock("@/components/ui/toast", () => ({ useToast: () => jest.fn() })); const ok = (data: unknown) => Promise.resolve({ data, response: { ok: true, status: 200 } }); -const BACKENDS = { local: { label: "This machine", fields: {}, secrets: [] } }; +const BACKENDS = { + local: { label: "This machine", fields: {}, secrets: [] }, + smb: { label: "SMB", fields: { host: "", user: "", port: "", domain: "" }, secrets: ["pass"] }, +}; function show(onSaved: (id: number) => void) { const qc = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } }); @@ -32,16 +35,45 @@ describe("adding a connection", () => { (Sources.updateSource as jest.Mock).mockReset().mockImplementation(() => ok({ id: 7, health: { ok: true } })); }); + it("adds a folder on this machine at once: nothing to fill in, test or name", async () => { + const saved = jest.fn(); + show(saved); + fireEvent.click(screen.getByRole("radio", { name: /This machine/ })); + await waitFor(() => expect(saved).toHaveBeenCalledWith(7)); + expect((Sources.createSource as jest.Mock).mock.calls[0][0].body).toEqual({ + type: "local", + name: "Folder on this machine", + params: {}, + secrets: {}, + }); + expect(Sources.updateSource).not.toHaveBeenCalled(); + }); + + it("shows the test step for a folder on this machine only when its test fails", async () => { + (Sources.createSource as jest.Mock).mockImplementation(() => + ok({ id: 7, health: { ok: false, error: "no sources.local_roots are configured" } }), + ); + const saved = jest.fn(); + show(saved); + fireEvent.click(screen.getByRole("radio", { name: /This machine/ })); + expect(await screen.findByText("no sources.local_roots are configured")).toBeInTheDocument(); + expect(screen.getByLabelText("Name")).toHaveValue("Folder on this machine"); + expect(screen.getByRole("button", { name: "Discard" })).toBeInTheDocument(); + expect(saved).not.toHaveBeenCalled(); + }); + it("goes type → details → tested and named, with no extra steps", async () => { const saved = jest.fn(); show(saved); - fireEvent.click(screen.getByRole("radio", { name: /This machine/ })); // one click on the type moves on + fireEvent.click(screen.getByRole("radio", { name: /SMB/ })); // one click on the type moves on + fireEvent.change(screen.getByLabelText("Host"), { target: { value: "nas.lan" } }); fireEvent.click(screen.getByRole("button", { name: "Test connection" })); const name = await screen.findByLabelText("Name"); - expect(name).toHaveValue("Folder on this machine"); + expect(name).toHaveValue("SMB · nas.lan"); expect((Sources.createSource as jest.Mock).mock.calls[0][0].body).toMatchObject({ - type: "local", - name: "Folder on this machine", + type: "smb", + name: "SMB · nas.lan", + params: { host: "nas.lan" }, }); fireEvent.click(screen.getByRole("button", { name: "Done" })); await waitFor(() => expect(saved).toHaveBeenCalledWith(7)); @@ -51,7 +83,8 @@ describe("adding a connection", () => { it("saves a name changed on the test screen", async () => { const saved = jest.fn(); show(saved); - fireEvent.click(screen.getByRole("radio", { name: /This machine/ })); + fireEvent.click(screen.getByRole("radio", { name: /SMB/ })); + fireEvent.change(screen.getByLabelText("Host"), { target: { value: "nas.lan" } }); fireEvent.click(screen.getByRole("button", { name: "Test connection" })); fireEvent.change(await screen.findByLabelText("Name"), { target: { value: "Scans" } }); fireEvent.click(screen.getByRole("button", { name: "Done" })); diff --git a/nextjs-frontend/__tests__/sensors-ui.test.tsx b/nextjs-frontend/__tests__/sensors-ui.test.tsx index 658aa63b8..49e02a061 100644 --- a/nextjs-frontend/__tests__/sensors-ui.test.tsx +++ b/nextjs-frontend/__tests__/sensors-ui.test.tsx @@ -82,6 +82,8 @@ test("the Sensors page shows the hub, new sensors with their suggestion, streams expect(screen.getByText(/keep everything for two weeks/)).toBeInTheDocument(); expect(screen.getByText("Changes for a week, hourly summaries for 2 years")).toBeInTheDocument(); expect(screen.getByText("NAS")).toBeInTheDocument(); + // one click to this machine's allowed folders + expect(screen.getByRole("link", { name: "Watch a local folder" })).toHaveAttribute("href", "/sources?watch=local"); fireEvent.click(screen.getByRole("button", { name: "Apply" })); await waitFor(() => expect(Sensors.applySuggestion).toHaveBeenCalled()); expect(m(Sensors.applySuggestion).mock.calls[0][0].path).toEqual({ sid: 2 }); diff --git a/nextjs-frontend/__tests__/watch-folder.test.tsx b/nextjs-frontend/__tests__/watch-folder.test.tsx new file mode 100644 index 000000000..08a11c355 --- /dev/null +++ b/nextjs-frontend/__tests__/watch-folder.test.tsx @@ -0,0 +1,99 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; +import "@testing-library/jest-dom"; +import type { ReactNode } from "react"; + +import { Pipelines, Sources } from "@/app/openapi-client"; +import { FolderBrowser } from "@/components/sources/folder-browser"; +import { SourcesPage } from "@/components/sources/sources-page"; +import { TooltipProvider } from "@/components/ui/tooltip"; + +jest.mock("@/app/openapi-client", () => ({ + Sources: { + listBackends: jest.fn(), + listSources: jest.fn(), + listWatches: jest.fn(), + createSource: jest.fn(), + browseSource: jest.fn(), + previewWatch: jest.fn(), + }, + Pipelines: { listPipelines: jest.fn() }, +})); +jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: "t" } }) })); +const replace = jest.fn(); +let search = ""; +jest.mock("next/navigation", () => ({ + useRouter: () => ({ push: jest.fn(), replace }), + usePathname: () => "/sources", + useSearchParams: () => new URLSearchParams(search), +})); +jest.mock("@/lib/hooks/session", () => ({ + useArchive: () => ({ + admin: true, + can: () => true, + namespace: "home", + namespaces: [{ id: 3, name: "home" }], + }), +})); +jest.mock("@/components/ui/toast", () => ({ useToast: () => jest.fn() })); + +const ok = (data: unknown) => Promise.resolve({ data, response: { ok: true, status: 200 } }); +const m = (f: unknown) => f as jest.Mock; +const LOCAL = { id: 5, name: "Folder on this machine", type: "local", params: {}, health: { ok: true } }; + +function wrap(ui: ReactNode) { + const qc = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } }); + return render( + + {ui} + , + ); +} + +beforeEach(() => { + search = ""; + replace.mockReset(); + m(Sources.listBackends).mockReturnValue(ok({ local: { label: "Folder on this machine", fields: {}, secrets: [] } })); + m(Sources.listWatches).mockReturnValue(ok([])); + m(Sources.browseSource).mockReturnValue(ok([{ path: "/audio", rel: "/audio", name: "/audio", dir: true }])); + m(Sources.previewWatch).mockReturnValue(ok({ files: 0, audio: 0, transcripts: 0, documents: 0, images: 0 })); + m(Sources.createSource).mockReset(); + m(Pipelines.listPipelines).mockReturnValue(ok({ pipelines: [] })); +}); + +test("an allowed folder is watched from the list, without opening it first", async () => { + const onWatch = jest.fn(); + wrap( undefined} onWatch={onWatch} canWatch />); + fireEvent.click(await screen.findByRole("button", { name: "Watch /audio" })); + expect(onWatch).toHaveBeenCalledWith("/audio"); + // opening it still works, to watch a folder inside + expect(screen.getByRole("button", { name: "/audio" })).toBeInTheDocument(); +}); + +test("Watch a local folder opens this machine's allowed folders on its existing connection", async () => { + search = "watch=local"; + m(Sources.listSources).mockReturnValue(ok([LOCAL])); + wrap(); + expect(await screen.findByRole("dialog", { name: "Browse Folder on this machine" })).toBeInTheDocument(); + expect(replace).toHaveBeenCalledWith("/sources"); + expect(Sources.createSource).not.toHaveBeenCalled(); + fireEvent.click(await screen.findByRole("button", { name: "Watch /audio" })); + expect(await screen.findByRole("dialog", { name: "Watch Folder on this machine:/audio" })).toBeInTheDocument(); +}); + +test("Watch a local folder makes this machine's connection when there's none, with nothing to fill in", async () => { + search = "watch=local"; + m(Sources.listSources) + .mockReturnValueOnce(ok([])) + .mockReturnValue(ok([LOCAL])); + m(Sources.createSource).mockReturnValue(ok({ id: 5, health: { ok: true } })); + wrap(); + expect(await screen.findByRole("dialog", { name: "Browse Folder on this machine" })).toBeInTheDocument(); + await waitFor(() => expect(Sources.createSource).toHaveBeenCalledTimes(1)); + expect(m(Sources.createSource).mock.calls[0][0].body).toEqual({ + type: "local", + name: "Folder on this machine", + params: {}, + secrets: {}, + }); +}); diff --git a/nextjs-frontend/components/sensors/sensors-page.tsx b/nextjs-frontend/components/sensors/sensors-page.tsx index 3d8212319..553f9930d 100644 --- a/nextjs-frontend/components/sensors/sensors-page.tsx +++ b/nextjs-frontend/components/sensors/sensors-page.tsx @@ -1,6 +1,6 @@ "use client"; -import { Check, EyeOff, Inbox, Plus, RadioTower, Settings } from "lucide-react"; +import { Check, EyeOff, FolderSearch, Inbox, Plus, RadioTower, Settings } from "lucide-react"; import Link from "next/link"; import { useState } from "react"; @@ -247,8 +247,14 @@ export function SensorsPage() { ) )}
-
+

Files, email and calendars

+ {/* straight to this machine's allowed folders, its connection made on the way if there's none */} + diff --git a/nextjs-frontend/components/sources/connection-dialog.tsx b/nextjs-frontend/components/sources/connection-dialog.tsx index 1704fdd80..4292746a1 100644 --- a/nextjs-frontend/components/sources/connection-dialog.tsx +++ b/nextjs-frontend/components/sources/connection-dialog.tsx @@ -51,7 +51,9 @@ function StepTrail({ step, editing }: { step: Step; editing: boolean }) { ); } -/** SO2: add a connection (choose type → details → test, named there), or edit one (details → test). */ +/** SO2: add a connection (choose type → details → test, named there), or edit one (details → test). A type with + * nothing to fill in (a folder on this machine) is added, tested and named at once: the test step only shows if it + * fails. */ export function ConnectionDialog({ open, onOpenChange, @@ -117,21 +119,32 @@ export function ConnectionDialog({ }; const test = useMutation({ - mutationFn: async (mode: "save" | "retest") => { + // `type` and `form` are passed when the type was only just picked (this render's are the previous ones) + mutationFn: async ({ + mode, + type: t = type, + form: f = form, + }: { + mode: "save" | "retest"; + type?: SourceType; + form?: ConnForm; + finish?: boolean; + }) => { const t0 = performance.now(); let id = createdId; let health: SourceHealth; let sent: Record = {}; + let name = ""; if (mode === "retest" && id != null) health = await data(Sources.testSource({ client, path: { sid: id } })); else { - const body = buildPayload(spec, form, type); + const body = buildPayload(backends[t], f, t); sent = body.secrets; if (id == null) { - const name = form.name.trim() || suggestName(type, form.params); + name = f.name.trim() || suggestName(t, f.params); const r = await data( Sources.createSource({ client, - body: { type, name, ...body }, + body: { type: t, name, ...body }, }), ); setCreatedName(name); @@ -151,9 +164,9 @@ export function ConnectionDialog({ health = r.health; } } - return { id, health, ms: Math.round(performance.now() - t0), sent }; + return { id, health, ms: Math.round(performance.now() - t0), sent, name }; }, - onSuccess: ({ id, health, ms, sent }) => { + onSuccess: ({ id, health, ms, sent, name }, { finish }) => { setCreatedId(id); setResult({ health, ms }); // What was just saved counts as set (or cleared) from now on, so going back to the details doesn't ask again. @@ -165,9 +178,13 @@ export function ConnectionDialog({ } return next; }); - setForm((f) => ({ ...f, secrets: {}, tokenText: "" })); - if (!form.name) setForm((f) => ({ ...f, name: suggestName(type, f.params) })); + setForm((f) => ({ ...f, secrets: {}, tokenText: "", name: f.name || name })); refresh(); + if (finish && health.ok && id != null) { + toast({ tone: "green", title: "Connection added", body: name }); + onSaved?.(id); + onOpenChange(false); + } }, onError: (e: Error) => { setStep("details"); @@ -202,7 +219,7 @@ export function ConnectionDialog({ if (Object.keys(e).length) return; setResult(null); setStep("test"); - test.mutate("save"); + test.mutate({ mode: "save" }); }; const title = @@ -251,10 +268,16 @@ export function ConnectionDialog({ aria-checked={on} tabIndex={on ? 0 : -1} onClick={() => { + const blank = emptyForm(backends[t]); setType(t); - setForm(emptyForm(backends[t])); + setForm(blank); setErrors({}); - setStep("details"); + if (Object.keys(backends[t].fields).length || backends[t].secrets.length) setStep("details"); + else { + setResult(null); + setStep("test"); + test.mutate({ mode: "save", type: t, form: blank, finish: true }); + } }} className={cn( "flex h-11 items-center gap-2 rounded-[10px] px-3 text-left text-[13px] font-semibold text-fg transition-colors duration-fast", @@ -396,7 +419,7 @@ export function ConnectionDialog({ Edit details {!result?.health.ok && ( - )} @@ -439,7 +462,7 @@ export function ConnectionDialog({ > Keep anyway - diff --git a/nextjs-frontend/components/sources/folder-browser.tsx b/nextjs-frontend/components/sources/folder-browser.tsx index e82b9ec07..bb5cf7183 100644 --- a/nextjs-frontend/components/sources/folder-browser.tsx +++ b/nextjs-frontend/components/sources/folder-browser.tsx @@ -37,7 +37,8 @@ function crumbs(path: string, local: boolean): { label: string; path: string }[] return out; } -/** SO3: browse a connection's folders, then watch one. */ +/** SO3: browse a connection's folders, then watch one. On this machine, an allowed folder can be watched from the + * list, without opening it first. */ export function FolderBrowser({ source, open, @@ -139,12 +140,20 @@ export function FolderBrowser({ Name - - Size - - - Modified - + {atRoots ? ( + + Watch + + ) : ( + <> + + Size + + + Modified + + + )} @@ -172,8 +181,25 @@ export function FolderBrowser({ {e.name} )} - {e.dir ? "—" : bytes(e.size)} - {e.modified ? shortDate(e.modified) : "—"} + {atRoots ? ( + + + + ) : ( + <> + {e.dir ? "—" : bytes(e.size)} + {e.modified ? shortDate(e.modified) : "—"} + + )} ); })} @@ -194,7 +220,7 @@ export function FolderBrowser({ {q.isSuccess && !atRoots ? `${pickupText({ files: files.length - n("other"), ...here })} in this folder` : atRoots - ? "Pick an allowed folder" + ? "Watch an allowed folder, or open it to pick a folder inside" : ""}