'use client'
import { Button, toast, Spinner } from '@ciphera-net/ui'
import { GoogleLogo, ArrowSquareOut, Plugs, Trash } from '@phosphor-icons/react'
import { useGSCStatus, useBunnyStatus } from '@/lib/swr/dashboard'
import { disconnectGSC, getGSCAuthURL } from '@/lib/api/gsc'
import { disconnectBunny } from '@/lib/api/bunny'
import { getAuthErrorMessage } from '@ciphera-net/ui'
function IntegrationCard({
icon,
name,
description,
connected,
detail,
onConnect,
onDisconnect,
connectLabel = 'Connect',
}: {
icon: React.ReactNode
name: string
description: string
connected: boolean
detail?: string
onConnect: () => void
onDisconnect: () => void
connectLabel?: string
}) {
return (
{icon}
{name}
{connected && (
Connected
)}
{detail || description}
{connected ? (
) : (
)}
)
}
export default function SiteIntegrationsTab({ siteId }: { siteId: string }) {
const { data: gscStatus, mutate: mutateGSC } = useGSCStatus(siteId)
const { data: bunnyStatus, mutate: mutateBunny } = useBunnyStatus(siteId)
const handleConnectGSC = async () => {
try {
const data = await getGSCAuthURL(siteId)
window.open(data.auth_url, '_blank')
} catch (err) {
toast.error(getAuthErrorMessage(err as Error) || 'Failed to start Google authorization')
}
}
const handleDisconnectGSC = async () => {
if (!confirm('Disconnect Google Search Console? This will remove all synced data.')) return
try {
await disconnectGSC(siteId)
await mutateGSC()
toast.success('Google Search Console disconnected')
} catch (err) {
toast.error(getAuthErrorMessage(err as Error) || 'Failed to disconnect')
}
}
const handleConnectBunny = () => {
// Redirect to full settings page for BunnyCDN setup (requires API key input)
window.location.href = `/sites/${siteId}/settings?tab=integrations`
}
const handleDisconnectBunny = async () => {
if (!confirm('Disconnect BunnyCDN? This will remove all synced CDN data.')) return
try {
await disconnectBunny(siteId)
await mutateBunny()
toast.success('BunnyCDN disconnected')
} catch (err) {
toast.error(getAuthErrorMessage(err as Error) || 'Failed to disconnect')
}
}
return (
Integrations
Connect third-party services to enrich your analytics.
}
name="Google Search Console"
description="View search queries, clicks, impressions, and ranking data."
connected={gscStatus?.connected ?? false}
detail={gscStatus?.connected ? `Connected as ${gscStatus.google_email || 'unknown'}` : undefined}
onConnect={handleConnectGSC}
onDisconnect={handleDisconnectGSC}
connectLabel="Connect with Google"
/>
{ (e.target as HTMLImageElement).style.display = 'none' }} />}
name="BunnyCDN"
description="Monitor bandwidth, cache hit rates, and CDN performance."
connected={bunnyStatus?.connected ?? false}
detail={bunnyStatus?.connected ? `Pull zone: ${bunnyStatus.pull_zone_name || 'connected'}` : undefined}
onConnect={handleConnectBunny}
onDisconnect={handleDisconnectBunny}
/>
)
}