From 46a5ca2a1e655bcfb07a89bcb6355197d0278082 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 22 Sep 2025 01:24:31 -0500 Subject: [PATCH] Add useBuildProject, useDeployProject hooks --- src/hooks/useBuildProject.ts | 17 ++++++ src/hooks/useDeployProject.ts | 26 ++++++++ src/pages/ProjectView.tsx | 109 ++++++++++++++-------------------- 3 files changed, 86 insertions(+), 66 deletions(-) create mode 100644 src/hooks/useBuildProject.ts create mode 100644 src/hooks/useDeployProject.ts diff --git a/src/hooks/useBuildProject.ts b/src/hooks/useBuildProject.ts new file mode 100644 index 00000000..3fd705db --- /dev/null +++ b/src/hooks/useBuildProject.ts @@ -0,0 +1,17 @@ +import { useMutation } from "@tanstack/react-query"; +import { buildProject } from "@/lib/build"; +import { useFS } from "./useFS"; + +export function useBuildProject(projectId: string) { + const { fs } = useFS(); + + return useMutation({ + mutationFn: async () => { + return await buildProject({ + fs, + projectPath: `/projects/${projectId}`, + domParser: new DOMParser(), + }); + }, + }); +} \ No newline at end of file diff --git a/src/hooks/useDeployProject.ts b/src/hooks/useDeployProject.ts new file mode 100644 index 00000000..9793ee1d --- /dev/null +++ b/src/hooks/useDeployProject.ts @@ -0,0 +1,26 @@ +import { useMutation } from "@tanstack/react-query"; +import { deployProject } from "@/lib/deploy"; +import { useFS } from "./useFS"; +import { useAppContext } from "./useAppContext"; +import { useCurrentUser } from "./useCurrentUser"; + +export function useDeployProject(projectId: string) { + const { fs } = useFS(); + const { user } = useCurrentUser(); + const { config } = useAppContext(); + + return useMutation({ + mutationFn: async () => { + if (!user) { + throw new Error("You must be logged into Nostr to deploy a project"); + } + return await deployProject({ + fs, + projectId: projectId, + deployServer: config.deployServer, + projectPath: `/projects/${projectId}`, + signer: user.signer, + }); + }, + }); +} \ No newline at end of file diff --git a/src/pages/ProjectView.tsx b/src/pages/ProjectView.tsx index 01e9d2fd..439bf9a1 100644 --- a/src/pages/ProjectView.tsx +++ b/src/pages/ProjectView.tsx @@ -13,18 +13,14 @@ import { Skeleton } from '@/components/ui/skeleton'; import { ArrowLeft, MessageSquare, Eye, Code, Menu } from 'lucide-react'; import { useNavigate } from 'react-router-dom'; import { ActionsMenu } from '@/components/ActionsMenu'; - import { useIsMobile } from '@/hooks/useIsMobile'; -import { useFS } from '@/hooks/useFS'; import { useKeepAlive } from '@/hooks/useKeepAlive'; import { GitStatusIndicator } from '@/components/GitStatusIndicator'; import { StarButton } from '@/components/StarButton'; - -import { buildProject } from "@/lib/build"; -import { deployProject } from "@/lib/deploy"; import { useCurrentUser } from "@/hooks/useCurrentUser"; -import { useAppContext } from "@/hooks/useAppContext"; import { useToast } from "@/hooks/useToast"; +import { useBuildProject } from '@/hooks/useBuildProject'; +import { useDeployProject } from '@/hooks/useDeployProject'; export function ProjectView() { const { projectId } = useParams<{ projectId: string }>(); @@ -33,23 +29,22 @@ export function ProjectView() { const [isLoading, setIsLoading] = useState(true); const [activeTab, setActiveTab] = useState<'preview' | 'code'>('preview'); const [mobileView, setMobileView] = useState<'chat' | 'preview' | 'code'>('chat'); - const [isBuildLoading, setIsBuildLoading] = useState(false); - const [isDeployLoading, setIsDeployLoading] = useState(false); const [isAILoading, setIsAILoading] = useState(false); const [isProjectInfoOpen, setIsProjectInfoOpen] = useState(false); const projectsManager = useProjectsManager(); const chatPaneRef = useRef(null); const navigate = useNavigate(); const isMobile = useIsMobile(); - const { fs } = useFS(); const { user } = useCurrentUser(); - const { config } = useAppContext(); const { toast } = useToast(); const [isSidebarVisible, setIsSidebarVisible] = useState(!isMobile); + const build = useBuildProject(projectId!); + const deploy = useDeployProject(projectId!); + // Keep-alive functionality to prevent tab throttling during AI processing const { updateMetadata } = useKeepAlive({ - enabled: isAILoading || isBuildLoading || isDeployLoading, + enabled: isAILoading || build.isPending || deploy.isPending, title: 'Shakespeare', artist: project ? `Working on ${project.name}...` : 'Working...', artwork: [ @@ -79,28 +74,21 @@ export function ProjectView() { }, [loadProject]); const runBuild = async () => { - if (isBuildLoading || !project) return; - - setIsBuildLoading(true); + if (build.isPending || !project) return; updateMetadata('Shakespeare', `Building ${project.name}...`); - try { - const result = await buildProject({ - fs, - projectPath: `/projects/${project.id}`, - domParser: new DOMParser(), - }); - - console.log('Build completed:', result); - } catch (error) { - console.error('Build failed:', error); - } finally { - setIsBuildLoading(false); - } + await build.mutateAsync(undefined, { + onSuccess(result) { + console.log('Project built:', result); + }, + onError(error) { + console.error('Build failed:', error); + }, + }); }; const runDeploy = async () => { - if (isDeployLoading || !project) return; + if (deploy.isPending || !project) return; // Check if user is logged in if (!user || !user.signer) { @@ -112,38 +100,27 @@ export function ProjectView() { return; } - setIsDeployLoading(true); updateMetadata('Shakespeare', `Deploying ${project.name}...`); - try { - const result = await deployProject({ - projectId: project.id, - deployServer: config.deployServer, - fs, - projectPath: `/projects/${project.id}`, - signer: user.signer, - }); - - console.log('Project deployed:', result.url); - - toast({ - title: "Deployment successful!", - description: `Your project is now live at ${result.hostname}`, - }); - - // Open the deployed site in a new tab - window.open(result.url, '_blank'); - } catch (error) { - console.error('Deploy failed:', error); - - toast({ - title: "Deployment failed", - description: error instanceof Error ? error.message : "An unexpected error occurred", - variant: "destructive", - }); - } finally { - setIsDeployLoading(false); - } + await deploy.mutateAsync(undefined, { + onSuccess(result) { + console.log('Project deployed:', result.url); + toast({ + title: "Deployment successful!", + description: `Your project is now live at ${result.hostname}`, + }); + // Open the deployed site in a new tab + window.open(result.url, '_blank'); + }, + onError(error) { + console.error('Deploy failed:', error); + toast({ + title: "Deployment failed", + description: error instanceof Error ? error.message : "An unexpected error occurred", + variant: "destructive", + }); + }, + }); }; const handleNewChat = () => { @@ -228,8 +205,8 @@ export function ProjectView() { onBuild={runBuild} onDeploy={runDeploy} isLoading={isAILoading} - isBuildLoading={isBuildLoading} - isDeployLoading={isDeployLoading} + isBuildLoading={build.isPending} + isDeployLoading={deploy.isPending} onFirstInteraction={handleFirstInteraction} onProjectDeleted={handleProjectDeleted} /> @@ -283,8 +260,8 @@ export function ProjectView() { onFirstInteraction={handleFirstInteraction} onLoadingChange={handleAILoadingChange} isLoading={isAILoading} - isBuildLoading={isBuildLoading} - isDeployLoading={isDeployLoading} + isBuildLoading={build.isPending} + isDeployLoading={deploy.isPending} /> ) : (
@@ -435,8 +412,8 @@ export function ProjectView() { onBuild={runBuild} onDeploy={runDeploy} isLoading={isAILoading} - isBuildLoading={isBuildLoading} - isDeployLoading={isDeployLoading} + isBuildLoading={build.isPending} + isDeployLoading={deploy.isPending} onFirstInteraction={handleFirstInteraction} onProjectDeleted={handleProjectDeleted} /> @@ -463,8 +440,8 @@ export function ProjectView() { onFirstInteraction={handleFirstInteraction} onLoadingChange={handleAILoadingChange} isLoading={isAILoading} - isBuildLoading={isBuildLoading} - isDeployLoading={isDeployLoading} + isBuildLoading={build.isPending} + isDeployLoading={deploy.isPending} /> ) : (