'use client' import { useState } from 'react' import { Input, Button, toast } from '@ciphera-net/ui' import { Plus, Pencil, Trash, X } from '@phosphor-icons/react' import { Spinner } from '@ciphera-net/ui' import { useGoals } from '@/lib/swr/dashboard' import { createGoal, updateGoal, deleteGoal } from '@/lib/api/goals' import { getAuthErrorMessage } from '@ciphera-net/ui' export default function SiteGoalsTab({ siteId }: { siteId: string }) { const { data: goals = [], mutate, isLoading } = useGoals(siteId) const [editing, setEditing] = useState(null) const [creating, setCreating] = useState(false) const [name, setName] = useState('') const [eventName, setEventName] = useState('') const [saving, setSaving] = useState(false) const startCreate = () => { setCreating(true) setEditing(null) setName('') setEventName('') } const startEdit = (goal: { id: string; name: string; event_name: string }) => { setEditing(goal.id) setCreating(false) setName(goal.name) setEventName(goal.event_name) } const cancel = () => { setCreating(false) setEditing(null) setName('') setEventName('') } const handleSave = async () => { if (!name.trim() || !eventName.trim()) { toast.error('Name and event name are required') return } if (!/^[a-zA-Z0-9_]+$/.test(eventName)) { toast.error('Event name can only contain letters, numbers, and underscores') return } setSaving(true) try { if (editing) { await updateGoal(siteId, editing, { name, event_name: eventName }) toast.success('Goal updated') } else { await createGoal(siteId, { name, event_name: eventName }) toast.success('Goal created') } await mutate() cancel() } catch (err) { toast.error(getAuthErrorMessage(err as Error) || 'Failed to save goal') } finally { setSaving(false) } } const handleDelete = async (goalId: string) => { try { await deleteGoal(siteId, goalId) toast.success('Goal deleted') await mutate() } catch (err) { toast.error(getAuthErrorMessage(err as Error) || 'Failed to delete goal') } } if (isLoading) { return (
) } return (

Goals

Track custom events as conversion goals.

{!creating && !editing && ( )}
{/* Create/Edit form */} {(creating || editing) && (
setName(e.target.value)} placeholder="e.g. Sign Up" />
setEventName(e.target.value)} placeholder="e.g. signup_click" disabled={!!editing} />
)} {/* Goals list */} {goals.length === 0 && !creating ? (

No goals yet. Add a goal to track custom events.

) : (
{goals.map(goal => (

{goal.name}

{goal.event_name}

))}
)}
) }