mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Add DeployDialog
This commit is contained in:
6 files changed
+292
-74
No files matched your search
@@ -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({
|
||||
</DropdownMenuItem>
|
||||
|
||||
<DropdownMenuItem
|
||||
onClick={() => {
|
||||
if (onFirstInteraction) onFirstInteraction();
|
||||
onDeploy();
|
||||
}}
|
||||
onClick={() => setDeployDialogOpen(true)}
|
||||
disabled={isAnyLoading}
|
||||
className="gap-2"
|
||||
>
|
||||
{isDeployLoading ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<CloudUpload className="h-4 w-4" />
|
||||
)}
|
||||
{isDeployLoading ? 'Deploying...' : 'Deploy'}
|
||||
<CloudUpload className="h-4 w-4" />
|
||||
Deploy
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
@@ -117,6 +109,14 @@ export function ActionsMenu({
|
||||
open={gitHistoryOpen}
|
||||
onOpenChange={setGitHistoryOpen}
|
||||
/>
|
||||
|
||||
<DeployDialog
|
||||
projectId={projectId}
|
||||
projectName={projectName}
|
||||
open={deployDialogOpen}
|
||||
onOpenChange={setDeployDialogOpen}
|
||||
onFirstInteraction={onFirstInteraction}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -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 (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle className="flex items-center gap-2">
|
||||
<CloudUpload className="h-5 w-5" />
|
||||
Deploy "{projectName}"
|
||||
</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="deploy-url">Deploy URL</Label>
|
||||
<div className="relative">
|
||||
<span className="absolute left-3 top-1/2 -translate-y-1/2 text-sm text-muted-foreground">
|
||||
https://
|
||||
</span>
|
||||
<Input
|
||||
id="deploy-url"
|
||||
value={deployUrl}
|
||||
onChange={(e) => handleUrlChange(e.target.value)}
|
||||
placeholder={`${projectId}.${config.deployServer}`}
|
||||
className="pl-16"
|
||||
disabled={deployMutation.isPending}
|
||||
/>
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Your project will be deployed to this URL. You can customize the subdomain.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{!user && (
|
||||
<div className="p-3 bg-muted rounded-lg">
|
||||
<p className="text-sm text-muted-foreground">
|
||||
You must be logged into Nostr to deploy projects.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => onOpenChange(false)}
|
||||
disabled={deployMutation.isPending}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
onClick={handleDeploy}
|
||||
disabled={!user || !isValidUrl() || deployMutation.isPending}
|
||||
className="gap-2"
|
||||
>
|
||||
{deployMutation.isPending ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<CloudUpload className="h-4 w-4" />
|
||||
)}
|
||||
{deployMutation.isPending ? 'Deploying...' : 'Deploy'}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -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<ChatPaneRef, ChatPaneProps>(({
|
||||
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<ChatPaneRef, ChatPaneProps>(({
|
||||
|
||||
// Use external state if provided, otherwise default to false
|
||||
const isBuildLoading = externalIsBuildLoading || false;
|
||||
const isDeployLoading = externalIsDeployLoading || false;
|
||||
const scrollAreaRef = useRef<HTMLDivElement>(null);
|
||||
const { isConfigured, settings, addRecentlyUsedModel } = useAISettings();
|
||||
const [providerModel, setProviderModel] = useState(() => {
|
||||
@@ -142,7 +137,7 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
|
||||
// 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: [
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
+5
-3
@@ -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<DeployResult> {
|
||||
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}`;
|
||||
|
||||
|
||||
@@ -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<ChatPaneRef>(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() {
|
||||
/>
|
||||
<ActionsMenu
|
||||
projectId={project.id}
|
||||
projectName={project.name}
|
||||
onNewChat={handleNewChat}
|
||||
onDeploy={runDeploy}
|
||||
isLoading={isAILoading}
|
||||
isBuildLoading={build.isPending}
|
||||
isDeployLoading={deploy.isPending}
|
||||
onFirstInteraction={handleFirstInteraction}
|
||||
/>
|
||||
</>
|
||||
@@ -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}
|
||||
/>
|
||||
) : (
|
||||
<div className="h-full p-4 space-y-4">
|
||||
@@ -405,11 +364,10 @@ export function ProjectView() {
|
||||
/>
|
||||
<ActionsMenu
|
||||
projectId={project.id}
|
||||
projectName={project.name}
|
||||
onNewChat={handleNewChat}
|
||||
onDeploy={runDeploy}
|
||||
isLoading={isAILoading}
|
||||
isBuildLoading={build.isPending}
|
||||
isDeployLoading={deploy.isPending}
|
||||
onFirstInteraction={handleFirstInteraction}
|
||||
/>
|
||||
</>
|
||||
@@ -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}
|
||||
/>
|
||||
) : (
|
||||
<div className="h-full p-4 space-y-4">
|
||||
|
||||
Reference in new issue
Block a user