Skip to content

Commit b97756a

Browse files
committed
feat(repo-tree): update root folder label & add repo name extraction
1 parent 5549143 commit b97756a

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/app/generator/customization-options.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ const CustomizationOptions: React.FC<CustomizationOptionsProps> = ({ options, on
6767
onCheckedChange={(checked: boolean) => onChange({ showDescriptions: checked })}
6868
/>
6969
</div>
70-
{/* Root Directory */}
70+
{/* Root Folder Name */}
7171
<div className="flex items-center justify-between">
7272
<div className="flex flex-col">
73-
<Label htmlFor="show-root-directory">Root Directory</Label>
73+
<Label htmlFor="show-root-folder-name">Root Folder Name</Label>
7474
<span className="text-xs text-muted-foreground italic">ASCII VIEW ONLY</span>
7575
</div>
7676
<Switch
77-
id="show-root-directory"
77+
id="show-root-folder-name"
7878
checked={options.showRootDirectory}
7979
onCheckedChange={(checked: boolean) => onChange({ showRootDirectory: checked })}
8080
/>

src/app/generator/repo-tree-generator.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,10 @@ export default function RepoProjectStructure() {
290290
// For very large structures, limit rendering to prevent performance issues
291291
const mapSize = structureMap.size
292292
if (mapSize > PERFORMANCE_THRESHOLDS.LARGE_REPO_ENTRIES) {
293-
return buildStructureString(filteredStructureMap, "", customizationOptions, "", 20) // Limit depth
293+
return buildStructureString(filteredStructureMap, customizationOptions, repoUrl, "", "", 20) // Limit depth
294294
}
295-
return buildStructureString(filteredStructureMap, "", customizationOptions)
296-
}, [filteredStructureMap, customizationOptions, structureMap.size])
295+
return buildStructureString(filteredStructureMap, customizationOptions, repoUrl)
296+
}, [filteredStructureMap, customizationOptions, structureMap.size, repoUrl])
297297

298298
const copyToClipboard = useCallback(() => {
299299
navigator.clipboard.writeText(customizedStructure).then(() => {

src/lib/repo-tree-utils.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ export const validateGitLabUrl = (url: string): boolean => {
4949
return gitlabUrlPattern.test(url)
5050
}
5151

52+
// Extract repository name from URL
53+
export const extractRepoName = (repoUrl: string): string => {
54+
try {
55+
// Handle both GitHub and GitLab URLs
56+
const urlParts = repoUrl.replace(/\/$/, '').split('/')
57+
const repoName = urlParts[urlParts.length - 1]
58+
return repoName || 'Repository'
59+
} catch {
60+
return 'Repository'
61+
}
62+
}
63+
5264
// Validate repository size and structure
5365
export const validateRepositoryStructure = (tree: TreeItem[]): RepoValidationResult => {
5466
const result: RepoValidationResult = {
@@ -131,14 +143,14 @@ const getGitLabToken = (): string | undefined => {
131143
export const fetchProjectStructure = async (
132144
repoUrl: string,
133145
repoType: "github" | "gitlab"
134-
): Promise<{ tree: TreeItem[]; validation: RepoValidationResult }> => {
146+
): Promise<{ tree: TreeItem[]; validation: RepoValidationResult; repoUrl: string }> => {
135147
const tree = repoType === "github"
136148
? await fetchGitHubProjectStructure(repoUrl)
137149
: await fetchGitLabProjectStructure(repoUrl)
138150

139151
const validation = validateRepositoryStructure(tree)
140152

141-
return { tree, validation }
153+
return { tree, validation, repoUrl }
142154
}
143155

144156
const fetchGitHubProjectStructure = async (repoUrl: string): Promise<TreeItem[]> => {
@@ -277,8 +289,9 @@ export const generateStructure = (tree: TreeItem[]): DirectoryMap => {
277289
// Optimized structure building with chunking for large trees
278290
export const buildStructureString = (
279291
map: DirectoryMap,
292+
customizationOptions: TreeCustomizationOptions,
293+
repoUrl = "", // Repository URL parameter
280294
prefix = "",
281-
options: TreeCustomizationOptions,
282295
currentPath = "",
283296
maxDepth = 50 // Prevent infinite recursion
284297
): string => {
@@ -288,9 +301,11 @@ export const buildStructureString = (
288301

289302
let result = ""
290303

291-
// Add root directory indicator if enabled
292-
if (prefix === "" && options.showRootDirectory) {
293-
result += "./\n"
304+
// Add root folder name if enabled and at root level
305+
if (prefix === "" && customizationOptions.showRootDirectory && repoUrl) {
306+
const repoName = extractRepoName(repoUrl)
307+
const icon = customizationOptions.useIcons ? "📂 " : ""
308+
result += `${icon}${repoName}\n`
294309
}
295310

296311
const entries = Array.from(map.entries())
@@ -317,25 +332,25 @@ export const buildStructureString = (
317332
for (let index = 0; index < sortedEntries.length; index++) {
318333
const [key, value] = sortedEntries[index]
319334
const isLast = index === lastIndex
320-
const connector = getConnector(isLast, options.asciiStyle)
321-
const childPrefix = getChildPrefix(isLast, options.asciiStyle)
322-
const icon = options.useIcons ? getIcon(value instanceof Map) : ""
335+
const connector = getConnector(isLast, customizationOptions.asciiStyle)
336+
const childPrefix = getChildPrefix(isLast, customizationOptions.asciiStyle)
337+
const icon = customizationOptions.useIcons ? getIcon(value instanceof Map) : ""
323338
const isDirectory = value instanceof Map
324339

325340
// Build current file/directory path
326341
const itemPath = currentPath ? `${currentPath}/${key}` : key
327342

328343
// Add trailing slash for directories if enabled
329-
const displayName = (isDirectory && options.showTrailingSlash) ? `${key}/` : key
344+
const displayName = (isDirectory && customizationOptions.showTrailingSlash) ? `${key}/` : key
330345

331346
// Get description for this item (cached for performance)
332347
const description = getDescription(key, isDirectory, itemPath)
333-
const descriptionText = options.showDescriptions && description ? ` # ${description}` : ""
348+
const descriptionText = customizationOptions.showDescriptions && description ? ` # ${description}` : ""
334349

335350
result += `${prefix}${connector}${icon}${displayName}${descriptionText}\n`
336351

337352
if (isDirectory) {
338-
result += buildStructureString(value, `${prefix}${childPrefix}`, options, itemPath, maxDepth - 1)
353+
result += buildStructureString(value, customizationOptions, repoUrl, `${prefix}${childPrefix}`, itemPath, maxDepth - 1)
339354
}
340355
}
341356

0 commit comments

Comments
 (0)