ubiquitous-invention/apps/web/app/(app)/[workspaceSlug]/settings/templates/page.tsx
Randall Stillwell c582d621ce multi-tenancy: promote workspaces to top-level table
Block A of the EchoDo plan. Workspaces used to live as `objects(type='workspace')`,
which made it impossible to put a real RLS-friendly tenant boundary on the schema
or to give each workspace a stable URL slug. This commit:

- Adds a top-level `workspaces` table (slug unique, owner FK, plan_tier hook).
- Migrates the 8 anchor tables (objects, workspace_members, object_type_defs,
  property_definitions, templates, forms, markdown_backlog_items,
  cursor_sync_mappings) to FK into `workspaces.id` instead of `objects.id`,
  with a hand-augmented data-copy migration that preserves IDs and slug-collision-
  proofs on backfill.
- Introduces a `workspaceProcedure` tRPC middleware + `resolveWorkspace` helper
  that take a UUID-or-slug `workspace` handle and expose `ctx.workspace`. All
  tenant-scoped routers (objects, types, properties, templates, forms, search,
  ai, relations, favorites) now flow through it.
- Updates the web app to pass `workspace` slugs from the URL (or store) instead
  of the old `workspaceId`, including a workspace-sync layer that rewrites
  /<UUID>/... links to /<slug>/...
- Updates the MCP tools (list_objects, create_object, search_objects) and the
  workspace://{handle}/tree resource to accept either a slug or UUID so existing
  agents keep working.
- Adds a Create Workspace dialog and a Workspace Settings page (rename + slug
  rename with redirect, owner-only archive).

Verified locally against a fresh Postgres: migration applies cleanly, slug
uniqueness holds, tenant data is isolated by workspace_id, slug↔UUID resolution
works in both directions, and ON DELETE CASCADE cleans up child rows in the
correct workspace only.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-06 23:02:55 -05:00

138 lines
4.7 KiB
TypeScript

"use client";
import { useParams } from "next/navigation";
import { FileStack, Plus, Loader2 } from "lucide-react";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { api } from "@/lib/trpc";
import { cn } from "@/lib/utils";
import { TemplateEditor } from "@/components/templates";
export default function TemplatesSettingsPage() {
const params = useParams();
const workspaceSlug =
typeof params?.workspaceSlug === "string" ? params.workspaceSlug : "";
const [selectedId, setSelectedId] = useState<string | null>(null);
const utils = api.useUtils();
const { data, isLoading } = api.templates.list.useQuery(
{ workspace: workspaceSlug },
{ enabled: Boolean(workspaceSlug) },
);
const templates = data?.templates ?? [];
const createMutation = api.templates.create.useMutation({
onSuccess: (newTemplate) => {
setSelectedId(newTemplate.id);
void utils.templates.list.invalidate({ workspace: workspaceSlug });
},
});
const getByIdQuery = api.templates.getById.useQuery(
{ workspace: workspaceSlug, id: selectedId! },
{ enabled: Boolean(workspaceSlug && selectedId) },
);
if (!workspaceSlug) {
return (
<div className="p-8 text-sm text-muted-foreground">
No workspace selected.
</div>
);
}
const invalidateAfterSave = () => {
void utils.templates.list.invalidate({ workspace: workspaceSlug });
if (selectedId)
void utils.templates.getById.invalidate({ workspace: workspaceSlug, id: selectedId });
};
return (
<div className="flex h-full flex-col">
<div className="flex shrink-0 items-center justify-between border-b px-6 py-4">
<div className="flex items-center gap-3">
<div className="flex h-9 w-9 items-center justify-center rounded-lg bg-primary/10">
<FileStack className="size-5 text-primary" />
</div>
<h1 className="text-lg font-semibold">Templates</h1>
</div>
<Button
size="sm"
className="gap-1.5"
onClick={() =>
createMutation.mutate({
workspace: workspaceSlug,
name: "Untitled Template",
targetType: "task",
schema: { properties: [], defaultContent: "" },
})
}
disabled={createMutation.isPending}
>
<Plus className="size-4" />
New Template
</Button>
</div>
<div className="flex min-h-0 flex-1">
{/* Left: Template list */}
<div className="w-64 shrink-0 overflow-y-auto border-r bg-muted/30 p-3">
{isLoading ? (
<div className="flex items-center justify-center py-10">
<Loader2 className="size-5 animate-spin text-muted-foreground" />
</div>
) : templates.length === 0 ? (
<p className="py-4 text-center text-xs text-muted-foreground">
No templates yet
</p>
) : (
<div className="flex flex-col gap-1">
{templates.map((t) => (
<button
key={t.id}
type="button"
onClick={() => setSelectedId(t.id)}
className={cn(
"rounded-md px-3 py-2 text-left text-sm transition-colors hover:bg-accent",
selectedId === t.id && "bg-accent font-medium",
)}
>
<p className="truncate">{t.name}</p>
<p className="text-xs text-muted-foreground capitalize">
{t.targetType}
</p>
</button>
))}
</div>
)}
</div>
{/* Right: Template editor */}
<div className="flex-1 overflow-y-auto p-6">
{selectedId ? (
getByIdQuery.isLoading ? (
<div className="flex h-full items-center justify-center">
<Loader2 className="size-8 animate-spin text-muted-foreground" />
</div>
) : getByIdQuery.data ? (
<TemplateEditor
key={selectedId}
template={getByIdQuery.data}
workspaceHandle={workspaceSlug}
onSave={invalidateAfterSave}
/>
) : (
<div className="flex h-full items-center justify-center text-sm text-muted-foreground">
Template not found
</div>
)
) : (
<div className="flex h-full items-center justify-center text-sm text-muted-foreground">
Select a template or create a new one
</div>
)}
</div>
</div>
</div>
);
}