@rudderhq/agent-runtime-opencode-local 0.6.6-canary.8 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/skills/app-builder/SKILL.md +104 -0
- package/skills/app-builder/agents/openai.yaml +14 -0
- package/skills/app-builder/assets/scaffold/.env.example +3 -0
- package/skills/app-builder/assets/scaffold/app/api/%5F%5Frudder/health/route.ts +20 -0
- package/skills/app-builder/assets/scaffold/app/api/contacts/[id]/route.ts +39 -0
- package/skills/app-builder/assets/scaffold/app/api/contacts/route.ts +31 -0
- package/skills/app-builder/assets/scaffold/app/api/data/export/route.ts +19 -0
- package/skills/app-builder/assets/scaffold/app/api/data/import/route.ts +40 -0
- package/skills/app-builder/assets/scaffold/app/globals.css +32 -0
- package/skills/app-builder/assets/scaffold/app/layout.tsx +15 -0
- package/skills/app-builder/assets/scaffold/app/page.tsx +9 -0
- package/skills/app-builder/assets/scaffold/components/contacts-workspace.tsx +292 -0
- package/skills/app-builder/assets/scaffold/components/ui/button.tsx +42 -0
- package/skills/app-builder/assets/scaffold/components/ui/card.tsx +19 -0
- package/skills/app-builder/assets/scaffold/components/ui/input.tsx +17 -0
- package/skills/app-builder/assets/scaffold/components/ui/label.tsx +6 -0
- package/skills/app-builder/assets/scaffold/data/.gitkeep +1 -0
- package/skills/app-builder/assets/scaffold/drizzle.config.ts +18 -0
- package/skills/app-builder/assets/scaffold/instrumentation.ts +5 -0
- package/skills/app-builder/assets/scaffold/lib/data-transfer.ts +44 -0
- package/skills/app-builder/assets/scaffold/lib/db/client.ts +78 -0
- package/skills/app-builder/assets/scaffold/lib/db/schema.ts +35 -0
- package/skills/app-builder/assets/scaffold/lib/domain.ts +18 -0
- package/skills/app-builder/assets/scaffold/lib/jobs/runner.ts +56 -0
- package/skills/app-builder/assets/scaffold/lib/utils.ts +6 -0
- package/skills/app-builder/assets/scaffold/migrations/0000_app_builder_foundation.sql +27 -0
- package/skills/app-builder/assets/scaffold/migrations/meta/_journal.json +13 -0
- package/skills/app-builder/assets/scaffold/next-env.d.ts +6 -0
- package/skills/app-builder/assets/scaffold/next.config.ts +8 -0
- package/skills/app-builder/assets/scaffold/package.json +47 -0
- package/skills/app-builder/assets/scaffold/playwright.config.ts +30 -0
- package/skills/app-builder/assets/scaffold/pnpm-lock.yaml +2687 -0
- package/skills/app-builder/assets/scaffold/postcss.config.mjs +5 -0
- package/skills/app-builder/assets/scaffold/rudder.app.json +32 -0
- package/skills/app-builder/assets/scaffold/scripts/migrate.ts +21 -0
- package/skills/app-builder/assets/scaffold/scripts/seed.ts +31 -0
- package/skills/app-builder/assets/scaffold/scripts/snapshot.ts +17 -0
- package/skills/app-builder/assets/scaffold/tests/e2e/app.spec.ts +50 -0
- package/skills/app-builder/assets/scaffold/tests/unit/data-transfer.test.ts +28 -0
- package/skills/app-builder/assets/scaffold/tests/unit/domain.test.ts +23 -0
- package/skills/app-builder/assets/scaffold/tsconfig.json +41 -0
- package/skills/app-builder/assets/scaffold/vitest.config.ts +14 -0
- package/skills/app-builder/evals/evals.json +65 -0
- package/skills/app-builder/references/data-safety.md +38 -0
- package/skills/app-builder/references/design-guidelines.md +20 -0
- package/skills/app-builder/references/migrations-and-promotion.md +36 -0
- package/skills/app-builder/references/scaffold-contract.md +77 -0
- package/skills/app-builder/references/verification.md +23 -0
- package/skills/app-builder/scripts/scaffold.mjs +58 -0
- package/skills/app-builder/scripts/validate-manifest.mjs +63 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rudderhq/agent-runtime-opencode-local",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"license": "SEE LICENSE IN LICENSE",
|
|
5
5
|
"homepage": "https://github.com/Undertone0809/rudder",
|
|
6
6
|
"bugs": {
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
"typecheck": "tsc --noEmit"
|
|
68
68
|
},
|
|
69
69
|
"dependencies": {
|
|
70
|
-
"@rudderhq/agent-runtime-utils": "0.
|
|
70
|
+
"@rudderhq/agent-runtime-utils": "0.7.0",
|
|
71
71
|
"picocolors": "^1.1.1"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: app-builder
|
|
3
|
+
description: "Create and iteratively improve local web products from natural-language requests, then prepare them to run as Rudder Apps. Use Rudder's maintained scaffold for new websites and preserve the existing stack for Next.js, React, Vue, Astro, SvelteKit, Nuxt, and other local web projects."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# App Builder
|
|
7
|
+
|
|
8
|
+
Create or improve a useful local web product without making a non-technical
|
|
9
|
+
user choose a framework, database, package manager, or process topology.
|
|
10
|
+
|
|
11
|
+
A Rudder App is an ordinary local website presented as an App inside Rudder.
|
|
12
|
+
This Skill owns requirements, source changes, and product verification. Rudder
|
|
13
|
+
Desktop owns folder selection, launch-definition review, process lifecycle,
|
|
14
|
+
loopback attestation, and embedded opening.
|
|
15
|
+
|
|
16
|
+
## Build Workflow
|
|
17
|
+
|
|
18
|
+
1. Capture a compact brief in the conversation: user/job, primary workflow,
|
|
19
|
+
essential screens, durable data, external integrations, constraints, and
|
|
20
|
+
exclusions. Ask at most three grouped questions, and only when an answer
|
|
21
|
+
materially changes behavior, real-data safety, or external authority.
|
|
22
|
+
2. Choose the source path:
|
|
23
|
+
- For a new App, use the maintained scaffold in `assets/scaffold/`. Prefer
|
|
24
|
+
the Apps workspace flow when it is available. Otherwise run
|
|
25
|
+
`scripts/scaffold.mjs` with an explicit workspace root and target. Never
|
|
26
|
+
recreate the foundation from memory or overwrite a non-empty directory.
|
|
27
|
+
- For an existing web project, inspect its current framework, package
|
|
28
|
+
manager, development script, data boundary, and tests before editing.
|
|
29
|
+
Preserve its stack and conventions; never replace it with the maintained
|
|
30
|
+
scaffold merely to make it a Rudder App.
|
|
31
|
+
3. Read [references/scaffold-contract.md](references/scaffold-contract.md)
|
|
32
|
+
before changing the runtime, database foundation, manifest, health endpoint,
|
|
33
|
+
import/export contract, or background-job runner.
|
|
34
|
+
4. Implement the user's domain model and workflows. Keep the generated project
|
|
35
|
+
conventional and independently runnable.
|
|
36
|
+
5. Follow [references/data-safety.md](references/data-safety.md) whenever an app
|
|
37
|
+
already has data, imports user data, changes its schema, or needs a real-data
|
|
38
|
+
diagnosis.
|
|
39
|
+
6. Follow [references/design-guidelines.md](references/design-guidelines.md)
|
|
40
|
+
for visible UI work. Prefer coherent workflow screens over a generic
|
|
41
|
+
dashboard assembled from decorative cards.
|
|
42
|
+
7. Run migrations against development or snapshot data, then run typecheck,
|
|
43
|
+
unit tests, build, and relevant app E2E tests.
|
|
44
|
+
8. Prepare the website for its Rudder Apps handoff:
|
|
45
|
+
- A new managed App created from Apps Home keeps its typed manifest and
|
|
46
|
+
returns to that App's **Register & preview** action. Do not load the same
|
|
47
|
+
source again through the manual Local App path.
|
|
48
|
+
- An independently authored existing project keeps a direct supported
|
|
49
|
+
development script. Add the `package.json` `rudder` readiness or open-path
|
|
50
|
+
fields only when discovery cannot infer them safely, then direct the
|
|
51
|
+
operator to **Apps + > Add local web project** to select the folder and
|
|
52
|
+
review the launch definition.
|
|
53
|
+
The Agent writes and verifies source; Desktop alone owns runtime approval and
|
|
54
|
+
process start. Do not invent a public URL, tunnel, cloud deployment, or
|
|
55
|
+
unreviewed launch command.
|
|
56
|
+
9. Read [references/verification.md](references/verification.md), then verify
|
|
57
|
+
the rendered app with Rudder Browser. Exercise the main workflow plus at
|
|
58
|
+
least one production-shaped edge case and inspect console errors.
|
|
59
|
+
10. Materialize final screenshots and validation evidence in the originating
|
|
60
|
+
Chat or Run. A localhost URL or tool-only screenshot is not durable output.
|
|
61
|
+
|
|
62
|
+
## Real Data Decision
|
|
63
|
+
|
|
64
|
+
- New app with no user data: use the scaffold's development database and
|
|
65
|
+
synthetic fixtures.
|
|
66
|
+
- UI or ordinary logic change on an app with user data: verify against a
|
|
67
|
+
temporary database snapshot by default.
|
|
68
|
+
- Diagnosis that genuinely depends on user records: ask whether to use the
|
|
69
|
+
original database, a snapshot, or a redacted copy. State that relevant data
|
|
70
|
+
may be sent to the configured model provider.
|
|
71
|
+
- Schema change: snapshot first and rehearse the migration on the snapshot.
|
|
72
|
+
Applying it to user data is an application-specific action and requires
|
|
73
|
+
explicit user intent.
|
|
74
|
+
- A direct operation on the formal database requires explicit user intent.
|
|
75
|
+
|
|
76
|
+
Never claim the Agent is sandboxed from files it can access. Use product
|
|
77
|
+
controls and least-data workflow choices; explain the boundary honestly.
|
|
78
|
+
|
|
79
|
+
## Runtime Boundary
|
|
80
|
+
|
|
81
|
+
- App source, data, builds, and execution stay on the user's device.
|
|
82
|
+
- The app may use outbound APIs only when requested and configured.
|
|
83
|
+
- Background tasks implemented by the App run only with that managed App
|
|
84
|
+
process. V1 does not provide a separate Rudder job scheduler or catch-up UI.
|
|
85
|
+
- Never place secret values in source, `rudder.app.json`, tool arguments, Chat,
|
|
86
|
+
screenshots, test fixtures, or logs. V1 does not provide a Rudder Secret
|
|
87
|
+
binding UI, so ask the user how the App should obtain any required
|
|
88
|
+
integration credential.
|
|
89
|
+
- Opening Chat, Apps, an App tab, or a saved view must not passively start an app.
|
|
90
|
+
- Cloud builds, hosted runtimes, public preview links, custom domains, managed
|
|
91
|
+
cloud databases, and cross-device synchronization are outside this skill.
|
|
92
|
+
|
|
93
|
+
## Completion Gate
|
|
94
|
+
|
|
95
|
+
Do not call the app complete until:
|
|
96
|
+
|
|
97
|
+
- source and any required manifest or `package.json` Rudder configuration are durable;
|
|
98
|
+
- typecheck, unit tests, and build pass;
|
|
99
|
+
- the reviewed readiness path is ready on an attested loopback process;
|
|
100
|
+
- Browser verification covers the primary workflow and a relevant edge case;
|
|
101
|
+
- persistence behavior is verified against development or snapshot data;
|
|
102
|
+
- current screenshot evidence is attached to the work;
|
|
103
|
+
- user data was not modified without explicit intent; and
|
|
104
|
+
- stop/cleanup leaves no owned process behind.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface:
|
|
2
|
+
display_name: "App Builder"
|
|
3
|
+
short_description: "Create or improve local web Apps for Rudder"
|
|
4
|
+
default_prompt: "Use $app-builder to create or improve this local web product, prepare it as a Rudder App, and verify it in the Browser."
|
|
5
|
+
|
|
6
|
+
dependencies:
|
|
7
|
+
tools:
|
|
8
|
+
- type: "mcp"
|
|
9
|
+
value: "rudder-browser"
|
|
10
|
+
description: "Run-scoped Rudder Browser verification"
|
|
11
|
+
transport: "stdio"
|
|
12
|
+
|
|
13
|
+
policy:
|
|
14
|
+
allow_implicit_invocation: true
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { getDatabase } from "@/lib/db/client";
|
|
2
|
+
import { NextResponse } from "next/server";
|
|
3
|
+
|
|
4
|
+
export const runtime = "nodejs";
|
|
5
|
+
export const dynamic = "force-dynamic";
|
|
6
|
+
|
|
7
|
+
export async function GET() {
|
|
8
|
+
try {
|
|
9
|
+
getDatabase().sqlite.prepare("select 1").get();
|
|
10
|
+
return NextResponse.json({
|
|
11
|
+
ok: true,
|
|
12
|
+
schemaVersion: 1,
|
|
13
|
+
dataMode: process.env.RUDDER_APP_DATA_MODE === "production"
|
|
14
|
+
? "production"
|
|
15
|
+
: "development",
|
|
16
|
+
});
|
|
17
|
+
} catch {
|
|
18
|
+
return NextResponse.json({ ok: false, error: "database_unavailable" }, { status: 503 });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { getDatabase } from "@/lib/db/client";
|
|
2
|
+
import { contacts } from "@/lib/db/schema";
|
|
3
|
+
import { contactUpdateSchema } from "@/lib/domain";
|
|
4
|
+
import { eq } from "drizzle-orm";
|
|
5
|
+
import { NextResponse } from "next/server";
|
|
6
|
+
|
|
7
|
+
export const runtime = "nodejs";
|
|
8
|
+
export const dynamic = "force-dynamic";
|
|
9
|
+
|
|
10
|
+
export async function PATCH(
|
|
11
|
+
request: Request,
|
|
12
|
+
context: { params: Promise<{ id: string }> },
|
|
13
|
+
) {
|
|
14
|
+
const parsed = contactUpdateSchema.safeParse(await request.json().catch(() => null));
|
|
15
|
+
if (!parsed.success) {
|
|
16
|
+
return NextResponse.json(
|
|
17
|
+
{ error: "invalid_contact", issues: parsed.error.issues },
|
|
18
|
+
{ status: 422 },
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
const { id } = await context.params;
|
|
22
|
+
const row = await getDatabase().db.update(contacts)
|
|
23
|
+
.set({ ...parsed.data, updatedAt: new Date() })
|
|
24
|
+
.where(eq(contacts.id, id))
|
|
25
|
+
.returning()
|
|
26
|
+
.get();
|
|
27
|
+
if (!row) return NextResponse.json({ error: "contact_not_found" }, { status: 404 });
|
|
28
|
+
return NextResponse.json({ contact: row });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function DELETE(
|
|
32
|
+
_request: Request,
|
|
33
|
+
context: { params: Promise<{ id: string }> },
|
|
34
|
+
) {
|
|
35
|
+
const { id } = await context.params;
|
|
36
|
+
const row = await getDatabase().db.delete(contacts).where(eq(contacts.id, id)).returning().get();
|
|
37
|
+
if (!row) return NextResponse.json({ error: "contact_not_found" }, { status: 404 });
|
|
38
|
+
return new NextResponse(null, { status: 204 });
|
|
39
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { getDatabase } from "@/lib/db/client";
|
|
2
|
+
import { contacts } from "@/lib/db/schema";
|
|
3
|
+
import { contactCreateSchema } from "@/lib/domain";
|
|
4
|
+
import { desc } from "drizzle-orm";
|
|
5
|
+
import { NextResponse } from "next/server";
|
|
6
|
+
|
|
7
|
+
export const runtime = "nodejs";
|
|
8
|
+
export const dynamic = "force-dynamic";
|
|
9
|
+
|
|
10
|
+
export async function GET() {
|
|
11
|
+
const rows = await getDatabase().db.select().from(contacts).orderBy(desc(contacts.updatedAt)).all();
|
|
12
|
+
return NextResponse.json({ contacts: rows });
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function POST(request: Request) {
|
|
16
|
+
const parsed = contactCreateSchema.safeParse(await request.json().catch(() => null));
|
|
17
|
+
if (!parsed.success) {
|
|
18
|
+
return NextResponse.json(
|
|
19
|
+
{ error: "invalid_contact", issues: parsed.error.issues },
|
|
20
|
+
{ status: 422 },
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
const now = new Date();
|
|
24
|
+
const row = await getDatabase().db.insert(contacts).values({
|
|
25
|
+
...parsed.data,
|
|
26
|
+
id: crypto.randomUUID(),
|
|
27
|
+
createdAt: now,
|
|
28
|
+
updatedAt: now,
|
|
29
|
+
}).returning().get();
|
|
30
|
+
return NextResponse.json({ contact: row }, { status: 201 });
|
|
31
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { buildExportEnvelope } from "@/lib/data-transfer";
|
|
2
|
+
import { getDatabase } from "@/lib/db/client";
|
|
3
|
+
import { contacts } from "@/lib/db/schema";
|
|
4
|
+
import { NextResponse } from "next/server";
|
|
5
|
+
|
|
6
|
+
export const runtime = "nodejs";
|
|
7
|
+
export const dynamic = "force-dynamic";
|
|
8
|
+
|
|
9
|
+
export async function GET() {
|
|
10
|
+
const payload = buildExportEnvelope(
|
|
11
|
+
await getDatabase().db.select().from(contacts).all(),
|
|
12
|
+
);
|
|
13
|
+
return new NextResponse(JSON.stringify(payload, null, 2), {
|
|
14
|
+
headers: {
|
|
15
|
+
"Content-Disposition": `attachment; filename="rudder-app-export-${Date.now()}.json"`,
|
|
16
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { importEnvelopeSchema } from "@/lib/data-transfer";
|
|
2
|
+
import { getDatabase } from "@/lib/db/client";
|
|
3
|
+
import { contacts } from "@/lib/db/schema";
|
|
4
|
+
import { NextResponse } from "next/server";
|
|
5
|
+
|
|
6
|
+
export const runtime = "nodejs";
|
|
7
|
+
export const dynamic = "force-dynamic";
|
|
8
|
+
|
|
9
|
+
export async function POST(request: Request) {
|
|
10
|
+
const parsed = importEnvelopeSchema.safeParse(await request.json().catch(() => null));
|
|
11
|
+
if (!parsed.success) {
|
|
12
|
+
return NextResponse.json(
|
|
13
|
+
{ error: "invalid_import", issues: parsed.error.issues },
|
|
14
|
+
{ status: 422 },
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const { db } = getDatabase();
|
|
19
|
+
const imported = await db.transaction(async (transaction) => {
|
|
20
|
+
for (const contact of parsed.data.data.contacts) {
|
|
21
|
+
await transaction.insert(contacts).values({
|
|
22
|
+
...contact,
|
|
23
|
+
createdAt: new Date(contact.createdAt),
|
|
24
|
+
updatedAt: new Date(contact.updatedAt),
|
|
25
|
+
}).onConflictDoUpdate({
|
|
26
|
+
target: contacts.id,
|
|
27
|
+
set: {
|
|
28
|
+
name: contact.name,
|
|
29
|
+
email: contact.email,
|
|
30
|
+
company: contact.company,
|
|
31
|
+
status: contact.status,
|
|
32
|
+
updatedAt: new Date(contact.updatedAt),
|
|
33
|
+
},
|
|
34
|
+
}).run();
|
|
35
|
+
}
|
|
36
|
+
return parsed.data.data.contacts.length;
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
return NextResponse.json({ imported });
|
|
40
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
@import "tailwindcss";
|
|
2
|
+
|
|
3
|
+
:root {
|
|
4
|
+
--background: #f8fafc;
|
|
5
|
+
--foreground: #172033;
|
|
6
|
+
--card: #ffffff;
|
|
7
|
+
--card-foreground: #172033;
|
|
8
|
+
--muted: #eef2f7;
|
|
9
|
+
--muted-foreground: #64748b;
|
|
10
|
+
--primary: #2563eb;
|
|
11
|
+
--primary-foreground: #ffffff;
|
|
12
|
+
--destructive: #dc2626;
|
|
13
|
+
--border: #dbe3ee;
|
|
14
|
+
--ring: #3b82f6;
|
|
15
|
+
--radius: 0.75rem;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
* {
|
|
19
|
+
border-color: var(--border);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
body {
|
|
23
|
+
margin: 0;
|
|
24
|
+
background: var(--background);
|
|
25
|
+
color: var(--foreground);
|
|
26
|
+
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
button,
|
|
30
|
+
input {
|
|
31
|
+
font: inherit;
|
|
32
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Metadata } from "next";
|
|
2
|
+
import "./globals.css";
|
|
3
|
+
|
|
4
|
+
export const metadata: Metadata = {
|
|
5
|
+
title: "Rudder App",
|
|
6
|
+
description: "A full-stack app built with Rudder App Builder",
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export default function RootLayout({ children }: Readonly<{ children: React.ReactNode }>) {
|
|
10
|
+
return (
|
|
11
|
+
<html lang="en">
|
|
12
|
+
<body>{children}</body>
|
|
13
|
+
</html>
|
|
14
|
+
);
|
|
15
|
+
}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Button } from "@/components/ui/button";
|
|
4
|
+
import { Card, CardContent, CardHeader } from "@/components/ui/card";
|
|
5
|
+
import { Input } from "@/components/ui/input";
|
|
6
|
+
import { Label } from "@/components/ui/label";
|
|
7
|
+
import { Download, Plus, Search, Trash2, Upload } from "lucide-react";
|
|
8
|
+
import { FormEvent, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
9
|
+
|
|
10
|
+
type Contact = {
|
|
11
|
+
id: string;
|
|
12
|
+
name: string;
|
|
13
|
+
email: string;
|
|
14
|
+
company: string;
|
|
15
|
+
status: "new" | "contacted" | "replied" | "paused";
|
|
16
|
+
createdAt: string;
|
|
17
|
+
updatedAt: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type Draft = {
|
|
21
|
+
name: string;
|
|
22
|
+
email: string;
|
|
23
|
+
company: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const emptyDraft: Draft = { name: "", email: "", company: "" };
|
|
27
|
+
|
|
28
|
+
export function ContactsWorkspace() {
|
|
29
|
+
const [contacts, setContacts] = useState<Contact[]>([]);
|
|
30
|
+
const [draft, setDraft] = useState<Draft>(emptyDraft);
|
|
31
|
+
const [query, setQuery] = useState("");
|
|
32
|
+
const [loading, setLoading] = useState(true);
|
|
33
|
+
const [saving, setSaving] = useState(false);
|
|
34
|
+
const [error, setError] = useState<string | null>(null);
|
|
35
|
+
const importRef = useRef<HTMLInputElement>(null);
|
|
36
|
+
|
|
37
|
+
const loadContacts = useCallback(async () => {
|
|
38
|
+
setLoading(true);
|
|
39
|
+
setError(null);
|
|
40
|
+
try {
|
|
41
|
+
const response = await fetch("/api/contacts", { cache: "no-store" });
|
|
42
|
+
if (!response.ok) throw new Error("Could not load contacts.");
|
|
43
|
+
const body = await response.json() as { contacts: Contact[] };
|
|
44
|
+
setContacts(body.contacts);
|
|
45
|
+
} catch (reason) {
|
|
46
|
+
setError(reason instanceof Error ? reason.message : "Could not load contacts.");
|
|
47
|
+
} finally {
|
|
48
|
+
setLoading(false);
|
|
49
|
+
}
|
|
50
|
+
}, []);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
void loadContacts();
|
|
54
|
+
}, [loadContacts]);
|
|
55
|
+
|
|
56
|
+
const visibleContacts = useMemo(() => {
|
|
57
|
+
const normalized = query.trim().toLowerCase();
|
|
58
|
+
if (!normalized) return contacts;
|
|
59
|
+
return contacts.filter((contact) => (
|
|
60
|
+
`${contact.name} ${contact.email} ${contact.company} ${contact.status}`
|
|
61
|
+
.toLowerCase()
|
|
62
|
+
.includes(normalized)
|
|
63
|
+
));
|
|
64
|
+
}, [contacts, query]);
|
|
65
|
+
|
|
66
|
+
async function createContact(event: FormEvent) {
|
|
67
|
+
event.preventDefault();
|
|
68
|
+
setSaving(true);
|
|
69
|
+
setError(null);
|
|
70
|
+
try {
|
|
71
|
+
const response = await fetch("/api/contacts", {
|
|
72
|
+
method: "POST",
|
|
73
|
+
headers: { "Content-Type": "application/json" },
|
|
74
|
+
body: JSON.stringify(draft),
|
|
75
|
+
});
|
|
76
|
+
if (!response.ok) {
|
|
77
|
+
const body = await response.json().catch(() => null) as { error?: string } | null;
|
|
78
|
+
throw new Error(body?.error === "invalid_contact"
|
|
79
|
+
? "Enter a name and a valid email address."
|
|
80
|
+
: "Could not create the contact.");
|
|
81
|
+
}
|
|
82
|
+
setDraft(emptyDraft);
|
|
83
|
+
await loadContacts();
|
|
84
|
+
} catch (reason) {
|
|
85
|
+
setError(reason instanceof Error ? reason.message : "Could not create the contact.");
|
|
86
|
+
} finally {
|
|
87
|
+
setSaving(false);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function deleteContact(contact: Contact) {
|
|
92
|
+
if (!window.confirm(`Delete ${contact.name}? This cannot be undone.`)) return;
|
|
93
|
+
setError(null);
|
|
94
|
+
const response = await fetch(`/api/contacts/${contact.id}`, { method: "DELETE" });
|
|
95
|
+
if (!response.ok) {
|
|
96
|
+
setError("Could not delete the contact.");
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
setContacts((current) => current.filter((candidate) => candidate.id !== contact.id));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function importData(file: File) {
|
|
103
|
+
setError(null);
|
|
104
|
+
try {
|
|
105
|
+
const payload = JSON.parse(await file.text());
|
|
106
|
+
const response = await fetch("/api/data/import", {
|
|
107
|
+
method: "POST",
|
|
108
|
+
headers: { "Content-Type": "application/json" },
|
|
109
|
+
body: JSON.stringify(payload),
|
|
110
|
+
});
|
|
111
|
+
if (!response.ok) throw new Error("The import file is invalid or incompatible.");
|
|
112
|
+
await loadContacts();
|
|
113
|
+
} catch (reason) {
|
|
114
|
+
setError(reason instanceof Error ? reason.message : "Could not import this file.");
|
|
115
|
+
} finally {
|
|
116
|
+
if (importRef.current) importRef.current.value = "";
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return (
|
|
121
|
+
<div className="mx-auto grid max-w-7xl gap-6 px-4 py-6 lg:grid-cols-[minmax(0,1fr)_22rem] lg:px-8">
|
|
122
|
+
<section className="min-w-0">
|
|
123
|
+
<header className="mb-5 flex flex-col gap-4 sm:flex-row sm:items-end sm:justify-between">
|
|
124
|
+
<div>
|
|
125
|
+
<p className="mb-1 text-sm font-medium text-[var(--primary)]">Contacts</p>
|
|
126
|
+
<h1 className="m-0 text-2xl font-semibold tracking-tight">Customer workspace</h1>
|
|
127
|
+
<p className="mb-0 mt-2 text-sm text-[var(--muted-foreground)]">
|
|
128
|
+
Keep the next conversation visible and every record on this device.
|
|
129
|
+
</p>
|
|
130
|
+
</div>
|
|
131
|
+
<div className="flex flex-wrap gap-2">
|
|
132
|
+
<input
|
|
133
|
+
ref={importRef}
|
|
134
|
+
aria-label="Import app data"
|
|
135
|
+
className="sr-only"
|
|
136
|
+
type="file"
|
|
137
|
+
accept="application/json,.json"
|
|
138
|
+
onChange={(event) => {
|
|
139
|
+
const file = event.currentTarget.files?.[0];
|
|
140
|
+
if (file) void importData(file);
|
|
141
|
+
}}
|
|
142
|
+
/>
|
|
143
|
+
<Button type="button" variant="outline" onClick={() => importRef.current?.click()}>
|
|
144
|
+
<Upload aria-hidden size={16} />
|
|
145
|
+
Import
|
|
146
|
+
</Button>
|
|
147
|
+
<Button asChild variant="outline">
|
|
148
|
+
<a href="/api/data/export" download>
|
|
149
|
+
<Download aria-hidden size={16} />
|
|
150
|
+
Export
|
|
151
|
+
</a>
|
|
152
|
+
</Button>
|
|
153
|
+
</div>
|
|
154
|
+
</header>
|
|
155
|
+
|
|
156
|
+
<Card>
|
|
157
|
+
<CardHeader className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
|
158
|
+
<div>
|
|
159
|
+
<h2 className="m-0 text-base font-semibold">All contacts</h2>
|
|
160
|
+
<p className="mb-0 mt-1 text-sm text-[var(--muted-foreground)]">
|
|
161
|
+
{contacts.length} {contacts.length === 1 ? "record" : "records"}
|
|
162
|
+
</p>
|
|
163
|
+
</div>
|
|
164
|
+
<div className="relative w-full sm:w-72">
|
|
165
|
+
<Search
|
|
166
|
+
aria-hidden
|
|
167
|
+
className="pointer-events-none absolute left-3 top-1/2 -translate-y-1/2 text-[var(--muted-foreground)]"
|
|
168
|
+
size={16}
|
|
169
|
+
/>
|
|
170
|
+
<Input
|
|
171
|
+
aria-label="Search contacts"
|
|
172
|
+
className="pl-9"
|
|
173
|
+
placeholder="Search name, email, company…"
|
|
174
|
+
value={query}
|
|
175
|
+
onChange={(event) => setQuery(event.target.value)}
|
|
176
|
+
/>
|
|
177
|
+
</div>
|
|
178
|
+
</CardHeader>
|
|
179
|
+
<div aria-live="polite">
|
|
180
|
+
{loading ? (
|
|
181
|
+
<p className="p-8 text-center text-sm text-[var(--muted-foreground)]">
|
|
182
|
+
Loading contacts…
|
|
183
|
+
</p>
|
|
184
|
+
) : visibleContacts.length === 0 ? (
|
|
185
|
+
<div className="p-10 text-center">
|
|
186
|
+
<h3 className="m-0 text-base font-semibold">
|
|
187
|
+
{contacts.length === 0 ? "No contacts yet" : "No matching contacts"}
|
|
188
|
+
</h3>
|
|
189
|
+
<p className="mb-0 mt-2 text-sm text-[var(--muted-foreground)]">
|
|
190
|
+
{contacts.length === 0
|
|
191
|
+
? "Add the first contact with the form beside this list."
|
|
192
|
+
: "Try a different name, email, company, or status."}
|
|
193
|
+
</p>
|
|
194
|
+
</div>
|
|
195
|
+
) : (
|
|
196
|
+
<div className="overflow-x-auto">
|
|
197
|
+
<table className="w-full min-w-[42rem] border-collapse text-left text-sm">
|
|
198
|
+
<thead className="bg-[var(--muted)] text-xs uppercase tracking-wide text-[var(--muted-foreground)]">
|
|
199
|
+
<tr>
|
|
200
|
+
<th className="px-5 py-3 font-medium" scope="col">Contact</th>
|
|
201
|
+
<th className="px-5 py-3 font-medium" scope="col">Company</th>
|
|
202
|
+
<th className="px-5 py-3 font-medium" scope="col">Status</th>
|
|
203
|
+
<th className="px-5 py-3 text-right font-medium" scope="col">Actions</th>
|
|
204
|
+
</tr>
|
|
205
|
+
</thead>
|
|
206
|
+
<tbody>
|
|
207
|
+
{visibleContacts.map((contact) => (
|
|
208
|
+
<tr className="border-t" key={contact.id}>
|
|
209
|
+
<td className="px-5 py-4">
|
|
210
|
+
<strong className="block font-medium">{contact.name}</strong>
|
|
211
|
+
<span className="text-[var(--muted-foreground)]">{contact.email}</span>
|
|
212
|
+
</td>
|
|
213
|
+
<td className="px-5 py-4">{contact.company || "—"}</td>
|
|
214
|
+
<td className="px-5 py-4">
|
|
215
|
+
<span className="rounded-full bg-[var(--muted)] px-2.5 py-1 text-xs font-medium capitalize">
|
|
216
|
+
{contact.status}
|
|
217
|
+
</span>
|
|
218
|
+
</td>
|
|
219
|
+
<td className="px-5 py-4 text-right">
|
|
220
|
+
<Button
|
|
221
|
+
aria-label={`Delete ${contact.name}`}
|
|
222
|
+
size="sm"
|
|
223
|
+
type="button"
|
|
224
|
+
variant="ghost"
|
|
225
|
+
onClick={() => void deleteContact(contact)}
|
|
226
|
+
>
|
|
227
|
+
<Trash2 aria-hidden size={16} />
|
|
228
|
+
</Button>
|
|
229
|
+
</td>
|
|
230
|
+
</tr>
|
|
231
|
+
))}
|
|
232
|
+
</tbody>
|
|
233
|
+
</table>
|
|
234
|
+
</div>
|
|
235
|
+
)}
|
|
236
|
+
</div>
|
|
237
|
+
</Card>
|
|
238
|
+
</section>
|
|
239
|
+
|
|
240
|
+
<aside>
|
|
241
|
+
<Card>
|
|
242
|
+
<CardHeader>
|
|
243
|
+
<h2 className="m-0 flex items-center gap-2 text-base font-semibold">
|
|
244
|
+
<Plus aria-hidden size={18} />
|
|
245
|
+
Add contact
|
|
246
|
+
</h2>
|
|
247
|
+
</CardHeader>
|
|
248
|
+
<CardContent>
|
|
249
|
+
<form className="grid gap-4" onSubmit={(event) => void createContact(event)}>
|
|
250
|
+
<div className="grid gap-1.5">
|
|
251
|
+
<Label htmlFor="contact-name">Name</Label>
|
|
252
|
+
<Input
|
|
253
|
+
id="contact-name"
|
|
254
|
+
required
|
|
255
|
+
autoComplete="name"
|
|
256
|
+
value={draft.name}
|
|
257
|
+
onChange={(event) => setDraft({ ...draft, name: event.target.value })}
|
|
258
|
+
/>
|
|
259
|
+
</div>
|
|
260
|
+
<div className="grid gap-1.5">
|
|
261
|
+
<Label htmlFor="contact-email">Email</Label>
|
|
262
|
+
<Input
|
|
263
|
+
id="contact-email"
|
|
264
|
+
required
|
|
265
|
+
autoComplete="email"
|
|
266
|
+
type="email"
|
|
267
|
+
value={draft.email}
|
|
268
|
+
onChange={(event) => setDraft({ ...draft, email: event.target.value })}
|
|
269
|
+
/>
|
|
270
|
+
</div>
|
|
271
|
+
<div className="grid gap-1.5">
|
|
272
|
+
<Label htmlFor="contact-company">Company</Label>
|
|
273
|
+
<Input
|
|
274
|
+
id="contact-company"
|
|
275
|
+
autoComplete="organization"
|
|
276
|
+
value={draft.company}
|
|
277
|
+
onChange={(event) => setDraft({ ...draft, company: event.target.value })}
|
|
278
|
+
/>
|
|
279
|
+
</div>
|
|
280
|
+
{error ? (
|
|
281
|
+
<p className="m-0 text-sm text-[var(--destructive)]" role="alert">{error}</p>
|
|
282
|
+
) : null}
|
|
283
|
+
<Button disabled={saving} type="submit">
|
|
284
|
+
{saving ? "Adding…" : "Add contact"}
|
|
285
|
+
</Button>
|
|
286
|
+
</form>
|
|
287
|
+
</CardContent>
|
|
288
|
+
</Card>
|
|
289
|
+
</aside>
|
|
290
|
+
</div>
|
|
291
|
+
);
|
|
292
|
+
}
|