Doc.tsx 4.2 KB

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