From f31ca89b4fb4dee79347ff3040b4dc1f92410c9d Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 14 Oct 2025 16:31:34 -0500 Subject: [PATCH] Fix linter issues --- package-lock.json | 22 +- src/components/GitDialog.tsx | 767 +++++++++++------------ src/components/git/BranchManager.tsx | 17 +- src/components/git/CompareView.tsx | 33 +- src/components/git/DiffViewer.tsx | 150 ++--- src/components/git/MergeDialog.tsx | 43 +- src/components/git/PullRequestDialog.tsx | 57 +- 7 files changed, 520 insertions(+), 569 deletions(-) diff --git a/package-lock.json b/package-lock.json index ed3449e2..856c6143 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3810,8 +3810,8 @@ "version": "1.26.5", "resolved": "https://registry.npmjs.org/@types/prismjs/-/prismjs-1.26.5.tgz", "integrity": "sha512-AUZTa7hQ2KY5L7AmtSiqxlhWxb4ina0yd8hNbl4TWuqnv/pFP0nDMb3YrfSBf4hJVGLh2YEIBfKaBW/9UEl6IQ==", - "license": "MIT", - "dev": true + "dev": true, + "license": "MIT" }, "node_modules/@types/prop-types": { "version": "15.7.14", @@ -9835,6 +9835,15 @@ "url": "https://github.com/sponsors/dcastil" } }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, "node_modules/string-width": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/string-width/-/string-width-5.1.2.tgz", @@ -9894,15 +9903,6 @@ "node": ">=8" } }, - "node_modules/string_decoder": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", - "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", - "license": "MIT", - "dependencies": { - "safe-buffer": "~5.2.0" - } - }, "node_modules/stringify-entities": { "version": "4.0.4", "resolved": "https://registry.npmjs.org/stringify-entities/-/stringify-entities-4.0.4.tgz", diff --git a/src/components/GitDialog.tsx b/src/components/GitDialog.tsx index 87da2910..1e4f4df9 100644 --- a/src/components/GitDialog.tsx +++ b/src/components/GitDialog.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect } from 'react'; +import { useState, useEffect, useCallback } from 'react'; import { useNavigate, useSearchParams } from 'react-router-dom'; import { Button } from '@/components/ui/button'; import { @@ -21,7 +21,6 @@ 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 { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { Select, SelectContent, @@ -104,14 +103,7 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog } }, [gitStatus?.remotes]); - // Load branches when dialog opens - useEffect(() => { - if (open && gitStatus?.isGitRepo) { - loadBranches(); - } - }, [open, gitStatus?.isGitRepo]); - - const loadBranches = async () => { + const loadBranches = useCallback(async () => { setIsLoadingBranches(true); try { const localBranches = await git.listBranches({ dir: projectPath }); @@ -121,7 +113,14 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog } finally { setIsLoadingBranches(false); } - }; + }, [git, projectPath]); + + // Load branches when dialog opens + useEffect(() => { + if (open && gitStatus?.isGitRepo) { + loadBranches(); + } + }, [open, gitStatus?.isGitRepo, loadBranches]); const handleSwitchBranch = async (branchName: string) => { setIsSwitchingBranch(true); @@ -706,390 +705,390 @@ export function GitDialog({ projectId, children, open, onOpenChange }: GitDialog -
- -
- {/* URL Configuration */} - {gitStatus?.isGitRepo && ( - - - Git URL - - -
-
-
+ {/* 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; + })() + )} + + + )} + + {/* Branch Selector */} + {gitStatus?.isGitRepo && gitStatus.currentBranch && ( + + + Current Branch + + +
+ + +
+
+
+ )} + + {/* Sync Status */} + {gitStatus?.isGitRepo && ( + + + Sync Status + + +
+ + + {syncStatus.text} + +
+ + {gitStatus.remotes.length > 0 && ( +
+ + + +
+ )} + + {/* Push to Nostr button when no remote is configured and user is logged in */} + {gitStatus.remotes.length === 0 && user && ( +
+ +
+ )} +
+
+ )} + + {/* Branch Operations */} + {gitStatus?.isGitRepo && gitStatus.currentBranch && ( + + + Branch Operations + + + refetchGitStatus()} + /> + {gitStatus.remotes.length > 0 && originUrl && ( + + )} + + + )} + + {/* Working Directory Changes */} + {gitStatus?.isGitRepo && gitStatus.changedFiles.length > 0 && ( + + + Working Directory Changes + + {gitStatus.changedFiles.length} file{gitStatus.changedFiles.length !== 1 ? 's' : ''} with changes + + + +
+ {gitStatus.changedFiles.map((file, index) => ( +
+ {getFileStatusIcon(file.status)} + + {file.filepath} + + + {file.status} + +
+ ))} +
+
+
+ )} + + {/* Clean Working Directory */} + {gitStatus?.isGitRepo && gitStatus.changedFiles.length === 0 && ( + + +
+ +

Working directory is clean

+

No uncommitted changes

+
+
+
+ )} +
+ +
- {/* Advanced Git Management Dialog */} - + {/* Advanced Git Management Dialog */} + - {/* Create Branch Dialog */} - - - - Create New Branch - + {/* Create Branch Dialog */} + + + + Create New Branch + Create a new branch from {gitStatus?.currentBranch || 'current HEAD'} - - -
-
- - 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;