doc.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 TemplateAdvancedChatEn from './template/template_advanced_chat.en.mdx'
  9. import TemplateAdvancedChatZh from './template/template_advanced_chat.zh.mdx'
  10. import TemplateWorkflowEn from './template/template_workflow.en.mdx'
  11. import TemplateWorkflowZh from './template/template_workflow.zh.mdx'
  12. import TemplateChatEn from './template/template_chat.en.mdx'
  13. import TemplateChatZh from './template/template_chat.zh.mdx'
  14. import I18n from '@/context/i18n'
  15. import { LanguagesSupported } from '@/i18n/language'
  16. type IDocProps = {
  17. appDetail: any
  18. }
  19. const Doc = ({ appDetail }: IDocProps) => {
  20. const { locale } = useContext(I18n)
  21. const { t } = useTranslation()
  22. const [toc, setToc] = useState<Array<{ href: string; text: string }>>([])
  23. const [isTocExpanded, setIsTocExpanded] = useState(false)
  24. const variables = appDetail?.model_config?.configs?.prompt_variables || []
  25. const inputs = variables.reduce((res: any, variable: any) => {
  26. res[variable.key] = variable.name || ''
  27. return res
  28. }, {})
  29. useEffect(() => {
  30. const mediaQuery = window.matchMedia('(min-width: 1280px)')
  31. setIsTocExpanded(mediaQuery.matches)
  32. }, [])
  33. useEffect(() => {
  34. const extractTOC = () => {
  35. const article = document.querySelector('article')
  36. if (article) {
  37. const headings = article.querySelectorAll('h2')
  38. const tocItems = Array.from(headings).map((heading) => {
  39. const anchor = heading.querySelector('a')
  40. if (anchor) {
  41. return {
  42. href: anchor.getAttribute('href') || '',
  43. text: anchor.textContent || '',
  44. }
  45. }
  46. return null
  47. }).filter((item): item is { href: string; text: string } => item !== null)
  48. setToc(tocItems)
  49. }
  50. }
  51. // Run after component has rendered
  52. setTimeout(extractTOC, 0)
  53. }, [appDetail, locale])
  54. return (
  55. <div className="flex">
  56. <div className={`fixed right-8 top-32 z-10 transition-all ${isTocExpanded ? 'w-64' : 'w-10'}`}>
  57. {isTocExpanded
  58. ? (
  59. <nav className="toc w-full bg-gray-50 p-4 rounded-lg shadow-md">
  60. <div className="flex justify-between items-center mb-4">
  61. <h3 className="text-lg font-semibold">{t('appApi.develop.toc')}</h3>
  62. <button
  63. onClick={() => setIsTocExpanded(false)}
  64. className="text-gray-500 hover:text-gray-700"
  65. >
  66. </button>
  67. </div>
  68. <ul className="space-y-2">
  69. {toc.map((item, index) => (
  70. <li key={index}>
  71. <a
  72. href={item.href}
  73. className="text-gray-600 hover:text-gray-900 hover:underline transition-colors duration-200"
  74. >
  75. {item.text}
  76. </a>
  77. </li>
  78. ))}
  79. </ul>
  80. </nav>
  81. )
  82. : (
  83. <button
  84. onClick={() => setIsTocExpanded(true)}
  85. 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"
  86. >
  87. <RiListUnordered className="w-6 h-6" />
  88. </button>
  89. )}
  90. </div>
  91. <article className="prose prose-xl" >
  92. {(appDetail?.mode === 'chat' || appDetail?.mode === 'agent-chat') && (
  93. locale !== LanguagesSupported[1] ? <TemplateChatEn appDetail={appDetail} variables={variables} inputs={inputs} /> : <TemplateChatZh appDetail={appDetail} variables={variables} inputs={inputs} />
  94. )}
  95. {appDetail?.mode === 'advanced-chat' && (
  96. locale !== LanguagesSupported[1] ? <TemplateAdvancedChatEn appDetail={appDetail} variables={variables} inputs={inputs} /> : <TemplateAdvancedChatZh appDetail={appDetail} variables={variables} inputs={inputs} />
  97. )}
  98. {appDetail?.mode === 'workflow' && (
  99. locale !== LanguagesSupported[1] ? <TemplateWorkflowEn appDetail={appDetail} variables={variables} inputs={inputs} /> : <TemplateWorkflowZh appDetail={appDetail} variables={variables} inputs={inputs} />
  100. )}
  101. {appDetail?.mode === 'completion' && (
  102. locale !== LanguagesSupported[1] ? <TemplateEn appDetail={appDetail} variables={variables} inputs={inputs} /> : <TemplateZh appDetail={appDetail} variables={variables} inputs={inputs} />
  103. )}
  104. </article>
  105. </div>
  106. )
  107. }
  108. export default Doc