diff --git a/src/components/GitSyncButton.tsx b/src/components/GitSyncButton.tsx new file mode 100644 index 00000000..be464ae3 --- /dev/null +++ b/src/components/GitSyncButton.tsx @@ -0,0 +1,645 @@ +import { useState, useEffect } from 'react'; +import { Cloud, Zap, GitBranch, Copy, Check } from 'lucide-react'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Label } from '@/components/ui/label'; +import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; +import { Alert, AlertDescription } from '@/components/ui/alert'; +import { ExternalFavicon } from '@/components/ExternalFavicon'; +import { useGitStatus } from '@/hooks/useGitStatus'; +import { useGitSettings } from '@/hooks/useGitSettings'; +import { useCurrentUser } from '@/hooks/useCurrentUser'; +import { useGit } from '@/hooks/useGit'; +import { useFSPaths } from '@/hooks/useFSPaths'; +import { useToast } from '@/hooks/useToast'; +import { Link } from 'react-router-dom'; +import { cn } from '@/lib/utils'; +import type { GitCredential } from '@/contexts/GitSettingsContext'; + +interface GitSyncButtonProps { + projectId: string; + className?: string; +} + +type SyncState = 'default' | 'select-provider' | 'configure-repo' | 'success' | 'error'; + +interface ProviderOption { + id: string; + name: string; + icon: React.ReactNode; + credential?: GitCredential; +} + +export function GitSyncButton({ projectId, className }: GitSyncButtonProps) { + const [open, setOpen] = useState(false); + const [syncState, setSyncState] = useState('default'); + const [selectedProvider, setSelectedProvider] = useState(null); + const [repositoryUrl, setRepositoryUrl] = useState(''); + const [isPushing, setIsPushing] = useState(false); + const [errorMessage, setErrorMessage] = useState(null); + const [copiedUrl, setCopiedUrl] = useState(false); + + const { data: gitStatus } = useGitStatus(projectId); + const { settings } = useGitSettings(); + const { user } = useCurrentUser(); + const { git } = useGit(); + const { projectsPath } = useFSPaths(); + const { toast } = useToast(); + + // Determine if we have a remote configured + const originRemote = gitStatus?.remotes.find(r => r.name === 'origin'); + const hasRemote = !!originRemote; + + // Match origin URL to a credential + const matchedCredential = originRemote + ? settings.credentials.find(c => originRemote.url.startsWith(c.origin)) + : null; + + // Determine if we need to show the indicator dot + const showIndicator = !hasRemote; + + // Reset state when popover opens/closes + useEffect(() => { + if (open) { + if (hasRemote) { + setSyncState('default'); + } else { + setSyncState('select-provider'); + } + setSelectedProvider(null); + setRepositoryUrl(''); + setErrorMessage(null); + } + }, [open, hasRemote]); + + // Build provider options + const providerOptions: ProviderOption[] = [ + { + id: 'nostr', + name: 'Nostr', + icon: , + }, + ...settings.credentials.map(credential => ({ + id: credential.id, + name: credential.name, + icon: ( + } + /> + ), + credential, + })), + { + id: 'other', + name: 'Other', + icon: , + }, + ]; + + const handleProviderSelect = (provider: ProviderOption) => { + setSelectedProvider(provider); + setErrorMessage(null); + + if (provider.id === 'other') { + // Show the "Other" state with link to settings + setSyncState('configure-repo'); + return; + } + + if (provider.id === 'nostr') { + if (!user) { + // Show the "Nostr" state with link to login + setSyncState('configure-repo'); + return; + } + // For Nostr, we can push directly + setSyncState('configure-repo'); + return; + } + + // For configured providers, show the repo URL input + setSyncState('configure-repo'); + }; + + const handlePush = async () => { + if (!selectedProvider) return; + + setIsPushing(true); + setErrorMessage(null); + + try { + const dir = `${projectsPath}/${projectId}`; + + if (selectedProvider.id === 'nostr') { + // TODO: Implement Nostr push + // For now, just simulate success + await new Promise(resolve => setTimeout(resolve, 1000)); + + // Configure a Nostr URI as the origin + const nostrUri = `nostr://npub1example/${projectId}`; + await git.addRemote({ + dir, + remote: 'origin', + url: nostrUri, + }); + + setSyncState('success'); + } else { + // Validate repository URL + if (!repositoryUrl.trim()) { + throw new Error('Repository URL is required'); + } + + // Add remote + await git.addRemote({ + dir, + remote: 'origin', + url: repositoryUrl, + force: true, // Override if exists + }); + + // Push to remote + await git.push({ + dir, + remote: 'origin', + ref: gitStatus?.currentBranch || 'main', + onAuth: () => { + if (selectedProvider.credential) { + return { + username: selectedProvider.credential.username, + password: selectedProvider.credential.password, + }; + } + return undefined; + }, + }); + + setSyncState('success'); + } + + toast({ + title: 'Push Successful', + description: 'Your code has been synced to the remote repository.', + }); + } catch (error) { + const message = error instanceof Error ? error.message : 'Failed to push'; + setErrorMessage(message); + setSyncState('error'); + } finally { + setIsPushing(false); + } + }; + + const handlePull = async () => { + if (!originRemote) return; + + setIsPushing(true); + setErrorMessage(null); + + try { + const dir = `${projectsPath}/${projectId}`; + + await git.pull({ + dir, + ref: gitStatus?.currentBranch || 'main', + singleBranch: true, + onAuth: () => { + if (matchedCredential) { + return { + username: matchedCredential.username, + password: matchedCredential.password, + }; + } + return undefined; + }, + }); + + toast({ + title: 'Pull Successful', + description: 'Your local code has been updated from the remote repository.', + }); + } catch (error) { + const message = error instanceof Error ? error.message : 'Failed to pull'; + setErrorMessage(message); + toast({ + title: 'Pull Failed', + description: message, + variant: 'destructive', + }); + } finally { + setIsPushing(false); + } + }; + + const handlePushConfigured = async () => { + if (!originRemote) return; + + setIsPushing(true); + setErrorMessage(null); + + try { + const dir = `${projectsPath}/${projectId}`; + + await git.push({ + dir, + remote: 'origin', + ref: gitStatus?.currentBranch || 'main', + onAuth: () => { + if (matchedCredential) { + return { + username: matchedCredential.username, + password: matchedCredential.password, + }; + } + return undefined; + }, + }); + + toast({ + title: 'Push Successful', + description: 'Your code has been synced to the remote repository.', + }); + } catch (error) { + const message = error instanceof Error ? error.message : 'Failed to push'; + setErrorMessage(message); + toast({ + title: 'Push Failed', + description: message, + variant: 'destructive', + }); + } finally { + setIsPushing(false); + } + }; + + const handleCopyUrl = async () => { + if (!originRemote) return; + + try { + await navigator.clipboard.writeText(originRemote.url); + setCopiedUrl(true); + setTimeout(() => setCopiedUrl(false), 2000); + } catch (error) { + console.error('Failed to copy URL:', error); + } + }; + + const getProviderInfo = () => { + if (!originRemote) return null; + + // Check if it's a Nostr URI + if (originRemote.url.startsWith('nostr://')) { + return { + name: 'Nostr Git', + icon: , + }; + } + + // Check if we have a matched credential + if (matchedCredential) { + return { + name: matchedCredential.name, + icon: ( + } + /> + ), + }; + } + + // Fallback to generic Git + return { + name: 'Git', + icon: , + }; + }; + + const providerInfo = getProviderInfo(); + + const renderContent = () => { + // Configured state - show origin URL and pull/push buttons + if (hasRemote && syncState === 'default') { + return ( +
+
+ {providerInfo?.icon} +

{providerInfo?.name}

+
+ +
+ +
+ + +
+
+ + {errorMessage && ( + + {errorMessage} + + )} + +
+ + +
+
+ ); + } + + // Success state + if (syncState === 'success') { + return ( +
+ + + Successfully synced to remote repository! + + + + +
+ ); + } + + // Error state + if (syncState === 'error') { + return ( +
+ + {errorMessage} + + +
+ + +
+
+ ); + } + + // Configure repo state + if (syncState === 'configure-repo' && selectedProvider) { + // "Other" provider - show link to git settings + if (selectedProvider.id === 'other') { + return ( +
+
+ {selectedProvider.icon} +

{selectedProvider.name}

+
+ +

+ Configure additional git providers in{' '} + setOpen(false)} + > + Git Settings + + . +

+ + +
+ ); + } + + // Nostr provider - check if user is logged in + if (selectedProvider.id === 'nostr') { + if (!user) { + return ( +
+
+ {selectedProvider.icon} +

{selectedProvider.name}

+
+ +

+ You need to be logged in with Nostr to use this feature. Please visit{' '} + setOpen(false)} + > + Nostr Settings + {' '} + to log in. +

+ + +
+ ); + } + + return ( +
+
+ {selectedProvider.icon} +

{selectedProvider.name}

+
+ +

+ Push your code to Nostr with one click. Your repository will be stored on Nostr relays. +

+ + {errorMessage && ( + + {errorMessage} + + )} + + +
+ ); + } + + // Regular git provider - show repository URL input + return ( +
+
+ {selectedProvider.icon} +

{selectedProvider.name}

+
+ +
+ + setRepositoryUrl(e.target.value)} + /> +

+ First create the repository on {selectedProvider.name}, then paste the URL here. +

+
+ + {errorMessage && ( + + {errorMessage} + + )} + + +
+ ); + } + + // Default state - select provider + return ( +
+
+

Sync your code to git

+

+ Choose a provider to sync your project +

+
+ +
+ +
+ {providerOptions.map((provider) => ( + + ))} +
+
+
+ ); + }; + + return ( + + + + + + {renderContent()} + + + ); +} diff --git a/src/pages/ProjectView.tsx b/src/pages/ProjectView.tsx index 6501ce96..e704f3af 100644 --- a/src/pages/ProjectView.tsx +++ b/src/pages/ProjectView.tsx @@ -19,6 +19,7 @@ import { DeployDialog } from '@/components/DeployDialog'; import { DuplicateProjectDialog } from '@/components/DuplicateProjectDialog'; import { GitStatusIndicator } from '@/components/GitStatusIndicator'; import { StarButton } from '@/components/StarButton'; +import { GitSyncButton } from '@/components/GitSyncButton'; import { useBuildProject } from '@/hooks/useBuildProject'; import { useIsProjectPreviewable } from '@/hooks/useIsProjectPreviewable'; import { useConsoleError } from '@/hooks/useConsoleError'; @@ -177,13 +178,22 @@ export function ProjectView() {
{project ? ( - + <> + + + ) : ( - + <> + + + )}
@@ -392,16 +402,25 @@ export function ProjectView() { - {/* Right side - Star button only */} + {/* Right side - Sync and Star buttons */}
{project ? ( - + <> + + + ) : ( - + <> + + + )}