From 5f054a9a4a540995387e38e68c127d17bdfae6ee Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 24 Dec 2025 18:58:51 -0600 Subject: [PATCH] Pass a parsed remoteUrl down into GitSyncSteps to simplify conditionals --- src/components/git-sync/GitSyncSteps.tsx | 19 ++++- .../git-sync/steps/ForcePullDialog.tsx | 4 +- .../git-sync/steps/ForcePushDialog.tsx | 4 +- src/components/git-sync/steps/SyncStep.tsx | 74 +++++++++---------- .../git-sync/steps/SyncStepError.tsx | 28 ++++--- src/components/git-sync/types.ts | 4 + src/lib/gitCredentials.ts | 12 +-- 7 files changed, 79 insertions(+), 66 deletions(-) diff --git a/src/components/git-sync/GitSyncSteps.tsx b/src/components/git-sync/GitSyncSteps.tsx index c8b1015b..b15ebf08 100644 --- a/src/components/git-sync/GitSyncSteps.tsx +++ b/src/components/git-sync/GitSyncSteps.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect } from 'react'; +import { useState, useEffect, useMemo } from 'react'; import { useGitStatus } from '@/hooks/useGitStatus'; import { SelectProviderStep } from './steps/SelectProviderStep'; import { ConfigureRepoStep } from './steps/ConfigureRepoStep'; @@ -14,9 +14,19 @@ interface GitSyncStepsProps { export function GitSyncSteps({ projectId, onClose }: GitSyncStepsProps) { 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; + + const remoteUrl = useMemo(() => { + if (originRemote) { + try { + return new URL(originRemote.url); + } catch { + return; + } + } + }, [originRemote]); + + const hasRemote = !!remoteUrl; const [currentStep, setCurrentStep] = useState(hasRemote ? 'sync' : 'select-provider'); const [selectedProvider, setSelectedProvider] = useState(null); @@ -64,7 +74,7 @@ export function GitSyncSteps({ projectId, onClose }: GitSyncStepsProps) { } // Render the appropriate step - if (currentStep === 'select-provider') { + if (!remoteUrl || currentStep === 'select-provider') { return ( diff --git a/src/components/git-sync/steps/ForcePullDialog.tsx b/src/components/git-sync/steps/ForcePullDialog.tsx index 952e149c..c10855eb 100644 --- a/src/components/git-sync/steps/ForcePullDialog.tsx +++ b/src/components/git-sync/steps/ForcePullDialog.tsx @@ -13,14 +13,14 @@ interface ForcePullDialogProps { open: boolean; onOpenChange: (open: boolean) => void; onConfirm: () => void; - remoteName?: string; + remoteName: string; } export function ForcePullDialog({ open, onOpenChange, onConfirm, - remoteName = 'the remote' + remoteName, }: ForcePullDialogProps) { const handleConfirm = () => { onOpenChange(false); diff --git a/src/components/git-sync/steps/ForcePushDialog.tsx b/src/components/git-sync/steps/ForcePushDialog.tsx index 8fa6eb5a..dcc7ef9d 100644 --- a/src/components/git-sync/steps/ForcePushDialog.tsx +++ b/src/components/git-sync/steps/ForcePushDialog.tsx @@ -13,14 +13,14 @@ interface ForcePushDialogProps { open: boolean; onOpenChange: (open: boolean) => void; onConfirm: () => void; - remoteName?: string; + remoteName: string; } export function ForcePushDialog({ open, onOpenChange, onConfirm, - remoteName = 'the remote' + remoteName, }: ForcePushDialogProps) { const handleConfirm = () => { onOpenChange(false); diff --git a/src/components/git-sync/steps/SyncStep.tsx b/src/components/git-sync/steps/SyncStep.tsx index 7ba11aae..a1db6de6 100644 --- a/src/components/git-sync/steps/SyncStep.tsx +++ b/src/components/git-sync/steps/SyncStep.tsx @@ -18,15 +18,16 @@ import { useGitSettings } from '@/hooks/useGitSettings'; import { useGit } from '@/hooks/useGit'; import { useFSPaths } from '@/hooks/useFSPaths'; import { useAppContext } from '@/hooks/useAppContext'; -import type { StepProps } from '../types'; +import type { SyncStepProps } from '../types'; import { cn } from '@/lib/utils'; import { NostrURI } from '@/lib/NostrURI'; import { ngitWebUrl } from '@/lib/ngitWebUrl'; import { useQueryClient } from '@tanstack/react-query'; +import { findCredentialsForRepo } from '@/lib/gitCredentials'; type GitOperation = 'sync' | 'pull' | 'push' | 'force-pull' | 'force-push'; -export function SyncStep({ projectId }: StepProps) { +export function SyncStep({ projectId, remoteUrl }: SyncStepProps) { const queryClient = useQueryClient(); const [isSyncing, setIsSyncing] = useState(false); @@ -44,13 +45,6 @@ export function SyncStep({ projectId }: StepProps) { const { projectsPath } = useFSPaths(); const { config } = useAppContext(); - 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 dir = `${projectsPath}/${projectId}`; const ref = gitStatus?.currentBranch || 'main'; @@ -59,7 +53,7 @@ export function SyncStep({ projectId }: StepProps) { operation: GitOperation, action: () => Promise ) => { - if (!originRemote) return; + if (!remoteUrl) return; setIsSyncing(true); setDropdownOpen(false); @@ -80,7 +74,7 @@ export function SyncStep({ projectId }: StepProps) { setIsSyncing(false); setCurrentOperation(null); } - }, [originRemote]); + }, [remoteUrl]); const handleSync = async () => { await executeGitOperation('sync', async () => { @@ -153,10 +147,10 @@ export function SyncStep({ projectId }: StepProps) { }, []); const handleCopyUrl = async () => { - if (!originRemote) return; + if (!remoteUrl) return; try { - await navigator.clipboard.writeText(originRemote.url); + await navigator.clipboard.writeText(remoteUrl.href); showTemporaryFeedback(setCopiedUrl, 2000); } catch (err) { console.error('Failed to copy URL:', err); @@ -164,7 +158,7 @@ export function SyncStep({ projectId }: StepProps) { }; const handleDisconnect = async () => { - if (!originRemote) return; + if (!remoteUrl) return; try { await git.deleteRemote({ dir, remote: 'origin' }); @@ -175,13 +169,13 @@ export function SyncStep({ projectId }: StepProps) { } }; - const getExternalUrl = useCallback(async (url: string): Promise => { - if (url.startsWith('https://')) { - return url; + const getExternalUrl = useCallback(async (url: URL): Promise => { + if (url.protocol === 'http:' || url.protocol === 'https:') { + return url.href; } - if (url.startsWith('nostr://')) { - const nostrUri = await NostrURI.parse(url); + if (url.protocol === 'nostr:') { + const nostrUri = await NostrURI.parse(url.href); return ngitWebUrl(config.ngitWebUrl, nostrUri); } @@ -189,10 +183,10 @@ export function SyncStep({ projectId }: StepProps) { }, [config.ngitWebUrl]); const handleOpenExternal = async () => { - if (!originRemote) return; + if (!remoteUrl) return; try { - const externalUrl = await getExternalUrl(originRemote.url); + const externalUrl = await getExternalUrl(remoteUrl); if (externalUrl) { window.open(externalUrl, '_blank', 'noopener,noreferrer'); } @@ -202,17 +196,10 @@ export function SyncStep({ projectId }: StepProps) { }; const getProviderInfo = useCallback(() => { - if (!originRemote) return null; - - let url: URL | null; - try { - url = new URL(originRemote.url); - } catch { - url = null; - } + if (!remoteUrl) return null; // Check if it's a Nostr URI - if (originRemote.url.startsWith('nostr://')) { + if (remoteUrl.protocol === 'nostr:') { return { name: 'Nostr Git', icon: , @@ -220,6 +207,7 @@ export function SyncStep({ projectId }: StepProps) { } // Check if we have a matched credential + const matchedCredential = findCredentialsForRepo(remoteUrl, settings.credentials); if (matchedCredential) { return { name: matchedCredential.name, @@ -232,14 +220,11 @@ export function SyncStep({ projectId }: StepProps) { }; } - if (url && (url.protocol === 'http:' || url.protocol === 'https:')) { - return { name: url.hostname }; - } - - return { name: 'Git' }; - }, [originRemote, matchedCredential]); + return { name: remoteUrl.hostname }; + }, [remoteUrl, settings.credentials]); const providerInfo = getProviderInfo(); + const remoteName = providerInfo?.name ?? 'the remote'; // Get the button label based on current operation const getButtonLabel = (): string => { @@ -261,8 +246,13 @@ export function SyncStep({ projectId }: StepProps) { } }; - if (!originRemote) { - return null; + if (!remoteUrl) { + return ( +
+

No remote repository is configured for this project.

+

Please add a remote to enable Git synchronization.

+
+ ); } return ( @@ -301,7 +291,7 @@ export function SyncStep({ projectId }: StepProps) {
@@ -327,6 +317,8 @@ export function SyncStep({ projectId }: StepProps) { onForcePull={() => setShowForcePullDialog(true)} onForcePush={() => setShowForcePushDialog(true)} onPull={handlePull} + remoteName={remoteName} + remoteUrl={remoteUrl} /> )} @@ -382,14 +374,14 @@ export function SyncStep({ projectId }: StepProps) { open={showForcePullDialog} onOpenChange={setShowForcePullDialog} onConfirm={handleForcePull} - remoteName={providerInfo?.name} + remoteName={remoteName} />
); diff --git a/src/components/git-sync/steps/SyncStepError.tsx b/src/components/git-sync/steps/SyncStepError.tsx index fe992f44..f5645345 100644 --- a/src/components/git-sync/steps/SyncStepError.tsx +++ b/src/components/git-sync/steps/SyncStepError.tsx @@ -10,9 +10,11 @@ interface SyncStepErrorProps { onForcePull: () => void; onForcePush: () => void; onPull: () => void; + remoteUrl: URL; + remoteName: string; } -export function SyncStepError({ error, onDismiss, onForcePull, onForcePush, onPull }: SyncStepErrorProps) { +export function SyncStepError({ error, onDismiss, onForcePull, onForcePush, onPull, remoteName, remoteUrl }: SyncStepErrorProps) { const renderForcePullButton = () => (