doc.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use client'
  2. import { useEffect, useState } from 'react'
  3. import { useContext } from 'use-context-selector'
  4. import { useTranslation } from 'react-i18next'
  5. import TemplateEn from './template/template.en.mdx'
  6. import TemplateZh from './template/template.zh.mdx'
  7. import TemplateAdvancedChatEn from './template/template_advanced_chat.en.mdx'
  8. import TemplateAdvancedChatZh from './template/template_advanced_chat.zh.mdx'
  9. import TemplateWorkflowEn from './template/template_workflow.en.mdx'
  10. import TemplateWorkflowZh from './template/template_workflow.zh.mdx'
  11. import TemplateChatEn from './template/template_chat.en.mdx'
  12. import TemplateChatZh from './template/template_chat.zh.mdx'
  13. import I18n from '@/context/i18n'
  14. import { LanguagesSupported } from '@/i18n/language'
  15. type IDocProps = {
  16. appDetail: any
  17. }
  18. const Doc = ({ appDetail }: IDocProps) => {
  19. const { locale } = useContext(I18n)
  20. const { t } = useTranslation()
  21. const [toc, setToc] = useState<Array<{ href: string; text: string }>>([])
  22. const variables = appDetail?.model_config?.configs?.prompt_variables || []
  23. const inputs = variables.reduce((res: any, variable: any) => {
  24. res[variable.key] = variable.name || ''
  25. return res
  26. }, {})
  27. useEffect(() => {
  28. const extractTOC = () => {
  29. const article = document.querySelector('article')
  30. if (article) {
  31. const headings = article.querySelectorAll('h2')
  32. const tocItems = Array.from(headings).map((heading) => {
  33. const anchor = heading.querySelector('a')
  34. if (anchor) {
  35. return {
  36. href: anchor.getAttribute('href') || '',
  37. text: anchor.textContent || '',
  38. }
  39. }
  40. return null
  41. }).filter((item): item is { href: string; text: string } => item !== null)
  42. setToc(tocItems)
  43. }
  44. }
  45. // Run after component has rendered
  46. setTimeout(extractTOC, 0)
  47. }, [appDetail, locale])
  48. return (
  49. <div className="flex">
  50. <nav className="toc w-64 fixed right-8 top-32 bg-gray-50 p-4 rounded-lg shadow-md z-10">
  51. <h3 className="text-lg font-semibold mb-4">{t('appApi.develop.toc')}</h3>
  52. <ul className="space-y-2">
  53. {toc.map((item, index) => (
  54. <li key={index}>
  55. <a
  56. href={item.href}
  57. className="text-gray-600 hover:text-gray-900 hover:underline transition-colors duration-200"
  58. >
  59. {item.text}
  60. </a>
  61. </li>
  62. ))}
  63. </ul>
  64. </nav>
  65. <article className="prose prose-xl" >
  66. {(appDetail?.mode === 'chat' || appDetail?.mode === 'agent-chat') && (
  67. locale !== LanguagesSupported[1] ? <TemplateChatEn appDetail={appDetail} variables={variables} inputs={inputs} /> : <TemplateChatZh appDetail={appDetail} variables={variables} inputs={inputs} />
  68. )}
  69. {appDetail?.mode === 'advanced-chat' && (
  70. locale !== LanguagesSupported[1] ? <TemplateAdvancedChatEn appDetail={appDetail} variables={variables} inputs={inputs} /> : <TemplateAdvancedChatZh appDetail={appDetail} variables={variables} inputs={inputs} />
  71. )}
  72. {appDetail?.mode === 'workflow' && (
  73. locale !== LanguagesSupported[1] ? <TemplateWorkflowEn appDetail={appDetail} variables={variables} inputs={inputs} /> : <TemplateWorkflowZh appDetail={appDetail} variables={variables} inputs={inputs} />
  74. )}
  75. {appDetail?.mode === 'completion' && (
  76. locale !== LanguagesSupported[1] ? <TemplateEn appDetail={appDetail} variables={variables} inputs={inputs} /> : <TemplateZh appDetail={appDetail} variables={variables} inputs={inputs} />
  77. )}
  78. </article>
  79. </div>
  80. )
  81. }
  82. export default Doc