import { useServerFn } from "@tanstack/react-start"
import { useId, useState } from "react"
import { FormDialog, FormField, SelectField, fieldControl } from "@/components/primitives"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Textarea } from "@/components/ui/textarea"
import { linkSchema, type Profile } from "./portal.contract"
import { uploadPortalLogo } from "./portal.functions"
export const EMPTY_PROFILE: Profile = {
name: "",
acronym: "",
descriptionIt: "",
descriptionEn: "",
email: "",
logo: null,
links: [],
}
export const roleOptions = [
{ value: "responsible", label: "Responsabile" },
{ value: "collaborator", label: "Collaboratore" },
] as const
export const statusLabel = {
pending: "Da registrare",
active: "Attiva",
suspended: "Sospesa",
archived: "Archiviata",
draft: "Bozza",
submitted: "In revisione",
changes: "Integrazioni richieste",
approved: "Approvata",
rejected: "Rifiutata",
withdrawn: "Ritirata",
registration: "Registrazione",
access: "Accesso",
membership: "Referenti",
content: "Scheda pubblica",
responsible: "Responsabile",
collaborator: "Collaboratore",
}
export const panelClass = "rounded-xl border border-(--pn-line) bg-(--pn-surface) p-5 space-y-4"
export function TextField({
label,
value,
onChange,
multiline = false,
hint,
type = "text",
maxLength = 20000,
}: {
label: string
value: string
onChange: (value: string) => void
multiline?: boolean
hint?: string
type?: string
maxLength?: number
}) {
const id = useId()
return (
{multiline ? (
)
}
export function ProfileFields({
value,
onChange,
associationId,
}: {
value: Profile
onChange: (value: Profile) => void
associationId?: string
}) {
const upload = useServerFn(uploadPortalLogo)
const [uploading, setUploading] = useState(false)
const [uploadError, setUploadError] = useState("")
const prefix = useId()
const set = (key: keyof Profile, next: string | null) => onChange({ ...value, [key]: next })
return (
Questi dati saranno pubblici solo dopo l’approvazione della scheda.
set("name", v)} maxLength={200} />
set("acronym", v)}
maxLength={50}
/>
set("descriptionIt", v)}
/>
set("descriptionEn", v)}
hint="Puoi completarla dopo la registrazione, prima di chiedere la pubblicazione."
/>
set("email", v)}
/>
{associationId && (
{
const file = event.target.files?.[0]
if (!file) return
setUploadError("")
setUploading(true)
try {
if (file.size > 1024 * 1024) throw new Error("Il logo non può superare 1 MB")
const data = new FormData()
data.set("id", associationId)
data.set("file", file)
const result = await upload({ data })
onChange({ ...value, logo: result.url })
} catch (error) {
console.error(error)
setUploadError(error instanceof Error ? error.message : "Caricamento non riuscito")
} finally {
setUploading(false)
}
}}
/>
)}
set("logo", v || null)}
hint="Inserisci un indirizzo HTTPS del logo dell’associazione."
/>
Sito, social e canali
{value.links.map((link, index) => (
))}
)
}
export function ProfilePreview({ profile }: { profile: Profile | null }) {
if (!profile) return Nessuna versione pubblicata.
return (
{profile.logo && (

)}
{profile.name}
{profile.acronym ? ` (${profile.acronym})` : ""}
{profile.descriptionIt}
{profile.descriptionEn}
{profile.email &&
{profile.email}
}
)
}
export function ReasonDialog({
title,
description,
label = "Conferma",
danger = false,
initial = "",
onClose,
onSubmit,
}: {
title: string
description?: string
label?: string
danger?: boolean
initial?: string
onClose: () => void
onSubmit: (reason: string) => Promise
}) {
const [reason, setReason] = useState(initial)
return (
{
if (!open) onClose()
}}
title={title}
description={description}
noun="richiesta"
locale="it"
dirty={reason !== initial}
submitLabel={label}
tone={danger ? "danger" : undefined}
canSubmit={!!reason.trim()}
onSubmit={async () => {
await onSubmit(reason)
onClose()
}}
>
)
}