File size: 1,799 Bytes
689359a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | "use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { useTranslations } from "next-intl";
import { Switch } from "@/components/ui/switch";
export type FeatureFlagField = "web_enabled" | "ios_enabled" | "admins_bypass";
interface FeatureFlagSwitchProps {
flagKey: string;
field: FeatureFlagField;
checked: boolean;
label: string;
}
/** Switches one column of a feature flag (`/api/admin/feature-flags/[key]`). */
export function FeatureFlagSwitch({ flagKey, field, checked, label }: FeatureFlagSwitchProps) {
const t = useTranslations("admin");
const [value, setValue] = useState(checked);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const router = useRouter();
const handleChange = async (next: boolean) => {
setError(null);
setIsLoading(true);
setValue(next);
try {
const response = await fetch(`/api/admin/feature-flags/${encodeURIComponent(flagKey)}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ [field]: next }),
});
if (!response.ok) {
const data = await response.json().catch(() => ({}));
throw new Error(data.error || t("flags.updateFailed"));
}
router.refresh();
} catch (changeError) {
setValue(!next);
setError(changeError instanceof Error ? changeError.message : t("flags.updateFailed"));
} finally {
setIsLoading(false);
}
};
return (
<div className="flex flex-col items-start gap-1">
<Switch checked={value} onCheckedChange={handleChange} disabled={isLoading} aria-label={label} />
{error && <span className="text-xs text-red-600">{error}</span>}
</div>
);
}
|