import React, { useMemo, useRef, useState } from 'react' import { router, usePage } from '@inertiajs/react' import StudioLayout from '../../Layouts/StudioLayout' import NovaSelect from '../../components/ui/NovaSelect' function resolveMediaPreviewUrl(path, filesCdnUrl) { const trimmed = String(path || '').trim() if (!trimmed) { return '' } if (trimmed.startsWith('blob:') || trimmed.startsWith('data:') || trimmed.startsWith('http://') || trimmed.startsWith('https://')) { return trimmed } return `${String(filesCdnUrl || '').replace(/\/$/, '')}/${trimmed.replace(/^\/+/, '')}` } export default function StudioGroupSettings() { const { props } = usePage() const group = props.studioGroup || {} const filesCdnUrl = props?.cdn?.files_url || '' const featuredArtworkOptions = Array.isArray(props.featuredArtworkOptions) ? props.featuredArtworkOptions : [] const avatarInputRef = useRef(null) const bannerInputRef = useRef(null) const [form, setForm] = useState({ name: group.name || '', slug: group.slug || '', headline: group.headline || '', bio: group.bio || '', type: group.type || '', founded_at: group.founded_at ? String(group.founded_at).slice(0, 10) : '', avatar_path: group.avatar_path || group.avatar_url || '', banner_path: group.banner_path || group.banner_url || '', visibility: group.visibility || 'public', membership_policy: group.membership_policy || 'invite_only', website_url: group.website_url || '', links_json: Array.isArray(group.links) && group.links.length > 0 ? group.links : [{ label: '', url: '' }], featured_artwork_id: group.featured_artwork_id || '', avatar_file: null, banner_file: null, }) const [avatarPreview, setAvatarPreview] = useState('') const [bannerPreview, setBannerPreview] = useState('') const resolvedAvatarPreview = useMemo(() => avatarPreview || resolveMediaPreviewUrl(form.avatar_path || group.avatar_url, filesCdnUrl), [avatarPreview, form.avatar_path, group.avatar_url, filesCdnUrl]) const resolvedBannerPreview = useMemo(() => bannerPreview || resolveMediaPreviewUrl(form.banner_path || group.banner_url, filesCdnUrl), [bannerPreview, form.banner_path, group.banner_url, filesCdnUrl]) const selectedFeaturedArtwork = useMemo( () => featuredArtworkOptions.find((item) => Number(item.id) === Number(form.featured_artwork_id)) || null, [featuredArtworkOptions, form.featured_artwork_id], ) const updateLink = (index, key, value) => { setForm((current) => ({ ...current, links_json: current.links_json.map((item, itemIndex) => itemIndex === index ? { ...item, [key]: value } : item), })) } const addLink = () => { setForm((current) => ({ ...current, links_json: [...current.links_json, { label: '', url: '' }], })) } const removeLink = (index) => { setForm((current) => ({ ...current, links_json: current.links_json.filter((_, itemIndex) => itemIndex !== index), })) } const submit = () => { router.post(props.endpoints?.update, { _method: 'patch', ...form, links_json: form.links_json.filter((item) => item.label.trim() !== '' || item.url.trim() !== ''), }, { forceFormData: true, }) } const handleFileSelected = (field, setPreview) => (event) => { const file = event.target.files?.[0] || null setForm((current) => ({ ...current, [field]: file })) setPreview(file ? URL.createObjectURL(file) : '') } const clearSelectedFile = (field, setPreview, inputRef) => { setForm((current) => ({ ...current, [field]: null })) setPreview('') if (inputRef.current) { inputRef.current.value = '' } } const archiveGroup = () => { if (!window.confirm('Archive this group? New group publishing will stop immediately until you reopen it through admin tooling.')) { return } router.post(props.endpoints?.archive) } return (