diff --git a/src/components/GitStatusIndicator.test.tsx b/src/components/GitStatusIndicator.test.tsx new file mode 100644 index 00000000..cc94ced3 --- /dev/null +++ b/src/components/GitStatusIndicator.test.tsx @@ -0,0 +1,90 @@ +import { describe, it, expect, vi } from 'vitest'; +import { render } from '@testing-library/react'; +import { TestApp } from '@/test/TestApp'; +import { GitStatusIndicator } from './GitStatusIndicator'; + +// Mock the useGitStatus hook +vi.mock('@/hooks/useGitStatus', () => ({ + useGitStatus: vi.fn(), +})); + +import { useGitStatus } from '@/hooks/useGitStatus'; + +describe('GitStatusIndicator', () => { + it('should not render when not in a git repo', () => { + vi.mocked(useGitStatus).mockReturnValue({ + data: { + hasUncommittedChanges: false, + changedFiles: [], + isGitRepo: false, + }, + } as unknown as ReturnType); + + const { container } = render( + + + + ); + + expect(container.firstChild).toBeNull(); + }); + + it('should not render when no uncommitted changes', () => { + vi.mocked(useGitStatus).mockReturnValue({ + data: { + hasUncommittedChanges: false, + changedFiles: [], + isGitRepo: true, + }, + } as unknown as ReturnType); + + const { container } = render( + + + + ); + + expect(container.firstChild).toBeNull(); + }); + + it('should render yellow dot when there are uncommitted changes', () => { + vi.mocked(useGitStatus).mockReturnValue({ + data: { + hasUncommittedChanges: true, + changedFiles: ['file1.txt', 'file2.txt'], + isGitRepo: true, + }, + } as unknown as ReturnType); + + const { container } = render( + + + + ); + + const indicator = container.firstChild as HTMLElement; + expect(indicator).toBeTruthy(); + expect(indicator.className).toContain('bg-yellow-500'); + expect(indicator.className).toContain('rounded-full'); + expect(indicator.title).toBe('2 uncommitted changes'); + }); + + it('should handle singular file count in tooltip', () => { + vi.mocked(useGitStatus).mockReturnValue({ + data: { + hasUncommittedChanges: true, + changedFiles: ['file1.txt'], + isGitRepo: true, + }, + } as unknown as ReturnType); + + const { container } = render( + + + + ); + + const indicator = container.firstChild as HTMLElement; + expect(indicator.title).toBe('1 uncommitted change'); + }); +}); \ No newline at end of file diff --git a/src/components/GitStatusIndicator.tsx b/src/components/GitStatusIndicator.tsx new file mode 100644 index 00000000..e7456af7 --- /dev/null +++ b/src/components/GitStatusIndicator.tsx @@ -0,0 +1,27 @@ +import { useGitStatus } from '@/hooks/useGitStatus'; +import { cn } from '@/lib/utils'; + +interface GitStatusIndicatorProps { + projectId: string | null; + className?: string; +} + +export function GitStatusIndicator({ projectId, className }: GitStatusIndicatorProps) { + const { data: gitStatus } = useGitStatus(projectId); + + // Only show the indicator if we're in a git repo and have uncommitted changes + if (!gitStatus?.isGitRepo || !gitStatus?.hasUncommittedChanges) { + return null; + } + + return ( +
+ ); +} \ No newline at end of file diff --git a/src/hooks/useGitStatus.ts b/src/hooks/useGitStatus.ts new file mode 100644 index 00000000..cb8f5227 --- /dev/null +++ b/src/hooks/useGitStatus.ts @@ -0,0 +1,71 @@ +import { useQuery } from '@tanstack/react-query'; +import git from 'isomorphic-git'; +import { useFS } from './useFS'; + +interface GitStatus { + hasUncommittedChanges: boolean; + changedFiles: string[]; + isGitRepo: boolean; +} + +export function useGitStatus(projectId: string | null) { + const { fs } = useFS(); + + return useQuery({ + queryKey: ['git-status', projectId], + queryFn: async (): Promise => { + if (!projectId) { + return { + hasUncommittedChanges: false, + changedFiles: [], + isGitRepo: false, + }; + } + + try { + const cwd = `/projects/${projectId}`; + + // Check if we're in a git repository + try { + await fs.stat(`${cwd}/.git`); + } catch { + return { + hasUncommittedChanges: false, + changedFiles: [], + isGitRepo: false, + }; + } + + // Get git status to see what files have changed + const statusMatrix = await git.statusMatrix({ + fs, + dir: cwd, + }); + + // Filter for files that have changes (not committed) + const changedFiles = statusMatrix.filter(([_filepath, headStatus, workdirStatus, stageStatus]) => { + // File has changes if: + // - workdir differs from head (modified/new/deleted) + // - stage differs from workdir (unstaged changes) + return headStatus !== workdirStatus || workdirStatus !== stageStatus; + }); + + return { + hasUncommittedChanges: changedFiles.length > 0, + changedFiles: changedFiles.map(([filepath]) => filepath), + isGitRepo: true, + }; + } catch (error) { + console.error('Error checking git status:', error); + return { + hasUncommittedChanges: false, + changedFiles: [], + isGitRepo: false, + }; + } + }, + enabled: !!projectId, + refetchInterval: 5000, // Check every 5 seconds + staleTime: 2000, // Consider data stale after 2 seconds + }); +} \ No newline at end of file diff --git a/src/pages/ProjectView.tsx b/src/pages/ProjectView.tsx index 69d05074..d16ddf28 100644 --- a/src/pages/ProjectView.tsx +++ b/src/pages/ProjectView.tsx @@ -10,6 +10,7 @@ import { Button } from '@/components/ui/button'; import { ArrowLeft, MessageSquare, Eye, Code, Menu, Columns2 } from 'lucide-react'; import { useNavigate } from 'react-router-dom'; import { ActionsMenu } from '@/components/ActionsMenu'; +import { GitStatusIndicator } from '@/components/GitStatusIndicator'; import { useIsMobile } from '@/hooks/useIsMobile'; import { useFS } from '@/hooks/useFS'; import { useJSRuntime } from '@/hooks/useJSRuntime'; @@ -219,7 +220,10 @@ BASE_DOMAIN=nostrdeploy.com`);
-

{project.name}

+
+

{project.name}

+ +
@@ -368,6 +372,9 @@ BASE_DOMAIN=nostrdeploy.com`);

{project.name}

+
+ +