Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 43 additions & 3 deletions packages/flags/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,48 @@ document. While another deployment loads, the previous table stays on screen,
dimmed and `aria-busy`, under the new deployment's header. The tab title names
the open flag and deployment, e.g. `dares_enabled · scout/prod · Flags`.

"Review save" opens a dialog with the stored flag before and the draft after;
Save writes that revision. A conflicting write is left unchanged: a warning
above the form shows the other value beside the draft, with a button to load it.
### Editing modes

The editor has a Form / JSON toggle over one draft. Switching modes never loses
an edit: the JSON text is rebuilt from the draft only when the form changed it,
so text that does not parse yet is still there when you switch back.

- **Form** shows typed controls: a boolean flag's default and segments; a
variant's stored string in a number field (`number` encoding), a wrapping
text area (`json` encoding), or a text field (`string` encoding).
- **JSON** edits in Monaco. A boolean flag is edited as its whole object, with
`type` and `key` fixed by the schema. A `json`-encoded variant is edited as
its decoded value, pretty-printed (an array, not a one-line string), and
saved compactly with object defaults filled in. `number` and `string`
variants are edited as a JSON number or string literal.

`json`-encoded variants open in JSON mode; everything else opens in Form mode.
A variant whose key is not in the value registry is shown as plain text with a
notice that the server rejects writes to unmanaged keys.

Monaco is bundled locally with only its JSON language and loads, with its two
workers, the first time an editor or diff is shown. The flag table never loads
it. Its colours and monospace font come from the `console-ui` tokens of the
current theme.

### Validation

JSON mode validates as you type against a JSON Schema. For variants this is
`flag-value-schemas.generated.json` from `@shepherdjerred/feature-flags`. For
boolean flags it is the flag schema (`src/schema.ts`). Schema errors show as
squiggles with hover messages. The draft is also checked against the registry's
Zod schema, the same check the server runs. Its issues are listed under the
editor by JSON Pointer path. Review stays disabled while the JSON does not parse
or the value fails the registry. If the server still refuses a save with
`400 invalid flag values`, its issues are listed the same way until the draft
changes.

### Saving

"Review save" opens a dialog with a read-only Monaco diff of the stored value
against the draft: side by side when there is room, inline when narrow.
`json`-encoded variants are diffed as their decoded values. Save writes that
revision. A conflicting write is left unchanged: a warning above the form diffs
their flag against the draft, with a button to load theirs.

`toolkit flags` is the command surface. See `packages/toolkit/README.md`.
7 changes: 4 additions & 3 deletions packages/flags/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@
"@shepherdjerred/console-ui": "workspace:*",
"@shepherdjerred/eslint-config": "workspace:*",
"@shepherdjerred/ops-model": "workspace:*",
"@types/bun": "1.4.5",
"@typescript/native": "npm:typescript@7.0.2",
"@vitest/coverage-istanbul": "5.0.5",
"@tanstack/react-query": "^5.101.2",
"@types/bun": "1.4.5",
"@types/react": "^19.3.0",
"@types/react-dom": "^19.3.0",
"@typescript/native": "npm:typescript@7.0.2",
"@vitejs/plugin-react": "^6.0.1",
"@vitest/coverage-istanbul": "5.0.5",
"eslint": "^10.7.0",
"jsdom": "^31.0.1",
"lucide-react": "^1.14.0",
"monaco-editor": "^0.57.0",
"react": "19.3.1",
"react-dom": "19.3.1",
"react-router": "8.4.0",
Expand Down
24 changes: 24 additions & 0 deletions packages/flags/src/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "@tanstack/react-query";
import { z } from "zod";
import type { FlagValue } from "@shepherdjerred/feature-flags/evaluate.ts";
import type { FlagValueKeyIssue } from "@shepherdjerred/feature-flags/flag-write-validation.ts";
import { FlagValueSchema } from "@shepherdjerred/flags/schema.ts";

const DeploymentSummarySchema = z.strictObject({
Expand All @@ -27,6 +28,14 @@ const ConflictSchema = z.strictObject({
flags: z.array(FlagValueSchema),
});

/** The 400 a write gets when a value fails its registry schema (see `README.md`). */
const InvalidValuesSchema = z.strictObject({
error: z.string(),
issues: z.array(
z.strictObject({ key: z.string(), path: z.string(), message: z.string() }),
),
});

export type DeploymentDocument = z.infer<typeof DocumentSchema>;

export type ApplyResult =
Expand All @@ -35,6 +44,11 @@ export type ApplyResult =
readonly kind: "conflict";
readonly revision: number;
readonly flags: readonly FlagValue[];
}
| {
readonly kind: "invalid";
readonly error: string;
readonly issues: readonly FlagValueKeyIssue[];
};

async function readJson(response: Response): Promise<unknown> {
Expand Down Expand Up @@ -131,6 +145,16 @@ export function useApplyFlag(catalog: string, stage: string) {
flags: conflict.flags,
};
}
if (response.status === 400) {
const invalid = InvalidValuesSchema.safeParse(body);
if (invalid.success) {
return {
kind: "invalid",
error: invalid.data.error,
issues: invalid.data.issues,
};
}
}
if (!response.ok) throw new Error(errorMessage(body, response.status));
const saved = z.strictObject({ revision: z.number().int() }).parse(body);
return { kind: "saved", revision: saved.revision };
Expand Down
6 changes: 5 additions & 1 deletion packages/flags/src/client/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
type Route,
} from "./model.ts";
import { DeploymentPicker, DeploymentSidebar } from "./sidebar.tsx";
import { ThemeContext } from "./theme-context.ts";

/** The deployment and flag the URL names; undefined on pages outside a deployment. */
function useDeploymentRoute(): Route | undefined {
Expand Down Expand Up @@ -218,5 +219,8 @@ const PAGES: RouteObject[] = [

/** Client-side routes: links and the picker navigate without reloading the page. */
export function App({ theme }: { theme: ThemeStore }) {
return useRoutes([{ element: <Shell theme={theme} />, children: PAGES }]);
const page = useRoutes([
{ element: <Shell theme={theme} />, children: PAGES },
]);
return <ThemeContext value={theme}>{page}</ThemeContext>;
}
52 changes: 52 additions & 0 deletions packages/flags/src/client/code.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { lazy, Suspense } from "react";

export type JsonEditorProps = {
/** The text shown. Changing it from outside replaces the editor's text. */
readonly value: string;
readonly onChange: (text: string) => void;
/** JSON Schema the text is validated against, as squiggles and hovers. */
readonly schema: Record<string, unknown>;
/** Unique per edited flag, e.g. `scout/prod/key`; names the Monaco model. */
readonly modelPath: string;
readonly label: string;
/** Id of the element listing the draft's issues. */
readonly describedBy?: string | undefined;
readonly invalid: boolean;
};

export type DiffViewProps = {
readonly original: string;
readonly modified: string;
/** Names both sides, e.g. `{ original: "Stored", modified: "Draft" }`. */
readonly labels: { readonly original: string; readonly modified: string };
};

// Monaco is several hundred kilobytes; these chunks load the first time an
// editor or diff is shown, never for the flag table.
const MonacoJsonEditor = lazy(() => import("./monaco/json-editor.tsx"));
const MonacoDiffView = lazy(() => import("./monaco/diff-view.tsx"));

/** The text, unstyled by Monaco, while the editor chunk loads. */
function Loading({ text }: { text: string }) {
return (
<pre className="fl-code fl-code-loading" aria-busy="true">
{text}
</pre>
);
}

export function JsonEditor(props: JsonEditorProps): React.JSX.Element {
return (
<Suspense fallback={<Loading text={props.value} />}>
<MonacoJsonEditor {...props} />
</Suspense>
);
}

export function DiffView(props: DiffViewProps): React.JSX.Element {
return (
<Suspense fallback={<Loading text={props.modified} />}>
<MonacoDiffView {...props} />
</Suspense>
);
}
39 changes: 19 additions & 20 deletions packages/flags/src/client/conflict-banner.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { TriangleAlertIcon } from "lucide-react";
import { Button } from "@shepherdjerred/console-ui";
import type { FlagValue } from "@shepherdjerred/feature-flags/evaluate.ts";
import { flagText } from "./model.ts";
import { DiffView } from "./code.tsx";
import { reviewText } from "./model.ts";

export type ConflictBannerProps = {
/** The revision another write landed at. */
Expand All @@ -12,7 +13,7 @@ export type ConflictBannerProps = {
readonly onLoadTheirs: () => void;
};

/** Warns that a save was not applied and shows both versions side by side. */
/** Warns that a save was not applied and diffs their flag against the draft. */
export function ConflictBanner({
revision,
theirs,
Expand All @@ -32,24 +33,22 @@ export function ConflictBanner({
<p>
Another write landed at revision {revision}. This save was not applied.
</p>
<div className="fl-compare">
<section aria-labelledby="conflict-theirs">
<h4 id="conflict-theirs">Their flag</h4>
<pre>
{theirs === undefined
? "This flag is gone from their document."
: flagText(theirs)}
</pre>
</section>
<section aria-labelledby="conflict-yours">
<h4 id="conflict-yours">Your draft</h4>
<pre>{flagText(draft)}</pre>
</section>
</div>
{theirs === undefined ? null : (
<Button type="button" onClick={onLoadTheirs}>
Load their version
</Button>
{theirs === undefined ? (
<p>This flag is gone from their document.</p>
) : (
<>
<p className="fl-conflict-legend ui-muted">
Their flag (before) against your draft (after).
</p>
<DiffView
original={reviewText(theirs)}
modified={reviewText(draft)}
labels={{ original: "Their flag", modified: "Your draft" }}
/>
<Button type="button" onClick={onLoadTheirs}>
Load their version
</Button>
</>
)}
</section>
);
Expand Down
Loading