"use client"; import { Icon } from "@/components/icons"; import type { QrFormProps } from "@/components/qr-forms/types"; import { Button, Field, Select, TextArea, TextInput } from "@/components/ui"; import { SOCIAL_NETWORKS, getSocialNetwork } from "@/lib/qr/social-networks"; import { MAX_PROFILES } from "@/lib/qr/types/social"; type Profile = { network: string; url: string }; function readProfiles(value: unknown): Profile[] { if (!Array.isArray(value)) return [{ network: "instagram", url: "" }]; const entries = value.map((entry) => { const record = (entry ?? {}) as Record; return { network: typeof record.network === "string" ? record.network : "website", url: typeof record.url === "string" ? record.url : "", }; }); return entries.length > 0 ? entries : [{ network: "instagram", url: "" }]; } export function SocialForm({ value, onChange, error, disabled }: QrFormProps) { const title = typeof value.title === "string" ? value.title : ""; const description = typeof value.description === "string" ? value.description : ""; const profiles = readProfiles(value.profiles); const belegt = new Set(profiles.map((profile) => profile.network)); const naechstesFreies = SOCIAL_NETWORKS.find((network) => !belegt.has(network.id))?.id ?? "website"; const setProfiles = (next: Profile[]) => onChange({ ...value, profiles: next }); const updateProfile = (index: number, patch: Partial) => setProfiles( profiles.map((profile, i) => (i === index ? { ...profile, ...patch } : profile)), ); return (
onChange({ ...value, title: event.target.value })} />