Skip to content

Commit 159883e

Browse files
committed
Enhance AddonsDetails component with markdown fetching and loading state management
1 parent 9cf87c0 commit 159883e

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

src/context/AddonsContext.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { createContext, FC, ReactNode, useContext } from 'react'
22
import { useQuery } from '@tanstack/react-query'
33
import { AddonsDataState } from '@/types'
44

5+
// const jsonUrl =
6+
// 'https://raw.githubusercontent.com/PentSec/MaddonsManager/refs/heads/main/API/Maddons.json'
7+
58
const jsonUrl =
69
'https://raw.githubusercontent.com/PentSec/MaddonsManager/refs/heads/main/API/Maddons.json'
710

@@ -30,14 +33,7 @@ const fetchAddons = async (url: string) => {
3033
const logoUrl = `https://raw.githubusercontent.com/PentSec/MaddonsManager/refs/heads/main/API/Addons/${item.file_name}/${item.file_name}.webp`
3134
const zipUrl = `https://github.com/PentSec/MaddonsManager/raw/refs/heads/main/API/Addons/${item.file_name}/${item.file_name}.zip`
3235

33-
const [md] = await Promise.all([
34-
fetch(mdUrl).then((res) => {
35-
if (!res.ok) throw new Error(`Failed to fetch md for ${item.file_name}`)
36-
return res.text()
37-
})
38-
])
39-
40-
return { ...item, md, logo: logoUrl, zip: zipUrl }
36+
return { ...item, md: mdUrl, logo: logoUrl, zip: zipUrl }
4137
})
4238
)
4339
}

src/pages/Addons/AddonsDetails.tsx

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import {
99
Chip,
1010
Link,
1111
Image,
12-
Avatar
12+
Avatar,
13+
Spinner
1314
} from '@heroui/react'
1415
import ReactMarkdown from 'react-markdown'
1516
import { classIcon, roleIcon } from '@/utils/classIcon'
1617
import remarkGfm from 'remark-gfm'
1718
import rehypeRaw from 'rehype-raw'
1819
import hljs from 'highlight.js'
19-
import { useEffect } from 'react'
20+
import { useEffect, useState } from 'react'
2021
import { GroupIcon } from '@/assets/Icons'
2122
interface AddonsDetailsProps {
2223
addon: any
@@ -25,6 +26,34 @@ interface AddonsDetailsProps {
2526
}
2627

2728
const AddonsDetails = ({ addon, isOpen, onOpenChange }: AddonsDetailsProps) => {
29+
const [markdownContent, setMarkdownContent] = useState<string | null>(null)
30+
const [isLoading, setIsLoading] = useState(false)
31+
32+
useEffect(() => {
33+
if (isOpen && addon.md) {
34+
setIsLoading(true)
35+
fetch(addon.md)
36+
.then((response) => {
37+
if (!response.ok) {
38+
throw new Error('Failed to fetch markdown content.')
39+
}
40+
return response.text()
41+
})
42+
.then((data) => {
43+
setMarkdownContent(data)
44+
})
45+
.catch((error) => {
46+
console.error('Error fetching markdown content:', error)
47+
setMarkdownContent('Failed to load content.')
48+
})
49+
.finally(() => {
50+
setIsLoading(false)
51+
})
52+
} else {
53+
setMarkdownContent(null)
54+
}
55+
}, [isOpen, addon.md])
56+
2857
useEffect(() => {
2958
document.querySelectorAll('pre code').forEach((block) => {
3059
hljs.highlightElement(block as HTMLElement)
@@ -120,13 +149,17 @@ const AddonsDetails = ({ addon, isOpen, onOpenChange }: AddonsDetailsProps) => {
120149
<Divider className="my-2" />
121150
<h1 className="text-lg font-extrabold">Description</h1>
122151
<article className="markdown-body p-1 !bg-transparent">
123-
<ReactMarkdown
124-
remarkPlugins={[remarkGfm]}
125-
rehypePlugins={[rehypeRaw]}
126-
className="text-default-900 gap-4 w-auto p-4 mx-auto flex-col lg:flex-row rounded-md"
127-
>
128-
{addon.md}
129-
</ReactMarkdown>
152+
{isLoading ? (
153+
<Spinner className="items-center justify-center" />
154+
) : (
155+
<ReactMarkdown
156+
remarkPlugins={[remarkGfm]}
157+
rehypePlugins={[rehypeRaw]}
158+
className="text-default-900 gap-4 w-auto p-4 mx-auto flex-col lg:flex-row rounded-md"
159+
>
160+
{markdownContent || 'No content available.'}
161+
</ReactMarkdown>
162+
)}
130163
</article>
131164
</DrawerBody>
132165
<DrawerFooter>

src/pages/Home/Home.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ import PREVIEW_IMG from '/preview.webp'
77
import LK_IMG from '/lk.webp'
88
import CAT_IMG from '/cata.webp'
99
import PANDA_IMG from '/panda.webp'
10-
import { useDownloadM } from '@/hook/useDownloadM'
1110

1211
function Home() {
13-
const { downloadFile, downloadUrl, isPending, error } = useDownloadM()
14-
1512
return (
1613
<section className="flex flex-col items-center justify-center gap-4">
1714
<div className="justify-center text-center">

0 commit comments

Comments
 (0)