Skip to content

Commit 352f30e

Browse files
committed
chore: add DirectoryMap types for JSON export
1 parent 2529ace commit 352f30e

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
validateGitHubUrl,
2424
validateGitLabUrl,
2525
} from "@/lib/repo-tree-utils"
26+
import { convertMapToJson } from "@/lib/utils"
2627
import type { TreeCustomizationOptions } from "@/types/tree-customization"
2728
import { saveAs } from "file-saver"
2829
import {
@@ -282,7 +283,7 @@ export default function RepoProjectStructure() {
282283
fileName = "directory-structure.txt"
283284
break
284285
case "json":
285-
content = JSON.stringify(Array.from(filteredStructureMap), null, 2)
286+
content = JSON.stringify(convertMapToJson(filteredStructureMap), null, 2)
286287
mimeType = "application/json;charset=utf-8"
287288
fileName = "directory-structure.json"
288289
break
@@ -336,8 +337,11 @@ export default function RepoProjectStructure() {
336337
>
337338
<CardHeader>
338339
<CardTitle className="text-3xl md:text-4xl lg:text-5xl font-semibold text-black dark:text-white flex items-center justify-center gap-2">
339-
ASCII<span className="text-blue-600">Tree</span>Generator
340+
Repo<span className="text-blue-600">Tree</span>Generator
340341
</CardTitle>
342+
<p className="text-center text-muted-foreground text-base">
343+
Generate and share clean ASCII trees of your GitHub & GitLab repositories.
344+
</p>
341345
</CardHeader>
342346
<CardContent>
343347
<div className="space-y-6">
@@ -471,8 +475,10 @@ export default function RepoProjectStructure() {
471475
</Button>
472476
) : (
473477
<Button
474-
onClick={copyToClipboard}
475-
className="p-2 text-white dark:text-gray-400 dark:hover:text-gray-900 bg-transparent border-none"
478+
onClick={structureMap.size === 0 ? () => {} : copyToClipboard}
479+
className={`p-2 text-white dark:text-gray-400 dark:hover:text-gray-900 bg-transparent border-none ${
480+
structureMap.size === 0 ? "cursor-not-allowed opacity-50" : ""
481+
}`}
476482
aria-label="Copy to clipboard"
477483
title="Copy to clipboard"
478484
>
@@ -508,8 +514,10 @@ export default function RepoProjectStructure() {
508514
</SelectContent>
509515
</Select>
510516
<Button
511-
onClick={handleDownload}
512-
className="bg-blue-600 hover:bg-blue-700 text-white"
517+
onClick={structureMap.size === 0 ? () => {} : handleDownload}
518+
className={`bg-blue-600 hover:bg-blue-700 text-white ${
519+
structureMap.size === 0 ? "cursor-not-allowed opacity-50" : ""
520+
}`}
513521
aria-label="Download file"
514522
>
515523
<Download aria-hidden="true" /> Download

src/lib/utils.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
1-
import { clsx, type ClassValue } from 'clsx';
2-
import { twMerge } from 'tailwind-merge';
1+
import { clsx, type ClassValue } from "clsx"
2+
import { twMerge } from "tailwind-merge"
3+
import { DirectoryMap } from "@/types/tree-customization"
34

45
export function cn(...inputs: ClassValue[]) {
5-
return twMerge(clsx(inputs));
6+
return twMerge(clsx(inputs))
7+
}
8+
9+
// JSON structure type for export
10+
export type DirectoryNode = {
11+
type: "file" | "folder"
12+
name: string
13+
children?: DirectoryNode[]
14+
}
15+
16+
// Convert Map structure to JSON-friendly format
17+
export const convertMapToJson = (map: DirectoryMap): DirectoryNode[] => {
18+
const result: DirectoryNode[] = []
19+
20+
for (const [name, value] of map.entries()) {
21+
if (value && typeof value === "object" && "type" in value && value.type === "file") {
22+
result.push({
23+
type: "file",
24+
name,
25+
})
26+
} else if (value instanceof Map) {
27+
result.push({
28+
type: "folder",
29+
name,
30+
children: convertMapToJson(value),
31+
})
32+
}
33+
}
34+
35+
return result
636
}

src/types/tree-customization.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ export interface TreeCustomizationOptions {
55
showRootDirectory: boolean;
66
showTrailingSlash: boolean;
77
}
8+
9+
export type DirectoryEntry = { type: "file" } | DirectoryMap
10+
export type DirectoryMap = Map<string, DirectoryEntry>

0 commit comments

Comments
 (0)