Break git-sync up into steps

This commit is contained in:
Alex Gleason committed 2025-12-22 19:41:55 -06:00
1 parent 062e2e41bc
commit ab879c9023
10 files changed
+753 -631

No files matched your search

-630
View File
@@ -1,630 +0,0 @@
import { useState, useEffect } from 'react';
import { CloudUpload, Zap, GitBranch, Copy, Check, CheckCircle2 } 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 { 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<SyncState>('default');
const [selectedProvider, setSelectedProvider] = useState<ProviderOption | null>(null);
const [repositoryUrl, setRepositoryUrl] = useState('');
const [isPushing, setIsPushing] = useState(false);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const [copiedUrl, setCopiedUrl] = useState(false);
const { data: gitStatus, isLoading: isGitStatusLoading, refetch: refetchGitStatus } = useGitStatus(projectId);
const { settings } = useGitSettings();
const { user } = useCurrentUser();
const { git } = useGit();
const { projectsPath } = useFSPaths();
// 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 = !isGitStatusLoading && !hasRemote;
// Auto-reset success state after 2 seconds
useEffect(() => {
if (syncState === 'success') {
const timer = setTimeout(() => {
setSyncState('default');
}, 2000);
return () => clearTimeout(timer);
}
}, [syncState])
// 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: <Zap className="h-4 w-4" />,
},
...settings.credentials.map(credential => ({
id: credential.id,
name: credential.name,
icon: (
<ExternalFavicon
url={credential.origin}
size={16}
fallback={<GitBranch className="h-4 w-4" />}
/>
),
credential,
})),
{
id: 'other',
name: 'Other',
icon: <GitBranch className="h-4 w-4" />,
},
];
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 handlePushInitial = 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,
});
} 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;
},
});
}
// Refresh git status before showing success
await refetchGitStatus();
// Display success state
setSyncState('success');
} 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;
},
});
setSyncState('success');
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to pull';
setErrorMessage(message);
setSyncState('error');
} finally {
setIsPushing(false);
}
};
const handlePush = 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;
},
});
setSyncState('success');
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to push';
setErrorMessage(message);
setSyncState('error');
} 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: <Zap className="h-4 w-4" />,
};
}
// Check if we have a matched credential
if (matchedCredential) {
return {
name: matchedCredential.name,
icon: (
<ExternalFavicon
url={matchedCredential.origin}
size={16}
fallback={<GitBranch className="h-4 w-4" />}
/>
),
};
}
// Fallback to generic Git
return {
name: 'Git',
icon: <GitBranch className="h-4 w-4" />,
};
};
const providerInfo = getProviderInfo();
const renderContent = () => {
// Configured state - show origin URL and pull/push buttons
if (hasRemote && syncState === 'default') {
return (
<div className="space-y-4">
<div className="flex items-center gap-2">
{providerInfo?.icon}
<h3 className="font-semibold">{providerInfo?.name}</h3>
</div>
<div className="space-y-2">
<Label className="text-xs text-muted-foreground">Repository URL</Label>
<div className="flex gap-2">
<Input
value={originRemote.url}
readOnly
className="flex-1 text-sm"
/>
<Button
size="sm"
variant="outline"
onClick={handleCopyUrl}
className="shrink-0 h-10"
>
{copiedUrl ? (
<Check className="h-4 w-4" />
) : (
<Copy className="h-4 w-4" />
)}
</Button>
</div>
</div>
{errorMessage && (
<Alert variant="destructive">
<AlertDescription className="text-sm">{errorMessage}</AlertDescription>
</Alert>
)}
<div className="flex gap-2">
<Button
onClick={handlePull}
disabled={isPushing}
className="flex-1"
variant="outline"
>
{isPushing ? (
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-current" />
) : (
'Pull'
)}
</Button>
<Button
onClick={handlePush}
disabled={isPushing}
className="flex-1"
>
{isPushing ? (
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-current" />
) : (
'Push'
)}
</Button>
</div>
</div>
);
}
// Success state
if (syncState === 'success') {
return (
<div className="flex flex-col items-center justify-center py-8 space-y-4">
<div className="relative">
<CheckCircle2 className="h-20 w-20 text-green-500 animate-in zoom-in duration-300" />
<div className="absolute inset-0 h-20 w-20 rounded-full bg-green-500/20 animate-ping" />
</div>
<div className="text-center space-y-1">
<h3 className="font-semibold text-lg">Successfully Synced!</h3>
<p className="text-sm text-muted-foreground">
Your code has been synced to the remote repository.
</p>
</div>
</div>
);
}
// Error state
if (syncState === 'error') {
return (
<div className="space-y-4">
<Alert variant="destructive">
<AlertDescription className="text-sm">{errorMessage}</AlertDescription>
</Alert>
<div className="flex gap-2">
<Button
onClick={() => setSyncState('configure-repo')}
variant="outline"
className="flex-1"
>
Try Again
</Button>
<Button
onClick={() => {
setOpen(false);
setSyncState('default');
}}
className="flex-1"
>
Cancel
</Button>
</div>
</div>
);
}
// Configure repo state
if (syncState === 'configure-repo' && selectedProvider) {
// "Other" provider - show link to git settings
if (selectedProvider.id === 'other') {
return (
<div className="space-y-4">
<div className="flex items-center gap-2">
{selectedProvider.icon}
<h3 className="font-semibold">{selectedProvider.name}</h3>
</div>
<p className="text-sm text-muted-foreground">
Configure additional git providers in{' '}
<Link
to="/settings/git"
className="underline hover:no-underline font-medium"
onClick={() => setOpen(false)}
>
Git Settings
</Link>
.
</p>
<Button
onClick={() => setSyncState('select-provider')}
variant="outline"
className="w-full"
>
Back
</Button>
</div>
);
}
// Nostr provider - check if user is logged in
if (selectedProvider.id === 'nostr') {
if (!user) {
return (
<div className="space-y-4">
<div className="flex items-center gap-2">
{selectedProvider.icon}
<h3 className="font-semibold">{selectedProvider.name}</h3>
</div>
<p className="text-sm text-muted-foreground">
You need to be logged in with Nostr to use this feature. Please visit{' '}
<Link
to="/settings/nostr"
className="underline hover:no-underline font-medium"
onClick={() => setOpen(false)}
>
Nostr Settings
</Link>{' '}
to log in.
</p>
<Button
onClick={() => setSyncState('select-provider')}
variant="outline"
className="w-full"
>
Back
</Button>
</div>
);
}
return (
<div className="space-y-4">
<div className="flex items-center gap-2">
{selectedProvider.icon}
<h3 className="font-semibold">{selectedProvider.name}</h3>
</div>
<p className="text-sm text-muted-foreground">
Push your code to Nostr with one click. Your repository will be stored on Nostr relays.
</p>
{errorMessage && (
<Alert variant="destructive">
<AlertDescription className="text-sm">{errorMessage}</AlertDescription>
</Alert>
)}
<Button
onClick={handlePushInitial}
disabled={isPushing}
className="w-full"
>
{isPushing ? (
<>
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-current mr-2" />
Pushing...
</>
) : (
'Push to Nostr'
)}
</Button>
</div>
);
}
// Regular git provider - show repository URL input
return (
<div className="space-y-4">
<div className="flex items-center gap-2">
{selectedProvider.icon}
<h3 className="font-semibold">{selectedProvider.name}</h3>
</div>
<div className="space-y-2">
<Label htmlFor="repo-url">Repository URL</Label>
<Input
id="repo-url"
placeholder="https://github.com/username/repo.git"
value={repositoryUrl}
onChange={(e) => setRepositoryUrl(e.target.value)}
/>
<p className="text-xs text-muted-foreground">
First create the repository on {selectedProvider.name}, then paste the URL here.
</p>
</div>
{errorMessage && (
<Alert variant="destructive">
<AlertDescription className="text-sm">{errorMessage}</AlertDescription>
</Alert>
)}
<Button
onClick={handlePushInitial}
disabled={isPushing || !repositoryUrl.trim()}
className="w-full"
>
{isPushing ? (
<>
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-current mr-2" />
Pushing...
</>
) : (
'Push'
)}
</Button>
</div>
);
}
// Default state - select provider
return (
<div className="space-y-4">
<div>
<h3 className="font-semibold mb-1">Sync your code to git</h3>
<p className="text-sm text-muted-foreground">
Choose a provider to sync your project
</p>
</div>
<div className="space-y-2">
<Label className="text-xs text-muted-foreground">Select Provider</Label>
<div className="flex flex-wrap gap-2">
{providerOptions.map((provider) => (
<button
key={provider.id}
type="button"
onClick={() => handleProviderSelect(provider)}
className={cn(
"inline-flex items-center gap-2 px-3 py-1.5 rounded-full text-sm font-medium transition-all",
"border-2 hover:scale-105 active:scale-95",
"bg-background text-foreground border-border hover:border-primary/50"
)}
>
{provider.icon}
<span>{provider.name}</span>
</button>
))}
</div>
</div>
</div>
);
};
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="ghost"
size="sm"
className={cn("h-8 w-8 p-0 relative", className)}
aria-label="Sync with Git"
>
<CloudUpload className="size-5 text-muted-foreground" />
{showIndicator && (
<span className="absolute top-1 right-1 h-2 w-2 rounded-full bg-primary" />
)}
</Button>
</PopoverTrigger>
<PopoverContent align="start" className="w-96">
{renderContent()}
</PopoverContent>
</Popover>
);
}
+46
View File
@@ -0,0 +1,46 @@
import { useState } from 'react';
import { CloudUpload } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import { useGitStatus } from '@/hooks/useGitStatus';
import { cn } from '@/lib/utils';
import { GitSyncSteps } from './GitSyncSteps';
interface GitSyncButtonProps {
projectId: string;
className?: string;
}
export function GitSyncButton({ projectId, className }: GitSyncButtonProps) {
const [open, setOpen] = useState(false);
const { data: gitStatus, isLoading: isGitStatusLoading } = useGitStatus(projectId);
// Determine if we have a remote configured
const originRemote = gitStatus?.remotes.find(r => r.name === 'origin');
const hasRemote = !!originRemote;
// Determine if we need to show the indicator dot
const showIndicator = !isGitStatusLoading && !hasRemote;
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="ghost"
size="sm"
className={cn("h-8 w-8 p-0 relative", className)}
aria-label="Sync with Git"
>
<CloudUpload className="size-5 text-muted-foreground" />
{showIndicator && (
<span className="absolute top-1 right-1 h-2 w-2 rounded-full bg-primary" />
)}
</Button>
</PopoverTrigger>
<PopoverContent align="start" className="w-96">
<GitSyncSteps projectId={projectId} isOpen={open} onClose={() => setOpen(false)} />
</PopoverContent>
</Popover>
);
}
+122
View File
@@ -0,0 +1,122 @@
import { useState, useEffect } from 'react';
import { useGitStatus } from '@/hooks/useGitStatus';
import { SelectProviderStep } from './steps/SelectProviderStep';
import { ConfigureRepoStep } from './steps/ConfigureRepoStep';
import { SyncStep } from './steps/SyncStep';
import { InitSuccessStep } from './steps/InitSuccessStep';
import type { StepType, ProviderOption } from './types';
interface GitSyncStepsProps {
projectId: string;
isOpen: boolean;
onClose: () => void;
}
export function GitSyncSteps({ projectId, isOpen, onClose }: GitSyncStepsProps) {
const [currentStep, setCurrentStep] = useState<StepType>('select-provider');
const [selectedProvider, setSelectedProvider] = useState<ProviderOption | null>(null);
const { data: gitStatus, isLoading: isGitStatusLoading, refetch: refetchGitStatus } = useGitStatus(projectId);
// Determine if we have a remote configured
const originRemote = gitStatus?.remotes.find(r => r.name === 'origin');
const hasRemote = !!originRemote;
// Reset state when popover opens/closes
useEffect(() => {
if (isOpen) {
if (hasRemote) {
setCurrentStep('sync');
} else {
setCurrentStep('select-provider');
}
setSelectedProvider(null);
}
}, [isOpen, hasRemote]);
// Auto-reset success state after 2 seconds
useEffect(() => {
if (currentStep === 'init-success') {
const timer = setTimeout(() => {
setCurrentStep('sync');
}, 2000);
return () => clearTimeout(timer);
}
}, [currentStep]);
const handleSelectProvider = (provider: ProviderOption) => {
setSelectedProvider(provider);
setCurrentStep('configure-repo');
};
const handleBack = () => {
setCurrentStep('select-provider');
setSelectedProvider(null);
};
const handleSuccess = () => {
setCurrentStep('init-success');
};
const handleRefetchStatus = async () => {
await refetchGitStatus();
};
// Don't render anything while loading git status
if (isGitStatusLoading) {
return (
<div className="flex items-center justify-center py-8">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary" />
</div>
);
}
// Render the appropriate step
if (currentStep === 'select-provider') {
return (
<SelectProviderStep
projectId={projectId}
onSelectProvider={handleSelectProvider}
onChangeStep={setCurrentStep}
onClose={onClose}
/>
);
}
if (currentStep === 'configure-repo' && selectedProvider) {
return (
<ConfigureRepoStep
projectId={projectId}
selectedProvider={selectedProvider}
onBack={handleBack}
onSuccess={handleSuccess}
onChangeStep={setCurrentStep}
onClose={onClose}
onRefetchStatus={handleRefetchStatus}
/>
);
}
if (currentStep === 'sync') {
return (
<SyncStep
projectId={projectId}
onChangeStep={setCurrentStep}
onClose={onClose}
onRefetchStatus={handleRefetchStatus}
/>
);
}
if (currentStep === 'init-success') {
return (
<InitSuccessStep
projectId={projectId}
onChangeStep={setCurrentStep}
onClose={onClose}
/>
);
}
return null;
}
+2
View File
@@ -0,0 +1,2 @@
export { GitSyncButton } from './GitSyncButton';
export type { ProviderOption, StepType } from './types';
@@ -0,0 +1,253 @@
import { useState } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { useCurrentUser } from '@/hooks/useCurrentUser';
import { useGit } from '@/hooks/useGit';
import { useGitStatus } from '@/hooks/useGitStatus';
import { useFSPaths } from '@/hooks/useFSPaths';
import { Link } from 'react-router-dom';
import type { ConfigureRepoStepProps } from '../types';
export function ConfigureRepoStep({
projectId,
selectedProvider,
onBack,
onSuccess,
onClose,
onRefetchStatus,
}: ConfigureRepoStepProps & { onRefetchStatus: () => void }) {
const [repositoryUrl, setRepositoryUrl] = useState('');
const [isPushing, setIsPushing] = useState(false);
const [error, setError] = useState<string | null>(null);
const { user } = useCurrentUser();
const { git } = useGit();
const { projectsPath } = useFSPaths();
const { data: gitStatus } = useGitStatus(projectId);
const handlePushInitial = async () => {
setIsPushing(true);
setError(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,
});
} 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;
},
});
}
// Refresh git status before showing success
await onRefetchStatus();
// Show success state
onSuccess();
} catch (err) {
const message = err instanceof Error ? err.message : 'Failed to push';
setError(message);
} finally {
setIsPushing(false);
}
};
// "Other" provider - show link to git settings
if (selectedProvider.id === 'other') {
return (
<div className="space-y-4">
<div className="flex items-center gap-2">
{selectedProvider.icon}
<h3 className="font-semibold">{selectedProvider.name}</h3>
</div>
<p className="text-sm text-muted-foreground">
Configure additional git providers in{' '}
<Link
to="/settings/git"
className="underline hover:no-underline font-medium"
onClick={onClose}
>
Git Settings
</Link>
.
</p>
<Button
onClick={onBack}
variant="outline"
className="w-full"
>
Back
</Button>
</div>
);
}
// Nostr provider - check if user is logged in
if (selectedProvider.id === 'nostr') {
if (!user) {
return (
<div className="space-y-4">
<div className="flex items-center gap-2">
{selectedProvider.icon}
<h3 className="font-semibold">{selectedProvider.name}</h3>
</div>
<p className="text-sm text-muted-foreground">
You need to be logged in with Nostr to use this feature. Please visit{' '}
<Link
to="/settings/nostr"
className="underline hover:no-underline font-medium"
onClick={onClose}
>
Nostr Settings
</Link>{' '}
to log in.
</p>
<Button
onClick={onBack}
variant="outline"
className="w-full"
>
Back
</Button>
</div>
);
}
return (
<div className="space-y-4">
<div className="flex items-center gap-2">
{selectedProvider.icon}
<h3 className="font-semibold">{selectedProvider.name}</h3>
</div>
<p className="text-sm text-muted-foreground">
Push your code to Nostr with one click. Your repository will be stored on Nostr relays.
</p>
{error && (
<Alert variant="destructive">
<AlertDescription className="text-sm">{error}</AlertDescription>
</Alert>
)}
<div className="flex gap-2">
<Button
onClick={onBack}
variant="outline"
className="flex-1"
>
Back
</Button>
<Button
onClick={handlePushInitial}
disabled={isPushing}
className="flex-1"
>
{isPushing ? (
<>
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-current mr-2" />
Pushing...
</>
) : (
'Push'
)}
</Button>
</div>
</div>
);
}
// Regular git provider - show repository URL input
return (
<div className="space-y-4">
<div className="flex items-center gap-2">
{selectedProvider.icon}
<h3 className="font-semibold">{selectedProvider.name}</h3>
</div>
<div className="space-y-2">
<Label htmlFor="repo-url">Repository URL</Label>
<Input
id="repo-url"
placeholder="https://github.com/username/repo.git"
value={repositoryUrl}
onChange={(e) => setRepositoryUrl(e.target.value)}
/>
<p className="text-xs text-muted-foreground">
First create the repository on {selectedProvider.name}, then paste the URL here.
</p>
</div>
{error && (
<Alert variant="destructive">
<AlertDescription className="text-sm">{error}</AlertDescription>
</Alert>
)}
<div className="flex gap-2">
<Button
onClick={onBack}
variant="outline"
className="flex-1"
>
Back
</Button>
<Button
onClick={handlePushInitial}
disabled={isPushing || !repositoryUrl.trim()}
className="flex-1"
>
{isPushing ? (
<>
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-current mr-2" />
Pushing...
</>
) : (
'Push'
)}
</Button>
</div>
</div>
);
}
@@ -0,0 +1,19 @@
import { CheckCircle2 } from 'lucide-react';
import type { StepProps } from '../types';
export function InitSuccessStep(_props: StepProps) {
return (
<div className="flex flex-col items-center justify-center py-8 space-y-4">
<div className="relative">
<CheckCircle2 className="h-20 w-20 text-green-500 animate-in zoom-in duration-300" />
<div className="absolute inset-0 h-20 w-20 rounded-full bg-green-500/20 animate-ping" />
</div>
<div className="text-center space-y-1">
<h3 className="font-semibold text-lg">Successfully Synced!</h3>
<p className="text-sm text-muted-foreground">
Your code has been synced to the remote repository.
</p>
</div>
</div>
);
}
@@ -0,0 +1,69 @@
import { Zap, GitBranch } from 'lucide-react';
import { Label } from '@/components/ui/label';
import { ExternalFavicon } from '@/components/ExternalFavicon';
import { useGitSettings } from '@/hooks/useGitSettings';
import { cn } from '@/lib/utils';
import type { SelectProviderStepProps, ProviderOption } from '../types';
export function SelectProviderStep({
onSelectProvider,
}: SelectProviderStepProps) {
const { settings } = useGitSettings();
const providerOptions: ProviderOption[] = [
{
id: 'nostr',
name: 'Nostr',
icon: <Zap className="h-4 w-4" />,
},
...settings.credentials.map(credential => ({
id: credential.id,
name: credential.name,
icon: (
<ExternalFavicon
url={credential.origin}
size={16}
fallback={<GitBranch className="h-4 w-4" />}
/>
),
credential,
})),
{
id: 'other',
name: 'Other',
icon: <GitBranch className="h-4 w-4" />,
},
];
return (
<div className="space-y-4">
<div>
<h3 className="font-semibold mb-1">Sync your code to git</h3>
<p className="text-sm text-muted-foreground">
Choose a provider to sync your project
</p>
</div>
<div className="space-y-2">
<Label className="text-xs text-muted-foreground">Select Provider</Label>
<div className="flex flex-wrap gap-2">
{providerOptions.map((provider) => (
<button
key={provider.id}
type="button"
onClick={() => onSelectProvider(provider)}
className={cn(
"inline-flex items-center gap-2 px-3 py-1.5 rounded-full text-sm font-medium transition-all",
"border-2 hover:scale-105 active:scale-95",
"bg-background text-foreground border-border hover:border-primary/50"
)}
>
{provider.icon}
<span>{provider.name}</span>
</button>
))}
</div>
</div>
</div>
);
}
+206
View File
@@ -0,0 +1,206 @@
import { useState } from 'react';
import { 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 { Alert, AlertDescription } from '@/components/ui/alert';
import { ExternalFavicon } from '@/components/ExternalFavicon';
import { useGitStatus } from '@/hooks/useGitStatus';
import { useGitSettings } from '@/hooks/useGitSettings';
import { useGit } from '@/hooks/useGit';
import { useFSPaths } from '@/hooks/useFSPaths';
import type { SyncStepProps } from '../types';
export function SyncStep({ projectId }: SyncStepProps) {
const [isPushing, setIsPushing] = useState(false);
const [error, setError] = useState<string | null>(null);
const [copiedUrl, setCopiedUrl] = useState(false);
const { data: gitStatus } = useGitStatus(projectId);
const { settings } = useGitSettings();
const { git } = useGit();
const { projectsPath } = useFSPaths();
const originRemote = gitStatus?.remotes.find(r => r.name === 'origin');
// Match origin URL to a credential
const matchedCredential = originRemote
? settings.credentials.find(c => originRemote.url.startsWith(c.origin))
: null;
const handlePull = async () => {
if (!originRemote) return;
setIsPushing(true);
setError(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;
},
});
} catch (err) {
const message = err instanceof Error ? err.message : 'Failed to pull';
setError(message);
} finally {
setIsPushing(false);
}
};
const handlePush = async () => {
if (!originRemote) return;
setIsPushing(true);
setError(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;
},
});
} catch (err) {
const message = err instanceof Error ? err.message : 'Failed to push';
setError(message);
} finally {
setIsPushing(false);
}
};
const handleCopyUrl = async () => {
if (!originRemote) return;
try {
await navigator.clipboard.writeText(originRemote.url);
setCopiedUrl(true);
setTimeout(() => setCopiedUrl(false), 2000);
} catch (err) {
console.error('Failed to copy URL:', err);
}
};
const getProviderInfo = () => {
if (!originRemote) return null;
// Check if it's a Nostr URI
if (originRemote.url.startsWith('nostr://')) {
return {
name: 'Nostr Git',
icon: <Zap className="h-4 w-4" />,
};
}
// Check if we have a matched credential
if (matchedCredential) {
return {
name: matchedCredential.name,
icon: (
<ExternalFavicon
url={matchedCredential.origin}
size={16}
fallback={<GitBranch className="h-4 w-4" />}
/>
),
};
}
// Fallback to generic Git
return {
name: 'Git',
icon: <GitBranch className="h-4 w-4" />,
};
};
const providerInfo = getProviderInfo();
if (!originRemote) {
return null;
}
return (
<div className="space-y-4">
<div className="flex items-center gap-2">
{providerInfo?.icon}
<h3 className="font-semibold">{providerInfo?.name}</h3>
</div>
<div className="space-y-2">
<Label className="text-xs text-muted-foreground">Repository URL</Label>
<div className="flex gap-2">
<Input
value={originRemote.url}
readOnly
className="flex-1 text-sm"
/>
<Button
size="sm"
variant="outline"
onClick={handleCopyUrl}
className="shrink-0 h-10"
>
{copiedUrl ? (
<Check className="h-4 w-4" />
) : (
<Copy className="h-4 w-4" />
)}
</Button>
</div>
</div>
{error && (
<Alert variant="destructive">
<AlertDescription className="text-sm">{error}</AlertDescription>
</Alert>
)}
<div className="flex gap-2">
<Button
onClick={handlePull}
disabled={isPushing}
className="flex-1"
variant="outline"
>
{isPushing ? (
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-current" />
) : (
'Pull'
)}
</Button>
<Button
onClick={handlePush}
disabled={isPushing}
className="flex-1"
>
{isPushing ? (
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-current" />
) : (
'Push'
)}
</Button>
</div>
</div>
);
}
+35
View File
@@ -0,0 +1,35 @@
import type { GitCredential } from '@/contexts/GitSettingsContext';
import type { ReactNode } from 'react';
export interface ProviderOption {
id: string;
name: string;
icon: ReactNode;
credential?: GitCredential;
}
export type StepType =
| 'select-provider'
| 'configure-repo'
| 'sync'
| 'init-success';
export interface StepProps {
projectId: string;
onChangeStep: (step: StepType) => void;
onClose: () => void;
}
export interface SelectProviderStepProps extends StepProps {
onSelectProvider: (provider: ProviderOption) => void;
}
export interface ConfigureRepoStepProps extends StepProps {
selectedProvider: ProviderOption;
onBack: () => void;
onSuccess: () => void;
}
export interface SyncStepProps extends StepProps {
onRefetchStatus: () => void;
}
+1 -1
View File
@@ -19,7 +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 { GitSyncButton } from '@/components/git-sync';
import { useBuildProject } from '@/hooks/useBuildProject';
import { useIsProjectPreviewable } from '@/hooks/useIsProjectPreviewable';
import { useConsoleError } from '@/hooks/useConsoleError';