"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): Promise { 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(null); const [error, setError] = useState(null); const [key, setKey] = useState(""); const run = async (id: string, flag: Record) => { setBusy(id); setError(null); const e = await save(app, env, flag); setBusy(null); if (e) setError(e); else router.refresh(); }; return ( <> {flags.length ? (
    {flags.map((f) => (
  • {f.key} {f.value ? {f.value} : null}
  • ))}
) : (

No flags here yet.

)}
{ e.preventDefault(); if (key.trim()) void run("new", { key: key.trim(), enabled: false }).then(() => setKey("")); }} > setKey(e.target.value)} />
{error ?

{error}

: null} ); }