refactor: tenants surface uses @crema/table-ui + search-ui + feedback-ui

Replaces the hand-rolled shadcn primitives in app/routes/tenants.tsx
with manifest libs: DataTable + ActionsCell + BadgeCell + DateCell +
Pagination + useTable from table-ui, SearchInput from search-ui, and
AlertBanner + ConfirmDialog + EmptyState + LoadingOverlay from feedback-ui.

Behaviour preserved: same columns, same row actions, same suspend/
deactivate/activate handlers. Gains: built-in sortable columns,
pagination controls, density toggle support, proper confirmation dialogs
for destructive actions, accessible empty/loading/error states.

Wires three new sibling libs into tsconfig.json paths and app.css
@source lines: lib-table-ui, lib-search-ui, lib-feedback-ui, plus
lib-auth-ui (used by lib-arcadia-auth-ui after its refactor).

Corrects the earlier miss of not checking docs/LIBS.md before rolling
custom UI — the manifest already had what we needed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
jules
2026-04-29 22:00:33 +10:00
parent 080597d046
commit b513fbe405
3 changed files with 229 additions and 162 deletions

View File

@@ -13,6 +13,10 @@
@source "../../lib-action-bus/src"; @source "../../lib-action-bus/src";
@source "../../lib-arcadia-client/src"; @source "../../lib-arcadia-client/src";
@source "../../lib-arcadia-auth-ui/src"; @source "../../lib-arcadia-auth-ui/src";
@source "../../lib-table-ui/src";
@source "../../lib-search-ui/src";
@source "../../lib-feedback-ui/src";
@source "../../lib-auth-ui/src";
/* CREMA:SOURCES */ /* CREMA:SOURCES */
@custom-variant dark (&:is(.dark *)); @custom-variant dark (&:is(.dark *));

View File

@@ -1,11 +1,23 @@
import { useCallback, useEffect, useMemo, useState } from "react" import { useCallback, useEffect, useMemo, useState } from "react"
import { Link } from "react-router" import { Link } from "react-router"
import { AlertTriangle, Loader2, MoreHorizontal, Pause, Play, Plus, RefreshCw, Search, X } from "lucide-react" import { Pause, Play, Plus, RefreshCw } from "lucide-react"
import { ArcadiaError, useArcadiaClient } from "@crema/arcadia-client" import { ArcadiaError, useArcadiaClient } from "@crema/arcadia-client"
import {
ActionsCell,
BadgeCell,
DataTable,
DateCell,
Pagination,
useTable,
type ActionItem,
type BadgeTone,
type Column,
} from "@crema/table-ui"
import { SearchInput } from "@crema/search-ui"
import { AlertBanner, ConfirmDialog, EmptyState, LoadingOverlay } from "@crema/feedback-ui"
import { AppShell } from "~/components/layout/app-shell" import { AppShell } from "~/components/layout/app-shell"
import { Badge } from "~/components/ui/badge"
import { Button } from "~/components/ui/button" import { Button } from "~/components/ui/button"
import { import {
Card, Card,
@@ -15,27 +27,23 @@ import {
CardTitle, CardTitle,
} from "~/components/ui/card" } from "~/components/ui/card"
import { import {
DropdownMenu, activateTenant,
DropdownMenuContent, deactivateTenant,
DropdownMenuItem, listTenants,
DropdownMenuSeparator, suspendTenant,
DropdownMenuTrigger, type Tenant,
} from "~/components/ui/dropdown-menu" type TenantStatus,
import { Input } from "~/components/ui/input" } from "~/lib/arcadia/tenants"
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "~/components/ui/table"
import { activateTenant, deactivateTenant, listTenants, suspendTenant, type Tenant, type TenantStatus } from "~/lib/arcadia/tenants"
import { pageTitle } from "~/lib/page-meta" import { pageTitle } from "~/lib/page-meta"
import { useSession } from "~/lib/session" import { useSession } from "~/lib/session"
export const meta = () => pageTitle("Tenants") export const meta = () => pageTitle("Tenants")
type PendingAction = {
kind: "suspend" | "deactivate"
tenant: Tenant
} | null
export default function TenantsRoute() { export default function TenantsRoute() {
const session = useSession() const session = useSession()
const arcadia = useArcadiaClient() const arcadia = useArcadiaClient()
@@ -43,7 +51,8 @@ export default function TenantsRoute() {
const [tenants, setTenants] = useState<Tenant[]>([]) const [tenants, setTenants] = useState<Tenant[]>([])
const [loading, setLoading] = useState(true) const [loading, setLoading] = useState(true)
const [error, setError] = useState<string | null>(null) const [error, setError] = useState<string | null>(null)
const [query, setQuery] = useState("") const [pending, setPending] = useState<PendingAction>(null)
const [search, setSearch] = useState("")
const refresh = useCallback(async () => { const refresh = useCallback(async () => {
setError(null) setError(null)
@@ -62,16 +71,84 @@ export default function TenantsRoute() {
if (session) refresh() if (session) refresh()
}, [session, refresh]) }, [session, refresh])
const filtered = useMemo(() => { const runAction = useCallback(
const q = query.trim().toLowerCase() async (action: PendingAction) => {
if (!q) return tenants if (!action) return
return tenants.filter( try {
(t) => if (action.kind === "suspend") await suspendTenant(arcadia, action.tenant.id)
t.name.toLowerCase().includes(q) || else await deactivateTenant(arcadia, action.tenant.id)
t.slug.toLowerCase().includes(q) || setPending(null)
t.id.toLowerCase().includes(q), await refresh()
} catch (err) {
setError(err instanceof ArcadiaError ? err.message : "Action failed.")
setPending(null)
}
},
[arcadia, refresh],
) )
}, [tenants, query])
const columns = useMemo<Column<Tenant>[]>(
() => [
{
id: "name",
header: "Name",
accessor: "name",
sortable: true,
cell: (t) => <span className="font-medium">{t.name}</span>,
},
{
id: "slug",
header: "Slug",
accessor: "slug",
sortable: true,
cell: (t) => (
<code className="rounded bg-muted px-1.5 py-0.5 font-mono text-xs">{t.slug}</code>
),
},
{
id: "status",
header: "Status",
accessor: "status",
sortable: true,
cell: (t) => <BadgeCell label={t.status} tone={statusTone(t.status)} />,
},
{
id: "plan",
header: "Plan",
accessor: (t) => t.plan?.name ?? "",
sortable: true,
cell: (t) => <span className="text-muted-foreground">{t.plan?.name ?? "—"}</span>,
},
{
id: "created",
header: "Created",
accessor: "inserted_at",
sortable: true,
cell: (t) => <DateCell value={t.inserted_at} format="short" />,
},
{
id: "actions",
header: "",
align: "right",
cell: (t) => (
<ActionsCell items={rowActions(t, arcadia, refresh, setPending, setError)} />
),
},
],
[arcadia, refresh],
)
const table = useTable<Tenant>({
data: tenants,
columns,
getRowId: (t) => t.id,
initialPageSize: 25,
initialSearch: search,
})
// Keep useTable's search in lockstep with our SearchInput.
useEffect(() => {
table.setSearch(search)
}, [search, table])
if (!session) { if (!session) {
return ( return (
@@ -123,154 +200,132 @@ export default function TenantsRoute() {
</div> </div>
</header> </header>
{error ? (
<AlertBanner variant="error" dismissible onDismiss={() => setError(null)}>
{error}
</AlertBanner>
) : null}
<Card> <Card>
<CardHeader className="flex flex-row items-center justify-between gap-4"> <CardHeader className="flex flex-row items-center justify-between gap-4">
<div className="relative max-w-sm flex-1"> <SearchInput
<Search className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" /> value={search}
<Input onValueChange={setSearch}
placeholder="Search by name, slug, or status"
data-action="tenants-search" data-action="tenants-search"
placeholder="Search by name, slug, or ID" className="max-w-sm flex-1"
value={query}
onChange={(e) => setQuery(e.target.value)}
className="pl-9"
/> />
{query ? (
<button
type="button"
data-action="tenants-search-clear"
onClick={() => setQuery("")}
className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground"
>
<X className="size-4" />
</button>
) : null}
</div>
<div className="text-xs text-muted-foreground"> <div className="text-xs text-muted-foreground">
{filtered.length} of {tenants.length} {table.total} of {tenants.length}
</div> </div>
</CardHeader> </CardHeader>
<CardContent className="p-0"> <CardContent className="relative p-0">
{error ? ( <LoadingOverlay active={loading && tenants.length === 0} label="Loading tenants…" />
<div className="flex items-start gap-2 border-t border-destructive/30 bg-destructive/10 px-4 py-3 text-sm text-destructive"> {table.total === 0 && !loading ? (
<AlertTriangle className="mt-0.5 size-4 shrink-0" /> <EmptyState
<span>{error}</span> title={search ? "No tenants match that search." : "No tenants yet."}
</div> description={
) : null} search ? "Try a different name, slug, or status." : "Create your first tenant to get started."
}
{loading && tenants.length === 0 ? ( className="py-12"
<div className="flex items-center justify-center gap-2 p-12 text-sm text-muted-foreground"> />
<Loader2 className="size-4 animate-spin" />
Loading tenants
</div>
) : filtered.length === 0 ? (
<div className="p-12 text-center text-sm text-muted-foreground">
{query ? "No tenants match that search." : "No tenants yet."}
</div>
) : ( ) : (
<Table> <>
<TableHeader> <DataTable
<TableRow> columns={columns}
<TableHead>Name</TableHead> rows={table.pageRows}
<TableHead>Slug</TableHead> getRowId={(t) => t.id}
<TableHead>Status</TableHead> sort={table.sort}
<TableHead>Plan</TableHead> onSortToggle={table.toggleSort}
<TableHead>Created</TableHead> loading={loading && tenants.length > 0}
<TableHead className="w-10" /> stickyHeader
</TableRow> />
</TableHeader> <Pagination
<TableBody> page={table.page}
{filtered.map((t) => ( pageSize={table.pageSize}
<TenantRow key={t.id} tenant={t} onChange={refresh} /> total={table.total}
))} onPageChange={table.setPage}
</TableBody> onPageSizeChange={table.setPageSize}
</Table> />
</>
)} )}
</CardContent> </CardContent>
</Card> </Card>
</div> </div>
<ConfirmDialog
open={pending?.kind === "suspend"}
onOpenChange={(o) => !o && setPending(null)}
title="Suspend tenant?"
description={
pending
? `${pending.tenant.name} will be suspended. Members won't be able to sign in until you reactivate.`
: ""
}
confirmLabel="Suspend"
variant="default"
onConfirm={() => runAction(pending)}
/>
<ConfirmDialog
open={pending?.kind === "deactivate"}
onOpenChange={(o) => !o && setPending(null)}
title="Deactivate tenant?"
description={
pending
? `${pending.tenant.name} will be deactivated. This is more severe than suspending.`
: ""
}
confirmLabel="Deactivate"
variant="danger"
onConfirm={() => runAction(pending)}
/>
</AppShell> </AppShell>
) )
} }
function TenantRow({ tenant, onChange }: { tenant: Tenant; onChange: () => void | Promise<void> }) { function statusTone(status: TenantStatus): BadgeTone {
const arcadia = useArcadiaClient() if (status === "active") return "success"
const [busy, setBusy] = useState(false) if (status === "suspended") return "warning"
if (status === "deactivated") return "danger"
return "default"
}
const act = async (fn: () => Promise<unknown>) => { function rowActions(
setBusy(true) t: Tenant,
arcadia: ReturnType<typeof useArcadiaClient>,
refresh: () => Promise<void>,
setPending: (p: PendingAction) => void,
setError: (msg: string | null) => void,
): ActionItem[] {
const items: ActionItem[] = []
if (t.status === "active") {
items.push({
id: "suspend",
label: "Suspend",
icon: <Pause className="size-4" />,
onSelect: () => setPending({ kind: "suspend", tenant: t }),
})
} else {
items.push({
id: "activate",
label: "Activate",
icon: <Play className="size-4" />,
onSelect: async () => {
try { try {
await fn() await activateTenant(arcadia, t.id)
await onChange() await refresh()
} finally { } catch (err) {
setBusy(false) setError(err instanceof ArcadiaError ? err.message : "Activate failed.")
} }
},
})
} }
items.push({
return ( id: "deactivate",
<TableRow data-action={`tenants-row-${tenant.slug}`}> label: "Deactivate",
<TableCell className="font-medium">{tenant.name}</TableCell> destructive: true,
<TableCell> onSelect: () => setPending({ kind: "deactivate", tenant: t }),
<code className="rounded bg-muted px-1.5 py-0.5 font-mono text-xs">{tenant.slug}</code> })
</TableCell> return items
<TableCell>
<StatusBadge status={tenant.status} />
</TableCell>
<TableCell>
<span className="text-sm text-muted-foreground">{tenant.plan?.name ?? "—"}</span>
</TableCell>
<TableCell>
<span className="text-xs text-muted-foreground">{formatDate(tenant.inserted_at)}</span>
</TableCell>
<TableCell>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="ghost"
size="icon"
disabled={busy}
data-action={`tenants-row-${tenant.slug}-menu`}
>
{busy ? <Loader2 className="size-4 animate-spin" /> : <MoreHorizontal className="size-4" />}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{tenant.status === "active" ? (
<DropdownMenuItem onClick={() => act(() => suspendTenant(arcadia, tenant.id))}>
<Pause className="size-4" />
Suspend
</DropdownMenuItem>
) : (
<DropdownMenuItem onClick={() => act(() => activateTenant(arcadia, tenant.id))}>
<Play className="size-4" />
Activate
</DropdownMenuItem>
)}
<DropdownMenuSeparator />
<DropdownMenuItem onClick={() => act(() => deactivateTenant(arcadia, tenant.id))}>
Deactivate
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</TableCell>
</TableRow>
)
}
function StatusBadge({ status }: { status: TenantStatus }) {
const variant: "default" | "secondary" | "destructive" | "outline" =
status === "active" ? "default" : status === "suspended" ? "secondary" : "outline"
return (
<Badge variant={variant} className="capitalize">
{status}
</Badge>
)
}
function formatDate(iso: string): string {
try {
return new Date(iso).toLocaleDateString(undefined, { year: "numeric", month: "short", day: "numeric" })
} catch {
return iso
}
} }

View File

@@ -30,6 +30,14 @@
"@crema/arcadia-client/*": ["../lib-arcadia-client/src/*"], "@crema/arcadia-client/*": ["../lib-arcadia-client/src/*"],
"@crema/arcadia-auth-ui": ["../lib-arcadia-auth-ui/src/index.tsx"], "@crema/arcadia-auth-ui": ["../lib-arcadia-auth-ui/src/index.tsx"],
"@crema/arcadia-auth-ui/*": ["../lib-arcadia-auth-ui/src/*"], "@crema/arcadia-auth-ui/*": ["../lib-arcadia-auth-ui/src/*"],
"@crema/table-ui": ["../lib-table-ui/src/index.tsx"],
"@crema/table-ui/*": ["../lib-table-ui/src/*"],
"@crema/search-ui": ["../lib-search-ui/src/index.tsx"],
"@crema/search-ui/*": ["../lib-search-ui/src/*"],
"@crema/feedback-ui": ["../lib-feedback-ui/src/index.tsx"],
"@crema/feedback-ui/*": ["../lib-feedback-ui/src/*"],
"@crema/auth-ui": ["../lib-auth-ui/src/index.tsx"],
"@crema/auth-ui/*": ["../lib-auth-ui/src/*"],
"// CREMA:PATHS": [""], "// CREMA:PATHS": [""],
"react": ["./node_modules/@types/react"], "react": ["./node_modules/@types/react"],
"react/*": ["./node_modules/@types/react/*"], "react/*": ["./node_modules/@types/react/*"],