caelum0x 247e860a16 feat(console): Railyard console with Scribase sign-in and the Dodo test-mode money ladder (RYP-D1, Y-2)
Next.js console for Cloudflare Workers (OpenNext config, wrangler.jsonc)
with data in the console's own Scribase project, schema railyard
(migration 0001: orgs, org_members, apps, deploys, entitlements,
billing_events, api_tokens, device_codes; idempotent, RLS on, no anon
grants).

- Sign-in with Google or GitHub through Scribase Auth (PKCE), user read
  from the auth server, org and entitlements row created, signed
  HttpOnly session.
- Money ladder (pure, immutable): $1 card check credited back to the
  customer wallet, 14-day trial with the plan's limits, $19 a month or
  $190 a year, pause switch and one-click cancel (ends at the next
  billing date; a trial ends now).
- Webhook: Standard Webhooks signature and timestamp verified, replays
  ignored by unique message id, signed checkout metadata, other brands
  ignored, a refund of the card check reverses the credit and the
  trial, a failed wallet call releases the id for the provider's retry.
- Apps, Usage and Billing screens, one primary action each; Add app
  shows the deploy command and the Insights tag in two clicks.
- CLI device-flow login and org-scoped hashed tokens for Y-3.
- Test mode only: the live payment host is refused unless the owner
  sets RAILYARD_DODO_LIVE=1.

Tests (node --test, 25): journey from sign-in to entitlements and the
snippet, refund, pause and cancel, webhook security, ladder, tokens and
org isolation, PostgREST repo, a component test of Add app with the copy
lint, a next build grep for sentinel secrets in the client bundle, and
the migration applied twice on a scratch Postgres.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 15:35:24 +03:00

116 lines
3.9 KiB
TypeScript

"use client";
import { useReducer, useState } from "react";
import { addAppFlow, type FlowState } from "../lib/add-app-flow.ts";
import type { Snippet } from "../lib/apps.ts";
import { CloseIcon, CopyIcon } from "./Icons.tsx";
export function AddAppView({
state,
remaining,
onAdd,
onName,
onCreate,
onClose,
onCopy,
copied,
}: {
state: FlowState;
remaining: number;
onAdd: () => void;
onName: (name: string) => void;
onCreate: () => void;
onClose: () => void;
onCopy: (text: string) => void;
copied: string | null;
}) {
if (state.step === "list") {
return (
<div className="row">
<button className="primary" type="button" onClick={onAdd} disabled={remaining <= 0}>
Add app
</button>
<span className="muted">{remaining > 0 ? `${remaining} left on your plan` : "Your plan's apps are all in use"}</span>
</div>
);
}
if (state.step === "form" || state.step === "saving") {
return (
<form
className="row"
onSubmit={(e) => {
e.preventDefault();
onCreate();
}}
>
<input type="text" autoFocus aria-label="App name" placeholder="my-app" value={state.name} onChange={(e) => onName(e.target.value)} />
<button className="primary" type="submit" disabled={state.step === "saving"}>
{state.step === "saving" ? "Creating" : "Create"}
</button>
<button className="icon" type="button" aria-label="Close" title="Close" onClick={onClose}>
<CloseIcon />
</button>
{state.step === "form" && state.error ? <p className="error">{state.error}</p> : null}
</form>
);
}
const block = (label: string, text: string) => (
<div style={{ marginTop: 14 }}>
<div className="row" style={{ justifyContent: "space-between" }}>
<h2>{label}</h2>
<button className="icon" type="button" aria-label={`Copy ${label.toLowerCase()}`} title="Copy" onClick={() => onCopy(text)}>
<CopyIcon />
</button>
</div>
<pre>{text}</pre>
{copied === text ? <span className="muted">Copied</span> : null}
</div>
);
return (
<div data-testid="snippet">
<div className="row" style={{ justifyContent: "space-between" }}>
<strong>{state.name} is ready</strong>
<button className="icon" type="button" aria-label="Close" title="Close" onClick={onClose}>
<CloseIcon />
</button>
</div>
{block("Deploy", state.snippet.deploy)}
{block("Add to your page", state.snippet.script)}
</div>
);
}
export function AddApp({ remaining }: { remaining: number }) {
const [state, dispatch] = useReducer(addAppFlow, { step: "list" } as FlowState);
const [copied, setCopied] = useState<string | null>(null);
async function create() {
if (state.step !== "form" || !state.name.trim()) return;
const name = state.name;
dispatch({ type: "click_create" });
try {
const res = await fetch("/api/apps", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ name }) });
const body = (await res.json()) as { snippet?: Snippet; error?: string };
if (!res.ok || !body.snippet) dispatch({ type: "failed", error: body.error ?? "Could not add the app." });
else dispatch({ type: "created", snippet: body.snippet });
} catch {
dispatch({ type: "failed", error: "Could not reach Railyard. Check your connection." });
}
}
return (
<AddAppView
state={state}
remaining={remaining}
copied={copied}
onAdd={() => dispatch({ type: "click_add" })}
onName={(name) => dispatch({ type: "type_name", name })}
onCreate={create}
onClose={() => {
dispatch({ type: "click_close" });
if (state.step === "done") window.location.reload();
}}
onCopy={(text) => {
navigator.clipboard?.writeText(text).then(() => setCopied(text), () => setCopied(null));
}}
/>
);
}