You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

doc.tsx 7.1KB

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