import { useNavigate, useSearch } from "@tanstack/react-router"; import { useAtom, useSetAtom } from "jotai"; import { chatInputValueAtom } from "../atoms/chatAtoms"; import { selectedAppIdAtom } from "@/atoms/appAtoms"; import { IpcClient } from "@/ipc/ipc_client"; import { generateCuteAppName } from "@/lib/utils"; import { useLoadApps } from "@/hooks/useLoadApps"; import { useSettings } from "@/hooks/useSettings"; import { SetupBanner } from "@/components/SetupBanner"; import { ChatInput } from "@/components/chat/ChatInput"; import { isPreviewOpenAtom } from "@/atoms/viewAtoms"; import { useState, useEffect } from "react"; import { useStreamChat } from "@/hooks/useStreamChat"; export default function HomePage() { const [inputValue, setInputValue] = useAtom(chatInputValueAtom); const navigate = useNavigate(); const search = useSearch({ from: "/" }); const setSelectedAppId = useSetAtom(selectedAppIdAtom); const { refreshApps } = useLoadApps(); const { settings, isAnyProviderSetup } = useSettings(); const setIsPreviewOpen = useSetAtom(isPreviewOpenAtom); const [isLoading, setIsLoading] = useState(false); const { streamMessage } = useStreamChat(); // Get the appId from search params const appId = search.appId ? Number(search.appId) : null; // Redirect to app details page if appId is present useEffect(() => { if (appId) { navigate({ to: "/app-details", search: { appId } }); } }, [appId, navigate]); const handleSubmit = async () => { if (!inputValue.trim()) return; try { setIsLoading(true); // Create the chat and navigate const result = await IpcClient.getInstance().createApp({ name: generateCuteAppName(), }); // Stream the message streamMessage({ prompt: inputValue, chatId: result.chatId }); await new Promise((resolve) => setTimeout(resolve, 2000)); setInputValue(""); setSelectedAppId(result.app.id); setIsPreviewOpen(false); await refreshApps(); // Ensure refreshApps is awaited if it's async navigate({ to: "/chat", search: { id: result.chatId } }); } catch (error) { console.error("Failed to create chat:", error); setIsLoading(false); // Ensure loading state is reset on error } // No finally block needed for setIsLoading(false) here if navigation happens on success }; // Loading overlay for app creation if (isLoading) { return (
{/* Loading Spinner */}

Building your app

We're setting up your app with AI magic.
This might take a moment...

); } // Main Home Page Content (Rendered only if runtimeMode is set) return (

Build your dream app

{[ { icon: ( ), label: "TODO list app", }, { icon: ( ), label: "Landing Page", }, { icon: ( ), label: "Sign Up Form", }, ].map((item, index) => ( ))}
); }