11import React , { useEffect , useRef , useState , lazy , Suspense } from 'react' ;
22import { BrowserRouter , Routes , Navigate , Route , useLocation , useNavigate , Outlet } from 'react-router-dom' ;
3+ import semver from 'semver' ;
34import siteConfig from './site.config' ;
45import { getRoute , filterVersions } from './utils' ;
56import packageJson from '@/package.json' ;
67
78const LazyDemo = lazy ( ( ) => import ( './Demo' ) ) ;
89
9- const { docs : routerList } = JSON . parse ( JSON . stringify ( siteConfig ) . replace ( / c o m p o n e n t : .+ / g, '' ) ) ;
10+ const { docs, enDocs } = JSON . parse ( JSON . stringify ( siteConfig ) . replace ( / c o m p o n e n t : .+ / g, '' ) ) ;
11+
12+ const docsMap = {
13+ zh : docs ,
14+ en : enDocs ,
15+ } ;
1016
1117const registryUrl =
1218 'https://service-edbzjd6y-1257786608.hk.apigw.tencentcs.com/release/npm/versions/tdesign-mobile-react' ;
1319const currentVersion = packageJson . version . replace ( / \. / g, '_' ) ;
1420
15- const docRoutes = getRoute ( siteConfig . docs , [ ] ) ;
21+ // eslint-disable-next-line import/no-named-as-default-member
22+ const docRoutes = [ ...getRoute ( siteConfig . docs , [ ] ) , ...getRoute ( siteConfig . enDocs , [ ] ) ] ;
1623const renderRouter = docRoutes . map ( ( nav , i ) => {
1724 const LazyCom = lazy ( nav . component ) ;
1825
@@ -36,37 +43,36 @@ function Components() {
3643 const tdHeaderRef = useRef ( ) ;
3744 const tdDocAsideRef = useRef ( ) ;
3845 const tdDocContentRef = useRef ( ) ;
46+ const tdSelectRef = useRef ( ) ;
3947 const tdDocSearch = useRef ( ) ;
4048
41- const [ versionOptions , setVersionOptions ] = useState ( [ ] ) ;
4249 const [ version ] = useState ( currentVersion ) ;
4350
44- function changeVersion ( version ) {
45- if ( version === currentVersion ) return ;
46- const historyUrl = `//${ version } -tdesign-mobile-react.surge.sh` ;
47- window . open ( historyUrl , '_blank' ) ;
48- }
49-
5051 function initHistoryVersions ( ) {
5152 fetch ( registryUrl )
5253 . then ( ( res ) => res . json ( ) )
5354 . then ( ( res ) => {
5455 const options = [ ] ;
55- const versions = filterVersions ( Object . keys ( res . versions ) . filter ( ( v ) => ! v . includes ( 'alpha' ) ) ) ;
56+ const versions = filterVersions ( Object . keys ( res . versions ) ) ;
5657
5758 versions . forEach ( ( v ) => {
5859 const nums = v . split ( '.' ) ;
60+ if ( nums [ 0 ] === '0' && nums [ 1 ] < 21 ) return false ;
5961
6062 options . unshift ( { label : v , value : v . replace ( / \. / g, '_' ) } ) ;
6163 } ) ;
62- setVersionOptions ( options ) ;
64+
65+ tdSelectRef . current . options = options . sort ( ( a , b ) => ( semver . gt ( a . label , b . label ) ? - 1 : 1 ) ) ;
6366 } ) ;
6467 }
6568
6669 useEffect ( ( ) => {
6770 tdHeaderRef . current . framework = 'react' ;
68- // tdDocSearch.current.docsearchInfo = { indexName: 'tdesign_doc_mobile_react' };
69- tdDocAsideRef . current . routerList = routerList ;
71+ tdDocSearch . current . docsearchInfo = { indexName : 'tdesign_doc_react_mobile' } ;
72+
73+ const isEn = window . location . pathname . endsWith ( 'en' ) ;
74+
75+ tdDocAsideRef . current . routerList = isEn ? docsMap . en : docsMap . zh ;
7076 tdDocAsideRef . current . onchange = ( { detail } ) => {
7177 if ( location . pathname === detail ) return ;
7278 tdDocContentRef . current . pageStatus = 'hidden' ;
@@ -83,20 +89,25 @@ function Components() {
8389 } ) ;
8490 } ;
8591
86- // initHistoryVersions();
92+ tdSelectRef . current . onchange = ( { detail } ) => {
93+ const { value : version } = detail ;
94+ if ( version === currentVersion ) return ;
95+
96+ const historyUrl = `https://${ version } -tdesign-mobile-react.surge.sh` ;
97+ window . open ( historyUrl , '_blank' ) ;
98+ tdSelectRef . current . value = currentVersion ;
99+ } ;
100+
101+ initHistoryVersions ( ) ;
87102 } , [ location , navigate ] ) ;
88103
89104 return (
90105 < td-doc-layout >
91106 < td-header ref = { tdHeaderRef } slot = "header" platform = "mobile" >
92- { /* <td-doc-search slot="search" ref={tdDocSearch} /> */ }
107+ < td-doc-search slot = "search" ref = { tdDocSearch } />
93108 </ td-header >
94109 < td-doc-aside ref = { tdDocAsideRef } title = "React for Mobile" >
95- { /* {versionOptions.length ? (
96- <div slot="extra">
97- <Select popupProps={{ zIndex: 800 }} value={version} options={versionOptions} onChange={changeVersion} />
98- </div>
99- ) : null} */ }
110+ < td-select ref = { tdSelectRef } value = { version } slot = "extra" > </ td-select >
100111 </ td-doc-aside >
101112
102113 < td-doc-content ref = { tdDocContentRef } platform = "mobile" >
@@ -111,8 +122,8 @@ function App() {
111122 return (
112123 < BrowserRouter >
113124 < Routes >
114- < Route exact path = "/" element = { < Navigate replace to = "/mobile-react/getting-started " /> } />
115- < Route exact path = "/mobile-react" element = { < Navigate replace to = "/mobile-react/getting-started " /> } />
125+ < Route exact path = "/" element = { < Navigate replace to = "/mobile-react/overview " /> } />
126+ < Route exact path = "/mobile-react" element = { < Navigate replace to = "/mobile-react/overview " /> } />
116127 < Route
117128 path = "/mobile-react/demos/*"
118129 element = {
@@ -124,7 +135,7 @@ function App() {
124135 < Route path = "/mobile-react/*" element = { < Components /> } >
125136 { renderRouter }
126137 </ Route >
127- < Route path = "*" element = { < Navigate replace to = "/mobile-react/getting-started " /> } />
138+ < Route path = "*" element = { < Navigate replace to = "/mobile-react/overview " /> } />
128139 </ Routes >
129140 </ BrowserRouter >
130141 ) ;
0 commit comments