doc.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. return (
  59. <div className="flex">
  60. <div className={`fixed right-8 top-32 z-10 transition-all ${isTocExpanded ? 'w-64' : 'w-10'}`}>
  61. {isTocExpanded
  62. ? (
  63. <nav className="toc w-full bg-gray-50 p-4 rounded-lg shadow-md">
  64. <div className="flex justify-between items-center mb-4">
  65. <h3 className="text-lg font-semibold">{t('appApi.develop.toc')}</h3>
  66. <button
  67. onClick={() => setIsTocExpanded(false)}
  68. className="text-gray-500 hover:text-gray-700"
  69. >
  70. ✕
  71. </button>
  72. </div>
  73. <ul className="space-y-2">
  74. {toc.map((item, index) => (
  75. <li key={index}>
  76. <a
  77. href={item.href}
  78. className="text-gray-600 hover:text-gray-900 hover:underline transition-colors duration-200"
  79. >
  80. {item.text}
  81. </a>
  82. </li>
  83. ))}
  84. </ul>
  85. </nav>
  86. )
  87. : (
  88. <button
  89. onClick={() => setIsTocExpanded(true)}
  90. 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"
  91. >
  92. <RiListUnordered className="w-6 h-6" />
  93. </button>
  94. )}
  95. </div>
  96. <article className="prose prose-xl" >
  97. {(appDetail?.mode === 'chat' || appDetail?.mode === 'agent-chat') && (
  98. (() => {
  99. switch (locale) {
  100. case LanguagesSupported[1]:
  101. return <TemplateChatZh appDetail={appDetail} variables={variables} inputs={inputs} />
  102. case LanguagesSupported[7]:
  103. return <TemplateChatJa appDetail={appDetail} variables={variables} inputs={inputs} />
  104. default:
  105. return <TemplateChatEn appDetail={appDetail} variables={variables} inputs={inputs} />
  106. }
  107. })()
  108. )}
  109. {appDetail?.mode === 'advanced-chat' && (
  110. (() => {
  111. switch (locale) {
  112. case LanguagesSupported[1]:
  113. return <TemplateAdvancedChatZh appDetail={appDetail} variables={variables} inputs={inputs} />
  114. case LanguagesSupported[7]:
  115. return <TemplateAdvancedChatJa appDetail={appDetail} variables={variables} inputs={inputs} />
  116. default:
  117. return <TemplateAdvancedChatEn appDetail={appDetail} variables={variables} inputs={inputs} />
  118. }
  119. })()
  120. )}
  121. {appDetail?.mode === 'workflow' && (
  122. (() => {
  123. switch (locale) {
  124. case LanguagesSupported[1]:
  125. return <TemplateWorkflowZh appDetail={appDetail} variables={variables} inputs={inputs} />
  126. case LanguagesSupported[7]:
  127. return <TemplateWorkflowJa appDetail={appDetail} variables={variables} inputs={inputs} />
  128. default:
  129. return <TemplateWorkflowEn appDetail={appDetail} variables={variables} inputs={inputs} />
  130. }
  131. })()
  132. )}
  133. {appDetail?.mode === 'completion' && (
  134. (() => {
  135. switch (locale) {
  136. case LanguagesSupported[1]:
  137. return <TemplateZh appDetail={appDetail} variables={variables} inputs={inputs} />
  138. case LanguagesSupported[7]:
  139. return <TemplateJa appDetail={appDetail} variables={variables} inputs={inputs} />
  140. default:
  141. return <TemplateEn appDetail={appDetail} variables={variables} inputs={inputs} />
  142. }
  143. })()
  144. )}
  145. </article>
  146. </div>
  147. )
  148. }
  149. export default Doc