From fed44cef5e0ece564013d572597276e67298297e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 21 Sep 2025 23:36:08 -0500 Subject: [PATCH] GitDialog improvements --- src/components/GitDialog.test.tsx | 66 +++++-- src/components/GitDialog.tsx | 293 +++++++++++++++++++----------- 2 files changed, 240 insertions(+), 119 deletions(-) diff --git a/src/components/GitDialog.test.tsx b/src/components/GitDialog.test.tsx index fa03051d..ab2a5081 100644 --- a/src/components/GitDialog.test.tsx +++ b/src/components/GitDialog.test.tsx @@ -43,17 +43,10 @@ vi.mock('@/hooks/useToast', () => ({ })), })); +const mockUseGitSettings = vi.fn(); + vi.mock('@/hooks/useGitSettings', () => ({ - useGitSettings: vi.fn(() => ({ - settings: { - credentials: { - 'https://github.com': { - username: 'git', - password: 'github-token', - }, - }, - }, - })), + useGitSettings: () => mockUseGitSettings(), })); describe('GitDialog', () => { @@ -62,6 +55,17 @@ describe('GitDialog', () => { data: baseGitStatus, refetch: mockRefetch, }); + + mockUseGitSettings.mockReturnValue({ + settings: { + credentials: { + 'https://github.com': { + username: 'git', + password: 'github-token', + }, + }, + }, + }); }); it('renders git dialog without repository information section', () => { @@ -73,9 +77,9 @@ describe('GitDialog', () => { ); - expect(screen.getByText('Git Repository Status')).toBeInTheDocument(); + expect(screen.getByText('Repository')).toBeInTheDocument(); expect(screen.queryByText('Repository Information')).not.toBeInTheDocument(); - expect(screen.getByText('origin')).toBeInTheDocument(); // Appears in credentials section + expect(screen.queryByText('Authentication')).not.toBeInTheDocument(); // Authentication section is removed }); it('shows sync status', () => { @@ -143,7 +147,7 @@ describe('GitDialog', () => { expect(pushButton.closest('button')).toBeDisabled(); }); - it('shows configured credentials status', () => { + it('does not show credentials warning when credentials are configured', () => { render( {}}> @@ -152,8 +156,38 @@ describe('GitDialog', () => { ); - expect(screen.getByText('Authentication')).toBeInTheDocument(); - expect(screen.getByText('github.com')).toBeInTheDocument(); - expect(screen.getByText('Configured')).toBeInTheDocument(); + // Should not show warning when credentials are configured + expect(screen.queryByText(/No credentials configured/)).not.toBeInTheDocument(); + expect(screen.queryByText('Authentication')).not.toBeInTheDocument(); // Authentication section is removed + }); + + it('shows credentials warning when no credentials are configured', () => { + // Mock git settings with no credentials and mock git status with an https remote + mockUseGitSettings.mockReturnValue({ + settings: { + credentials: {}, + }, + }); + + mockUseGitStatus.mockReturnValue({ + data: { + ...baseGitStatus, + remotes: [{ name: 'origin', url: 'https://github.com/example/repo.git' }], + }, + refetch: mockRefetch, + }); + + render( + + {}}> + + + + ); + + // Should show warning when credentials are missing + expect(screen.getByText(/You are not logged into github.com/)).toBeInTheDocument(); + expect(screen.getByText('log in')).toBeInTheDocument(); + expect(screen.queryByText('Authentication')).not.toBeInTheDocument(); // Authentication section is removed }); }); \ No newline at end of file diff --git a/src/components/GitDialog.tsx b/src/components/GitDialog.tsx index 8c444aff..da1ef12c 100644 --- a/src/components/GitDialog.tsx +++ b/src/components/GitDialog.tsx @@ -1,10 +1,9 @@ -import { useState } from 'react'; +import { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, - DialogDescription, DialogHeader, DialogTitle, DialogTrigger, @@ -19,6 +18,8 @@ import { import { Badge } from '@/components/ui/badge'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Alert, AlertDescription } from '@/components/ui/alert'; +import { Input } from '@/components/ui/input'; +import { Label } from '@/components/ui/label'; import { GitBranch, Upload, @@ -31,9 +32,9 @@ import { Plus, Minus, Edit, - Settings, AlertTriangle, Zap, + Save, } from 'lucide-react'; import { useGitStatus } from '@/hooks/useGitStatus'; import { useGitSettings } from '@/hooks/useGitSettings'; @@ -43,7 +44,7 @@ import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useNostr } from '@nostrify/react'; import { useNostrPublish } from '@/hooks/useNostrPublish'; import { cn } from '@/lib/utils'; -import { findCredentialsForRepo, getOriginDisplayName } from '@/lib/gitCredentials'; +import { findCredentialsForRepo } from '@/lib/gitCredentials'; import { nip19 } from 'nostr-tools'; const GRASP_SERVERS = ["git.shakespeare.diy", "relay.ngit.dev", "gitnostr.com"]; @@ -59,8 +60,8 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog const [isPushing, setIsPushing] = useState(false); const [isPulling, setIsPulling] = useState(false); const [isPushingToNostr, setIsPushingToNostr] = useState(false); - const [pushResult, setPushResult] = useState(null); - const [pullResult, setPullResult] = useState(null); + const [originUrl, setOriginUrl] = useState(''); + const [isSavingOrigin, setIsSavingOrigin] = useState(false); const navigate = useNavigate(); const { data: gitStatus, refetch: refetchGitStatus } = useGitStatus(projectId); @@ -73,6 +74,70 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog const projectPath = `/projects/${projectId}`; + // Initialize origin URL from git status + useEffect(() => { + if (gitStatus?.remotes) { + const originRemote = gitStatus.remotes.find(remote => remote.name === 'origin'); + setOriginUrl(originRemote?.url || ''); + } + }, [gitStatus?.remotes]); + + const handleSaveOrigin = async () => { + if (!gitStatus?.isGitRepo) { + toast({ + title: "Cannot configure origin", + description: "Not a git repository", + variant: "destructive", + }); + return; + } + + setIsSavingOrigin(true); + + try { + // Remove existing origin if it exists + try { + await git.deleteRemote({ + dir: projectPath, + remote: 'origin', + }); + } catch { + // Ignore if origin doesn't exist + } + + // Add new origin if URL is provided + if (originUrl.trim()) { + await git.addRemote({ + dir: projectPath, + remote: 'origin', + url: originUrl.trim(), + }); + + toast({ + title: "Origin configured", + description: `Origin remote set to ${originUrl.trim()}`, + }); + } else { + toast({ + title: "Origin removed", + description: "Origin remote has been removed", + }); + } + + // Refresh git status after changing origin + await refetchGitStatus(); + } catch (error) { + const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; + toast({ + title: "Failed to configure origin", + description: errorMessage, + variant: "destructive", + }); + } finally { + setIsSavingOrigin(false); + } + }; + const handlePushToNostr = async () => { if (!user) { toast({ @@ -93,7 +158,6 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog } setIsPushingToNostr(true); - setPushResult(null); try { // Check if repo announcement already exists @@ -264,10 +328,8 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog description: "Waiting for GRASP servers to update...", }); - setPushResult(`✅ Successfully published repository to Nostr`); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; - setPushResult(`❌ Push to Nostr failed: ${errorMessage}`); toast({ title: "Push to Nostr failed", description: errorMessage, @@ -307,7 +369,6 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog } setIsPushing(true); - setPushResult(null); try { const remote = gitStatus.remotes[0]; // Use first remote (usually 'origin') @@ -320,7 +381,7 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog signer: user?.signer, // For signing if pushing to Nostr }); - setPushResult(`✅ Successfully pushed ${gitStatus.ahead} commit${gitStatus.ahead !== 1 ? 's' : ''} to ${remote.name}/${gitStatus.currentBranch}`); + toast({ title: "Push successful", description: `${gitStatus.ahead} commit${gitStatus.ahead !== 1 ? 's' : ''} pushed to ${remote.name}/${gitStatus.currentBranch}`, @@ -330,7 +391,6 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog await refetchGitStatus(); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; - setPushResult(`❌ Push failed: ${errorMessage}`); toast({ title: "Push failed", description: errorMessage, @@ -361,7 +421,6 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog } setIsPulling(true); - setPullResult(null); try { const remote = gitStatus.remotes[0]; // Use first remote (usually 'origin') @@ -377,7 +436,7 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog onAuth: (url) => findCredentialsForRepo(url, settings.credentials), }); - setPullResult(`✅ Successfully pulled from ${remote.name}/${gitStatus.currentBranch}`); + toast({ title: "Pull successful", description: `Latest changes pulled from ${remote.name}/${gitStatus.currentBranch}`, @@ -387,7 +446,6 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog await refetchGitStatus(); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; - setPullResult(`❌ Pull failed: ${errorMessage}`); toast({ title: "Pull failed", description: errorMessage, @@ -472,85 +530,128 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog - Git Repository Status + Repository - - View repository status and sync with remote repositories - + {/* URL Configuration */} + {gitStatus?.isGitRepo && ( + + + Git URL + + +
+
+ + setOriginUrl(e.target.value)} + disabled={isSavingOrigin} + /> +
+ +
+ + {/* Credentials Warning */} + {gitStatus.remotes.length > 0 && ( + (() => { + const remoteWithoutCredentials = gitStatus.remotes.find((remote) => !findCredentialsForRepo(remote.url, settings.credentials)); + if (!remoteWithoutCredentials) return null; + + let protocol = ''; + let hostname = 'the remote host'; + try { + const url = new URL(remoteWithoutCredentials.url); + protocol = url.protocol; + hostname = url.hostname; + } catch { + // Invalid URL + return ( + + + + Invalid URL format. Enter an https URL. + + + ); + } + + // Check for unsupported protocols + if (protocol !== 'https:' && protocol !== 'nostr:') { + return ( + + + + The {protocol.replace(':', '')} URL type is not supported. Enter an https URL. + + + ); + } + + // For nostr protocol, only show warning if user is not logged in + if (protocol === 'nostr:' && !user) { + return ( + + + + You are not logged into Nostr. Push & pull might not work unless you{' '} + . + + + ); + } + + // For https protocol, show warning if no credentials + if (protocol === 'https:') { + return ( + + + + You are not logged into {hostname}. Push & pull might not work unless you{' '} + . + + + ); + } + + return null; + })() + )} +
+
+ )} +
- {/* Credentials Status */} - {gitStatus?.isGitRepo && gitStatus.remotes.length > 0 && ( - - - - Authentication - - - - Credentials for push/pull operations - - - - {gitStatus.remotes.map((remote) => { - const credentials = findCredentialsForRepo(remote.url, settings.credentials); - const originDisplayName = getOriginDisplayName(remote.url); - - return ( -
-
-
- {remote.name} - - {originDisplayName} - -
- {credentials ? ( -
- - Configured -
- ) : ( -
- - No credentials -
- )} -
- - {!credentials && ( - - - - No credentials configured for {originDisplayName}. - Push/pull operations may fail for private repositories.{' '} - - - - )} -
- ); - })} -
-
- )} - {/* Sync Status */} {gitStatus?.isGitRepo && ( @@ -619,21 +720,7 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog
)} - {/* Operation Results */} - {(pushResult || pullResult) && ( -
- {pushResult && ( -
- {pushResult} -
- )} - {pullResult && ( -
- {pullResult} -
- )} -
- )} + )}