| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 'use client' | 
					
						
							| 
									
										
										
										
											2024-11-19 09:06:12 +08:00
										 |  |  | import { useEffect, useState } from 'react' | 
					
						
							| 
									
										
										
										
											2024-01-04 15:37:51 +08:00
										 |  |  | import { useContext } from 'use-context-selector' | 
					
						
							| 
									
										
										
										
											2024-11-19 09:06:12 +08:00
										 |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							| 
									
										
										
										
											2024-11-20 10:07:30 +08:00
										 |  |  | import { RiListUnordered } from '@remixicon/react' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import TemplateEn from './template/template.en.mdx' | 
					
						
							|  |  |  | import TemplateZh from './template/template.zh.mdx' | 
					
						
							| 
									
										
										
										
											2024-11-21 15:02:46 +09:00
										 |  |  | import TemplateJa from './template/template.ja.mdx' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import TemplateAdvancedChatEn from './template/template_advanced_chat.en.mdx' | 
					
						
							|  |  |  | import TemplateAdvancedChatZh from './template/template_advanced_chat.zh.mdx' | 
					
						
							| 
									
										
										
										
											2024-11-21 15:02:46 +09:00
										 |  |  | import TemplateAdvancedChatJa from './template/template_advanced_chat.ja.mdx' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import TemplateWorkflowEn from './template/template_workflow.en.mdx' | 
					
						
							|  |  |  | import TemplateWorkflowZh from './template/template_workflow.zh.mdx' | 
					
						
							| 
									
										
										
										
											2024-11-21 15:02:46 +09:00
										 |  |  | import TemplateWorkflowJa from './template/template_workflow.ja.mdx' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import TemplateChatEn from './template/template_chat.en.mdx' | 
					
						
							|  |  |  | import TemplateChatZh from './template/template_chat.zh.mdx' | 
					
						
							| 
									
										
										
										
											2024-11-21 15:02:46 +09:00
										 |  |  | import TemplateChatJa from './template/template_chat.ja.mdx' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import I18n from '@/context/i18n' | 
					
						
							| 
									
										
										
										
											2024-02-23 14:31:06 +08:00
										 |  |  | import { LanguagesSupported } from '@/i18n/language' | 
					
						
							| 
									
										
										
										
											2025-04-15 16:13:18 +08:00
										 |  |  | import useTheme from '@/hooks/use-theme' | 
					
						
							|  |  |  | import { Theme } from '@/types/app' | 
					
						
							|  |  |  | import cn from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type IDocProps = { | 
					
						
							|  |  |  |   appDetail: any | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const Doc = ({ appDetail }: IDocProps) => { | 
					
						
							|  |  |  |   const { locale } = useContext(I18n) | 
					
						
							| 
									
										
										
										
											2024-11-19 09:06:12 +08:00
										 |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const [toc, setToc] = useState<Array<{ href: string; text: string }>>([]) | 
					
						
							| 
									
										
										
										
											2024-11-20 10:07:30 +08:00
										 |  |  |   const [isTocExpanded, setIsTocExpanded] = useState(false) | 
					
						
							| 
									
										
										
										
											2025-04-15 16:13:18 +08:00
										 |  |  |   const { theme } = useTheme() | 
					
						
							| 
									
										
										
										
											2024-02-23 14:31:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   const variables = appDetail?.model_config?.configs?.prompt_variables || [] | 
					
						
							|  |  |  |   const inputs = variables.reduce((res: any, variable: any) => { | 
					
						
							| 
									
										
										
										
											2024-01-04 15:37:51 +08:00
										 |  |  |     res[variable.key] = variable.name || '' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     return res | 
					
						
							|  |  |  |   }, {}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-20 10:07:30 +08:00
										 |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     const mediaQuery = window.matchMedia('(min-width: 1280px)') | 
					
						
							|  |  |  |     setIsTocExpanded(mediaQuery.matches) | 
					
						
							|  |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-19 09:06:12 +08:00
										 |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     const extractTOC = () => { | 
					
						
							|  |  |  |       const article = document.querySelector('article') | 
					
						
							|  |  |  |       if (article) { | 
					
						
							|  |  |  |         const headings = article.querySelectorAll('h2') | 
					
						
							|  |  |  |         const tocItems = Array.from(headings).map((heading) => { | 
					
						
							|  |  |  |           const anchor = heading.querySelector('a') | 
					
						
							|  |  |  |           if (anchor) { | 
					
						
							|  |  |  |             return { | 
					
						
							|  |  |  |               href: anchor.getAttribute('href') || '', | 
					
						
							|  |  |  |               text: anchor.textContent || '', | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           return null | 
					
						
							|  |  |  |         }).filter((item): item is { href: string; text: string } => item !== null) | 
					
						
							|  |  |  |         setToc(tocItems) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Run after component has rendered
 | 
					
						
							|  |  |  |     setTimeout(extractTOC, 0) | 
					
						
							|  |  |  |   }, [appDetail, locale]) | 
					
						
							| 
									
										
										
										
											2025-01-08 18:37:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const handleTocClick = (e: React.MouseEvent<HTMLAnchorElement>, item: { href: string; text: string }) => { | 
					
						
							|  |  |  |     e.preventDefault() | 
					
						
							|  |  |  |     const targetId = item.href.replace('#', '') | 
					
						
							|  |  |  |     const element = document.getElementById(targetId) | 
					
						
							|  |  |  |     if (element) { | 
					
						
							|  |  |  |       const scrollContainer = document.querySelector('.overflow-auto') | 
					
						
							|  |  |  |       if (scrollContainer) { | 
					
						
							|  |  |  |         const headerOffset = 80 | 
					
						
							|  |  |  |         const elementTop = element.offsetTop - headerOffset | 
					
						
							|  |  |  |         scrollContainer.scrollTo({ | 
					
						
							|  |  |  |           top: elementTop, | 
					
						
							|  |  |  |           behavior: 'smooth', | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2024-11-19 09:06:12 +08:00
										 |  |  |     <div className="flex"> | 
					
						
							| 
									
										
										
										
											2024-11-20 10:07:30 +08:00
										 |  |  |       <div className={`fixed right-8 top-32 z-10 transition-all ${isTocExpanded ? 'w-64' : 'w-10'}`}> | 
					
						
							|  |  |  |         {isTocExpanded | 
					
						
							|  |  |  |           ? ( | 
					
						
							| 
									
										
										
										
											2025-04-15 16:13:18 +08:00
										 |  |  |             <nav className="toc w-full rounded-lg bg-components-panel-bg p-4 shadow-md"> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               <div className="mb-4 flex items-center justify-between"> | 
					
						
							| 
									
										
										
										
											2025-04-15 16:13:18 +08:00
										 |  |  |                 <h3 className="text-lg font-semibold text-text-primary">{t('appApi.develop.toc')}</h3> | 
					
						
							| 
									
										
										
										
											2024-11-20 10:07:30 +08:00
										 |  |  |                 <button | 
					
						
							|  |  |  |                   onClick={() => setIsTocExpanded(false)} | 
					
						
							| 
									
										
										
										
											2025-04-15 16:13:18 +08:00
										 |  |  |                   className="text-text-tertiary hover:text-text-secondary" | 
					
						
							| 
									
										
										
										
											2024-11-20 10:07:30 +08:00
										 |  |  |                 > | 
					
						
							|  |  |  |                   ✕ | 
					
						
							|  |  |  |                 </button> | 
					
						
							|  |  |  |               </div> | 
					
						
							|  |  |  |               <ul className="space-y-2"> | 
					
						
							|  |  |  |                 {toc.map((item, index) => ( | 
					
						
							|  |  |  |                   <li key={index}> | 
					
						
							|  |  |  |                     <a | 
					
						
							|  |  |  |                       href={item.href} | 
					
						
							| 
									
										
										
										
											2025-04-15 16:13:18 +08:00
										 |  |  |                       className="text-text-secondary transition-colors duration-200 hover:text-text-primary hover:underline" | 
					
						
							| 
									
										
										
										
											2025-01-08 18:37:12 +08:00
										 |  |  |                       onClick={e => handleTocClick(e, item)} | 
					
						
							| 
									
										
										
										
											2024-11-20 10:07:30 +08:00
										 |  |  |                     > | 
					
						
							|  |  |  |                       {item.text} | 
					
						
							|  |  |  |                     </a> | 
					
						
							|  |  |  |                   </li> | 
					
						
							|  |  |  |                 ))} | 
					
						
							|  |  |  |               </ul> | 
					
						
							|  |  |  |             </nav> | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |           : ( | 
					
						
							|  |  |  |             <button | 
					
						
							|  |  |  |               onClick={() => setIsTocExpanded(true)} | 
					
						
							| 
									
										
										
										
											2025-04-15 16:13:18 +08:00
										 |  |  |               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" | 
					
						
							| 
									
										
										
										
											2024-11-20 10:07:30 +08:00
										 |  |  |             > | 
					
						
							| 
									
										
										
										
											2025-04-15 16:13:18 +08:00
										 |  |  |               <RiListUnordered className="h-6 w-6 text-components-button-secondary-text" /> | 
					
						
							| 
									
										
										
										
											2024-11-20 10:07:30 +08:00
										 |  |  |             </button> | 
					
						
							|  |  |  |           )} | 
					
						
							|  |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2025-04-15 16:13:18 +08:00
										 |  |  |       <article className={cn('prose-xl prose', theme === Theme.dark && 'dark:prose-invert')} > | 
					
						
							| 
									
										
										
										
											2024-11-19 09:06:12 +08:00
										 |  |  |         {(appDetail?.mode === 'chat' || appDetail?.mode === 'agent-chat') && ( | 
					
						
							| 
									
										
										
										
											2024-11-21 15:02:46 +09:00
										 |  |  |           (() => { | 
					
						
							|  |  |  |             switch (locale) { | 
					
						
							|  |  |  |               case LanguagesSupported[1]: | 
					
						
							|  |  |  |                 return <TemplateChatZh appDetail={appDetail} variables={variables} inputs={inputs} /> | 
					
						
							|  |  |  |               case LanguagesSupported[7]: | 
					
						
							|  |  |  |                 return <TemplateChatJa appDetail={appDetail} variables={variables} inputs={inputs} /> | 
					
						
							|  |  |  |               default: | 
					
						
							|  |  |  |                 return <TemplateChatEn appDetail={appDetail} variables={variables} inputs={inputs} /> | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           })() | 
					
						
							| 
									
										
										
										
											2024-11-19 09:06:12 +08:00
										 |  |  |         )} | 
					
						
							|  |  |  |         {appDetail?.mode === 'advanced-chat' && ( | 
					
						
							| 
									
										
										
										
											2024-11-21 15:02:46 +09:00
										 |  |  |           (() => { | 
					
						
							|  |  |  |             switch (locale) { | 
					
						
							|  |  |  |               case LanguagesSupported[1]: | 
					
						
							|  |  |  |                 return <TemplateAdvancedChatZh appDetail={appDetail} variables={variables} inputs={inputs} /> | 
					
						
							|  |  |  |               case LanguagesSupported[7]: | 
					
						
							|  |  |  |                 return <TemplateAdvancedChatJa appDetail={appDetail} variables={variables} inputs={inputs} /> | 
					
						
							|  |  |  |               default: | 
					
						
							|  |  |  |                 return <TemplateAdvancedChatEn appDetail={appDetail} variables={variables} inputs={inputs} /> | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           })() | 
					
						
							| 
									
										
										
										
											2024-11-19 09:06:12 +08:00
										 |  |  |         )} | 
					
						
							|  |  |  |         {appDetail?.mode === 'workflow' && ( | 
					
						
							| 
									
										
										
										
											2024-11-21 15:02:46 +09:00
										 |  |  |           (() => { | 
					
						
							|  |  |  |             switch (locale) { | 
					
						
							|  |  |  |               case LanguagesSupported[1]: | 
					
						
							|  |  |  |                 return <TemplateWorkflowZh appDetail={appDetail} variables={variables} inputs={inputs} /> | 
					
						
							|  |  |  |               case LanguagesSupported[7]: | 
					
						
							|  |  |  |                 return <TemplateWorkflowJa appDetail={appDetail} variables={variables} inputs={inputs} /> | 
					
						
							|  |  |  |               default: | 
					
						
							|  |  |  |                 return <TemplateWorkflowEn appDetail={appDetail} variables={variables} inputs={inputs} /> | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           })() | 
					
						
							| 
									
										
										
										
											2024-11-19 09:06:12 +08:00
										 |  |  |         )} | 
					
						
							|  |  |  |         {appDetail?.mode === 'completion' && ( | 
					
						
							| 
									
										
										
										
											2024-11-21 15:02:46 +09:00
										 |  |  |           (() => { | 
					
						
							|  |  |  |             switch (locale) { | 
					
						
							|  |  |  |               case LanguagesSupported[1]: | 
					
						
							|  |  |  |                 return <TemplateZh appDetail={appDetail} variables={variables} inputs={inputs} /> | 
					
						
							|  |  |  |               case LanguagesSupported[7]: | 
					
						
							|  |  |  |                 return <TemplateJa appDetail={appDetail} variables={variables} inputs={inputs} /> | 
					
						
							|  |  |  |               default: | 
					
						
							|  |  |  |                 return <TemplateEn appDetail={appDetail} variables={variables} inputs={inputs} /> | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           })() | 
					
						
							| 
									
										
										
										
											2024-11-19 09:06:12 +08:00
										 |  |  |         )} | 
					
						
							|  |  |  |       </article> | 
					
						
							|  |  |  |     </div> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default Doc |