'use client' import { useEffect, useState } from 'react' import { useContext } from 'use-context-selector' import { useTranslation } from 'react-i18next' import { RiListUnordered } from '@remixicon/react' import TemplateEn from './template/template.en.mdx' import TemplateZh from './template/template.zh.mdx' import I18n from '@/context/i18n' import { LanguagesSupported } from '@/i18n/language' type DocProps = { apiBaseUrl: string } const Doc = ({ apiBaseUrl }: DocProps) => { const { locale } = useContext(I18n) const { t } = useTranslation() const [toc, setToc] = useState>([]) const [isTocExpanded, setIsTocExpanded] = useState(false) // Set initial TOC expanded state based on screen width useEffect(() => { const mediaQuery = window.matchMedia('(min-width: 1280px)') setIsTocExpanded(mediaQuery.matches) }, []) // Extract TOC from article content useEffect(() => { const extractTOC = () => { const article = document.querySelector('article') if (article) { const headings = article.querySelectorAll('h2') const tocItems = Array.from(headings).map((heading) => { const anchor = heading.querySelector('a') if (anchor) { return { href: anchor.getAttribute('href') || '', text: anchor.textContent || '', } } return null }).filter((item): item is { href: string; text: string } => item !== null) setToc(tocItems) } } setTimeout(extractTOC, 0) }, [locale]) // Handle TOC item click const handleTocClick = (e: React.MouseEvent, item: { href: string; text: string }) => { e.preventDefault() const targetId = item.href.replace('#', '') const element = document.getElementById(targetId) if (element) { const scrollContainer = document.querySelector('.scroll-container') if (scrollContainer) { const headerOffset = -40 const elementTop = element.offsetTop - headerOffset scrollContainer.scrollTo({ top: elementTop, behavior: 'smooth', }) } } } return (
{isTocExpanded ? ( ) : ( )}
{locale !== LanguagesSupported[1] ? : }
) } export default Doc