1
1
import React , { useEffect , useState , useRef } from "react" ;
2
- import reverse from "lodash/reverse" ;
3
2
import classNames from "classnames" ;
4
3
import Table from "./table" ;
5
4
import { H2 , H3 } from "./headers" ;
@@ -14,32 +13,36 @@ import "prismjs/plugins/line-highlight/prism-line-highlight.css";
14
13
import "prismjs/plugins/toolbar/prism-toolbar" ;
15
14
import "prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard" ;
16
15
import "prismjs/plugins/normalize-whitespace/prism-normalize-whitespace" ;
16
+ import getConfig from "next/config" ;
17
+ const { publicRuntimeConfig } = getConfig ( ) ;
17
18
18
19
import styles from "./autofunction.module.css" ;
19
- import { name } from "file-loader" ;
20
+ import { looksLikeVersionAndPlatformString } from "../../context/VersionContext" ;
21
+
22
+ const LATEST_VERSION = publicRuntimeConfig . LATEST_VERSION ;
23
+ const DEFAULT_VERSION = publicRuntimeConfig . DEFAULT_VERSION ;
24
+ const VERSIONS_LIST = publicRuntimeConfig . VERSIONS_LIST ;
20
25
21
26
const cleanHref = ( name ) => {
22
27
return String ( name ) . replace ( / \. / g, "" ) . replace ( / \s + / g, "-" ) ;
23
28
} ;
24
29
25
30
const Autofunction = ( {
26
31
version,
27
- versions,
28
32
streamlitFunction,
29
- streamlit ,
33
+ docstrings ,
30
34
slug,
31
35
hideHeader,
32
36
deprecated,
33
37
deprecatedText,
34
38
oldStreamlitFunction,
39
+ goToLatest,
35
40
} ) => {
36
41
const blockRef = useRef ( ) ;
37
42
const router = useRouter ( ) ;
38
- const maxVersion = versions [ versions . length - 1 ] ;
39
43
const [ isHighlighted , setIsHighlighted ] = useState ( false ) ;
40
- const [ currentVersion , setCurrentVersion ] = useState (
41
- version ? version : versions [ versions . length - 1 ] ,
42
- ) ;
44
+ const currentNumericVersion =
45
+ version == DEFAULT_VERSION ? LATEST_VERSION : version ;
43
46
44
47
useEffect ( ( ) => {
45
48
highlightWithPrism ( ) ;
@@ -97,15 +100,9 @@ const Autofunction = ({
97
100
setIsHighlighted ( true ) ;
98
101
} ;
99
102
100
- const VersionSelector = ( {
101
- versionList,
102
- currentVersion,
103
- handleSelectVersion,
104
- } ) => {
105
- const isSiS = currentVersion . startsWith ( "SiS" ) ? true : false ;
106
- const selectClass = isSiS
107
- ? "version-select sis-version"
108
- : currentVersion !== versionList [ 0 ]
103
+ const VersionSelector = ( { currentNumericVersion, handleSelectVersion } ) => {
104
+ const selectClass =
105
+ currentNumericVersion != LATEST_VERSION
109
106
? "version-select old-version"
110
107
: "version-select" ;
111
108
@@ -114,17 +111,13 @@ const Autofunction = ({
114
111
< label >
115
112
< span className = "sr-only" > Streamlit Version</ span >
116
113
< select
117
- value = { currentVersion }
114
+ value = { currentNumericVersion }
118
115
onChange = { handleSelectVersion }
119
116
className = { styles . Select }
120
117
>
121
- { versionList . map ( ( version , index ) => (
118
+ { VERSIONS_LIST . map ( ( version , index ) => (
122
119
< option value = { version } key = { version } >
123
- { version == "SiS"
124
- ? "Streamlit in Snowflake"
125
- : version . startsWith ( "SiS." )
126
- ? version . replace ( "SiS." , "Streamlit in Snowflake " )
127
- : "Version " + version }
120
+ { "Version " + version }
128
121
</ option >
129
122
) ) }
130
123
</ select >
@@ -135,20 +128,17 @@ const Autofunction = ({
135
128
136
129
const handleSelectVersion = ( event ) => {
137
130
const functionObject =
138
- streamlit [ streamlitFunction ] ?? streamlit [ oldStreamlitFunction ] ;
131
+ docstrings [ streamlitFunction ] ?? docstrings [ oldStreamlitFunction ] ;
139
132
const slicedSlug = slug . slice ( ) ;
140
133
141
- if ( event . target . value !== currentVersion ) {
142
- setCurrentVersion ( event . target . value ) ;
143
- if ( event . target . value !== maxVersion ) {
144
- let isnum = / ^ [ \d \. ] + $ / . test ( slicedSlug [ 0 ] ) ;
145
- let isSiS = / ^ S i S [ \d \. ] * $ / . test ( slicedSlug [ 0 ] ) ;
146
- if ( isnum || isSiS ) {
147
- slicedSlug [ 0 ] = event . target . value ;
148
- } else {
149
- slicedSlug . unshift ( event . target . value ) ;
150
- }
151
- slug . unshift ( event . target . value ) ;
134
+ if ( event . target . value !== currentNumericVersion ) {
135
+ if ( looksLikeVersionAndPlatformString ( slicedSlug [ 0 ] ) ) {
136
+ slicedSlug . shift ( ) ;
137
+ }
138
+ if ( event . target . value !== LATEST_VERSION ) {
139
+ slicedSlug . unshift ( event . target . value ) ;
140
+ } else {
141
+ goToLatest ( ) ;
152
142
}
153
143
}
154
144
@@ -163,7 +153,6 @@ const Autofunction = ({
163
153
const footers = [ ] ;
164
154
const args = [ ] ;
165
155
const returns = [ ] ;
166
- const versionList = reverse ( versions . slice ( ) ) ;
167
156
let functionObject ;
168
157
let functionDescription ;
169
158
let header ;
@@ -174,9 +163,9 @@ const Autofunction = ({
174
163
let methods = [ ] ;
175
164
let properties = [ ] ;
176
165
177
- if ( streamlitFunction in streamlit || oldStreamlitFunction in streamlit ) {
166
+ if ( streamlitFunction in docstrings || oldStreamlitFunction in docstrings ) {
178
167
functionObject =
179
- streamlit [ streamlitFunction ] ?? streamlit [ oldStreamlitFunction ] ;
168
+ docstrings [ streamlitFunction ] ?? docstrings [ oldStreamlitFunction ] ;
180
169
isClass = functionObject . is_class ;
181
170
isAttributeDict = functionObject . is_attribute_dict ?? false ;
182
171
if (
@@ -208,20 +197,15 @@ const Autofunction = ({
208
197
{ streamlitFunction . replace ( "streamlit" , "st" ) }
209
198
</ H2 >
210
199
< VersionSelector
211
- versionList = { versionList }
212
- currentVersion = { currentVersion }
200
+ currentNumericVersion = { currentNumericVersion }
213
201
handleSelectVersion = { handleSelectVersion }
214
202
/>
215
203
</ div >
216
204
< Warning >
217
- { version && version . startsWith ( "SiS" ) ? (
218
- < p > This method does not exist in Streamlit in Snowflake.</ p >
219
- ) : (
220
- < p >
221
- This method did not exist in version < code > { currentVersion } </ code > { " " }
222
- of Streamlit.
223
- </ p >
224
- ) }
205
+ < p >
206
+ This method did not exist in version{ " " }
207
+ < code > { currentNumericVersion } </ code > of Streamlit.
208
+ </ p >
225
209
</ Warning >
226
210
</ div >
227
211
) ;
@@ -276,8 +260,7 @@ const Autofunction = ({
276
260
>
277
261
{ headerTitle }
278
262
< VersionSelector
279
- versionList = { versionList }
280
- currentVersion = { currentVersion }
263
+ currentNumericVersion = { currentNumericVersion }
281
264
handleSelectVersion = { handleSelectVersion }
282
265
/>
283
266
</ div >
0 commit comments