-
-
- setNewBranchName(e.target.value)}
- onKeyDown={(e) => {
- if (e.key === 'Enter' && !isCreatingBranch) {
- handleCreateBranch();
- }
- }}
- autoFocus
- />
-
- {gitStatus?.currentBranch && (
-
- Creating from:
{gitStatus.currentBranch}
+
+
+
+
+
+ setNewBranchName(e.target.value)}
+ onKeyDown={(e) => {
+ if (e.key === 'Enter' && !isCreatingBranch) {
+ handleCreateBranch();
+ }
+ }}
+ autoFocus
+ />
- )}
-
-
-
-
-
-
-
- >
+
+
+
+
+
+
+
+ >
);
}
\ No newline at end of file
diff --git a/src/components/git/BranchManager.tsx b/src/components/git/BranchManager.tsx
index 5fb91820..42e8b42e 100644
--- a/src/components/git/BranchManager.tsx
+++ b/src/components/git/BranchManager.tsx
@@ -1,4 +1,4 @@
-import { useState, useEffect } from 'react';
+import { useState, useEffect, useCallback } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
@@ -20,14 +20,11 @@ import {
AlertDialogHeader,
AlertDialogTitle,
} from '@/components/ui/alert-dialog';
-import { ScrollArea } from '@/components/ui/scroll-area';
import { Badge } from '@/components/ui/badge';
import {
GitBranch,
Plus,
Trash2,
- GitMerge,
- GitCompare,
Check,
Loader2,
ArrowUpRight,
@@ -65,11 +62,7 @@ export function BranchManager({ projectId, currentBranch, onBranchChange }: Bran
const { toast } = useToast();
const projectPath = `/projects/${projectId}`;
- useEffect(() => {
- loadBranches();
- }, [projectId, currentBranch]);
-
- const loadBranches = async () => {
+ const loadBranches = useCallback(async () => {
setIsLoading(true);
try {
// Get local branches
@@ -150,7 +143,11 @@ export function BranchManager({ projectId, currentBranch, onBranchChange }: Bran
} finally {
setIsLoading(false);
}
- };
+ }, [git, projectPath, toast]);
+
+ useEffect(() => {
+ loadBranches();
+ }, [projectId, currentBranch, loadBranches]);
const handleCreateBranch = async () => {
if (!newBranchName.trim()) {
diff --git a/src/components/git/CompareView.tsx b/src/components/git/CompareView.tsx
index 1062829e..169008cf 100644
--- a/src/components/git/CompareView.tsx
+++ b/src/components/git/CompareView.tsx
@@ -1,5 +1,4 @@
-import { useState, useEffect } from 'react';
-import { Button } from '@/components/ui/button';
+import { useState, useEffect, useCallback } from 'react';
import {
Select,
SelectContent,
@@ -8,13 +7,11 @@ import {
SelectValue,
} from '@/components/ui/select';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
-import { Badge } from '@/components/ui/badge';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { GitCompare, Loader2, GitCommit, FileIcon } from 'lucide-react';
import { useGit } from '@/hooks/useGit';
import { useToast } from '@/hooks/useToast';
import { DiffViewer } from './DiffViewer';
-import { cn } from '@/lib/utils';
interface CompareViewProps {
projectId: string;
@@ -44,17 +41,7 @@ export function CompareView({ projectId }: CompareViewProps) {
const { toast } = useToast();
const projectPath = `/projects/${projectId}`;
- useEffect(() => {
- loadReferences();
- }, [projectId]);
-
- useEffect(() => {
- if (baseRef && compareRef && baseRef !== compareRef) {
- performComparison();
- }
- }, [baseRef, compareRef]);
-
- const loadReferences = async () => {
+ const loadReferences = useCallback(async () => {
setIsLoading(true);
try {
// Get all branches
@@ -85,9 +72,9 @@ export function CompareView({ projectId }: CompareViewProps) {
} finally {
setIsLoading(false);
}
- };
+ }, [git, projectPath, toast]);
- const performComparison = async () => {
+ const performComparison = useCallback(async () => {
if (!baseRef || !compareRef) return;
setIsComparing(true);
@@ -158,7 +145,17 @@ export function CompareView({ projectId }: CompareViewProps) {
} finally {
setIsComparing(false);
}
- };
+ }, [baseRef, compareRef, git, projectPath, toast]);
+
+ useEffect(() => {
+ loadReferences();
+ }, [projectId, loadReferences]);
+
+ useEffect(() => {
+ if (baseRef && compareRef && baseRef !== compareRef) {
+ performComparison();
+ }
+ }, [baseRef, compareRef, performComparison]);
const formatDate = (timestamp: number) => {
const date = new Date(timestamp * 1000);
diff --git a/src/components/git/DiffViewer.tsx b/src/components/git/DiffViewer.tsx
index 228189b2..5f540035 100644
--- a/src/components/git/DiffViewer.tsx
+++ b/src/components/git/DiffViewer.tsx
@@ -1,15 +1,8 @@
-import { useState, useEffect } from 'react';
+import { useState, useEffect, useCallback } from 'react';
import { Button } from '@/components/ui/button';
import { ScrollArea } from '@/components/ui/scroll-area';
import { Badge } from '@/components/ui/badge';
-import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
-import {
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
- SelectValue,
-} from '@/components/ui/select';
+import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
import {
FileIcon,
Plus,
@@ -25,14 +18,6 @@ import { useGit } from '@/hooks/useGit';
import { useFS } from '@/hooks/useFS';
import { useToast } from '@/hooks/useToast';
import { cn } from '@/lib/utils';
-import Prism from 'prismjs';
-import 'prismjs/components/prism-typescript';
-import 'prismjs/components/prism-javascript';
-import 'prismjs/components/prism-jsx';
-import 'prismjs/components/prism-tsx';
-import 'prismjs/components/prism-css';
-import 'prismjs/components/prism-json';
-import 'prismjs/components/prism-markdown';
interface DiffHunk {
oldStart: number;
@@ -76,58 +61,7 @@ export function DiffViewer({ projectId, compareFrom = 'HEAD', compareTo, filepat
const { toast } = useToast();
const projectPath = `/projects/${projectId}`;
- useEffect(() => {
- loadDiffs();
- }, [projectId, compareFrom, compareTo, filepath]);
-
- const loadDiffs = async () => {
- setIsLoading(true);
- try {
- let files: string[] = [];
-
- if (filepath) {
- files = [filepath];
- } else if (compareTo) {
- // Compare two commits/branches
- files = await getChangedFilesBetweenRefs(compareFrom, compareTo);
- } else {
- // Compare against working directory
- const statusMatrix = await git.statusMatrix({ dir: projectPath });
- files = statusMatrix
- .filter(([, head, workdir]) => head !== workdir)
- .map(([file]) => file);
- }
-
- const fileDiffs: FileDiff[] = [];
-
- for (const file of files) {
- try {
- const diff = await generateFileDiff(file, compareFrom, compareTo);
- if (diff) {
- fileDiffs.push(diff);
- }
- } catch (error) {
- console.warn(`Failed to generate diff for ${file}:`, error);
- }
- }
-
- setDiffs(fileDiffs);
- if (fileDiffs.length > 0 && !selectedFile) {
- setSelectedFile(fileDiffs[0].filepath);
- }
- } catch (error) {
- console.error('Failed to load diffs:', error);
- toast({
- title: 'Failed to load diffs',
- description: error instanceof Error ? error.message : 'Unknown error',
- variant: 'destructive',
- });
- } finally {
- setIsLoading(false);
- }
- };
-
- const getChangedFilesBetweenRefs = async (from: string, to: string): Promise
=> {
+ const getChangedFilesBetweenRefs = useCallback(async (from: string, to: string): Promise => {
// Get list of files from both refs and find differences
const filesFrom = await git.listFiles({ dir: projectPath, ref: from });
const filesTo = await git.listFiles({ dir: projectPath, ref: to });
@@ -158,9 +92,9 @@ export function DiffViewer({ projectId, compareFrom = 'HEAD', compareTo, filepat
}
return Array.from(changedFiles);
- };
+ }, [git, projectPath]);
- const generateFileDiff = async (
+ const generateFileDiff = useCallback(async (
file: string,
from: string,
to?: string
@@ -210,7 +144,7 @@ export function DiffViewer({ projectId, compareFrom = 'HEAD', compareTo, filepat
console.error(`Error generating diff for ${file}:`, error);
return null;
}
- };
+ }, [git, projectPath, fs]);
const generateDiffHunks = (oldContent: string, newContent: string): DiffHunk[] => {
const oldLines = oldContent.split('\n');
@@ -342,28 +276,56 @@ export function DiffViewer({ projectId, compareFrom = 'HEAD', compareTo, filepat
return hunks;
};
- const getLanguageFromFilepath = (filepath: string): string => {
- const ext = filepath.split('.').pop()?.toLowerCase();
- const languageMap: Record = {
- ts: 'typescript',
- tsx: 'tsx',
- js: 'javascript',
- jsx: 'jsx',
- css: 'css',
- json: 'json',
- md: 'markdown',
- };
- return languageMap[ext || ''] || 'javascript';
- };
-
- const highlightCode = (code: string, filepath: string): string => {
+ const loadDiffs = useCallback(async () => {
+ setIsLoading(true);
try {
- const language = getLanguageFromFilepath(filepath);
- return Prism.highlight(code, Prism.languages[language] || Prism.languages.javascript, language);
- } catch {
- return code;
+ let files: string[] = [];
+
+ if (filepath) {
+ files = [filepath];
+ } else if (compareTo) {
+ // Compare two commits/branches
+ files = await getChangedFilesBetweenRefs(compareFrom, compareTo);
+ } else {
+ // Compare against working directory
+ const statusMatrix = await git.statusMatrix({ dir: projectPath });
+ files = statusMatrix
+ .filter(([, head, workdir]) => head !== workdir)
+ .map(([file]) => file);
+ }
+
+ const fileDiffs: FileDiff[] = [];
+
+ for (const file of files) {
+ try {
+ const diff = await generateFileDiff(file, compareFrom, compareTo);
+ if (diff) {
+ fileDiffs.push(diff);
+ }
+ } catch (error) {
+ console.warn(`Failed to generate diff for ${file}:`, error);
+ }
+ }
+
+ setDiffs(fileDiffs);
+ if (fileDiffs.length > 0 && !selectedFile) {
+ setSelectedFile(fileDiffs[0].filepath);
+ }
+ } catch (error) {
+ console.error('Failed to load diffs:', error);
+ toast({
+ title: 'Failed to load diffs',
+ description: error instanceof Error ? error.message : 'Unknown error',
+ variant: 'destructive',
+ });
+ } finally {
+ setIsLoading(false);
}
- };
+ }, [compareFrom, compareTo, filepath, git, projectPath, selectedFile, toast, getChangedFilesBetweenRefs, generateFileDiff]);
+
+ useEffect(() => {
+ loadDiffs();
+ }, [projectId, compareFrom, compareTo, filepath, loadDiffs]);
const copyHunkToClipboard = async (hunk: DiffHunk, index: number) => {
const hunkText = hunk.lines.map(line => line.content).join('\n');
@@ -504,7 +466,7 @@ export function DiffViewer({ projectId, compareFrom = 'HEAD', compareTo, filepat
);
}
-function SplitDiffView({ hunk, filepath }: { hunk: DiffHunk; filepath: string }) {
+function SplitDiffView({ hunk }: { hunk: DiffHunk; filepath: string }) {
return (
{/* Old (left) side */}
@@ -560,7 +522,7 @@ function SplitDiffView({ hunk, filepath }: { hunk: DiffHunk; filepath: string })
);
}
-function UnifiedDiffView({ hunk, filepath }: { hunk: DiffHunk; filepath: string }) {
+function UnifiedDiffView({ hunk }: { hunk: DiffHunk; filepath: string }) {
return (
{hunk.lines.map((line, i) => (
diff --git a/src/components/git/MergeDialog.tsx b/src/components/git/MergeDialog.tsx
index 5d80bd2a..466b32cc 100644
--- a/src/components/git/MergeDialog.tsx
+++ b/src/components/git/MergeDialog.tsx
@@ -1,4 +1,4 @@
-import { useState, useEffect } from 'react';
+import { useState, useEffect, useCallback } from 'react';
import { Button } from '@/components/ui/button';
import {
Dialog,
@@ -26,13 +26,11 @@ import {
AlertCircle,
CheckCircle,
GitCommit,
- FileIcon,
AlertTriangle,
} from 'lucide-react';
import { useGit } from '@/hooks/useGit';
import { useToast } from '@/hooks/useToast';
-import { useNavigate, useSearchParams } from 'react-router-dom';
-import { cn } from '@/lib/utils';
+import { useSearchParams } from 'react-router-dom';
interface MergeDialogProps {
projectId: string;
@@ -76,7 +74,6 @@ export function MergeDialog({
const { git } = useGit();
const { toast } = useToast();
- const navigate = useNavigate();
const [searchParams, setSearchParams] = useSearchParams();
const projectPath = `/projects/${projectId}`;
@@ -86,21 +83,7 @@ export function MergeDialog({
}
}, [open]);
- useEffect(() => {
- if (isOpen) {
- loadBranches();
- }
- }, [isOpen]);
-
- useEffect(() => {
- if (sourceBranch && currentBranch && sourceBranch !== currentBranch) {
- loadMergePreview();
- } else {
- setMergePreview(null);
- }
- }, [sourceBranch, currentBranch]);
-
- const loadBranches = async () => {
+ const loadBranches = useCallback(async () => {
setIsLoadingBranches(true);
try {
const localBranches = await git.listBranches({ dir: projectPath });
@@ -122,9 +105,9 @@ export function MergeDialog({
} finally {
setIsLoadingBranches(false);
}
- };
+ }, [git, projectPath, currentBranch, toast]);
- const loadMergePreview = async () => {
+ const loadMergePreview = useCallback(async () => {
if (!sourceBranch || !currentBranch) return;
setIsLoadingPreview(true);
@@ -200,7 +183,21 @@ export function MergeDialog({
} finally {
setIsLoadingPreview(false);
}
- };
+ }, [sourceBranch, currentBranch, git, projectPath]);
+
+ useEffect(() => {
+ if (isOpen) {
+ loadBranches();
+ }
+ }, [isOpen, loadBranches]);
+
+ useEffect(() => {
+ if (sourceBranch && currentBranch && sourceBranch !== currentBranch) {
+ loadMergePreview();
+ } else {
+ setMergePreview(null);
+ }
+ }, [sourceBranch, currentBranch, loadMergePreview]);
const performMerge = async () => {
if (!sourceBranch || !currentBranch) return;
diff --git a/src/components/git/PullRequestDialog.tsx b/src/components/git/PullRequestDialog.tsx
index 716e0da0..1997fb40 100644
--- a/src/components/git/PullRequestDialog.tsx
+++ b/src/components/git/PullRequestDialog.tsx
@@ -1,4 +1,4 @@
-import { useState, useEffect } from 'react';
+import { useState, useEffect, useCallback } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
@@ -33,7 +33,6 @@ import {
import { useGit } from '@/hooks/useGit';
import { useGitSettings } from '@/hooks/useGitSettings';
import { useToast } from '@/hooks/useToast';
-import { cn } from '@/lib/utils';
import { findCredentialsForRepo } from '@/lib/gitCredentials';
interface PullRequestDialogProps {
@@ -80,12 +79,38 @@ export function PullRequestDialog({
}
}, [open]);
+ const loadRemoteBranches = useCallback(async () => {
+ setIsLoadingBranches(true);
+ try {
+ const branches = await git.listBranches({
+ dir: projectPath,
+ remote: 'origin',
+ });
+ setRemoteBranches(branches);
+
+ // Set default target branch
+ if (branches.includes('main')) {
+ setTargetBranch('main');
+ } else if (branches.includes('master')) {
+ setTargetBranch('master');
+ } else if (branches.length > 0) {
+ setTargetBranch(branches[0]);
+ }
+ } catch (err) {
+ console.warn('Failed to load remote branches:', err);
+ // Set default branches if we can't fetch
+ setRemoteBranches(['main', 'master', 'develop']);
+ } finally {
+ setIsLoadingBranches(false);
+ }
+ }, [git, projectPath]);
+
useEffect(() => {
if (isOpen && remoteUrl) {
parseRemoteUrl(remoteUrl);
loadRemoteBranches();
}
- }, [isOpen, remoteUrl]);
+ }, [isOpen, remoteUrl, loadRemoteBranches]);
const parseRemoteUrl = (url: string) => {
setError(null);
@@ -161,32 +186,6 @@ export function PullRequestDialog({
}
};
- const loadRemoteBranches = async () => {
- setIsLoadingBranches(true);
- try {
- const branches = await git.listBranches({
- dir: projectPath,
- remote: 'origin',
- });
- setRemoteBranches(branches);
-
- // Set default target branch
- if (branches.includes('main')) {
- setTargetBranch('main');
- } else if (branches.includes('master')) {
- setTargetBranch('master');
- } else if (branches.length > 0) {
- setTargetBranch(branches[0]);
- }
- } catch (err) {
- console.warn('Failed to load remote branches:', err);
- // Set default branches if we can't fetch
- setRemoteBranches(['main', 'master', 'develop']);
- } finally {
- setIsLoadingBranches(false);
- }
- };
-
const createPullRequest = async () => {
if (!remoteInfo || !currentBranch) return;