Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+34
-23
@@ -18,8 +18,8 @@ import { validateCSRFToken, getUserInfo } from "~/utils/session.server";
|
||||
import { appendCSRFToken } from "~/utils/csrf";
|
||||
import { evaluateRule, type TargetingRule } from "@hivegrid/feature-flags";
|
||||
import { invalidateFlagCache } from "~/utils/services/.server/featureFlagEvaluator.server";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { useLocaleFormatters } from "~/locale/format";
|
||||
import { useSaveToast } from "~/hooks/use-save-toast";
|
||||
|
||||
type V2Flag = {
|
||||
id: string;
|
||||
@@ -47,6 +47,14 @@ type LoaderData = {
|
||||
audits: AuditRow[];
|
||||
};
|
||||
|
||||
type ActionData = {
|
||||
intent: string;
|
||||
ok?: true;
|
||||
messageKey?: string;
|
||||
errorKey?: string;
|
||||
errorValues?: Record<string, string | number>;
|
||||
};
|
||||
|
||||
type RuleValidationError = {
|
||||
key: string;
|
||||
options?: Record<string, string | number>;
|
||||
@@ -168,7 +176,6 @@ export const loader: LoaderFunction = async ({ request }) => {
|
||||
export const action = async ({ request }: ActionFunctionArgs) => {
|
||||
await verifyUserHasPermission(request, "admin.featureFlags.manage");
|
||||
await validateCSRFToken(request);
|
||||
const { t } = await getTranslations(request);
|
||||
const form = await request.formData();
|
||||
const intent = form.get("intent")?.toString() ?? "";
|
||||
const id = form.get("id")?.toString() ?? "";
|
||||
@@ -176,13 +183,13 @@ export const action = async ({ request }: ActionFunctionArgs) => {
|
||||
const actorUserId = userInfo.userId || null;
|
||||
|
||||
const existing = await db.featureFlag.findUnique({ where: { id } });
|
||||
if (!existing) return Response.json({ error: t("featureFlags.engine.errors.notFound") }, { status: 404 });
|
||||
if (!existing) return Response.json({ intent, errorKey: "featureFlags.engine.errors.notFound" } satisfies ActionData, { status: 404 });
|
||||
|
||||
if (intent === "toggle") {
|
||||
const newEnabled = !existing.enabled;
|
||||
await db.$transaction([
|
||||
db.featureFlag.update({ where: { id }, data: { enabled: newEnabled } }),
|
||||
db.featureFlagAudit.create({
|
||||
await db.$transaction(async (tx) => {
|
||||
await tx.featureFlag.update({ where: { id }, data: { enabled: newEnabled } });
|
||||
await tx.featureFlagAudit.create({
|
||||
data: {
|
||||
flagId: id,
|
||||
actorUserId,
|
||||
@@ -190,10 +197,10 @@ export const action = async ({ request }: ActionFunctionArgs) => {
|
||||
oldValue: { enabled: existing.enabled },
|
||||
newValue: { enabled: newEnabled },
|
||||
},
|
||||
}),
|
||||
]);
|
||||
});
|
||||
});
|
||||
await invalidateFlagCache(existing.name);
|
||||
return Response.json({ ok: true });
|
||||
return Response.json({ intent } satisfies ActionData);
|
||||
}
|
||||
|
||||
if (intent === "save-rules") {
|
||||
@@ -203,11 +210,14 @@ export const action = async ({ request }: ActionFunctionArgs) => {
|
||||
const value = JSON.parse(rulesRaw);
|
||||
parsed = value === null ? null : (value as TargetingRule);
|
||||
} catch {
|
||||
return Response.json({ error: t("featureFlags.engine.errors.invalidRulesJson") }, { status: 400 });
|
||||
return Response.json({ intent, errorKey: "featureFlags.engine.errors.invalidRulesJson" } satisfies ActionData, { status: 400 });
|
||||
}
|
||||
const validationError = parsed ? validateTargetingRuleShape(parsed) : null;
|
||||
if (validationError) {
|
||||
return Response.json({ error: t(validationError.key, validationError.options) }, { status: 400 });
|
||||
return Response.json(
|
||||
{ intent, errorKey: validationError.key, errorValues: validationError.options } satisfies ActionData,
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
// Validation: evaluate against a representative context to ensure
|
||||
// the rule does not throw (it never does — see evaluator.ts — but
|
||||
@@ -216,9 +226,8 @@ export const action = async ({ request }: ActionFunctionArgs) => {
|
||||
if (parsed) {
|
||||
try {
|
||||
evaluateRule(parsed, { flagName: existing.name, tenantId: "ctx_probe", userId: "ctx_probe" });
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : t("featureFlags.engine.errors.invalidRule");
|
||||
return Response.json({ error: message }, { status: 400 });
|
||||
} catch {
|
||||
return Response.json({ intent, errorKey: "featureFlags.engine.errors.invalidRule" } satisfies ActionData, { status: 400 });
|
||||
}
|
||||
}
|
||||
// Prisma's JsonValue type rejects our typed TargetingRule narrowing — cast
|
||||
@@ -227,9 +236,9 @@ export const action = async ({ request }: ActionFunctionArgs) => {
|
||||
const rulesForDb = (parsed ?? null) as unknown as never;
|
||||
const auditOldRules = (existing.rules ?? null) as unknown as never;
|
||||
const auditNewRules = (parsed ?? null) as unknown as never;
|
||||
await db.$transaction([
|
||||
db.featureFlag.update({ where: { id }, data: { rules: rulesForDb } }),
|
||||
db.featureFlagAudit.create({
|
||||
await db.$transaction(async (tx) => {
|
||||
await tx.featureFlag.update({ where: { id }, data: { rules: rulesForDb } });
|
||||
await tx.featureFlagAudit.create({
|
||||
data: {
|
||||
flagId: id,
|
||||
actorUserId,
|
||||
@@ -237,21 +246,23 @@ export const action = async ({ request }: ActionFunctionArgs) => {
|
||||
oldValue: { rules: auditOldRules } as unknown as never,
|
||||
newValue: { rules: auditNewRules } as unknown as never,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
});
|
||||
});
|
||||
await invalidateFlagCache(existing.name);
|
||||
return Response.json({ ok: true });
|
||||
return Response.json({ intent, ok: true, messageKey: "featureFlags.engine.toast.rulesSaved" } satisfies ActionData);
|
||||
}
|
||||
|
||||
return Response.json({ error: t("featureFlags.engine.errors.unknownIntent") }, { status: 400 });
|
||||
return Response.json({ intent, errorKey: "featureFlags.engine.errors.unknownIntent" } satisfies ActionData, { status: 400 });
|
||||
};
|
||||
|
||||
export default function FeatureFlagsEngineRoute() {
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const fetcher = useFetcher();
|
||||
const fetcher = useFetcher<ActionData>();
|
||||
const { t } = useTranslation();
|
||||
const formatters = useLocaleFormatters();
|
||||
|
||||
useSaveToast(fetcher.data);
|
||||
|
||||
function submitToggle(flagId: string) {
|
||||
const form = new FormData();
|
||||
appendCSRFToken(form);
|
||||
@@ -270,7 +281,7 @@ export default function FeatureFlagsEngineRoute() {
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mx-auto mb-12 max-w-5xl space-y-5 px-4 py-4 sm:px-6 lg:px-8 xl:max-w-7xl">
|
||||
<div className="mb-12 w-full space-y-5 px-6 py-6 mobile:px-4">
|
||||
<div className="border-border border-b pb-5">
|
||||
<h3 className="text-lg leading-6 font-medium">{t("featureFlags.engine.title")}</h3>
|
||||
<p className="text-muted-foreground text-sm">{t("featureFlags.engine.description")}</p>
|
||||
|
||||
Reference in new issue
Block a user