Skip to content

Commit d301313

Browse files
committed
Refactor Vmacros component to use custom useMacros hook and streamline repo context handling
1 parent 139000c commit d301313

File tree

4 files changed

+84
-59
lines changed

4 files changed

+84
-59
lines changed

src/context/MacroContext.tsx

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1+
import { RepoItem } from '@/types'
12
import { useQuery } from '@tanstack/react-query'
23
import { createContext, FC, ReactNode, useContext } from 'react'
34

45
const GITHUB_API_URL =
56
'https://api.github.com/repos/MaddonsManager/Vanilla-Macros/git/trees/master?recursive=1'
67

7-
interface RepoItem {
8-
path: string
9-
type: 'blob' | 'tree'
10-
mdContent: string | null
11-
}
12-
138
interface RepoContextValue {
149
repoTree: RepoItem[]
1510
error: string | null

src/hook/useVMacros.ts

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { useRepoContext } from '@/context/MacroContext'
2+
import { useEffect, useMemo, useState } from 'react'
3+
4+
export const useMacros = () => {
5+
const { repoTree, isPending, error } = useRepoContext()
6+
const [selectedFilePath, setSelectedFilePath] = useState<string | null>(null)
7+
const [fileContent, setFileContent] = useState<string>('')
8+
const [searchTerm, setSearchTerm] = useState<string>('')
9+
10+
useEffect(() => {
11+
if (selectedFilePath) {
12+
const file = repoTree.find((item) => item.path === selectedFilePath)
13+
setFileContent(file?.mdContent || 'Dont have content for this file.')
14+
}
15+
}, [selectedFilePath, repoTree])
16+
17+
const folders = useMemo(() => {
18+
return repoTree.reduce<
19+
Record<string, Record<string, { path: string; mdContent: string | null }[]>>
20+
>((acc, item) => {
21+
if (item.type === 'blob' && item.path.endsWith('.md')) {
22+
const parts = item.path.split('/')
23+
const folder = parts[0]
24+
const subFolder = parts.length > 2 ? parts[1] : ''
25+
26+
acc[folder] = acc[folder] || {}
27+
acc[folder][subFolder] = acc[folder][subFolder] || []
28+
acc[folder][subFolder].push({ path: item.path, mdContent: item.mdContent })
29+
}
30+
return acc
31+
}, {})
32+
}, [repoTree])
33+
34+
const filteredFolders = useMemo(() => {
35+
if (!searchTerm) return folders
36+
37+
const lowerSearch = searchTerm.toLowerCase()
38+
const result: typeof folders = {}
39+
40+
for (const [folder, subfolders] of Object.entries(folders)) {
41+
for (const [subfolder, files] of Object.entries(subfolders)) {
42+
const filteredFiles = files.filter(({ path }) =>
43+
path.toLowerCase().includes(lowerSearch)
44+
)
45+
46+
if (filteredFiles.length > 0) {
47+
if (!result[folder]) result[folder] = {}
48+
result[folder][subfolder] = filteredFiles
49+
}
50+
}
51+
}
52+
return result
53+
}, [folders, searchTerm])
54+
55+
return {
56+
isPending,
57+
error,
58+
selectedFilePath,
59+
setSelectedFilePath,
60+
fileContent,
61+
searchTerm,
62+
setSearchTerm,
63+
filteredFolders
64+
}
65+
}

src/pages/Vmacros/Vmacros.tsx

+12-53
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BookTextIcon, MenuIcon } from '@/assets/Icons'
22
import { Markdown } from '@/components'
3-
import { useRepoContext } from '@/context/MacroContext'
3+
import { useMacros } from '@/hook/useVMacros'
44
import {
55
Accordion,
66
AccordionItem,
@@ -11,7 +11,6 @@ import {
1111
Snippet,
1212
Spinner
1313
} from '@heroui/react'
14-
import { useEffect, useMemo, useState } from 'react'
1514

1615
const getBreadcrumbs = (path: string) =>
1716
path
@@ -23,55 +22,16 @@ const getBreadcrumbs = (path: string) =>
2322
))
2423

2524
export default function Vmacros() {
26-
const { repoTree, isPending, error } = useRepoContext()
27-
const [selectedFilePath, setSelectedFilePath] = useState<string | null>(null)
28-
const [fileContent, setFileContent] = useState<string>('')
29-
const [searchTerm, setSearchTerm] = useState<string>('')
30-
31-
useEffect(() => {
32-
if (selectedFilePath) {
33-
const file = repoTree.find((item) => item.path === selectedFilePath)
34-
setFileContent(file?.mdContent || 'No hay contenido disponible para este archivo.')
35-
}
36-
}, [selectedFilePath, repoTree])
37-
38-
const folders = useMemo(() => {
39-
return repoTree.reduce<
40-
Record<string, Record<string, { path: string; mdContent: string | null }[]>>
41-
>((acc, item) => {
42-
if (item.type === 'blob' && item.path.endsWith('.md')) {
43-
const parts = item.path.split('/')
44-
const folder = parts[0]
45-
const subFolder = parts.length > 2 ? parts[1] : ''
46-
47-
acc[folder] = acc[folder] || {}
48-
acc[folder][subFolder] = acc[folder][subFolder] || []
49-
acc[folder][subFolder].push({ path: item.path, mdContent: item.mdContent })
50-
}
51-
return acc
52-
}, {})
53-
}, [repoTree])
54-
55-
const filteredFolders = useMemo(() => {
56-
if (!searchTerm) return folders
57-
58-
const lowerSearch = searchTerm.toLowerCase()
59-
const result: typeof folders = {}
60-
61-
for (const [folder, subfolders] of Object.entries(folders)) {
62-
for (const [subfolder, files] of Object.entries(subfolders)) {
63-
const filteredFiles = files.filter(({ path }) =>
64-
path.toLowerCase().includes(lowerSearch)
65-
)
66-
67-
if (filteredFiles.length > 0) {
68-
if (!result[folder]) result[folder] = {}
69-
result[folder][subfolder] = filteredFiles
70-
}
71-
}
72-
}
73-
return result
74-
}, [folders, searchTerm])
25+
const {
26+
isPending,
27+
error,
28+
selectedFilePath,
29+
setSelectedFilePath,
30+
fileContent,
31+
searchTerm,
32+
setSearchTerm,
33+
filteredFolders
34+
} = useMacros()
7535

7636
return (
7737
<div className="flex h-screen">
@@ -167,8 +127,7 @@ export default function Vmacros() {
167127
</div>
168128
) : (
169129
<p className="">
170-
{repoTree.length} Vanilla Macros found, select a file to view its content or
171-
create a new one.
130+
Vanilla Macros found, select a file to view its content or create a new one.
172131
</p>
173132
)}
174133
</main>

src/types/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,9 @@ export type StringItems = {
7878
class: string[]
7979
content: string
8080
}
81+
82+
export type RepoItem = {
83+
path: string
84+
type: 'blob' | 'tree'
85+
mdContent: string | null
86+
}

0 commit comments

Comments
 (0)