Skip to content

Commit 187d7cb

Browse files
committed
fix: Replace �ny with specific types for directory structure map
1 parent 67ab794 commit 187d7cb

File tree

1 file changed

+55
-34
lines changed

1 file changed

+55
-34
lines changed

src/app/repo-structure.tsx

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,92 +8,111 @@ import { Input } from "@/components/ui/input"
88
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
99
import { Loader2, Download, Github } from 'lucide-react'
1010

11+
// Define types for TreeItem and ValidationError
1112
interface TreeItem {
1213
path: string
1314
type: string
1415
}
1516

1617
interface ValidationError {
17-
message: string;
18-
isError: boolean;
18+
message: string
19+
isError: boolean
1920
}
2021

22+
// Define a recursive type for the directory map structure
23+
type DirectoryMap = Map<string, DirectoryMap | { type: 'file' }>
24+
2125
export default function GitHubProjectStructure() {
2226
const [repoUrl, setRepoUrl] = useState('')
2327
const [structure, setStructure] = useState('')
2428
const [loading, setLoading] = useState(false)
2529
const [validation, setValidation] = useState<ValidationError>({ message: '', isError: false })
2630

31+
// Function to validate GitHub repository URL
2732
const validateUrl = (url: string): boolean => {
28-
const githubUrlPattern = /^https?:\/\/github\.com\/[\w-]+\/[\w.-]+\/?$/;
29-
return githubUrlPattern.test(url);
33+
const githubUrlPattern = /^https?:\/\/github\.com\/[\w-]+\/[\w.-]+\/?$/
34+
return githubUrlPattern.test(url)
3035
}
3136

37+
// Handle URL input change and validation
3238
const handleUrlChange = (e: React.ChangeEvent<HTMLInputElement>) => {
33-
const url = e.target.value;
34-
setRepoUrl(url);
35-
39+
const url = e.target.value
40+
setRepoUrl(url)
41+
3642
if (!url) {
37-
setValidation({ message: 'GitHub URL is required', isError: true });
43+
setValidation({ message: 'GitHub URL is required', isError: true })
3844
} else if (!validateUrl(url)) {
39-
setValidation({ message: 'Enter a valid GitHub URL', isError: true });
45+
setValidation({ message: 'Enter a valid GitHub URL', isError: true })
4046
} else {
41-
setValidation({ message: '', isError: false });
47+
setValidation({ message: '', isError: false })
4248
}
4349
}
4450

51+
// Function to fetch project structure from GitHub
4552
const fetchProjectStructure = async () => {
4653
if (!repoUrl) {
47-
setValidation({ message: 'GitHub URL is required', isError: true });
48-
return;
54+
setValidation({ message: 'GitHub URL is required', isError: true })
55+
return
4956
}
5057

5158
if (!validateUrl(repoUrl)) {
52-
setValidation({ message: 'Enter a valid GitHub URL', isError: true });
53-
return;
59+
setValidation({ message: 'Enter a valid GitHub URL', isError: true })
60+
return
5461
}
5562

56-
setLoading(true);
63+
setLoading(true)
5764
try {
58-
const [owner, repo] = repoUrl.split('/').slice(-2);
65+
const [owner, repo] = repoUrl.split('/').slice(-2)
5966

60-
// Fetch the default branch name (either 'main', 'master', or another branch)
61-
const { data: repoData } = await new Octokit().rest.repos.get({ owner, repo });
62-
const defaultBranch = repoData.default_branch;
67+
// Fetch the default branch name
68+
const { data: repoData } = await new Octokit().rest.repos.get({ owner, repo })
69+
const defaultBranch = repoData.default_branch
6370

6471
// Fetch the project structure from the default branch
65-
const { data } = await new Octokit().rest.git.getTree({ owner, repo, tree_sha: defaultBranch, recursive: 'true' });
66-
setStructure(generateStructure(data.tree as TreeItem[]));
67-
setValidation({ message: '', isError: false });
72+
const { data } = await new Octokit().rest.git.getTree({
73+
owner,
74+
repo,
75+
tree_sha: defaultBranch,
76+
recursive: 'true',
77+
})
78+
79+
setStructure(generateStructure(data.tree as TreeItem[]))
80+
setValidation({ message: '', isError: false })
6881
} catch (err) {
69-
console.error(err);
70-
setStructure('Error fetching repository structure. Please check the URL and try again.');
71-
setValidation({ message: 'Failed to fetch repository structure', isError: true });
82+
console.error(err)
83+
setStructure('Error fetching repository structure. Please check the URL and try again.')
84+
setValidation({ message: 'Failed to fetch repository structure', isError: true })
7285
}
73-
setLoading(false);
86+
setLoading(false)
7487
}
7588

89+
// Generate the ASCII directory structure
7690
const generateStructure = (tree: TreeItem[]): string => {
77-
const structureMap = new Map<string, Map<string, any>>()
91+
const structureMap: DirectoryMap = new Map<string, DirectoryMap | { type: 'file' }>()
7892
tree.forEach((item: TreeItem) => {
7993
const parts = item.path.split('/')
80-
let currentLevel: Map<string, any> = structureMap
94+
let currentLevel: DirectoryMap | { type: 'file' } = structureMap
95+
8196
parts.forEach((part: string, index: number) => {
97+
if (!(currentLevel instanceof Map)) return
8298
if (!currentLevel.has(part)) currentLevel.set(part, new Map())
83-
if (index === parts.length - 1 && item.type === 'blob') currentLevel.get(part)!.set('type', 'file')
99+
if (index === parts.length - 1 && item.type === 'blob') {
100+
currentLevel.set(part, { type: 'file' })
101+
}
84102
currentLevel = currentLevel.get(part)!
85103
})
86104
})
87105
return buildStructureString(structureMap)
88106
}
89107

90-
const buildStructureString = (map: Map<string, any>, prefix = ''): string => {
108+
// Build the structure string for display
109+
const buildStructureString = (map: DirectoryMap, prefix = ''): string => {
91110
let result = ''
92111
for (const [key, value] of map.entries()) {
93112
if (key === 'type') continue
94113
result += `${prefix}├── ${key}\n`
95-
if (value.size > 0 && value.get('type') !== 'file') {
96-
result += buildStructureString(value, prefix + '')
114+
if (value instanceof Map) {
115+
result += buildStructureString(value, `${prefix}`)
97116
}
98117
}
99118
return result
@@ -102,7 +121,9 @@ export default function GitHubProjectStructure() {
102121
return (
103122
<Card className="w-full max-w-4xl mx-auto p-6 md:p-8" id="generator">
104123
<CardHeader>
105-
<CardTitle className="text-xl font-bold text-center">Generate Your Repo<span className="text-blue-600 font-bold">Tree</span></CardTitle>
124+
<CardTitle className="text-xl font-bold text-center">
125+
Generate Your Repo<span className="text-blue-600 font-bold">Tree</span>
126+
</CardTitle>
106127
<p className="text-center text-gray-600">
107128
Convert GitHub repository structure into a clean ASCII format, perfect for documentation and sharing.
108129
</p>
@@ -158,5 +179,5 @@ export default function GitHubProjectStructure() {
158179
</div>
159180
</CardContent>
160181
</Card>
161-
);
182+
)
162183
}

0 commit comments

Comments
 (0)