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. </button>
  71. </div>
  72. <ul className="space-y-2">
  73. {toc.map((item, index) => (
  74. <li key={index}>
  75. <a
  76. href={item.href}
  77. className="text-gray-600 hover:text-gray-900 hover:underline transition-colors duration-200"
  78. >
  79. {item.text}
  80. </a>
  81. </li>
  82. ))}
  83. </ul>
  84. </nav>
  85. )
  86. : (
  87. <button
  88. onClick={() => setIsTocExpanded(true)}
  89. 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"
  90. >
  91. <RiListUnordered className="w-6 h-6" />
  92. </button>
  93. )}
  94. </div>
  95. <article className="prose prose-xl" >
  96. {(appDetail?.mode === 'chat' || appDetail?.mode === 'agent-chat') && (
  97. (() => {
  98. switch (locale) {
  99. case LanguagesSupported[1]:
  100. return <TemplateChatZh appDetail={appDetail} variables={variables} inputs={inputs} />
  101. case LanguagesSupported[7]:
  102. return <TemplateChatJa appDetail={appDetail} variables={variables} inputs={inputs} />
  103. default:
  104. return <TemplateChatEn appDetail={appDetail} variables={variables} inputs={inputs} />
  105. }
  106. })()
  107. )}
  108. {appDetail?.mode === 'advanced-chat' && (
  109. (() => {
  110. switch (locale) {
  111. case LanguagesSupported[1]:
  112. return <TemplateAdvancedChatZh appDetail={appDetail} variables={variables} inputs={inputs} />
  113. case LanguagesSupported[7]:
  114. return <TemplateAdvancedChatJa appDetail={appDetail} variables={variables} inputs={inputs} />
  115. default:
  116. return <TemplateAdvancedChatEn appDetail={appDetail} variables={variables} inputs={inputs} />
  117. }
  118. })()
  119. )}
  120. {appDetail?.mode === 'workflow' && (
  121. (() => {
  122. switch (locale) {
  123. case LanguagesSupported[1]:
  124. return <TemplateWorkflowZh appDetail={appDetail} variables={variables} inputs={inputs} />
  125. case LanguagesSupported[7]:
  126. return <TemplateWorkflowJa appDetail={appDetail} variables={variables} inputs={inputs} />
  127. default:
  128. return <TemplateWorkflowEn appDetail={appDetail} variables={variables} inputs={inputs} />
  129. }
  130. })()
  131. )}
  132. {appDetail?.mode === 'completion' && (
  133. (() => {
  134. switch (locale) {
  135. case LanguagesSupported[1]:
  136. return <TemplateZh appDetail={appDetail} variables={variables} inputs={inputs} />
  137. case LanguagesSupported[7]:
  138. return <TemplateJa appDetail={appDetail} variables={variables} inputs={inputs} />
  139. default:
  140. return <TemplateEn appDetail={appDetail} variables={variables} inputs={inputs} />
  141. }
  142. })()
  143. )}
  144. </article>
  145. </div>
  146. )
  147. }
  148. export default Doc