import React, { useState } from 'react'; import { useSetIsLoginModalOpen } from '../contexts/UIContext'; import { ASSISTANT_BRAND_NAME, getRestNonce, PLUGIN_REST_ENDPOINTS } from '../constants'; type SetupState = { assistantEnabled: boolean; allowLogReads: boolean; allowWriteOperations: boolean; requireReadApproval: boolean; }; const ToggleRow: React.FC<{ label: string; description: string; checked: boolean; disabled?: boolean; onChange: (checked: boolean) => void; }> = ({ label, description, checked, disabled = false, onChange }) => { return ( ); }; const InitialSetupGate: React.FC = () => { const setIsLoginModalOpen = useSetIsLoginModalOpen(); const [isSaving, setIsSaving] = useState(false); const [error, setError] = useState(null); const [setup, setSetup] = useState({ // Always start setup with strict defaults. Fresh installs should opt in explicitly. assistantEnabled: false, allowLogReads: false, allowWriteOperations: false, requireReadApproval: false, }); const updateSetup = (patch: Partial) => { setSetup((prev) => ({ ...prev, ...patch })); }; const handleAssistantToggle = (enabled: boolean) => { if (!enabled) { updateSetup({ assistantEnabled: false, allowLogReads: false, allowWriteOperations: false, requireReadApproval: false, }); return; } updateSetup({ assistantEnabled: true }); }; const handleSave = async () => { setError(null); setIsSaving(true); try { const response = await fetch(PLUGIN_REST_ENDPOINTS.settings, { method: 'POST', credentials: 'include', headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': getRestNonce() }, body: JSON.stringify({ assistantEnabled: setup.assistantEnabled, allowLogReads: setup.allowLogReads, allowWriteOperations: setup.allowWriteOperations, requireReadApproval: setup.requireReadApproval, setupCompleted: true, }), }); if (!response.ok) { let message = 'Failed to save setup.'; let nextSettings: Partial | null = null; try { const payload = await response.json(); if (payload?.message && typeof payload.message === 'string') { message = payload.message; } if (payload?.settings && typeof payload.settings === 'object') { nextSettings = { assistantEnabled: Boolean(payload.settings.assistantEnabled), allowLogReads: Boolean(payload.settings.allowLogReads), allowWriteOperations: Boolean(payload.settings.allowWriteOperations), requireReadApproval: Boolean(payload.settings.requireReadApproval), }; } if (nextSettings) { setSetup((prev) => ({ ...prev, ...nextSettings })); } } catch { // Ignore parse errors and use fallback. } throw new Error(message); } window.location.reload(); } catch (e) { const message = e instanceof Error ? e.message : 'Failed to save setup.'; setError(message); setIsSaving(false); } }; return (

{`Welcome to ${ASSISTANT_BRAND_NAME}`}

Choose your default safety settings before getting started. You can change these anytime in Plugin Settings.

updateSetup({ allowLogReads: checked })} /> updateSetup({ allowWriteOperations: checked })} /> updateSetup({ requireReadApproval: checked })} />

With great power comes great responsibility.

{`${ASSISTANT_BRAND_NAME} is meant to be a helpful tool, but it can and does make mistakes. AI generated`} {' '} outputs should not be taken as fact, and AI generated changes should be carefully reviewed before they are published to a live site. Please use this tool with caution.

{error && (
{error}
)}
); }; export default InitialSetupGate;