'use client' import { useState, useEffect } from 'react' import { useRouter } from 'next/navigation' import Link from 'next/link' import { createSite, listSites, getSite, type Site } from '@/lib/api/sites' import { getSubscription } from '@/lib/api/billing' import { getSitesLimitForPlan } from '@/lib/plans' import { trackSiteCreatedFromDashboard, trackSiteCreatedScriptCopied } from '@/lib/welcomeAnalytics' import { toast } from '@ciphera-net/ui' import { getAuthErrorMessage } from '@ciphera-net/ui' import { Button, Input } from '@ciphera-net/ui' import { CheckCircleIcon } from '@ciphera-net/ui' import ScriptSetupBlock from '@/components/sites/ScriptSetupBlock' import VerificationModal from '@/components/sites/VerificationModal' const LAST_CREATED_SITE_KEY = 'pulse_last_created_site' export default function NewSitePage() { const router = useRouter() const [loading, setLoading] = useState(false) const [formData, setFormData] = useState({ name: '', domain: '', }) const [createdSite, setCreatedSite] = useState(null) const [showVerificationModal, setShowVerificationModal] = useState(false) const [atLimit, setAtLimit] = useState(false) const [limitsChecked, setLimitsChecked] = useState(false) // * Restore step 2 from sessionStorage after refresh (e.g. pulse_last_created_site = { id } ) useEffect(() => { if (createdSite || typeof window === 'undefined') return try { const raw = sessionStorage.getItem(LAST_CREATED_SITE_KEY) if (!raw) return const { id } = JSON.parse(raw) as { id?: string } if (!id) return getSite(id) .then((site) => { setCreatedSite(site) setFormData({ name: site.name, domain: site.domain }) }) .catch(() => { sessionStorage.removeItem(LAST_CREATED_SITE_KEY) }) } catch { sessionStorage.removeItem(LAST_CREATED_SITE_KEY) } }, [createdSite]) // * Check for plan limits on mount useEffect(() => { const checkLimits = async () => { try { const [sites, subscription] = await Promise.all([ listSites(), getSubscription() ]) const siteLimit = subscription?.plan_id ? getSitesLimitForPlan(subscription.plan_id) : null if (siteLimit != null && sites.length >= siteLimit) { setAtLimit(true) toast.error(`${subscription.plan_id} plan limit reached (${siteLimit} site${siteLimit === 1 ? '' : 's'}). Please upgrade to add more sites.`) router.replace('/') } } catch (error) { console.error('Failed to check limits', error) } finally { setLimitsChecked(true) } } checkLimits() }, [router]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) try { const site = await createSite(formData) toast.success('Site created successfully') setCreatedSite(site) trackSiteCreatedFromDashboard() if (typeof window !== 'undefined') { sessionStorage.setItem(LAST_CREATED_SITE_KEY, JSON.stringify({ id: site.id })) } } catch (error: unknown) { toast.error(getAuthErrorMessage(error) || 'Failed to create site: ' + ((error as Error)?.message || 'Unknown error')) } finally { setLoading(false) } } const handleBackToForm = () => { setCreatedSite(null) if (typeof window !== 'undefined') sessionStorage.removeItem(LAST_CREATED_SITE_KEY) } const goToDashboard = () => { router.refresh() router.push('/') } // * Step 2: 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.

Check if your site is sending data correctly.

setShowVerificationModal(false)} site={createdSite} />
) } // * Step 1: Name & domain form return (

Create New Site

{atLimit && limitsChecked && (

Plan limit reached. Upgrade to add more sites.

)}
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://

) }