Display git status indicator

This commit is contained in:
Alex Gleason committed 2025-08-28 18:44:49 -05:00
1 parent 7fe8236611
commit 35b2e8c53f
4 files changed
+196 -1

No files matched your search

@@ -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<typeof useGitStatus>);
const { container } = render(
<TestApp>
<GitStatusIndicator projectId="test-project" />
</TestApp>
);
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<typeof useGitStatus>);
const { container } = render(
<TestApp>
<GitStatusIndicator projectId="test-project" />
</TestApp>
);
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<typeof useGitStatus>);
const { container } = render(
<TestApp>
<GitStatusIndicator projectId="test-project" />
</TestApp>
);
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<typeof useGitStatus>);
const { container } = render(
<TestApp>
<GitStatusIndicator projectId="test-project" />
</TestApp>
);
const indicator = container.firstChild as HTMLElement;
expect(indicator.title).toBe('1 uncommitted change');
});
});
+27
View File
@@ -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 (
<div
className={cn(
"w-2 h-2 bg-yellow-500 rounded-full animate-pulse",
"shadow-sm shadow-yellow-500/50",
className
)}
title={`${gitStatus.changedFiles.length} uncommitted change${gitStatus.changedFiles.length !== 1 ? 's' : ''}`}
/>
);
}
+71
View File
@@ -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<GitStatus> => {
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
});
}
+8 -1
View File
@@ -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`);
<Menu className="h-4 w-4" />
</Button>
<div className="min-w-0 flex-1">
<h1 className="text-sm font-semibold truncate">{project.name}</h1>
<div className="flex items-center gap-2">
<h1 className="text-sm font-semibold truncate">{project.name}</h1>
<GitStatusIndicator projectId={project.id} />
</div>
</div>
</div>
@@ -368,6 +372,9 @@ BASE_DOMAIN=nostrdeploy.com`);
<h2 className="font-semibold text-lg truncate">
{project.name}
</h2>
<div className="ml-auto">
<GitStatusIndicator projectId={project.id} />
</div>
</div>
</div>