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