Can you add a workspace filter

merged…
osebo51d ago

Let’s put this on the home page

6 repliesoldestnewest
Osebotfable-5-151d ago

Plan: add a workspace filter to the home page thread list, next to the live search box.

  • components/thread-browser.tsx: new <select name="workspace"> (All workspaces / each workspace / "General (no workspace)") inside the search form. It participates in the same live-fetch flow: changing it re-queries GET /api/threads?q=…&workspaceId=… immediately (no debounce), URL mirrored as ?workspace=… so the filtered view is shareable, and it degrades to a GET form submit without JS.
  • app/page.tsx: read ?workspace= from searchParams and server-render the first paint already filtered.
  • lib/data.ts / app/api/threads/route.ts: support workspaceId=none for general-discussion threads (workspace_id IS NULL) so the "General" option works.
  • Workspace page keeps its fixed workspaceId and no select (nothing to filter by).

Will post a screenshot via the static-mockup route since the app can't run in the sandbox.

oseboasked Osebot51d ago

I’d also like it to be divided by Everyone and Yours

Osebotfable-5-151d ago

Updated plan (adds the Everyone / Yours split):

  • Home page gets a filter row under the "Threads" title: a segmented Everyone | Yours toggle plus a workspace <select> (All workspaces / each workspace / General). Both live in the same GET form as the search box, so they drive the live fetch (GET /api/threads?q=&workspaceId=&mine=1) immediately on change, mirror into the URL (?who=mine&workspace=…) and degrade to a plain form submit without JS.
  • Yours = threads you started or replied to (same "participant" notion the email notifications use). Everyone = current behaviour.
  • lib/data.ts listThreads → options object { workspaceId, q, participantId }, where workspaceId: null means general (no workspace) threads. API route reads workspaceId=none + mine=1; app/page.tsx server-renders the first paint already filtered from the URL.
  • Workspace page keeps its fixed workspace and search only.

Screenshot via static mockup to follow.

Osebotfable-5-151d agothreadide@baeba30

Done — the home page now has a workspace filter next to the search box. Pushed to thread/bf9285dd as baeba30 (no PR opened; say the word if you want one).

What it does

  • A <select> left of "Search threads…": All workspaces / each workspace (sorted by name, same order as listWorkspaces) / General (no workspace).
  • Picking one re-fetches GET /api/threads?workspaceId=… immediately (typing still debounces 200ms) and swaps the list in place, reusing the live-search plumbing (seq counter, abort, .is-loading dim).
  • The choice is mirrored into the URL as ?workspace=<uuid|general> alongside ?q=, so a filtered view is shareable, and the first paint is server-rendered already filtered (no flash). Unknown ids fall back to "all".
  • The filter combines with search; empty states say e.g. No threads match "foo" in Threads. / No threads in General yet.
  • Without JS it's still a GET form: Enter submits q + workspace; the × clear link keeps the workspace param.

Server side

  • listThreads(workspaceId?: string | null, q?): null now means workspace_id IS NULL (general-discussion threads); undefined = everything, uuid = that workspace. Workspace page is unaffected.
  • /api/threads?workspaceId=general maps to that null. The sentinel GENERAL_WORKSPACE lives in new dependency-free lib/workspace-filter.ts so the client component and server can share it without pulling lib/data.ts into the client bundle.

UI

  • .thread-filter in globals.css: native select restyled to match the search input (3px radius, same height), chevron drawn with a CSS rotated border (no font glyph, per gotchas/unicode-icon-glyphs). Idle = muted text/border; .has-value = full-contrast so you can tell at a glance a filter is active. Dark theme follows via vars.
  • ≤560px: the select takes ~45% and the search box the rest of the full-width row under the title.
  • Only the home page passes filter; the workspace page's ThreadBrowser is unchanged.

Screenshots attached are static mockups on the real globals.css (the app needs Postgres + GitHub OAuth, so it can't run in the sandbox). tsc --noEmit is clean.

Home page at 390px: unfiltered (muted select) vs filtered to the "Threads" workspace — static mockup on real globals.css
Home page at 390px: unfiltered (muted select) vs filtered to the "Threads" workspace — static mockup on real globals.css
Home page at 960px with the workspace filter beside the search box
Home page at 960px with the workspace filter beside the search box
Diff · 6 files
diff --git a/threadide/app/api/threads/route.ts b/threadide/app/api/threads/route.ts
--- a/threadide/app/api/threads/route.ts
+++ b/threadide/app/api/threads/route.ts
@@ -1,6 +1,7 @@
 import { NextResponse } from "next/server";
 import { z } from "zod";
 import { createThread, listThreads } from "@/lib/data";
+import { GENERAL_WORKSPACE } from "@/lib/workspace-filter";
 import { guarded, requireMember, requireUserApi } from "@/lib/session";
@@ -9,7 +10,9 @@ export const maxDuration = 300;
 export const GET = guarded(async (req: Request) => {
   await requireUserApi();
   const params = new URL(req.url).searchParams;
-  const workspaceId = params.get("workspaceId") ?? undefined;
+  // workspaceId: omitted = all threads; a uuid = that workspace; "general" = threads with no workspace.
+  const rawWorkspace = params.get("workspaceId");
+  const workspaceId = rawWorkspace === GENERAL_WORKSPACE ? null : rawWorkspace || undefined;
diff --git a/threadide/app/globals.css b/threadide/app/globals.css
--- a/threadide/app/globals.css
+++ b/threadide/app/globals.css
@@ -67,7 +67,22 @@
-.thread-search { display: flex; align-items: center; flex: 0 1 300px; margin-left: auto; min-width: 0; }
+.thread-search { display: flex; align-items: center; gap: 6px; flex: 0 1 440px; margin-left: auto; min-width: 0; }
+/* Workspace filter (home page only): a native <select> restyled to match the search input, with the
+   chevron drawn as a rotated bordered square so it inherits the text colour in both themes. */
+.thread-filter { position: relative; display: flex; flex: 0 1 180px; min-width: 0; }
+.thread-filter select {
+  flex: 1 1 auto; min-width: 0; padding: 6px 26px 6px 10px; border-radius: 3px; border: 1px solid var(--border-strong);
+  background: var(--surface); color: var(--muted); font: inherit; font-size: 14px; line-height: normal; cursor: pointer;
+  -webkit-appearance: none; appearance: none; text-overflow: ellipsis; white-space: nowrap; overflow: hidden;
+}
+.thread-filter.has-value select { color: var(--text); border-color: var(--text); }
+.thread-filter select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
+.thread-filter::after {
+  content: ""; position: absolute; right: 11px; top: 50%; width: 6px; height: 6px; margin-top: -5px;
+  border: solid currentColor; border-width: 0 1.5px 1.5px 0; transform: rotate(45deg); color: var(--muted); pointer-events: none;
+}
+.thread-filter.has-value::after { color: var(--text); }
@@ -93,6 +108,7 @@
   .thread-search { order: 1; flex: 1 1 100%; margin-left: 0; }
+  .thread-filter { flex: 0 1 45%; }
 }
diff --git a/threadide/app/page.tsx b/threadide/app/page.tsx
--- a/threadide/app/page.tsx
+++ b/threadide/app/page.tsx
@@ -3,23 +3,30 @@
+import { GENERAL_WORKSPACE } from "@/lib/workspace-filter";
-export default async function Home({ searchParams }: { searchParams: Promise<{ q?: string }> }) {
+export default async function Home({ searchParams }: { searchParams: Promise<{ q?: string; workspace?: string }> }) {
   const user = await requireUser("/");
-  const q = (await searchParams).q?.trim() ?? "";
+  const sp = await searchParams;
+  const q = sp.q?.trim() ?? "";
-  const [threads, workspaces] = await Promise.all([listThreads(undefined, q), listWorkspaces(user.id)]);
+  const workspaces = await listWorkspaces(user.id);
+  // ?workspace= filter: a known workspace id, "general" (threads with no workspace), else everything.
+  const workspace = sp.workspace === GENERAL_WORKSPACE || workspaces.some((w) => w.id === sp.workspace) ? sp.workspace : "";
+  const threads = await listThreads(workspace === GENERAL_WORKSPACE ? null : workspace || undefined, q);
@@
     <ThreadBrowser
       initialQ={q}
+      initialWorkspace={workspace}
       initialThreads={threads}
       workspaces={wsById}
+      filter={workspaces.map((w) => ({ id: w.id, name: w.name }))}
diff --git a/threadide/lib/data.ts b/threadide/lib/data.ts
--- a/threadide/lib/data.ts
+++ b/threadide/lib/data.ts
@@ -184,17 +184,19 @@
+ * `workspaceId`: undefined = every thread; a uuid = that workspace only; null = general-discussion
+ * threads only (workspace_id IS NULL — the "General" option of the home-page filter).
-export async function listThreads(workspaceId?: string, q?: string) {
+export async function listThreads(workspaceId?: string | null, q?: string) {
@@
-    where: and(workspaceId ? eq(threads.workspaceId, workspaceId) : undefined, ...termFilters),
+    where: and(workspaceId === null ? isNull(threads.workspaceId) : workspaceId ? eq(threads.workspaceId, workspaceId) : undefined, ...termFilters),
diff --git a/threadide/lib/workspace-filter.ts b/threadide/lib/workspace-filter.ts
new file mode 100644
--- /dev/null
+++ b/threadide/lib/workspace-filter.ts
@@ -0,0 +1,6 @@
+/**
+ * Sentinel for the home-page workspace filter and GET /api/threads?workspaceId=…: selects threads
+ * that have NO workspace (general discussion). Lives in its own dependency-free module so both the
+ * client ThreadBrowser and the server (lib/data.ts, the API route) can import it.
+ */
+export const GENERAL_WORKSPACE = "general";
diff --git a/threadide/components/thread-browser.tsx b/threadide/components/thread-browser.tsx
--- a/threadide/components/thread-browser.tsx
+++ b/threadide/components/thread-browser.tsx
@@ (summary — 84 lines changed)
+import { GENERAL_WORKSPACE } from "@/lib/workspace-filter";
+  /** Show a workspace <select> (home page). Options are listed in this order; "General" is appended. */
+  filter?: { id: string; name: string }[];
+  /** Server-rendered ?workspace= value: a workspace id, GENERAL_WORKSPACE, or "" for all. */
+  initialWorkspace?: string;
...
+  const [ws, setWs] = useState(initialWorkspace);
+  const [results, setResults] = useState<{ q: string; ws: string; threads: ThreadRow[] } | null>(null);
+  const lastQ = useRef(initialQ);
+  const matchesServer = trimmed === initialQ && ws === initialWorkspace;
+  const shown = matchesServer ? initialThreads : results?.q === trimmed && results.ws === ws ? results.threads : null;
...
-  const search = (term: string) => {
+  const search = (term: string, workspace: string) => {
     if (workspaceId) params.set("workspaceId", workspaceId);
+    else if (workspace) params.set("workspaceId", workspace);
...
   useEffect(() => {
+    const typed = trimmed !== lastQ.current;
+    lastQ.current = trimmed;
...
-      timer.current = setTimeout(() => search(trimmed), 200);
+      timer.current = setTimeout(() => search(trimmed, ws), typed ? 200 : 0);
+    const params = new URLSearchParams();
+    if (trimmed) params.set("q", trimmed);
+    if (ws) params.set("workspace", ws);
...
-  }, [trimmed]);
+  }, [trimmed, ws]);
...
+  // No-JS target for the clear ×: drop q but keep the workspace filter.
+  const clearHref = ws ? `${action.split("#")[0]}?workspace=${encodeURIComponent(ws)}…` : action;
+  const wsName = ws === GENERAL_WORKSPACE ? "General" : filter?.find((w) => w.id === ws)?.name;
+  const empty = trimmed ? `No threads match “${trimmed}”${wsName ? ` in ${wsName}` : ""}.` : wsName ? `No threads in ${wsName} yet.` : emptyDefault;
...
+          {filter && (
+            <span className={ws ? "thread-filter has-value" : "thread-filter"}>
+              <select name="workspace" value={ws} onChange={(e) => setWs(e.target.value)} aria-label="Filter by workspace" title="Filter by workspace">
+                <option value="">All workspaces</option>
+                {filter.map((w) => (<option key={w.id} value={w.id}>{w.name}</option>))}
+                <option value={GENERAL_WORKSPACE}>General (no workspace)</option>
+              </select>
+            </span>
+          )}
Osebotfable-5-151d agothreadide@f51d264

Done — pushed to thread/bf9285dd (2 commits: the earlier workspace-select commit baeba30 from the first run, plus f51d264 which adds Everyone / Yours and moves both filters into their own row).

What the home page does now

A filter row sits under the Threads title, in the same GET form as the search box:

  • Everyone | Yours segmented toggle. Yours = threads you started or replied to (same "participant" notion the email notifications use). If you'd rather it mean only threads you created, it's a one-line change in listThreads — say the word.
  • Workspace select: All workspaces / each workspace / General (no workspace).
  • Changing either fetches immediately (GET /api/threads?workspaceId=<id|general>&mine=1&q=); typing still has the 200 ms debounce. URL mirrors the state as /?who=mine&workspace=<id|general>&q=…, so filtered views are shareable and the first paint is server-rendered already filtered. Without JS the radios/select still submit with Enter.
  • Empty states are filter-aware ("You haven't started or replied to any threads in X yet.", "No general discussion threads yet.").

Code notes for teammates

  • lib/data.ts listThreads now takes an object: { workspaceId?: string | null, q?, participantId? } — null = workspace_id IS NULL. Callers updated (API route, home, workspace page).
  • components/thread-browser.tsx: .page-head is now inside <form class="thread-browser"> (so .is-loading moved from .thread-search to the form). Filters render only with the filterable prop; parseFilters() is exported for the server page. lib/workspace-filter.ts keeps the "general" sentinel shared by client + server.
  • Workspace page: unchanged UI (fixed workspace, search only).
  • tsc --noEmit clean. Screenshots are static mockups (app can't run in the sandbox) at 390px and 960px.

No PR opened — tell me if you want one.

Home page at 390px: Everyone | Yours toggle + workspace select under the title (static mockup, "Yours" + "Threads" selected)
Home page at 390px: Everyone | Yours toggle + workspace select under the title (static mockup, "Yours" + "Threads" selected)
Home page at 960px: filter row sits between the title row and the list
Home page at 960px: filter row sits between the title row and the list
Home page at 390px (static mockup): Everyone | Yours toggle + workspace select under the title row, "Yours" + "Threads" selected
Home page at 390px (static mockup): Everyone | Yours toggle + workspace select under the title row, "Yours" + "Threads" selected
Home page at 960px (static mockup): filter row between the title row and the list
Home page at 960px (static mockup): filter row between the title row and the list
Diff · 7 files
diff --git a/threadide/lib/data.ts b/threadide/lib/data.ts
@@
+export type ThreadFilter = {
+  /** A workspace id, or null for general-discussion threads (workspace_id IS NULL). Omit for every thread. */
+  workspaceId?: string | null;
+  /** Keyword search over titles + post bodies. */
+  q?: string;
+  /** Only threads this user started or posted in (the home page's "Yours" filter). */
+  participantId?: string;
+};
-export async function listThreads(workspaceId?: string, q?: string) {
+export async function listThreads({ workspaceId, q, participantId }: ThreadFilter = {}) {
   const termFilters = searchTerms(q).map(...);
+  const scope = workspaceId === null ? isNull(threads.workspaceId) : workspaceId ? eq(threads.workspaceId, workspaceId) : undefined;
+  const mine = participantId
+    ? sql`(${threads.createdBy} = ${participantId} or exists (select 1 from posts p where p.thread_id = ${threads.id} and p.author_user_id = ${participantId}))`
+    : undefined;
   return db.query.threads.findMany({
-    where: and(workspaceId ? eq(threads.workspaceId, workspaceId) : undefined, ...termFilters),
+    where: and(scope, mine, ...termFilters),

diff --git a/threadide/app/api/threads/route.ts b/threadide/app/api/threads/route.ts
@@
-  await requireUserApi();
+  const user = await requireUserApi();
   const params = new URL(req.url).searchParams;
-  const workspaceId = params.get("workspaceId") ?? undefined;
+  // workspaceId: omitted = all threads; a uuid = that workspace; "general" = threads with no workspace.
+  const rawWorkspace = params.get("workspaceId");
+  const workspaceId = rawWorkspace === GENERAL_WORKSPACE ? null : rawWorkspace || undefined;
   const q = params.get("q") ?? undefined;
-  const rows = await listThreads(workspaceId, q);
+  // mine=1: only threads the caller started or posted in ("Yours").
+  const participantId = params.get("mine") === "1" ? user.id : undefined;
+  const rows = await listThreads({ workspaceId, q, participantId });

diff --git a/threadide/app/page.tsx b/threadide/app/page.tsx
@@
-export default async function Home({ searchParams }: { searchParams: Promise<{ q?: string }> }) {
+export default async function Home({ searchParams }: { searchParams: Promise<{ q?: string; workspace?: string; who?: string }> }) {
   const user = await requireUser("/");
-  const q = (await searchParams).q?.trim() ?? "";
-  const [threads, workspaces] = await Promise.all([listThreads(undefined, q), listWorkspaces(user.id)]);
+  const params = await searchParams;
+  const q = params.q?.trim() ?? "";
+  const workspaces = await listWorkspaces(user.id);
+  const wsById = Object.fromEntries(workspaces.map((w) => [w.id, { name: w.name, repos: w.repos.map((r) => ({ alias: r.alias })) }]));
+  // ?workspace=<id>|general and ?who=mine — the same filters the live fetch sends to /api/threads.
+  const filters = parseFilters(params, wsById);
+  const threads = await listThreads({
+    workspaceId: filters.workspace === GENERAL_WORKSPACE ? null : filters.workspace || undefined,
+    q,
+    participantId: filters.who === "mine" ? user.id : undefined,
+  });
   after(() => syncStaleThreads(threads));
   return (
     <ThreadBrowser
       initialQ={q}
+      initialFilters={filters}
       initialThreads={threads}
       workspaces={wsById}
+      filterable
       action="/"

diff --git a/threadide/components/thread-browser.tsx b/threadide/components/thread-browser.tsx
@@
+export type Who = "all" | "mine";
+export type ThreadFilters = { workspace: string; who: Who };
+
+/** `?workspace=…&who=…` → validated filters. Shared by the page (first paint) and the browser (URL parsing). */
+export function parseFilters(params: { workspace?: string; who?: string }, knownWorkspaces: Record<string, unknown>): ThreadFilters {
+  const ws = params.workspace ?? "";
+  return {
+    workspace: ws === GENERAL_WORKSPACE || ws in knownWorkspaces ? ws : "",
+    who: params.who === "mine" ? "mine" : "all",
+  };
+}
@@ export function ThreadBrowser({
   const [q, setQ] = useState(initialQ);
+  const [filters, setFilters] = useState<ThreadFilters>(initialFilters);
+  const lastQ = useRef(initialQ); // to tell "the query changed" (debounce) from "a filter changed" (fetch now)
   ...
-  const shown = trimmed === initialQ ? initialThreads : results?.q === trimmed ? results.threads : null;
+  const keyOf = (term: string, f: ThreadFilters) => JSON.stringify([term, f.workspace, f.who]);
+  const key = keyOf(trimmed, filters);
+  const initialKey = keyOf(initialQ, initialFilters);
+  const shown = key === initialKey ? initialThreads : results?.key === key ? results.threads : null;
@@ search():
-    if (workspaceId) params.set("workspaceId", workspaceId);
+    const ws = workspaceId ?? f.workspace; // GENERAL_WORKSPACE passes through; the API maps it to workspace_id IS NULL
+    if (ws) params.set("workspaceId", ws);
+    if (f.who === "mine") params.set("mine", "1");
@@ effect:
-    if (trimmed === initialQ) { …cancel… } else { timer = setTimeout(() => search(trimmed), 200); }
-    const url = `${base}${trimmed ? `?q=${encodeURIComponent(trimmed)}` : ""}${hash}`;
+    const typed = trimmed !== lastQ.current; lastQ.current = trimmed;
+    if (key === initialKey) { …cancel… }
+    else if (typed) { timer.current = setTimeout(() => search(trimmed, filters), 200); }
+    else { search(trimmed, filters); }
+    const params = new URLSearchParams();
+    if (trimmed) params.set("q", trimmed);
+    if (filters.workspace) params.set("workspace", filters.workspace);
+    if (filters.who === "mine") params.set("who", "mine");
+    const url = `${base}${qs ? `?${qs}` : ""}${window.location.hash}`;
@@ markup:
-      <div className="page-head" id={anchorId}>
-        {heading}
-        <form className="thread-search …" action={action} method="get" role="search" …>
-          <div className="thread-search-field">…</div>
-        </form>
-        {cta}
-      </div>
+      <form className={pending ? "thread-browser is-loading" : "thread-browser"} action={action} method="get" role="search" …>
+        <div className="page-head" id={anchorId}>
+          {heading}
+          <div className="thread-search"><div className="thread-search-field">…</div></div>
+          {cta}
+        </div>
+        {filterable && (
+          <div className="thread-filters">
+            {/* Radios (not buttons) so the toggle survives a no-JS GET submit; the label carries the look. */}
+            <div className="seg" role="radiogroup" aria-label="Whose threads">
+              {[["all", "Everyone"], ["mine", "Yours"]].map(([value, label]) => (
+                <label key={value} className={filters.who === value ? "is-on" : undefined}>
+                  <input type="radio" name="who" value={value === "all" ? "" : value} checked={filters.who === value} onChange={() => setFilters((f) => ({ ...f, who: value }))} />
+                  {label}
+                </label>
+              ))}
+            </div>
+            <div className={filters.workspace ? "thread-filter has-value" : "thread-filter"}>
+              <select name="workspace" value={filters.workspace} aria-label="Workspace" onChange={(e) => setFilters((f) => ({ ...f, workspace: e.target.value }))}>
+                <option value="">All workspaces</option>
+                {Object.entries(workspaces ?? {}).map(([id, w]) => <option key={id} value={id}>{w.name}</option>)}
+                <option value={GENERAL_WORKSPACE}>General (no workspace)</option>
+              </select>
+            </div>
+          </div>
+        )}
+      </form>

diff --git a/threadide/app/globals.css b/threadide/app/globals.css
@@
-.thread-search.is-loading input { border-color: var(--karma); }
+.thread-browser.is-loading .thread-search input { border-color: var(--karma); }
+.thread-filters { display: flex; align-items: center; gap: 12px; margin: 0 0 12px; }
+.seg { display: inline-flex; border: 1px solid var(--text); border-radius: 3px; overflow: hidden; flex: 0 0 auto; }
+.seg label { … 13px/600 uppercase, padding 6px 12px … }
+.seg label + label { border-left: 1px solid var(--text); }
+.seg label.is-on { background: var(--accent); color: var(--on-accent); }
+.seg input { position: absolute; opacity: 0; width: 0; height: 0; … }
+.seg label:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: -2px; }
+.thread-filter { position: relative; display: flex; flex: 0 1 220px; min-width: 0; margin-left: auto; }
+.thread-filter select { … appearance:none, matches the search input … }
+.thread-filter::after { … CSS chevron … }
 @media (max-width: 560px) {
+  .thread-filter { flex: 1 1 auto; }
 }
Osebotfable-5-151d agothreadide@f51d264

Shipped. Summary of what landed on thread/bf9285dd (threadide @ f51d264):

  • Home page filter row under the "Threads" title: an Everyone | Yours segmented toggle and a workspace select (All / each workspace / General). Both live in the same GET form as the search box, so they update the list live, mirror into the URL (?who=mine&workspace=…&q=…), and still work without JS.
  • Yours = threads you started or replied to. Easy to narrow to "created by you" if you'd prefer.
  • listThreads now takes a filter object (workspaceId null → general threads, participantId for Yours); GET /api/threads accepts workspaceId=<id|general> and mine=1.
  • Typecheck clean; phone + desktop mockup screenshots are attached to the diff post.

One note: the first run had already pushed a workspace-select-only commit to the branch without reporting it, so I rebased on top of it and kept its general sentinel and select styling. No PR opened — let me know if you'd like one.