import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { IpcClient } from "@/ipc/ipc_client"; import { useSettings } from "@/hooks/useSettings"; // Assuming useSettings provides a refresh function import { RuntimeMode } from "@/lib/schemas"; import { ExternalLink } from "lucide-react"; interface SetupRuntimeFlowProps { hideIntroText?: boolean; } export function SetupRuntimeFlow({ hideIntroText }: SetupRuntimeFlowProps) { const [isLoading, setIsLoading] = useState( null ); const [showNodeInstallPrompt, setShowNodeInstallPrompt] = useState(false); const [nodeVersion, setNodeVersion] = useState(null); const [npmVersion, setNpmVersion] = useState(null); const [downloadClicked, setDownloadClicked] = useState(false); const { updateSettings } = useSettings(); // Pre-check Node.js status on component mount (optional but good UX) useEffect(() => { const checkNode = async () => { try { const status = await IpcClient.getInstance().getNodejsStatus(); setNodeVersion(status.nodeVersion); setNpmVersion(status.npmVersion); } catch (error) { console.error("Failed to check Node.js status:", error); // Assume not installed if check fails setNodeVersion(null); setNpmVersion(null); } }; checkNode(); }, []); const handleSelect = async (mode: RuntimeMode) => { if (isLoading) return; // Prevent double clicks setIsLoading(mode); try { await updateSettings({ runtimeMode: mode }); // Component likely unmounts on success } catch (error) { console.error("Failed to set runtime mode:", error); alert( `Error setting runtime mode: ${ error instanceof Error ? error.message : String(error) }` ); setIsLoading(null); // Reset loading state on error } }; const handleLocalNodeClick = async () => { if (isLoading) return; setIsLoading("check"); try { if (nodeVersion && npmVersion) { // Node and npm found, proceed directly handleSelect("local-node"); } else { // Node or npm not found, show prompt setShowNodeInstallPrompt(true); setIsLoading(null); } } catch (error) { console.error("Failed to check Node.js status on click:", error); // Show prompt if check fails setShowNodeInstallPrompt(true); setIsLoading(null); } }; return (
{!hideIntroText && ( <>

Welcome to Dyad

Before you start building, choose how your apps will run.
Don’t worry — you can change this later anytime.

)}
) : ( )}
)}

Warning: this will run AI-generated code directly on your computer, which could put your computer at risk.

); }