mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 08:37:22 +00:00
SyncStep: DRY, improve display of unknown git URLs
This commit is contained in:
1 file changed
+67
-94
@@ -1,5 +1,5 @@
|
|||||||
import { useState } from 'react';
|
import { useState, useCallback } from 'react';
|
||||||
import { Zap, GitBranch, Copy, Check, RefreshCw, EllipsisVertical, CloudOff, ExternalLink, ChevronDown, ArrowDown, ArrowUp } from 'lucide-react';
|
import { Zap, Copy, Check, RefreshCw, EllipsisVertical, CloudOff, ExternalLink, ChevronDown, ArrowDown, ArrowUp } from 'lucide-react';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Input } from '@/components/ui/input';
|
import { Input } from '@/components/ui/input';
|
||||||
import { Label } from '@/components/ui/label';
|
import { Label } from '@/components/ui/label';
|
||||||
@@ -22,6 +22,8 @@ import { NostrURI } from '@/lib/NostrURI';
|
|||||||
import { ngitWebUrl } from '@/lib/ngitWebUrl';
|
import { ngitWebUrl } from '@/lib/ngitWebUrl';
|
||||||
import { useQueryClient } from '@tanstack/react-query';
|
import { useQueryClient } from '@tanstack/react-query';
|
||||||
|
|
||||||
|
type GitOperation = 'sync' | 'pull' | 'push';
|
||||||
|
|
||||||
export function SyncStep({ projectId }: StepProps) {
|
export function SyncStep({ projectId }: StepProps) {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
@@ -43,7 +45,14 @@ export function SyncStep({ projectId }: StepProps) {
|
|||||||
? settings.credentials.find(c => originRemote.url.startsWith(c.origin))
|
? settings.credentials.find(c => originRemote.url.startsWith(c.origin))
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
const handleSync = async () => {
|
const dir = `${projectsPath}/${projectId}`;
|
||||||
|
const ref = gitStatus?.currentBranch || 'main';
|
||||||
|
|
||||||
|
// Generic git operation handler
|
||||||
|
const executeGitOperation = useCallback(async (
|
||||||
|
operation: GitOperation,
|
||||||
|
action: () => Promise<void>
|
||||||
|
) => {
|
||||||
if (!originRemote) return;
|
if (!originRemote) return;
|
||||||
|
|
||||||
setIsSyncing(true);
|
setIsSyncing(true);
|
||||||
@@ -51,94 +60,55 @@ export function SyncStep({ projectId }: StepProps) {
|
|||||||
setSyncSuccess(false);
|
setSyncSuccess(false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const dir = `${projectsPath}/${projectId}`;
|
await action();
|
||||||
|
|
||||||
// Pull first to get latest changes
|
|
||||||
await git.pull({
|
|
||||||
dir,
|
|
||||||
ref: gitStatus?.currentBranch || 'main',
|
|
||||||
singleBranch: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Then push local changes
|
|
||||||
await git.push({
|
|
||||||
dir,
|
|
||||||
remote: 'origin',
|
|
||||||
ref: gitStatus?.currentBranch || 'main',
|
|
||||||
});
|
|
||||||
|
|
||||||
// Show success feedback
|
// Show success feedback
|
||||||
setSyncSuccess(true);
|
setSyncSuccess(true);
|
||||||
setTimeout(() => setSyncSuccess(false), 1500);
|
setTimeout(() => setSyncSuccess(false), 1500);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const message = err instanceof Error ? err.message : 'Failed to sync';
|
const message = err instanceof Error ? err.message : `Failed to ${operation}`;
|
||||||
setError(message);
|
setError(message);
|
||||||
} finally {
|
} finally {
|
||||||
setIsSyncing(false);
|
setIsSyncing(false);
|
||||||
}
|
}
|
||||||
|
}, [originRemote]);
|
||||||
|
|
||||||
|
const handleSync = async () => {
|
||||||
|
await executeGitOperation('sync', async () => {
|
||||||
|
// Pull first to get latest changes
|
||||||
|
await git.pull({ dir, ref, singleBranch: true });
|
||||||
|
// Then push local changes
|
||||||
|
await git.push({ dir, remote: 'origin', ref });
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlePull = async () => {
|
const handlePull = async () => {
|
||||||
if (!originRemote) return;
|
await executeGitOperation('pull', async () => {
|
||||||
|
await git.pull({ dir, ref, singleBranch: true });
|
||||||
setIsSyncing(true);
|
});
|
||||||
setError(null);
|
|
||||||
setSyncSuccess(false);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const dir = `${projectsPath}/${projectId}`;
|
|
||||||
|
|
||||||
await git.pull({
|
|
||||||
dir,
|
|
||||||
ref: gitStatus?.currentBranch || 'main',
|
|
||||||
singleBranch: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Show success feedback
|
|
||||||
setSyncSuccess(true);
|
|
||||||
setTimeout(() => setSyncSuccess(false), 1500);
|
|
||||||
} catch (err) {
|
|
||||||
const message = err instanceof Error ? err.message : 'Failed to pull';
|
|
||||||
setError(message);
|
|
||||||
} finally {
|
|
||||||
setIsSyncing(false);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlePush = async () => {
|
const handlePush = async () => {
|
||||||
if (!originRemote) return;
|
await executeGitOperation('push', async () => {
|
||||||
|
await git.push({ dir, remote: 'origin', ref });
|
||||||
setIsSyncing(true);
|
});
|
||||||
setError(null);
|
|
||||||
setSyncSuccess(false);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const dir = `${projectsPath}/${projectId}`;
|
|
||||||
|
|
||||||
await git.push({
|
|
||||||
dir,
|
|
||||||
remote: 'origin',
|
|
||||||
ref: gitStatus?.currentBranch || 'main',
|
|
||||||
});
|
|
||||||
|
|
||||||
// Show success feedback
|
|
||||||
setSyncSuccess(true);
|
|
||||||
setTimeout(() => setSyncSuccess(false), 1500);
|
|
||||||
} catch (err) {
|
|
||||||
const message = err instanceof Error ? err.message : 'Failed to push';
|
|
||||||
setError(message);
|
|
||||||
} finally {
|
|
||||||
setIsSyncing(false);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Temporary feedback state helper
|
||||||
|
const showTemporaryFeedback = useCallback((
|
||||||
|
setter: (value: boolean) => void,
|
||||||
|
duration: number
|
||||||
|
) => {
|
||||||
|
setter(true);
|
||||||
|
setTimeout(() => setter(false), duration);
|
||||||
|
}, []);
|
||||||
|
|
||||||
const handleCopyUrl = async () => {
|
const handleCopyUrl = async () => {
|
||||||
if (!originRemote) return;
|
if (!originRemote) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await navigator.clipboard.writeText(originRemote.url);
|
await navigator.clipboard.writeText(originRemote.url);
|
||||||
setCopiedUrl(true);
|
showTemporaryFeedback(setCopiedUrl, 2000);
|
||||||
setTimeout(() => setCopiedUrl(false), 2000);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to copy URL:', err);
|
console.error('Failed to copy URL:', err);
|
||||||
}
|
}
|
||||||
@@ -148,11 +118,7 @@ export function SyncStep({ projectId }: StepProps) {
|
|||||||
if (!originRemote) return;
|
if (!originRemote) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const dir = `${projectsPath}/${projectId}`;
|
await git.deleteRemote({ dir, remote: 'origin' });
|
||||||
await git.deleteRemote({
|
|
||||||
dir,
|
|
||||||
remote: 'origin',
|
|
||||||
});
|
|
||||||
queryClient.invalidateQueries({ queryKey: ['git-status', projectId] });
|
queryClient.invalidateQueries({ queryKey: ['git-status', projectId] });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const message = err instanceof Error ? err.message : 'Failed to disconnect';
|
const message = err instanceof Error ? err.message : 'Failed to disconnect';
|
||||||
@@ -160,38 +126,47 @@ export function SyncStep({ projectId }: StepProps) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getExternalUrl = useCallback(async (url: string): Promise<string | null> => {
|
||||||
|
if (url.startsWith('https://')) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.startsWith('nostr://')) {
|
||||||
|
const nostrUri = await NostrURI.parse(url);
|
||||||
|
return ngitWebUrl(config.ngitWebUrl, nostrUri);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}, [config.ngitWebUrl]);
|
||||||
|
|
||||||
const handleOpenExternal = async () => {
|
const handleOpenExternal = async () => {
|
||||||
if (!originRemote) return;
|
if (!originRemote) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let externalUrl: string;
|
const externalUrl = await getExternalUrl(originRemote.url);
|
||||||
|
if (externalUrl) {
|
||||||
if (originRemote.url.startsWith('https://')) {
|
window.open(externalUrl, '_blank', 'noopener,noreferrer');
|
||||||
// For HTTPS URLs, open directly
|
|
||||||
externalUrl = originRemote.url;
|
|
||||||
} else if (originRemote.url.startsWith('nostr://')) {
|
|
||||||
// For Nostr URLs, parse and generate URL using the configured template
|
|
||||||
const nostrUri = await NostrURI.parse(originRemote.url);
|
|
||||||
externalUrl = ngitWebUrl(config.ngitWebUrl, nostrUri);
|
|
||||||
} else {
|
|
||||||
// Unsupported URL type
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
window.open(externalUrl, '_blank', 'noopener,noreferrer');
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to open external URL:', err);
|
console.error('Failed to open external URL:', err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getProviderInfo = () => {
|
const getProviderInfo = useCallback(() => {
|
||||||
if (!originRemote) return null;
|
if (!originRemote) return null;
|
||||||
|
|
||||||
|
let url: URL | null;
|
||||||
|
try {
|
||||||
|
url = new URL(originRemote.url);
|
||||||
|
} catch {
|
||||||
|
url = null;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if it's a Nostr URI
|
// Check if it's a Nostr URI
|
||||||
if (originRemote.url.startsWith('nostr://')) {
|
if (originRemote.url.startsWith('nostr://')) {
|
||||||
return {
|
return {
|
||||||
name: 'Nostr Git',
|
name: 'Nostr Git',
|
||||||
icon: <Zap className="h-4 w-4" />,
|
icon: <Zap className="size-4" />,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,7 +178,6 @@ export function SyncStep({ projectId }: StepProps) {
|
|||||||
<ExternalFavicon
|
<ExternalFavicon
|
||||||
url={matchedCredential.origin}
|
url={matchedCredential.origin}
|
||||||
size={16}
|
size={16}
|
||||||
fallback={<GitBranch className="h-4 w-4" />}
|
|
||||||
/>
|
/>
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
@@ -211,10 +185,9 @@ export function SyncStep({ projectId }: StepProps) {
|
|||||||
|
|
||||||
// Fallback to generic Git
|
// Fallback to generic Git
|
||||||
return {
|
return {
|
||||||
name: 'Git',
|
name: url?.hostname || 'Git',
|
||||||
icon: <GitBranch className="h-4 w-4" />,
|
|
||||||
};
|
};
|
||||||
};
|
}, [originRemote, matchedCredential]);
|
||||||
|
|
||||||
const providerInfo = getProviderInfo();
|
const providerInfo = getProviderInfo();
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user