'use client' import { useState, useEffect, useCallback } from 'react' import { useRouter } from 'next/navigation' import Link from 'next/link' import { createSite, listSites, type Site } from '@/lib/api/sites' import { getSubscription } from '@/lib/api/billing' import { API_URL, APP_URL } from '@/lib/api/client' import { integrations, getIntegration } from '@/lib/integrations' import { toast } from '@ciphera-net/ui' import { getAuthErrorMessage } from '@/lib/utils/authErrors' import { Button, Input } from '@ciphera-net/ui' import { CheckCircleIcon, CheckIcon } from '@ciphera-net/ui' const popularIntegrations = integrations.filter((i) => i.category === 'framework').slice(0, 10) export default function NewSitePage() { const router = useRouter() const [loading, setLoading] = useState(false) const [formData, setFormData] = useState({ name: '', domain: '', }) const [createdSite, setCreatedSite] = useState(null) const [selectedIntegrationSlug, setSelectedIntegrationSlug] = useState(null) const [scriptCopied, setScriptCopied] = useState(false) // * Check for plan limits on mount useEffect(() => { const checkLimits = async () => { try { const [sites, subscription] = await Promise.all([ listSites(), getSubscription() ]) if (subscription?.plan_id === 'solo' && sites.length >= 1) { toast.error('Solo plan limit reached (1 site). Please upgrade to add more sites.') router.replace('/') } } catch (error) { // Ignore errors here, let the backend handle the hard check on submit console.error('Failed to check limits', error) } } checkLimits() }, [router]) const copyScript = useCallback(() => { if (!createdSite) return const script = `` navigator.clipboard.writeText(script) setScriptCopied(true) toast.success('Script copied to clipboard') setTimeout(() => setScriptCopied(false), 2000) }, [createdSite]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) try { const site = await createSite(formData) toast.success('Site created successfully') setCreatedSite(site) } catch (error: unknown) { toast.error(getAuthErrorMessage(error) || 'Failed to create site: ' + ((error as Error)?.message || 'Unknown error')) } finally { setLoading(false) } } // * Step 2: Show framework picker + script (same as /welcome after adding first site) if (createdSite) { return (

Site created

Add the script to your site to start collecting data.

Add the script to your site

Choose your framework for setup instructions.

{popularIntegrations.map((int) => ( ))}

View all integrations →

{``}
{selectedIntegrationSlug && getIntegration(selectedIntegrationSlug) && (

See full {getIntegration(selectedIntegrationSlug)!.name} guide →

)}
) } // * Step 1: Name & domain form return (

Create New Site

setFormData({ ...formData, name: e.target.value })} placeholder="My Website" />
setFormData({ ...formData, domain: e.target.value.toLowerCase().trim() })} placeholder="example.com" />

Enter your domain without http:// or https://

) }