From c88b4f175033bfd4fbf98b0fec930708696a766e Mon Sep 17 00:00:00 2001 From: "shakespeare.diy" Date: Tue, 14 Oct 2025 23:55:41 -0500 Subject: [PATCH] Reorganize Repository modal into 3 sections - Section 1: Git URL, Current Branch, Sync Status, and Merge Branch action - Section 2: Your open PRs and Create PR button (collapsible form) - Section 3: Working directory changes Changes: - Replaced tab-based navigation with card-based sections - Combined repository info, branch management, and sync in first section - Added PR list showing user's open PRs with status icons - Made branch manager and PR form collapsible to save space - Improved visual hierarchy with cards and better spacing --- src/components/git/GitManagementDialog.tsx | 437 +++++++++++++++++---- 1 file changed, 352 insertions(+), 85 deletions(-) diff --git a/src/components/git/GitManagementDialog.tsx b/src/components/git/GitManagementDialog.tsx index 6e52ae65..5d77e02b 100644 --- a/src/components/git/GitManagementDialog.tsx +++ b/src/components/git/GitManagementDialog.tsx @@ -1,4 +1,4 @@ -import { useState } from 'react'; +import { useState, useEffect, useCallback } from 'react'; import { Dialog, DialogContent, @@ -6,21 +6,31 @@ import { DialogTitle, DialogTrigger, } from '@/components/ui/dialog'; -import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { ScrollArea } from '@/components/ui/scroll-area'; +import { Button } from '@/components/ui/button'; +import { Badge } from '@/components/ui/badge'; +import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; +import { Separator } from '@/components/ui/separator'; import { GitBranch, - GitCompare, - Settings, + GitMerge, GitPullRequest, + ExternalLink, + Loader2, + RefreshCw, + CheckCircle, + XCircle, + Clock, } from 'lucide-react'; -import { BranchManager } from './BranchManager'; -import { CompareView } from './CompareView'; import { DiffViewer } from './DiffViewer'; +import { MergeDialog } from './MergeDialog'; import { PullRequestForm } from './PullRequestForm'; import { useGitStatus } from '@/hooks/useGitStatus'; import { useGit } from '@/hooks/useGit'; -import { useState, useEffect } from 'react'; +import { useGitSettings } from '@/hooks/useGitSettings'; +import { useToast } from '@/hooks/useToast'; +import { findCredentialsForRepo } from '@/lib/gitCredentials'; +import { BranchManager } from './BranchManager'; interface GitManagementDialogProps { projectId: string; @@ -30,16 +40,37 @@ interface GitManagementDialogProps { defaultTab?: string; } +interface PullRequest { + number: number; + title: string; + state: 'open' | 'closed' | 'merged'; + html_url: string; + created_at: string; + head: { + ref: string; + }; + base: { + ref: string; + }; +} + export function GitManagementDialog({ projectId, children, open, onOpenChange, - defaultTab = 'branches', }: GitManagementDialogProps) { - const [activeTab, setActiveTab] = useState(defaultTab); const [remoteUrl, setRemoteUrl] = useState(null); + const [isSyncing, setIsSyncing] = useState(false); + const [isMergeDialogOpen, setIsMergeDialogOpen] = useState(false); + const [showPRForm, setShowPRForm] = useState(false); + const [pullRequests, setPullRequests] = useState([]); + const [isLoadingPRs, setIsLoadingPRs] = useState(false); + const [showBranchManager, setShowBranchManager] = useState(false); + const { git } = useGit(); + const { settings } = useGitSettings(); + const { toast } = useToast(); const projectPath = `/projects/${projectId}`; const { data: gitStatus, refetch: refetchGitStatus } = useGitStatus(projectId); @@ -58,11 +89,140 @@ export function GitManagementDialog({ } }, [open, git, projectPath]); + const loadPullRequests = useCallback(async () => { + if (!remoteUrl) return; + + setIsLoadingPRs(true); + try { + const credentials = findCredentialsForRepo(remoteUrl, settings.credentials); + if (!credentials) { + setPullRequests([]); + return; + } + + // Parse GitHub/GitLab URL + let match; + let owner = ''; + let repo = ''; + let apiUrl = ''; + + const normalizedUrl = remoteUrl.replace(/\.git$/, ''); + + // GitHub + match = normalizedUrl.match(/git@github\.com:([^/]+)\/(.+)$/) || + normalizedUrl.match(/https?:\/\/github\.com\/([^/]+)\/(.+)$/); + if (match) { + owner = match[1]; + repo = match[2]; + apiUrl = `https://api.github.com/repos/${owner}/${repo}/pulls`; + } + + // GitLab + if (!match) { + match = normalizedUrl.match(/git@gitlab\.com:([^/]+)\/(.+)$/) || + normalizedUrl.match(/https?:\/\/gitlab\.com\/([^/]+)\/(.+)$/); + if (match) { + owner = match[1]; + repo = match[2]; + apiUrl = `https://gitlab.com/api/v4/projects/${encodeURIComponent(`${owner}/${repo}`)}/merge_requests`; + } + } + + if (!apiUrl) { + setPullRequests([]); + return; + } + + // Get current user to filter PRs + const response = await fetch(`${apiUrl}?state=open`, { + headers: { + 'Authorization': `token ${credentials.password}`, + }, + }); + + if (!response.ok) { + throw new Error('Failed to fetch pull requests'); + } + + const data = await response.json(); + + // Filter PRs authored by current user (based on credentials username) + const userPRs = Array.isArray(data) + ? data.filter((pr: any) => + pr.user?.login === credentials.username || + pr.author?.username === credentials.username + ) + : []; + + setPullRequests(userPRs); + } catch (err) { + console.error('Failed to load pull requests:', err); + setPullRequests([]); + } finally { + setIsLoadingPRs(false); + } + }, [remoteUrl, settings.credentials]); + + useEffect(() => { + if (open && remoteUrl) { + loadPullRequests(); + } + }, [open, remoteUrl, loadPullRequests]); + + const handleSync = async () => { + setIsSyncing(true); + try { + const credentials = findCredentialsForRepo(remoteUrl || '', settings.credentials); + + // Pull + await git.pull({ + dir: projectPath, + author: { + name: credentials?.username || 'shakespeare.diy', + email: credentials?.email || 'assistant@shakespeare.diy', + }, + onAuth: () => credentials || undefined, + }); + + // Push + await git.push({ + dir: projectPath, + onAuth: () => credentials || undefined, + }); + + toast({ + title: 'Sync successful', + description: 'Your changes have been synchronized', + }); + + refetchGitStatus(); + } catch (err) { + toast({ + title: 'Sync failed', + description: err instanceof Error ? err.message : 'Unknown error', + variant: 'destructive', + }); + } finally { + setIsSyncing(false); + } + }; + const handleBranchChange = () => { - // Refetch git status when branches change refetchGitStatus(); }; + const formatDate = (dateStr: string) => { + const date = new Date(dateStr); + const now = new Date(); + const diffMs = now.getTime() - date.getTime(); + const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24)); + + if (diffDays === 0) return 'today'; + if (diffDays === 1) return 'yesterday'; + if (diffDays < 7) return `${diffDays} days ago`; + return date.toLocaleDateString(); + }; + return ( <> @@ -72,94 +232,201 @@ export function GitManagementDialog({ - Git Management + Repository - {/* Tabs Container - takes remaining space */} -
- - {/* Fixed Tab List */} -
- - - - Branches - - - - Changes - - - - Compare - - {remoteUrl && ( - - - Pull Request - - )} - -
+ +
+ {/* Section 1: Git URL, Current Branch, Sync Status, Merge Branch */} + + + Repository Info + + + {/* Git URL */} +
+
Git URL
+
+ {remoteUrl || 'No remote configured'} +
+
- {/* Scrollable Tab Content */} - - -
- +
Current Branch
+
+ + {gitStatus?.currentBranch || 'Unknown'} + + +
+
+ + {/* Branch Manager (collapsible) */} + {showBranchManager && ( +
+ +
+ )} + + {/* Sync Status */} + {remoteUrl && ( +
+
Sync Status
+
+ + {gitStatus && ( +
+ {gitStatus.modified + gitStatus.added + gitStatus.deleted > 0 + ? `${gitStatus.modified + gitStatus.added + gitStatus.deleted} changes` + : 'Up to date'} +
+ )} +
+
+ )} + + {/* Merge Branch Action */} +
+ { + refetchGitStatus(); + setIsMergeDialogOpen(false); + }} />
-
-
+
+
- -
+ {/* Section 2: Pull Requests */} + {remoteUrl && ( + + +
+ Pull Requests + +
+
+ + {/* PR Form (collapsible) */} + {showPRForm && ( +
+ +
+ )} + + {/* Your Open PRs */} +
+
Your Open PRs
+ {isLoadingPRs ? ( +
+ +
+ ) : pullRequests.length > 0 ? ( +
+ {pullRequests.map((pr) => ( +
+
+ {pr.state === 'open' && } + {pr.state === 'merged' && } + {pr.state === 'closed' && } +
+
+
{pr.title}
+
+ #{pr.number} + • + {pr.head.ref} → {pr.base.ref} + • + {formatDate(pr.created_at)} +
+
+ +
+ ))} +
+ ) : ( +
+ +

No open pull requests

+
+ )} +
+
+
+ )} + + {/* Section 3: Working Directory */} + + + Working Directory + + -
-
- - - -
- -
-
-
- - {remoteUrl && ( - - -
- -
-
-
- )} - -
+ + +
+