caelum0x 332446652a feat(flags): Railyard Flags behind the add-on, the Flags screen and the OpenFeature SDK (PY2.1)
RYP-E2 and SP-E6, dispatch 4.2 PY2.1 (Railyard side):
- lib/flags: flag environments per app environment (a Scribase environment
  or branch), each with a public client key that reads that one environment.
  Two backends, one surface: the unmodified Flagsmith server (one Flagsmith
  organisation per Railyard org, a project per app, an environment per env;
  FLAGS_API_URL + FLAGS_ADMIN_TOKEN) and, as the fallback the dispatch names
  when the box 2 gate keeps Flagsmith down, the console's own tables
  (migration 0005). An environment keeps the backend it was made on.
- A branch environment starts with its parent's flags (copy_from), then
  changes alone.
- GET /api/v1/flags/client?key=: CORS-open, cached 15 s when keyed by URL;
  the first read by an app is the slot's proof (first_flag_read).
- Bridge route flags (contract: id_format, three cases, flags_item_keys);
  BRIDGE_SLOTS.flags landed.
- The Flags screen (nav item) flips with one click and adds a flag by key;
  POST /api/flags writes only the signed-in org's environments.
- packages/openfeature-railyard: a zero-dependency polling client (30 s,
  offline start from storage, back-off) and OpenFeature client and server
  providers, with Expo and Next.js examples.

Tests: flags core 11/11 (both backends, a fake of the Flagsmith API, tenant
isolation, branch copies, a 502 that leaves nothing half made), bridge 19/19,
contract 3/3, migration 6/6 (0005 twice on a scratch cluster), console 85/85
with the bundle build, tsc clean, SDK 7/7 (a flip reaches the app within 60 s
in the worst cache timing on a fake clock; copy lint on docs and the screen).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 23:58:12 +03:00

66 lines
2.4 KiB
TypeScript

"use client";
import { useRouter } from "next/navigation";
import { useState } from "react";
export interface PanelFlag {
key: string;
enabled: boolean;
value: string | null;
}
async function save(app: string, env: string, flag: Record<string, unknown>): Promise<string | null> {
const res = await fetch("/api/flags", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ app, env, flag }) });
if (res.ok) return null;
const body = (await res.json().catch(() => ({}))) as { error?: string };
return body.error ?? "Could not save the flag. Try again.";
}
/** One environment's flags: flip with one click, add one with a key (PY2.1). */
export function FlagsPanel({ app, env, flags }: { app: string; env: string; flags: PanelFlag[] }) {
const router = useRouter();
const [busy, setBusy] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const [key, setKey] = useState("");
const run = async (id: string, flag: Record<string, unknown>) => {
setBusy(id);
setError(null);
const e = await save(app, env, flag);
setBusy(null);
if (e) setError(e);
else router.refresh();
};
return (
<>
{flags.length ? (
<ul className="list">
{flags.map((f) => (
<li key={f.key}>
<span>
<strong>{f.key}</strong> {f.value ? <code className="muted">{f.value}</code> : null}
</span>
<button type="button" className={f.enabled ? "primary" : undefined} aria-pressed={f.enabled} disabled={busy !== null} onClick={() => run(f.key, { key: f.key, enabled: !f.enabled })}>
{busy === f.key ? "Saving" : f.enabled ? "On" : "Off"}
</button>
</li>
))}
</ul>
) : (
<p className="muted">No flags here yet.</p>
)}
<form
className="row"
onSubmit={(e) => {
e.preventDefault();
if (key.trim()) void run("new", { key: key.trim(), enabled: false }).then(() => setKey(""));
}}
>
<input type="text" aria-label="New flag key" placeholder="new_checkout" value={key} onChange={(e) => setKey(e.target.value)} />
<button type="submit" disabled={busy !== null || !key.trim()}>
{busy === "new" ? "Adding" : "Add flag"}
</button>
</form>
{error ? <p className="error">{error}</p> : null}
</>
);
}