/** * AI Discovery component - Configure AI and LLM-related features. * * @package GetMD * @since 1.0.0 */ import { useState, useEffect } from 'react'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from './ui/card'; import { Button } from './ui/button'; import { Switch } from './ui/switch'; import { Toast } from './ui/toast'; import { getAPI } from '../lib/api'; /** * Render the AI Discovery UI for configuring AI and LLM-related features. * * Currently includes the Public Markdown View feature which enables visitors * to view posts as Markdown by adding ?view=md to the URL - useful for AI agents * and LLMs that need to consume content in a clean format. * * @returns The AI Discovery panel as a JSX element */ export function AIDiscovery() { const { i18n, canManageOptions } = window.summixGetmdData || {}; const isAdmin = Boolean(canManageOptions); // Loading and notification states const [isLoading, setIsLoading] = useState(true); const [isSaving, setIsSaving] = useState(false); const [toast, setToast] = useState<{ message: string; variant: 'default' | 'destructive' } | null>(null); // Global Settings (admin only) const [frontendViewEnabled, setFrontendViewEnabled] = useState(false); // Load settings on mount useEffect(() => { const abortController = new AbortController(); const loadSettings = async () => { setIsLoading(true); try { if (isAdmin && !abortController.signal.aborted) { const globalSettings = await getAPI().getGlobalSettings(); if (!abortController.signal.aborted) { setFrontendViewEnabled(globalSettings.frontendViewEnabled); } } } catch (error) { if (!abortController.signal.aborted) { console.error('Failed to load global settings:', error); setFrontendViewEnabled(false); setToast({ message: i18n?.settingsLoadFailed || "Loaded defaults—couldn't reach saved settings.", variant: 'destructive', }); } } finally { if (!abortController.signal.aborted) { setIsLoading(false); } } }; loadSettings(); return () => { abortController.abort(); }; }, [isAdmin]); const handleSave = async () => { setIsSaving(true); setToast(null); try { const globalSettingsResponse = await getAPI().saveGlobalSettings({ frontendViewEnabled, }); setFrontendViewEnabled(globalSettingsResponse.settings.frontendViewEnabled); setToast({ message: i18n?.settingsSaved || 'Settings saved', variant: 'default', }); } catch (error) { console.error('Failed to save settings:', error); setToast({ message: error instanceof Error ? error.message : (i18n?.settingsSaveFailed || "Couldn't save. Try again?"), variant: 'destructive', }); } finally { setIsSaving(false); } }; // Show loading UI while fetching settings if (isLoading) { return (
{i18n?.loading || 'Loading...'}

{i18n?.loading || 'Loading...'}

); } // Non-admin users see a message if (!isAdmin) { return (

{i18n?.adminOnlyFeatures || 'These features are only available to administrators.'}

); } return (
{/* Toast Notification */} {toast && ( setToast(null)} /> )} {/* Public Markdown View */} {i18n?.publicViewTitle || 'Public Markdown View'} {i18n?.publicViewDesc || 'Let visitors view any post as Markdown by adding ?view=md to the URL.'} {/* Enable Frontend View Toggle */}

{i18n?.publicViewDesc || 'Let visitors view any post as Markdown by adding ?view=md to the URL.'}

{/* Example URL */} {frontendViewEnabled && (

{i18n?.tryIt || 'Try it:'}

{window.location.origin}/your-post-slug/?view=md
)}
{/* Save Button */}
); }