Doc.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use client'
  2. import { useEffect, useState } from 'react'
  3. import { useContext } from 'use-context-selector'
  4. import { useTranslation } from 'react-i18next'
  5. import { RiListUnordered } from '@remixicon/react'
  6. import TemplateEn from './template/template.en.mdx'
  7. import TemplateZh from './template/template.zh.mdx'
  8. import I18n from '@/context/i18n'
  9. import { LanguagesSupported } from '@/i18n/language'
  10. type DocProps = {
  11. apiBaseUrl: string
  12. }
  13. const Doc = ({ apiBaseUrl }: DocProps) => {
  14. const { locale } = useContext(I18n)
  15. const { t } = useTranslation()
  16. const [toc, setToc] = useState<Array<{ href: string; text: string }>>([])
  17. const [isTocExpanded, setIsTocExpanded] = useState(false)
  18. // Set initial TOC expanded state based on screen width
  19. useEffect(() => {
  20. const mediaQuery = window.matchMedia('(min-width: 1280px)')
  21. setIsTocExpanded(mediaQuery.matches)
  22. }, [])
  23. // Extract TOC from article content
  24. useEffect(() => {
  25. const extractTOC = () => {
  26. const article = document.querySelector('article')
  27. if (article) {
  28. const headings = article.querySelectorAll('h2')
  29. const tocItems = Array.from(headings).map((heading) => {
  30. const anchor = heading.querySelector('a')
  31. if (anchor) {
  32. return {
  33. href: anchor.getAttribute('href') || '',
  34. text: anchor.textContent || '',
  35. }
  36. }
  37. return null
  38. }).filter((item): item is { href: string; text: string } => item !== null)
  39. setToc(tocItems)
  40. }
  41. }
  42. setTimeout(extractTOC, 0)
  43. }, [locale])
  44. // Handle TOC item click
  45. const handleTocClick = (e: React.MouseEvent<HTMLAnchorElement>, item: { href: string; text: string }) => {
  46. e.preventDefault()
  47. const targetId = item.href.replace('#', '')
  48. const element = document.getElementById(targetId)
  49. if (element) {
  50. const scrollContainer = document.querySelector('.scroll-container')
  51. if (scrollContainer) {
  52. const headerOffset = -40
  53. const elementTop = element.offsetTop - headerOffset
  54. scrollContainer.scrollTo({
  55. top: elementTop,
  56. behavior: 'smooth',
  57. })
  58. }
  59. }
  60. }
  61. return (
  62. <div className="flex">
  63. <div className={`fixed right-16 top-32 z-10 transition-all ${isTocExpanded ? 'w-64' : 'w-10'}`}>
  64. {isTocExpanded
  65. ? (
  66. <nav className="toc w-full bg-gray-50 p-4 rounded-lg shadow-md max-h-[calc(100vh-150px)] overflow-y-auto">
  67. <div className="flex justify-between items-center mb-4">
  68. <h3 className="text-lg font-semibold">{t('appApi.develop.toc')}</h3>
  69. <button
  70. onClick={() => setIsTocExpanded(false)}
  71. className="text-gray-500 hover:text-gray-700"
  72. >
  73. </button>
  74. </div>
  75. <ul className="space-y-2">
  76. {toc.map((item, index) => (
  77. <li key={index}>
  78. <a
  79. href={item.href}
  80. className="text-gray-600 hover:text-gray-900 hover:underline transition-colors duration-200"
  81. onClick={e => handleTocClick(e, item)}
  82. >
  83. {item.text}
  84. </a>
  85. </li>
  86. ))}
  87. </ul>
  88. </nav>
  89. )
  90. : (
  91. <button
  92. onClick={() => setIsTocExpanded(true)}
  93. className="w-10 h-10 bg-gray-50 rounded-full shadow-md flex items-center justify-center hover:bg-gray-100 transition-colors duration-200"
  94. >
  95. <RiListUnordered className="w-6 h-6" />
  96. </button>
  97. )}
  98. </div>
  99. <article className='mx-1 px-4 sm:mx-12 pt-16 bg-white rounded-t-xl prose prose-xl'>
  100. {locale !== LanguagesSupported[1]
  101. ? <TemplateEn apiBaseUrl={apiBaseUrl} />
  102. : <TemplateZh apiBaseUrl={apiBaseUrl} />
  103. }
  104. </article>
  105. </div>
  106. )
  107. }
  108. export default Doc