doc.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 TemplateAdvancedChatEn from './template/template_advanced_chat.en.mdx'
  10. import TemplateAdvancedChatZh from './template/template_advanced_chat.zh.mdx'
  11. import TemplateAdvancedChatJa from './template/template_advanced_chat.ja.mdx'
  12. import TemplateWorkflowEn from './template/template_workflow.en.mdx'
  13. import TemplateWorkflowZh from './template/template_workflow.zh.mdx'
  14. import TemplateWorkflowJa from './template/template_workflow.ja.mdx'
  15. import TemplateChatEn from './template/template_chat.en.mdx'
  16. import TemplateChatZh from './template/template_chat.zh.mdx'
  17. import TemplateChatJa from './template/template_chat.ja.mdx'
  18. import I18n from '@/context/i18n'
  19. import { LanguagesSupported } from '@/i18n/language'
  20. type IDocProps = {
  21. appDetail: any
  22. }
  23. const Doc = ({ appDetail }: IDocProps) => {
  24. const { locale } = useContext(I18n)
  25. const { t } = useTranslation()
  26. const [toc, setToc] = useState<Array<{ href: string; text: string }>>([])
  27. const [isTocExpanded, setIsTocExpanded] = useState(false)
  28. const variables = appDetail?.model_config?.configs?.prompt_variables || []
  29. const inputs = variables.reduce((res: any, variable: any) => {
  30. res[variable.key] = variable.name || ''
  31. return res
  32. }, {})
  33. useEffect(() => {
  34. const mediaQuery = window.matchMedia('(min-width: 1280px)')
  35. setIsTocExpanded(mediaQuery.matches)
  36. }, [])
  37. useEffect(() => {
  38. const extractTOC = () => {
  39. const article = document.querySelector('article')
  40. if (article) {
  41. const headings = article.querySelectorAll('h2')
  42. const tocItems = Array.from(headings).map((heading) => {
  43. const anchor = heading.querySelector('a')
  44. if (anchor) {
  45. return {
  46. href: anchor.getAttribute('href') || '',
  47. text: anchor.textContent || '',
  48. }
  49. }
  50. return null
  51. }).filter((item): item is { href: string; text: string } => item !== null)
  52. setToc(tocItems)
  53. }
  54. }
  55. // Run after component has rendered
  56. setTimeout(extractTOC, 0)
  57. }, [appDetail, locale])
  58. const handleTocClick = (e: React.MouseEvent<HTMLAnchorElement>, item: { href: string; text: string }) => {
  59. e.preventDefault()
  60. const targetId = item.href.replace('#', '')
  61. const element = document.getElementById(targetId)
  62. if (element) {
  63. const scrollContainer = document.querySelector('.overflow-auto')
  64. if (scrollContainer) {
  65. const headerOffset = 80
  66. const elementTop = element.offsetTop - headerOffset
  67. scrollContainer.scrollTo({
  68. top: elementTop,
  69. behavior: 'smooth',
  70. })
  71. }
  72. }
  73. }
  74. return (
  75. <div className="flex">
  76. <div className={`fixed right-8 top-32 z-10 transition-all ${isTocExpanded ? 'w-64' : 'w-10'}`}>
  77. {isTocExpanded
  78. ? (
  79. <nav className="toc w-full bg-gray-50 p-4 rounded-lg shadow-md">
  80. <div className="flex justify-between items-center mb-4">
  81. <h3 className="text-lg font-semibold">{t('appApi.develop.toc')}</h3>
  82. <button
  83. onClick={() => setIsTocExpanded(false)}
  84. className="text-gray-500 hover:text-gray-700"
  85. >
  86. </button>
  87. </div>
  88. <ul className="space-y-2">
  89. {toc.map((item, index) => (
  90. <li key={index}>
  91. <a
  92. href={item.href}
  93. className="text-gray-600 hover:text-gray-900 hover:underline transition-colors duration-200"
  94. onClick={e => handleTocClick(e, item)}
  95. >
  96. {item.text}
  97. </a>
  98. </li>
  99. ))}
  100. </ul>
  101. </nav>
  102. )
  103. : (
  104. <button
  105. onClick={() => setIsTocExpanded(true)}
  106. 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"
  107. >
  108. <RiListUnordered className="w-6 h-6" />
  109. </button>
  110. )}
  111. </div>
  112. <article className="prose prose-xl" >
  113. {(appDetail?.mode === 'chat' || appDetail?.mode === 'agent-chat') && (
  114. (() => {
  115. switch (locale) {
  116. case LanguagesSupported[1]:
  117. return <TemplateChatZh appDetail={appDetail} variables={variables} inputs={inputs} />
  118. case LanguagesSupported[7]:
  119. return <TemplateChatJa appDetail={appDetail} variables={variables} inputs={inputs} />
  120. default:
  121. return <TemplateChatEn appDetail={appDetail} variables={variables} inputs={inputs} />
  122. }
  123. })()
  124. )}
  125. {appDetail?.mode === 'advanced-chat' && (
  126. (() => {
  127. switch (locale) {
  128. case LanguagesSupported[1]:
  129. return <TemplateAdvancedChatZh appDetail={appDetail} variables={variables} inputs={inputs} />
  130. case LanguagesSupported[7]:
  131. return <TemplateAdvancedChatJa appDetail={appDetail} variables={variables} inputs={inputs} />
  132. default:
  133. return <TemplateAdvancedChatEn appDetail={appDetail} variables={variables} inputs={inputs} />
  134. }
  135. })()
  136. )}
  137. {appDetail?.mode === 'workflow' && (
  138. (() => {
  139. switch (locale) {
  140. case LanguagesSupported[1]:
  141. return <TemplateWorkflowZh appDetail={appDetail} variables={variables} inputs={inputs} />
  142. case LanguagesSupported[7]:
  143. return <TemplateWorkflowJa appDetail={appDetail} variables={variables} inputs={inputs} />
  144. default:
  145. return <TemplateWorkflowEn appDetail={appDetail} variables={variables} inputs={inputs} />
  146. }
  147. })()
  148. )}
  149. {appDetail?.mode === 'completion' && (
  150. (() => {
  151. switch (locale) {
  152. case LanguagesSupported[1]:
  153. return <TemplateZh appDetail={appDetail} variables={variables} inputs={inputs} />
  154. case LanguagesSupported[7]:
  155. return <TemplateJa appDetail={appDetail} variables={variables} inputs={inputs} />
  156. default:
  157. return <TemplateEn appDetail={appDetail} variables={variables} inputs={inputs} />
  158. }
  159. })()
  160. )}
  161. </article>
  162. </div>
  163. )
  164. }
  165. export default Doc