import React, { useCallback, useEffect, useState } from 'react'; import apiFetch from '@wordpress/api-fetch'; import { __ } from '@wordpress/i18n'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from '@/components/ui/card'; import { Label } from '@/components/ui/label'; import { Separator } from '@/components/ui/separator'; import { Spinner } from '@/components/ui/spinner'; import { Switch } from '@/components/ui/switch'; interface AccountData { connected: boolean; user_name: string | null; error: string | null; manage_url: string; unlink_url: string; authorize_url: string; } interface SettingsData { telemetry_enabled: boolean; abilities_api_enabled: boolean; has_abilities_api: boolean; outbound_tracking_enabled: boolean; dashboard_takeover_enabled: boolean; } interface WebsiteSettingsData { weekly_email_enabled: boolean; } export default function Settings() { const [ account, setAccount ] = useState( null ); const [ settings, setSettings ] = useState( null ); const [ websiteSettings, setWebsiteSettings ] = useState( null ); const [ isLoading, setIsLoading ] = useState( true ); // Load data on mount useEffect( () => { const loadData = async () => { try { const [ accountRes, settingsRes ] = await Promise.all( [ apiFetch( { path: '/accelerate/v1/settings/account' } ), apiFetch( { path: '/accelerate/v1/settings' } ), ] ); setAccount( accountRes ); setSettings( settingsRes ); // Load website settings if connected. if ( accountRes?.connected ) { try { const websiteRes = await apiFetch( { path: '/accelerate/v1/settings/website', } ); setWebsiteSettings( websiteRes ); } catch { // Website settings are optional — don't block the page. } } } catch ( error ) { console.error( 'Failed to load settings:', error ); } finally { setIsLoading( false ); } }; loadData(); }, [] ); // Auto-save setting when changed const updateSetting = useCallback( async ( key: 'telemetry_enabled' | 'abilities_api_enabled' | 'outbound_tracking_enabled' | 'dashboard_takeover_enabled', value: boolean ) => { // Update local state immediately setSettings( prev => prev ? { ...prev, [ key ]: value, } : null ); // Save to server try { await apiFetch( { path: '/accelerate/v1/settings', method: 'POST', data: { [ key ]: value }, } ); } catch ( error ) { console.error( 'Failed to save setting:', error ); // Revert on error setSettings( prev => prev ? { ...prev, [ key ]: ! value, } : null ); } }, [] ); // Auto-save website setting when changed const updateWebsiteSetting = useCallback( async ( key: keyof WebsiteSettingsData, value: boolean ) => { // Update local state immediately setWebsiteSettings( prev => prev ? { ...prev, [ key ]: value, } : null ); // Save to server try { await apiFetch( { path: '/accelerate/v1/settings/website', method: 'POST', data: { [ key ]: value }, } ); } catch ( error ) { console.error( 'Failed to save website setting:', error ); // Revert on error setWebsiteSettings( prev => prev ? { ...prev, [ key ]: ! value, } : null ); } }, [] ); if ( isLoading ) { return (
{ __( 'Loading...', 'altis' ) }
); } return (
{ /* Account Card */ } { __( 'Account', 'altis' ) } { __( 'Manage your Accelerate account connection', 'altis' ) } { account?.connected ? ( <> { account.error ? (

{ __( 'Connection failed:', 'altis' ) }{ ' ' } { account.error }

) : (

{ __( 'Connected as', 'altis' ) }{ ' ' } { account.user_name }

) } ) : (

{ __( 'Connect your site to Accelerate to enable analytics and personalization features.', 'altis' ) }

) }
{ /* Settings Card */ } { __( 'Settings', 'altis' ) } { __( 'Configure Accelerate preferences', 'altis' ) } { /* Analytics Dashboard Toggle */ }

{ __( 'Replace the default WordPress dashboard with the Accelerate analytics view.', 'altis' ) }

updateSetting( 'dashboard_takeover_enabled', checked ) } />
{ /* Product Analytics Toggle */ }

{ __( 'We\'re building personalized site insights — like testing benchmarks and performance stats. Keep this enabled so your data is ready when they launch. This also helps us understand which features matter most so we can build the right things.', 'altis' ) }

updateSetting( 'telemetry_enabled', checked ) } />
{ /* AI Abilities API Toggle */ }

{ __( 'Connect AI assistants like ChatGPT or Claude to your site. They can access your analytics, create and manage A/B tests, and help optimize your content strategy.', 'altis' ) }

{ __( 'Learn more about AI integration', 'altis' ) }

{ ! settings?.has_abilities_api && (

{ __( 'Requires WordPress 6.9+', 'altis' ) }

) }
updateSetting( 'abilities_api_enabled', checked ) } />
{ /* Outbound Click Tracking Toggle */ }

{ __( 'Track clicks on links that lead off your site, grouped by destination domain. Powers the Outbound clicks tile in Content Explorer.', 'altis' ) }

{ __( 'Only the destination host is recorded — no full URLs, no query strings, no path. Turning this off later stops new tracking but keeps any existing data, so re-enabling restores the historical view.', 'altis' ) }

updateSetting( 'outbound_tracking_enabled', checked ) } />
{ /* Notifications Card — only when connected */ } { account?.connected && websiteSettings && ( { __( 'Notifications', 'altis' ) } { __( 'Manage email notifications from Accelerate', 'altis' ) }

{ __( 'Receive a weekly summary of your site analytics by email.', 'altis' ) }

updateWebsiteSetting( 'weekly_email_enabled', checked ) } />
) }
); }