"use client"; import { useState } from "react"; import { useAuth } from "@/hooks/use-auth"; import { updateProfile, type ProfileField } from "@/lib/api"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { toast } from "sonner"; import { Plus, Trash2 } from "lucide-react"; const MAX_FIELDS = 4; export function ProfileFieldsEditor({ initial, }: { initial: ProfileField[]; }) { const { token } = useAuth(); const [fields, setFields] = useState(initial); const [saving, setSaving] = useState(false); const update = (i: number, key: "name" | "value", val: string) => { setFields((prev) => prev.map((f, j) => (j === i ? { ...f, [key]: val } : f))); }; const add = () => { if (fields.length >= MAX_FIELDS) return; setFields((prev) => [...prev, { name: "", value: "" }]); }; const remove = (i: number) => { setFields((prev) => prev.filter((_, j) => j !== i)); }; const save = async () => { if (!token) return; const clean = fields.filter((f) => f.name.trim() || f.value.trim()); setSaving(true); try { await updateProfile({ profileFields: clean }, token); setFields(clean); toast.success("Profile fields saved."); } catch { toast.error("Failed to save profile fields."); } finally { setSaving(false); } }; return (

Profile fields

Add up to {MAX_FIELDS} custom fields visible on your profile and across the fediverse (e.g. Website, Pronouns).

{fields.map((f, i) => (
update(i, "name", e.target.value)} placeholder="Label" className="max-w-[8rem] text-sm" /> update(i, "value", e.target.value)} placeholder="Value" className="text-sm" />
))}
{fields.length < MAX_FIELDS && ( )}
); }