您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use client'
  2. import { useEffect, useMemo, 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. const Template = useMemo(() => {
  63. switch (locale) {
  64. case LanguagesSupported[1]:
  65. return <TemplateZh apiBaseUrl={apiBaseUrl} />
  66. case LanguagesSupported[7]:
  67. return <TemplateJa apiBaseUrl={apiBaseUrl} />
  68. default:
  69. return <TemplateEn apiBaseUrl={apiBaseUrl} />
  70. }
  71. }, [apiBaseUrl, locale])
  72. return (
  73. <div className="flex">
  74. <div className={`fixed right-20 top-32 z-10 transition-all ${isTocExpanded ? 'w-64' : 'w-10'}`}>
  75. {isTocExpanded
  76. ? (
  77. <nav className="toc max-h-[calc(100vh-150px)] w-full overflow-y-auto rounded-lg bg-gray-50 p-4 shadow-md">
  78. <div className="mb-4 flex items-center justify-between">
  79. <h3 className="text-lg font-semibold">{t('appApi.develop.toc')}</h3>
  80. <button
  81. onClick={() => setIsTocExpanded(false)}
  82. className="text-gray-500 hover:text-gray-700"
  83. >
  84. </button>
  85. </div>
  86. <ul className="space-y-2">
  87. {toc.map((item, index) => (
  88. <li key={index}>
  89. <a
  90. href={item.href}
  91. className="text-gray-600 transition-colors duration-200 hover:text-gray-900 hover:underline"
  92. onClick={e => handleTocClick(e, item)}
  93. >
  94. {item.text}
  95. </a>
  96. </li>
  97. ))}
  98. </ul>
  99. </nav>
  100. )
  101. : (
  102. <button
  103. onClick={() => setIsTocExpanded(true)}
  104. 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"
  105. >
  106. <RiListUnordered className="h-6 w-6" />
  107. </button>
  108. )}
  109. </div>
  110. <article className='prose-xl prose mx-1 rounded-t-xl bg-white px-4 pt-16 sm:mx-12'>
  111. {Template}
  112. </article>
  113. </div>
  114. )
  115. }
  116. export default Doc