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>
68 lines
2.6 KiB
TypeScript
68 lines
2.6 KiB
TypeScript
// The "Add app" flow on the Apps screen as a pure state machine. The React
|
|
// component (components/AddApp.tsx) renders these states, so the click count
|
|
// the acceptance test measures is the click count a user sees.
|
|
|
|
import type { Snippet } from "./apps.ts";
|
|
|
|
export type FlowState =
|
|
| { step: "list" }
|
|
| { step: "form"; name: string; error: string | null }
|
|
| { step: "saving"; name: string }
|
|
| { step: "done"; name: string; snippet: Snippet };
|
|
|
|
export type FlowAction =
|
|
| { type: "click_add" }
|
|
| { type: "type_name"; name: string }
|
|
| { type: "click_create" }
|
|
| { type: "created"; snippet: Snippet }
|
|
| { type: "failed"; error: string }
|
|
| { type: "click_close" };
|
|
|
|
/** Actions that are a click (typing is not a click). */
|
|
export const CLICKS: ReadonlySet<FlowAction["type"]> = new Set(["click_add", "click_create", "click_close"]);
|
|
|
|
export function addAppFlow(state: FlowState, action: FlowAction): FlowState {
|
|
switch (action.type) {
|
|
case "click_add":
|
|
return state.step === "list" ? { step: "form", name: "", error: null } : state;
|
|
case "type_name":
|
|
return state.step === "form" ? { ...state, name: action.name, error: null } : state;
|
|
case "click_create":
|
|
return state.step === "form" && state.name.trim() ? { step: "saving", name: state.name } : state;
|
|
case "created":
|
|
return state.step === "saving" ? { step: "done", name: state.name, snippet: action.snippet } : state;
|
|
case "failed":
|
|
return state.step === "saving" ? { step: "form", name: state.name, error: action.error } : state;
|
|
case "click_close":
|
|
return { step: "list" };
|
|
}
|
|
}
|
|
|
|
/** Drive the flow like a user would and count the clicks to the snippet. */
|
|
export async function clicksToSnippet(
|
|
reducer: typeof addAppFlow,
|
|
create: (name: string) => Promise<Snippet>,
|
|
name: string,
|
|
): Promise<{ clicks: number; snippet: Snippet | null; state: FlowState }> {
|
|
let clicks = 0;
|
|
let state: FlowState = { step: "list" };
|
|
const act = (a: FlowAction) => {
|
|
if (CLICKS.has(a.type)) clicks += 1;
|
|
state = reducer(state, a);
|
|
};
|
|
act({ type: "click_add" });
|
|
act({ type: "type_name", name });
|
|
act({ type: "click_create" });
|
|
// Read through a function: `act` changes `state` in a closure.
|
|
const now = (): FlowState => state;
|
|
const saving = now();
|
|
if (saving.step !== "saving") return { clicks, snippet: null, state: saving };
|
|
try {
|
|
act({ type: "created", snippet: await create(saving.name) });
|
|
} catch (err) {
|
|
act({ type: "failed", error: (err as Error).message });
|
|
}
|
|
const end = now();
|
|
return { clicks, snippet: end.step === "done" ? end.snippet : null, state: end };
|
|
}
|