diff --git a/src/components/ActionsMenu.tsx b/src/components/ActionsMenu.tsx index a580c18c..ff0bde1e 100644 --- a/src/components/ActionsMenu.tsx +++ b/src/components/ActionsMenu.tsx @@ -11,37 +11,36 @@ import { MoreHorizontal, MessageSquarePlus, CloudUpload, - Loader2, History, } from 'lucide-react'; import { GitHistoryDialog } from '@/components/ai/GitHistoryDialog'; import { GitDialog } from '@/components/GitDialog'; +import { DeployDialog } from '@/components/DeployDialog'; interface ActionsMenuProps { projectId: string; + projectName: string; onNewChat: () => void; - onDeploy: () => void; isLoading?: boolean; isBuildLoading?: boolean; - isDeployLoading?: boolean; disabled?: boolean; onFirstInteraction?: () => void; } export function ActionsMenu({ projectId, + projectName, onNewChat, - onDeploy, isLoading = false, isBuildLoading = false, - isDeployLoading = false, disabled = false, onFirstInteraction, }: ActionsMenuProps) { const [gitHistoryOpen, setGitHistoryOpen] = useState(false); const [gitDialogOpen, setGitDialogOpen] = useState(false); + const [deployDialogOpen, setDeployDialogOpen] = useState(false); - const isAnyLoading = isLoading || isBuildLoading || isDeployLoading; + const isAnyLoading = isLoading || isBuildLoading; @@ -88,19 +87,12 @@ export function ActionsMenu({ { - if (onFirstInteraction) onFirstInteraction(); - onDeploy(); - }} + onClick={() => setDeployDialogOpen(true)} disabled={isAnyLoading} className="gap-2" > - {isDeployLoading ? ( - - ) : ( - - )} - {isDeployLoading ? 'Deploying...' : 'Deploy'} + + Deploy @@ -117,6 +109,14 @@ export function ActionsMenu({ open={gitHistoryOpen} onOpenChange={setGitHistoryOpen} /> + + ); } \ No newline at end of file diff --git a/src/components/DeployDialog.tsx b/src/components/DeployDialog.tsx new file mode 100644 index 00000000..87d1f94f --- /dev/null +++ b/src/components/DeployDialog.tsx @@ -0,0 +1,182 @@ +import { useState, useEffect } from 'react'; +import { + Dialog, + DialogContent, + DialogHeader, + DialogTitle, + DialogFooter, +} from '@/components/ui/dialog'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Label } from '@/components/ui/label'; +import { useToast } from '@/hooks/useToast'; +import { useAppContext } from '@/hooks/useAppContext'; +import { useCurrentUser } from '@/hooks/useCurrentUser'; +import { useMutation } from '@tanstack/react-query'; +import { deployProject } from '@/lib/deploy'; +import { useFS } from '@/hooks/useFS'; +import { + CloudUpload, + Loader2, +} from 'lucide-react'; + +interface DeployDialogProps { + projectId: string; + projectName: string; + open: boolean; + onOpenChange: (open: boolean) => void; + onFirstInteraction?: () => void; +} + +export function DeployDialog({ + projectId, + projectName, + open, + onOpenChange, + onFirstInteraction +}: DeployDialogProps) { + const { config } = useAppContext(); + const { user } = useCurrentUser(); + const { fs } = useFS(); + const { toast } = useToast(); + + // State for the deploy URL input + const [deployUrl, setDeployUrl] = useState(''); + + // Initialize deploy URL when dialog opens + useEffect(() => { + if (open) { + setDeployUrl(`${projectId}.${config.deployServer}`); + } + }, [open, projectId, config.deployServer]); + + const deployMutation = useMutation({ + mutationFn: async () => { + if (!user) { + throw new Error("You must be logged into Nostr to deploy a project"); + } + + // Parse the deploy URL to extract hostname and server + const hostname = deployUrl.trim(); + const parts = hostname.split('.'); + + if (parts.length < 2) { + throw new Error("Invalid deploy URL format. Please use format: subdomain.domain.com"); + } + + // Extract the deploy server (everything after the first dot) + const deployServer = parts.slice(1).join('.'); + + return await deployProject({ + fs, + projectId, // Keep the original projectId for file path + deployServer, + projectPath: `/projects/${projectId}`, + signer: user.signer, + customHostname: hostname, // Pass the custom hostname + }); + }, + onSuccess: (result) => { + 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'); + + // Close the dialog + onOpenChange(false); + }, + onError: (error) => { + toast({ + title: "Deployment failed", + description: error instanceof Error ? error.message : "An unexpected error occurred", + variant: "destructive", + }); + }, + }); + + const handleDeploy = () => { + if (onFirstInteraction) { + onFirstInteraction(); + } + deployMutation.mutate(); + }; + + const handleUrlChange = (value: string) => { + // Remove protocol if user enters it + const cleanValue = value.replace(/^https?:\/\//, ''); + setDeployUrl(cleanValue); + }; + + const isValidUrl = () => { + const trimmed = deployUrl.trim(); + return trimmed.length > 0 && trimmed.includes('.') && !trimmed.startsWith('.') && !trimmed.endsWith('.'); + }; + + return ( + + + + + + Deploy "{projectName}" + + + +
+
+ +
+ + https:// + + handleUrlChange(e.target.value)} + placeholder={`${projectId}.${config.deployServer}`} + className="pl-16" + disabled={deployMutation.isPending} + /> +
+

+ Your project will be deployed to this URL. You can customize the subdomain. +

+
+ + {!user && ( +
+

+ You must be logged into Nostr to deploy projects. +

+
+ )} +
+ + + + + +
+
+ ); +} \ No newline at end of file diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index e6dc2218..88818270 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -45,12 +45,10 @@ interface ChatPaneProps { projectId: string; onNewChat?: () => void; onBuild?: () => void; - onDeploy?: () => void; onFirstInteraction?: () => void; onLoadingChange?: (isLoading: boolean) => void; isLoading?: boolean; isBuildLoading?: boolean; - isDeployLoading?: boolean; } export interface ChatPaneRef { @@ -61,12 +59,10 @@ export const ChatPane = forwardRef(({ projectId, onNewChat: _onNewChat, onBuild: _onBuild, - onDeploy: _onDeploy, onFirstInteraction, onLoadingChange, isLoading: externalIsLoading, isBuildLoading: externalIsBuildLoading, - isDeployLoading: externalIsDeployLoading }, ref) => { const { t } = useTranslation(); const [input, setInput] = useState(''); @@ -78,7 +74,6 @@ export const ChatPane = forwardRef(({ // Use external state if provided, otherwise default to false const isBuildLoading = externalIsBuildLoading || false; - const isDeployLoading = externalIsDeployLoading || false; const scrollAreaRef = useRef(null); const { isConfigured, settings, addRecentlyUsedModel } = useAISettings(); const [providerModel, setProviderModel] = useState(() => { @@ -142,7 +137,7 @@ export const ChatPane = forwardRef(({ // Keep-alive functionality to prevent tab throttling during AI processing const { updateMetadata } = useKeepAlive({ - enabled: externalIsLoading || isBuildLoading || isDeployLoading, + enabled: externalIsLoading || isBuildLoading, title: 'Shakespeare', artist: `Working on ${projectId}...`, artwork: [ diff --git a/src/lib/deploy.test.ts b/src/lib/deploy.test.ts new file mode 100644 index 00000000..d5eab2d4 --- /dev/null +++ b/src/lib/deploy.test.ts @@ -0,0 +1,83 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { deployProject } from './deploy'; +import type { JSRuntimeFS } from './JSRuntime'; +import type { NostrSigner } from '@nostrify/nostrify'; + +// Mock JSZip +vi.mock('jszip', () => { + return { + default: vi.fn().mockImplementation(() => ({ + folder: vi.fn().mockReturnValue({ + file: vi.fn(), + }), + file: vi.fn(), + generateAsync: vi.fn().mockResolvedValue(new Blob()), + })), + }; +}); + +// Mock NIP98Client +vi.mock('@nostrify/nostrify', () => ({ + NIP98Client: vi.fn().mockImplementation(() => ({ + fetch: vi.fn().mockResolvedValue({ + ok: true, + status: 200, + statusText: 'OK', + }), + })), +})); + +describe('deployProject', () => { + let mockFS: JSRuntimeFS; + let mockSigner: NostrSigner; + + beforeEach(() => { + mockFS = { + readFile: vi.fn().mockResolvedValue('mock file content'), + readdir: vi.fn().mockResolvedValue([]), + } as JSRuntimeFS; + + mockSigner = {} as NostrSigner; + }); + + it('should construct hostname from projectId and deployServer by default', async () => { + const result = await deployProject({ + projectId: 'test-project', + deployServer: 'shakespeare.wtf', + fs: mockFS, + projectPath: '/projects/test-project', + signer: mockSigner, + }); + + expect(result.hostname).toBe('test-project.shakespeare.wtf'); + expect(result.url).toBe('https://test-project.shakespeare.wtf'); + }); + + it('should use customHostname when provided', async () => { + const result = await deployProject({ + projectId: 'test-project', + deployServer: 'shakespeare.wtf', + fs: mockFS, + projectPath: '/projects/test-project', + signer: mockSigner, + customHostname: 'my-custom-domain.example.com', + }); + + expect(result.hostname).toBe('my-custom-domain.example.com'); + expect(result.url).toBe('https://my-custom-domain.example.com'); + }); + + it('should handle custom subdomain with same deploy server', async () => { + const result = await deployProject({ + projectId: 'original-id', + deployServer: 'shakespeare.wtf', + fs: mockFS, + projectPath: '/projects/original-id', + signer: mockSigner, + customHostname: 'custom-name.shakespeare.wtf', + }); + + expect(result.hostname).toBe('custom-name.shakespeare.wtf'); + expect(result.url).toBe('https://custom-name.shakespeare.wtf'); + }); +}); \ No newline at end of file diff --git a/src/lib/deploy.ts b/src/lib/deploy.ts index bfa1e12a..6d56583f 100644 --- a/src/lib/deploy.ts +++ b/src/lib/deploy.ts @@ -14,6 +14,8 @@ export interface DeployOptions { projectPath: string; /** Nostr signer for NIP-98 authentication */ signer: NostrSigner; + /** Custom hostname to use instead of constructing from projectId.deployServer */ + customHostname?: string; } export interface DeployResult { @@ -28,10 +30,10 @@ export interface DeployResult { * to the deployment server using NIP-98 authentication. */ export async function deployProject(options: DeployOptions): Promise { - const { projectId, deployServer, fs, projectPath, signer } = options; + const { projectId, deployServer, fs, projectPath, signer, customHostname } = options; - // Construct hostname and URL - const hostname = `${projectId}.${deployServer}`; + // Use custom hostname if provided, otherwise construct from projectId.deployServer + const hostname = customHostname || `${projectId}.${deployServer}`; const deployUrl = `https://${deployServer}/deploy`; const siteUrl = `https://${hostname}`; diff --git a/src/pages/ProjectView.tsx b/src/pages/ProjectView.tsx index e289dbf3..32501fe4 100644 --- a/src/pages/ProjectView.tsx +++ b/src/pages/ProjectView.tsx @@ -17,10 +17,8 @@ import { useIsMobile } from '@/hooks/useIsMobile'; import { useKeepAlive } from '@/hooks/useKeepAlive'; import { GitStatusIndicator } from '@/components/GitStatusIndicator'; import { StarButton } from '@/components/StarButton'; -import { useCurrentUser } from "@/hooks/useCurrentUser"; -import { useToast } from "@/hooks/useToast"; + import { useBuildProject } from '@/hooks/useBuildProject'; -import { useDeployProject } from '@/hooks/useDeployProject'; export function ProjectView() { const { projectId } = useParams<{ projectId: string }>(); @@ -35,16 +33,14 @@ export function ProjectView() { const chatPaneRef = useRef(null); const navigate = useNavigate(); const isMobile = useIsMobile(); - const { user } = useCurrentUser(); - 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 || build.isPending || deploy.isPending, + enabled: isAILoading || build.isPending, title: 'Shakespeare', artist: project ? `Working on ${project.name}...` : 'Working...', artwork: [ @@ -87,41 +83,7 @@ export function ProjectView() { }); }; - const runDeploy = async () => { - if (deploy.isPending || !project) return; - // Check if user is logged in - if (!user || !user.signer) { - toast({ - title: "Authentication required", - description: "Please log in to deploy your project.", - variant: "destructive", - }); - return; - } - - updateMetadata('Shakespeare', `Deploying ${project.name}...`); - - 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 = () => { // Reset to chat view on mobile when starting new chat @@ -200,11 +162,10 @@ export function ProjectView() { /> @@ -253,12 +214,10 @@ export function ProjectView() { projectId={project.id} onNewChat={handleNewChat} onBuild={runBuild} - onDeploy={runDeploy} onFirstInteraction={handleFirstInteraction} onLoadingChange={handleAILoadingChange} isLoading={isAILoading} isBuildLoading={build.isPending} - isDeployLoading={deploy.isPending} /> ) : (
@@ -405,11 +364,10 @@ export function ProjectView() { /> @@ -431,12 +389,10 @@ export function ProjectView() { projectId={project.id} onNewChat={handleNewChat} onBuild={runBuild} - onDeploy={runDeploy} onFirstInteraction={handleFirstInteraction} onLoadingChange={handleAILoadingChange} isLoading={isAILoading} isBuildLoading={build.isPending} - isDeployLoading={deploy.isPending} /> ) : (