mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Gray gitignored files in FileTree
This commit is contained in:
4 files changed
+108
-9
No files matched your search
Generated
+43
-3
@@ -54,6 +54,7 @@
|
||||
"decimal.js": "^10.6.0",
|
||||
"embla-carousel-react": "^8.3.0",
|
||||
"esbuild-wasm": "^0.25.8",
|
||||
"ignore": "^7.0.5",
|
||||
"input-otp": "^1.2.4",
|
||||
"isomorphic-git": "^1.32.1",
|
||||
"jszip": "^3.10.1",
|
||||
@@ -888,6 +889,16 @@
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/@eslint/eslintrc/node_modules/ignore": {
|
||||
"version": "5.3.2",
|
||||
"resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz",
|
||||
"integrity": "sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 4"
|
||||
}
|
||||
},
|
||||
"node_modules/@eslint/js": {
|
||||
"version": "9.31.0",
|
||||
"resolved": "https://registry.npmjs.org/@eslint/js/-/js-9.31.0.tgz",
|
||||
@@ -3879,6 +3890,16 @@
|
||||
"typescript": ">=4.8.4 <5.9.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@typescript-eslint/eslint-plugin/node_modules/ignore": {
|
||||
"version": "5.3.2",
|
||||
"resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz",
|
||||
"integrity": "sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 4"
|
||||
}
|
||||
},
|
||||
"node_modules/@typescript-eslint/parser": {
|
||||
"version": "8.31.1",
|
||||
"resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-8.31.1.tgz",
|
||||
@@ -5662,6 +5683,16 @@
|
||||
"url": "https://opencollective.com/eslint"
|
||||
}
|
||||
},
|
||||
"node_modules/eslint/node_modules/ignore": {
|
||||
"version": "5.3.2",
|
||||
"resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz",
|
||||
"integrity": "sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 4"
|
||||
}
|
||||
},
|
||||
"node_modules/espree": {
|
||||
"version": "10.4.0",
|
||||
"resolved": "https://registry.npmjs.org/espree/-/espree-10.4.0.tgz",
|
||||
@@ -6795,9 +6826,9 @@
|
||||
"license": "BSD-3-Clause"
|
||||
},
|
||||
"node_modules/ignore": {
|
||||
"version": "5.3.2",
|
||||
"resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz",
|
||||
"integrity": "sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==",
|
||||
"version": "7.0.5",
|
||||
"resolved": "https://registry.npmjs.org/ignore/-/ignore-7.0.5.tgz",
|
||||
"integrity": "sha512-Hs59xBNfUIunMFgWAbGX5cq6893IbWg4KnrjbYwX3tx0ztorVgTDA6B2sxf8ejHJ4wz8BqGUMYlnzNBer5NvGg==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 4"
|
||||
@@ -7071,6 +7102,15 @@
|
||||
"node": ">=14.17"
|
||||
}
|
||||
},
|
||||
"node_modules/isomorphic-git/node_modules/ignore": {
|
||||
"version": "5.3.2",
|
||||
"resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz",
|
||||
"integrity": "sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">= 4"
|
||||
}
|
||||
},
|
||||
"node_modules/isomorphic-git/node_modules/pify": {
|
||||
"version": "4.0.1",
|
||||
"resolved": "https://registry.npmjs.org/pify/-/pify-4.0.1.tgz",
|
||||
|
||||
@@ -56,6 +56,7 @@
|
||||
"decimal.js": "^10.6.0",
|
||||
"embla-carousel-react": "^8.3.0",
|
||||
"esbuild-wasm": "^0.25.8",
|
||||
"ignore": "^7.0.5",
|
||||
"input-otp": "^1.2.4",
|
||||
"isomorphic-git": "^1.32.1",
|
||||
"jszip": "^3.10.1",
|
||||
|
||||
@@ -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 && (
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user