diff --git a/src/AppRouter.tsx b/src/AppRouter.tsx index 3226b048..7b5d9da7 100644 --- a/src/AppRouter.tsx +++ b/src/AppRouter.tsx @@ -2,6 +2,7 @@ import { BrowserRouter, Route, Routes } from "react-router-dom"; import { ScrollToTop } from "./components/ScrollToTop"; import Index from "./pages/Index"; +import Clone from "./pages/Clone"; import { NIP19Page } from "./pages/NIP19Page"; import NotFound from "./pages/NotFound"; import { ProjectView } from "./pages/ProjectView"; @@ -12,6 +13,7 @@ export function AppRouter() { } /> + } /> } /> {/* NIP-19 route for npub1, note1, naddr1, nevent1, nprofile1 */} } /> diff --git a/src/components/AppLayout.tsx b/src/components/AppLayout.tsx new file mode 100644 index 00000000..9868a6ea --- /dev/null +++ b/src/components/AppLayout.tsx @@ -0,0 +1,156 @@ +import { useState, useEffect, useCallback } from 'react'; +import { useNavigate } from 'react-router-dom'; +import { type Project } from '@/lib/ProjectsManager'; +import { useProjectsManager } from '@/hooks/useProjectsManager'; +import { ProjectSidebar } from '@/components/ProjectSidebar'; +import { Button } from '@/components/ui/button'; +import { Menu, Columns2 } from 'lucide-react'; +import { useIsMobile } from '@/hooks/useIsMobile'; +import { cn } from '@/lib/utils'; + +interface AppLayoutProps { + children: React.ReactNode; + selectedProject?: Project | null; + title?: string; + showSidebar?: boolean; + headerContent?: React.ReactNode; +} + +export function AppLayout({ + children, + selectedProject = null, + title = "Shakespeare", + showSidebar = true, + headerContent +}: AppLayoutProps) { + const [projects, setProjects] = useState([]); + const projectsManager = useProjectsManager(); + const navigate = useNavigate(); + const isMobile = useIsMobile(); + + const [isSidebarVisible, setIsSidebarVisible] = useState(() => { + // Check if we're on mobile immediately to set correct initial state + if (typeof window !== 'undefined') { + return window.innerWidth >= 768; // Desktop starts open, mobile starts closed + } + return false; // Default to closed for SSR + }); + + const loadProjects = useCallback(async () => { + try { + await projectsManager.init(); + const projectList = await projectsManager.getProjects(); + setProjects(projectList); + } catch (error) { + console.error('Failed to load projects:', error); + } + }, [projectsManager]); + + useEffect(() => { + loadProjects(); + }, [loadProjects]); + + const handleProjectSelect = (project: Project | null) => { + if (project) { + setIsSidebarVisible(false); // Collapse sidebar on mobile navigation + navigate(`/project/${project.id}`); + } else { + navigate('/'); + } + }; + + if (isMobile) { + return ( +
+ {/* Mobile Header */} +
+
+ {showSidebar && projects.length > 0 && ( + + )} +

+ {title} +

+
+ {headerContent} +
+ + {/* Mobile Sidebar Overlay - only show if user has local projects and showSidebar is true */} + {showSidebar && projects.length > 0 && isSidebarVisible && ( +
+ {/* Backdrop - only covers the area not occupied by the sidebar */} +
setIsSidebarVisible(false)} + /> + {/* Sidebar - positioned above the backdrop */} +
+ { + setIsSidebarVisible(false); // Always collapse sidebar on mobile navigation + handleProjectSelect(project); + }} + /> +
+
+ )} + +
+ {children} +
+
+ ); + } + + return ( +
+ {/* Fixed Sidebar - only show if user has created local projects and showSidebar is true */} + {showSidebar && projects.length > 0 && ( +
+ +
+ )} + + {/* Main Content Area */} +
+
+ {/* Header - only show toggle button if user has local projects and showSidebar is true */} + {!isSidebarVisible && showSidebar && projects.length > 0 && ( +
+ +

+ {title} +

+
+ {headerContent} +
+
+ )} + +
+ {children} +
+
+
+
+ ); +} \ No newline at end of file diff --git a/src/components/ProjectSidebar.tsx b/src/components/ProjectSidebar.tsx index e9cbff58..501de731 100644 --- a/src/components/ProjectSidebar.tsx +++ b/src/components/ProjectSidebar.tsx @@ -1,6 +1,6 @@ import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; -import { Plus, Loader2, Info, MoreVertical, Trash2, Eye, Star, Bug, Folder, Download } from 'lucide-react'; +import { Plus, Loader2, Info, MoreVertical, Trash2, Eye, Star, Bug, Folder, Download, GitBranch } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Skeleton } from '@/components/ui/skeleton'; import { Label } from '@/components/ui/label'; @@ -309,6 +309,13 @@ export function ProjectSidebar({ + navigate('/clone')} + className="flex items-center gap-2 w-full" + > + + Import Repository + { + return this.cloneProject(name, GIT_TEMPLATE_URL); + } + + async cloneProject(name: string, repoUrl: string): Promise { const id = await this.generateUniqueProjectId(name); const projectPath = `${this.dir}/${id}`; await this.fs.mkdir(projectPath); - // Clone the template first - await this.cloneTemplate(projectPath); + // Clone the repository + await git.clone({ + fs: this.fs, + http, + dir: projectPath, + url: repoUrl, + singleBranch: true, + depth: 1, + }); // Get filesystem stats for timestamps const stats = await this.fs.stat(projectPath); @@ -57,16 +68,7 @@ export class ProjectsManager { }; } - async cloneTemplate(projectPath: string) { - await git.clone({ - fs: this.fs, - http, - dir: projectPath, - url: GIT_TEMPLATE_URL, - singleBranch: true, - depth: 1, - }); - } + async getProjects(): Promise { try { @@ -187,27 +189,20 @@ export class ProjectsManager { } private async deleteDirectory(dirPath: string): Promise { - try { - const files = await this.fs.readdir(dirPath); + const files = await this.fs.readdir(dirPath); - for (const file of files) { - const fullPath = `${dirPath}/${file}`; - const stat = await this.fs.stat(fullPath); + for (const file of files) { + const fullPath = `${dirPath}/${file}`; + const stat = await this.fs.lstat(fullPath); - if (stat.isDirectory()) { - await this.deleteDirectory(fullPath); - } else { - await this.fs.unlink(fullPath); - } - } - - await this.fs.rmdir(dirPath); - } catch (error) { - const fsError = error as NodeJS.ErrnoException; - if (fsError.code !== 'ENOENT') { - throw error; + if (stat.isDirectory()) { + await this.deleteDirectory(fullPath); + } else { + await this.fs.unlink(fullPath); } } + + await this.fs.rmdir(dirPath); } private async generateUniqueProjectId(name: string): Promise { diff --git a/src/pages/Clone.tsx b/src/pages/Clone.tsx new file mode 100644 index 00000000..51184b41 --- /dev/null +++ b/src/pages/Clone.tsx @@ -0,0 +1,210 @@ +import { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import { AppLayout } from '@/components/AppLayout'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; +import { Label } from '@/components/ui/label'; +import { GitBranch, Loader2, AlertCircle } from 'lucide-react'; +import { useProjectsManager } from '@/hooks/useProjectsManager'; +import { useToast } from '@/hooks/useToast'; + +import { useSeoMeta } from '@unhead/react'; + +export default function Clone() { + const [repoUrl, setRepoUrl] = useState(''); + const [isCloning, setIsCloning] = useState(false); + const [error, setError] = useState(null); + const navigate = useNavigate(); + const projectsManager = useProjectsManager(); + const { toast } = useToast(); + + useSeoMeta({ + title: 'Import Repository - Shakespeare', + description: 'Import a Git repository into your Shakespeare workspace', + }); + + const extractRepoName = (url: string): string => { + try { + // Handle different URL formats + const cleanUrl = url.trim(); + + // Extract from GitHub URLs, GitLab URLs, etc. + const match = cleanUrl.match(/\/([^/]+?)(?:\.git)?(?:\/)?$/); + if (match && match[1]) { + return match[1]; + } + + // Fallback: use the last part of the URL + const parts = cleanUrl.split('/').filter(Boolean); + const lastPart = parts[parts.length - 1]; + return lastPart.replace('.git', '') || 'imported-repo'; + } catch { + return 'imported-repo'; + } + }; + + const validateGitUrl = (url: string): boolean => { + if (!url.trim()) return false; + + // Basic URL validation + try { + new URL(url); + } catch { + return false; + } + + // Check if it looks like a git repository URL + const gitUrlPattern = /^https?:\/\/.*\.git$|^https?:\/\/github\.com\/.*\/.*$|^https?:\/\/gitlab\.com\/.*\/.*$/i; + return gitUrlPattern.test(url) || url.includes('github.com') || url.includes('gitlab.com'); + }; + + const handleClone = async () => { + if (!repoUrl.trim()) { + setError('Please enter a repository URL'); + return; + } + + if (!validateGitUrl(repoUrl)) { + setError('Please enter a valid Git repository URL (e.g., https://github.com/user/repo.git)'); + return; + } + + setIsCloning(true); + setError(null); + + try { + // Extract repository name from URL + const repoName = extractRepoName(repoUrl); + + // Clone the repository using ProjectsManager + await projectsManager.init(); + const project = await projectsManager.cloneProject(repoName, repoUrl.trim()); + + toast({ + title: "Repository imported successfully", + description: `"${repoName}" has been cloned and is ready for development.`, + }); + + // Navigate to the new project + navigate(`/project/${project.id}`); + } catch (error) { + console.error('Failed to clone repository:', error); + + let errorMessage = 'Failed to clone repository'; + if (error instanceof Error) { + if (error.message.includes('404') || error.message.includes('not found')) { + errorMessage = 'Repository not found. Please check the URL and try again.'; + } else if (error.message.includes('403') || error.message.includes('forbidden')) { + errorMessage = 'Access denied. The repository may be private or require authentication.'; + } else if (error.message.includes('network') || error.message.includes('fetch')) { + errorMessage = 'Network error. Please check your connection and try again.'; + } else { + errorMessage = error.message; + } + } + + setError(errorMessage); + + toast({ + title: "Failed to import repository", + description: errorMessage, + variant: "destructive", + }); + } finally { + setIsCloning(false); + } + }; + + const handleKeyDown = (e: React.KeyboardEvent) => { + if (e.key === 'Enter' && !isCloning) { + handleClone(); + } + }; + + return ( + +
+
+
+ +
+

+ Import Repository +

+

+ Clone a Git repository into your Shakespeare workspace +

+
+ + + + Repository URL + + Enter the HTTPS URL of the Git repository you want to import + + + +
+ + { + setRepoUrl(e.target.value); + setError(null); // Clear error when user types + }} + onKeyDown={handleKeyDown} + disabled={isCloning} + className="text-base" + /> +

+ Supports GitHub, GitLab, and other Git hosting services +

+
+ + {error && ( +
+ +

{error}

+
+ )} + + + +
+

Supported Repository Types

+
    +
  • • Public GitHub repositories
  • +
  • • Public GitLab repositories
  • +
  • • Any public Git repository accessible via HTTPS
  • +
+

+ Note: Private repositories requiring authentication are not currently supported. +

+
+
+
+
+
+ ); +} \ No newline at end of file diff --git a/src/pages/Index.tsx b/src/pages/Index.tsx index 785d2757..a5a2e706 100644 --- a/src/pages/Index.tsx +++ b/src/pages/Index.tsx @@ -1,29 +1,24 @@ -import { useState, useEffect, useCallback } from 'react'; +import { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; -import { type Project } from '@/lib/ProjectsManager'; import { useProjectsManager } from '@/hooks/useProjectsManager'; import { useOnboarding } from '@/hooks/useOnboarding'; import { useLocalStorage } from '@/hooks/useLocalStorage'; import { useCurrentUser } from '@/hooks/useCurrentUser'; -import { ProjectSidebar } from '@/components/ProjectSidebar'; +import { AppLayout } from '@/components/AppLayout'; import { OnboardingDialog } from '@/components/onboarding'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; -import { Plus, Menu, Columns2, Sparkles } from 'lucide-react'; -import { useIsMobile } from '@/hooks/useIsMobile'; +import { Plus, Sparkles } from 'lucide-react'; import { useSeoMeta } from '@unhead/react'; -import { cn } from '@/lib/utils'; export default function Index() { const [prompt, setPrompt] = useState(''); - const [projects, setProjects] = useState([]); const [isCreating, setIsCreating] = useState(false); const [storedPrompt, setStoredPrompt] = useLocalStorage('shakespeare-draft-prompt', ''); const navigate = useNavigate(); const projectsManager = useProjectsManager(); - const isMobile = useIsMobile(); const { user: _user } = useCurrentUser(); const { hasCompletedOnboarding, @@ -35,33 +30,11 @@ export default function Index() { handleCompleteOnboarding, } = useOnboarding(); - const [isSidebarVisible, setIsSidebarVisible] = useState(() => { - // Check if we're on mobile immediately to set correct initial state - if (typeof window !== 'undefined') { - return window.innerWidth >= 768; // Desktop starts open, mobile starts closed - } - return false; // Default to closed for SSR - }); - useSeoMeta({ title: 'Shakespeare - AI-Powered Nostr Development', description: 'Build custom Nostr websites with AI assistance using Shakespeare, an AI-powered development environment.', }); - const loadProjects = useCallback(async () => { - try { - await projectsManager.init(); - const projectList = await projectsManager.getProjects(); - setProjects(projectList); - } catch (error) { - console.error('Failed to load projects:', error); - } - }, [projectsManager]); - - useEffect(() => { - loadProjects(); - }, [loadProjects]); - // Restore prompt from local storage on mount useEffect(() => { if (storedPrompt) { @@ -93,155 +66,99 @@ export default function Index() { } }; - - - const handleProjectSelect = (project: Project | null) => { - if (project) { - setIsSidebarVisible(false); // Collapse sidebar on mobile navigation - navigate(`/project/${project.id}`); - } - }; - - if (isMobile) { - return ( -
- {/* Mobile Header */} -
- {projects.length > 0 ? ( - - ) : ( -
/* Spacer when no sidebar */ - )} -

- Shakespeare -

-
{/* Spacer */} -
- - {/* Mobile Sidebar Overlay - only show if user has local projects */} - {projects.length > 0 && isSidebarVisible && ( -
- {/* Backdrop - only covers the area not occupied by the sidebar */} -
setIsSidebarVisible(false)} - /> - {/* Sidebar - positioned above the backdrop */} -
- { - setIsSidebarVisible(false); // Always collapse sidebar on mobile navigation - handleProjectSelect(project); - }} - /> -
-
- )} - -
-
-
-
🎭
-

- Shakespeare -

-

- Build custom Nostr websites with AI assistance -

-

- - Vibed with MKStack - -

- {hasCompletedOnboarding && ( - - )} -
- -
- - -
-
- -
-
- Create a New Project - - Describe what kind of Nostr website you want to build - -
-
-
- -