"use client"; import { Building2, Gamepad2, ImageIcon, Link as LinkIcon, Loader2, Music, Save, Shield, UserPlus, } from "lucide-react"; import { useTranslations } from "next-intl"; import { useState } from "react"; import { saveManagedSettings } from "@/actions/admin-settings"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Switch } from "@/components/ui/switch"; import { Textarea } from "@/components/ui/textarea"; import { useServerAction } from "@/hooks/use-server-action"; import { BOOLEAN_KEYS, type ManagedField, SETTINGS_GROUPS, type SettingsGroup, } from "./cms-settings-config"; const ICONS = { building: Building2, image: ImageIcon, gamepad: Gamepad2, music: Music, shield: Shield, link: LinkIcon, "user-plus": UserPlus, } as const; export function CmsSettingsForm({ values: initialValues, canEdit, }: { values: Record; canEdit: boolean; }) { const [values, setValues] = useState(initialValues); const { isPending, run } = useServerAction(); const t = useTranslations("pages.admin.settings"); function handleChange(key: string, value: string) { setValues((prev) => ({ ...prev, [key]: value })); } function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!canEdit) return; const normalized = { ...values }; for (const key of BOOLEAN_KEYS) { normalized[key] = values[key] === "1" ? "1" : "0"; } run(() => saveManagedSettings({ settings: normalized }), { successMessage: t("saved"), errorMessage: t("saveFailed"), }); } return (
{SETTINGS_GROUPS.map((group) => ( ))} {canEdit ? (
) : (

{t("viewOnly")}

)} ); } function GroupSection({ group, values, canEdit, onChange, }: { group: SettingsGroup; values: Record; canEdit: boolean; onChange: (key: string, value: string) => void; }) { const Icon = ICONS[group.icon]; return (

{group.title}

{group.description ? (

{group.description}

) : null}
{group.fields.map((field) => ( ))}
); } function FieldControl({ field, value, canEdit, onChange, }: { field: ManagedField; value: string; canEdit: boolean; onChange: (key: string, value: string) => void; }) { const type = field.type ?? "text"; const wide = type === "textarea" || type === "boolean"; if (type === "boolean") { const checked = value === "1" || value.toLowerCase() === "true"; return (
{field.description ? (

{field.description}

) : null}
onChange(field.key, next ? "1" : "0")} />
); } return (
{field.description ? (

{field.description}

) : null} {type === "textarea" ? (