1
- import React from ' react' ;
1
+ import React , { useEffect , useState } from " react" ;
2
2
import useDocusaurusContext from '@docusaurus/useDocusaurusContext' ;
3
3
import Link from '@docusaurus/Link' ;
4
4
import Translate from '@docusaurus/Translate' ;
8
8
} from '@docusaurus/plugin-content-docs/client' ;
9
9
import Layout from '@theme/Layout' ;
10
10
import Heading from '@theme/Heading' ;
11
+
11
12
const docsPluginId = undefined ; // Default docs plugin instance
13
+ const baseGithubLinkDoc = 'https://github.com/thomaspoignant/go-feature-flag/tree/main/website/versioned_docs/version-' ;
12
14
13
15
function DocumentationLabel ( ) {
14
16
return (
@@ -24,19 +26,122 @@ function ReleaseNotesLabel() {
24
26
) ;
25
27
}
26
28
27
- export default function Version ( ) {
29
+
30
+ // getVersionedDocs is getting all the GO Feature Flag Versions directly from Github release.
31
+ async function getVersionedDocs ( owner , repo , path ) {
32
+ const apiUrl = `https://api.github.com/repos/${ owner } /${ repo } /contents/${ path } ` ;
33
+ const response = await fetch ( apiUrl ) ;
34
+
35
+ if ( ! response . ok ) {
36
+ throw new Error ( `GitHub API responded with status code: ${ response . status } ` ) ;
37
+ }
38
+
39
+ const directoryListing = await response . json ( ) ;
40
+ const versionDirectories = directoryListing
41
+ . filter ( item => item . type === 'dir' && item . name . startsWith ( 'version-' ) )
42
+ . map ( item => {
43
+ const version = item . name . replace ( 'version-' , '' )
44
+ return {
45
+ version,
46
+ link : item . html_url ,
47
+ }
48
+ } )
49
+ // Custom sort in descending order by version numbers
50
+ . sort ( ( a , b ) => {
51
+ const versionA = a . version . substring ( 1 ) . split ( '.' ) . map ( Number ) ; // Exclude 'v' and parse numbers
52
+ const versionB = b . version . substring ( 1 ) . split ( '.' ) . map ( Number ) ;
53
+ for ( let i = 0 ; i < Math . max ( versionA . length , versionB . length ) ; i ++ ) {
54
+ if ( ( versionA [ i ] || 0 ) > ( versionB [ i ] || 0 ) ) return - 1 ;
55
+ if ( ( versionA [ i ] || 0 ) < ( versionB [ i ] || 0 ) ) return 1 ;
56
+ }
57
+ return 0 ;
58
+ } ) ;
59
+
60
+ return versionDirectories ;
61
+ }
62
+
63
+
64
+ // This component is displaying all the versions of GO Feature Flag from GitHub release.
65
+ const VersionList = ( ) => {
66
+ const [ versions , setVersions ] = useState ( [ ] ) ;
67
+ const docusaurusVersions = useVersions ( docsPluginId ) ;
68
+
69
+ useEffect ( ( ) => {
70
+ const fetchVersions = async ( ) => {
71
+ try {
72
+ const versionData = await getVersionedDocs ( 'thomaspoignant' , 'go-feature-flag' , 'website/versioned_docs' ) ;
73
+ setVersions ( versionData ) ;
74
+ } catch ( error ) {
75
+ console . error ( error ) ;
76
+ }
77
+ } ;
78
+
79
+ fetchVersions ( ) ;
80
+ } , [ ] ) ;
81
+
82
+ function isDocusaurusVersion ( version ) {
83
+ return docusaurusVersions . some ( docusaurusVersion => docusaurusVersion . name === version ) ;
84
+ }
85
+
86
+ function getDocusaurusVersionLink ( version ) {
87
+ return docusaurusVersions . find ( docusaurusVersion => docusaurusVersion . name === version ) . path ;
88
+ }
89
+
28
90
const {
29
- siteConfig : { organizationName, projectName} ,
91
+ siteConfig : { organizationName, projectName }
30
92
} = useDocusaurusContext ( ) ;
93
+
94
+ return (
95
+ < div className = "margin-bottom--lg" >
96
+ < Heading as = "h3" id = "archive" >
97
+ < Translate id = "versionsPage.archived.title" >
98
+ All available Versions
99
+ </ Translate >
100
+ </ Heading >
101
+ < p >
102
+ < Translate id = "versionsPage.archived.description" >
103
+ Here you can find documentation for previous versions of GO Feature Flag.
104
+ </ Translate >
105
+ </ p >
106
+ < table >
107
+ < tbody >
108
+ { versions . map ( ( version , index ) => (
109
+ < tr key = { version . version } >
110
+ < th > { version . version } </ th >
111
+ < td >
112
+ { isDocusaurusVersion ( version . version ) ?
113
+ < Link to = { getDocusaurusVersionLink ( version . version ) } > < DocumentationLabel /> </ Link > :
114
+ < Link to = { version . link } > < DocumentationLabel />
115
+ < i className = "fa fa-external-link" aria-hidden = "true" > </ i >
116
+ </ Link > }
117
+ </ td >
118
+ < td >
119
+ < Link href = { `${ getRepoURL ( organizationName , projectName ) } /releases/tag/${ version . version } ` } >
120
+ < ReleaseNotesLabel />
121
+ </ Link >
122
+ </ td >
123
+ </ tr >
124
+ ) ) }
125
+ </ tbody >
126
+ </ table >
127
+ </ div > ) ;
128
+ } ;
129
+
130
+
131
+ const getRepoURL = ( organizationName , projectName ) => {
132
+ return `https://github.com/${ organizationName } /${ projectName } ` ;
133
+ }
134
+
135
+ export default function Version ( ) {
31
136
const versions = useVersions ( docsPluginId ) ;
32
137
const latestVersion = useLatestVersion ( docsPluginId ) ;
33
138
const currentVersion = versions . find (
34
- ( version ) => version . name === 'current' ,
35
- ) ;
36
- const pastVersions = versions . filter (
37
- ( version ) => version !== latestVersion && version . name !== 'current' ,
139
+ ( version ) => version . name === "current"
38
140
) ;
39
- const repoUrl = `https://github.com/${ organizationName } /${ projectName } ` ;
141
+
142
+ const {
143
+ siteConfig : { organizationName, projectName }
144
+ } = useDocusaurusContext ( ) ;
40
145
41
146
return (
42
147
< Layout
@@ -70,7 +175,7 @@ export default function Version() {
70
175
</ Link >
71
176
</ td >
72
177
< td >
73
- < Link to = { `${ repoUrl } /releases/tag/${ latestVersion . name } ` } >
178
+ < Link to = { `${ getRepoURL ( organizationName , projectName ) } /releases/tag/${ latestVersion . name } ` } >
74
179
< ReleaseNotesLabel />
75
180
</ Link >
76
181
</ td >
@@ -79,6 +184,7 @@ export default function Version() {
79
184
</ table >
80
185
</ div >
81
186
187
+
82
188
{ currentVersion !== latestVersion && (
83
189
< div className = "margin-bottom--lg" >
84
190
< Heading as = "h3" id = "latest" >
@@ -107,40 +213,8 @@ export default function Version() {
107
213
</ div >
108
214
) }
109
215
110
- { ( pastVersions . length > 0 ) && (
111
- < div className = "margin-bottom--lg" >
112
- < Heading as = "h3" id = "archive" >
113
- < Translate id = "versionsPage.archived.title" >
114
- Past versions
115
- </ Translate >
116
- </ Heading >
117
- < p >
118
- < Translate id = "versionsPage.archived.description" >
119
- Here you can find documentation for previous versions of
120
- GO Feature Flag.
121
- </ Translate >
122
- </ p >
123
- < table >
124
- < tbody >
125
- { pastVersions . map ( ( version ) => (
126
- < tr key = { version . name } >
127
- < th > { version . label } </ th >
128
- < td >
129
- < Link to = { version . path } >
130
- < DocumentationLabel />
131
- </ Link >
132
- </ td >
133
- < td >
134
- < Link href = { `${ repoUrl } /releases/tag/${ version . name } ` } >
135
- < ReleaseNotesLabel />
136
- </ Link >
137
- </ td >
138
- </ tr >
139
- ) ) }
140
- </ tbody >
141
- </ table >
142
- </ div >
143
- ) }
216
+
217
+ < VersionList />
144
218
</ main >
145
219
</ Layout >
146
220
) ;
0 commit comments