Gray gitignored files in FileTree

This commit is contained in:
Alex Gleason committed 2025-09-11 20:39:26 -05:00
1 parent 24b8b5bd05
commit 943a4e592b
4 files changed
+108 -9

No files matched your search

+23 -6
View File
@@ -4,6 +4,7 @@ import { useFS } from '@/hooks/useFS';
import { useGitStatus } from '@/hooks/useGitStatus';
import { ChevronRight, ChevronDown, File, Folder } from 'lucide-react';
import { cn } from '@/lib/utils';
import { createGitignoreFilter, normalizePathForGitignore } from '@/lib/gitignore';
interface FileTreeProps {
projectId: string;
@@ -22,6 +23,10 @@ interface FileNode {
export function FileTree({ projectId, onFileSelect, selectedFile }: FileTreeProps) {
const [tree, setTree] = useState<FileNode[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [gitignoreFilter, setGitignoreFilter] = useState<{
isIgnored: (path: string) => boolean;
shouldShow: (path: string) => boolean;
} | null>(null);
const { fs } = useFS();
const projectsManager = useProjectsManager();
const { data: gitStatus } = useGitStatus(projectId);
@@ -115,6 +120,11 @@ export function FileTree({ projectId, onFileSelect, selectedFile }: FileTreeProp
const loadFileTree = useCallback(async () => {
setIsLoading(true);
try {
// Load gitignore filter
const projectPath = `${projectsManager['dir']}/${projectId}`;
const filter = await createGitignoreFilter(fs, projectPath);
setGitignoreFilter(filter);
const structure = await buildFileTree(projectId, '');
setTree(structure);
} catch (_error) {
@@ -122,7 +132,7 @@ export function FileTree({ projectId, onFileSelect, selectedFile }: FileTreeProp
} finally {
setIsLoading(false);
}
}, [projectId, buildFileTree]);
}, [projectId, buildFileTree, fs, projectsManager]);
useEffect(() => {
loadFileTree();
@@ -160,9 +170,16 @@ export function FileTree({ projectId, onFileSelect, selectedFile }: FileTreeProp
const gitDirStatus = node.type === 'directory' ? getDirectoryGitStatus(node.path, node.children || []) : null;
const gitStatus = gitFileStatus || gitDirStatus;
// Check if this file/directory is gitignored
const normalizedPath = normalizePathForGitignore(node.path);
const isGitignored = gitignoreFilter?.isIgnored(normalizedPath) ?? false;
// Get styling classes for git status
const gitStatusClasses = getGitStatusClasses(gitStatus);
// Apply muted styling for gitignored files
const gitignoreClasses = isGitignored ? 'text-muted-foreground' : '';
return (
<div key={node.path}>
<div
@@ -177,19 +194,19 @@ export function FileTree({ projectId, onFileSelect, selectedFile }: FileTreeProp
{node.type === 'directory' ? (
<>
{node.isOpen ? (
<ChevronDown className="h-4 w-4 flex-shrink-0" />
<ChevronDown className={cn("h-4 w-4 flex-shrink-0", gitignoreClasses)} />
) : (
<ChevronRight className="h-4 w-4 flex-shrink-0" />
<ChevronRight className={cn("h-4 w-4 flex-shrink-0", gitignoreClasses)} />
)}
<Folder className="h-4 w-4 text-blue-500 flex-shrink-0" />
<Folder className={cn("h-4 w-4 text-blue-500 flex-shrink-0", gitignoreClasses)} />
</>
) : (
<>
<div className="w-4" /> {/* Spacer for alignment */}
<File className="h-4 w-4 text-gray-500 flex-shrink-0" />
<File className={cn("h-4 w-4 text-gray-500 flex-shrink-0", gitignoreClasses)} />
</>
)}
<span className={cn("text-sm truncate", gitStatusClasses)}>{node.name}</span>
<span className={cn("text-sm truncate", gitStatusClasses, gitignoreClasses)}>{node.name}</span>
</div>
{node.type === 'directory' && node.isOpen && node.children && (
+41
View File
@@ -0,0 +1,41 @@
import ignore from 'ignore';
import type { JSRuntimeFS } from './JSRuntime';
/**
* Creates a gitignore filter function that can check if files should be ignored
*/
export async function createGitignoreFilter(
fs: JSRuntimeFS,
projectPath: string
): Promise<{
isIgnored: (path: string) => boolean;
shouldShow: (path: string) => boolean;
}> {
const ig = ignore();
// Always ignore .git directory
ig.add('.git');
try {
// Try to read .gitignore file from the project root
const gitignorePath = `${projectPath}/.gitignore`;
const gitignoreContent = await fs.readFile(gitignorePath, 'utf8');
ig.add(gitignoreContent);
} catch {
// If there's an error reading .gitignore, continue without it
// We'll still show all files, just won't have gitignore patterns
}
return {
isIgnored: (path: string) => ig.ignores(path),
shouldShow: (path: string) => !ig.ignores(path) || path === '.gitignore', // Always show .gitignore itself
};
}
/**
* Normalizes a file path for gitignore checking by removing leading slashes
*/
export function normalizePathForGitignore(path: string): string {
// Remove leading slash if present
return path.startsWith('/') ? path.slice(1) : path;
}