Skip to content

Commit f1e3992

Browse files
Use runtime values in version context and selector
Former-commit-id: f5758ed
1 parent 9d7878e commit f1e3992

File tree

5 files changed

+41
-108
lines changed

5 files changed

+41
-108
lines changed

components/blocks/autofunction.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,7 @@ const Autofunction = ({
152152
</a>
153153
{streamlitFunction.replace("streamlit", "st")}
154154
</H2>
155-
<VersionSelector
156-
versionList={versions}
157-
snowflakeVersions={snowflakeVersions}
158-
functionObject={functionObject}
159-
slug={slug}
160-
/>
155+
<VersionSelector functionObject={functionObject} slug={slug} />
161156
</div>
162157
<Warning>
163158
{version && version.startsWith("SiS") ? (
@@ -221,12 +216,7 @@ const Autofunction = ({
221216
`}
222217
>
223218
{headerTitle}
224-
<VersionSelector
225-
versionList={versions}
226-
snowflakeVersions={snowflakeVersions}
227-
functionObject={functionObject}
228-
slug={slug}
229-
/>
219+
<VersionSelector functionObject={functionObject} slug={slug} />
230220
</div>
231221
{deprecated === true ? (
232222
<Deprecation>

components/utilities/versionSelector.js

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,15 @@ import {
1313
getBestNumericVersion,
1414
versionAndPlatformAreCompatible,
1515
} from "../../context/VersionContext";
16-
import { CurrentRefinements } from "react-instantsearch-dom";
1716

18-
// const VERSIONS_LIST = publicRuntimeConfig.VERSIONS_LIST;
19-
// const LATEST_OSS_VERSION = publicRuntimeConfig.LATEST_OSS_VERSION;
20-
// const PLATFORM_VERSIONS = publicRuntimeConfig.PLATFORM_VERSIONS;
21-
// const PLATFORM_LATEST_VERSIONS = publicRuntimeConfig.PLATFORM_LATEST_VERSIONS;
22-
// const PLATFORMS = [DEFAULT_PLATFORM].concat(Object.keys(PLATFORM_VERSIONS))
23-
const PLATFORMS = [
24-
{ id: "oss", name: "None" },
25-
{ id: "sis", name: "Streamlit in Snowflake" },
26-
{ id: "na", name: "Native Apps" },
27-
];
17+
const VERSIONS_LIST = publicRuntimeConfig.VERSIONS_LIST;
18+
const LATEST_OSS_VERSION = publicRuntimeConfig.LATEST_OSS_VERSION;
19+
const PLATFORM_VERSIONS = publicRuntimeConfig.PLATFORM_VERSIONS;
20+
const PLATFORM_LATEST_VERSIONS = publicRuntimeConfig.PLATFORM_LATEST_VERSIONS;
21+
const PLATFORMS = publicRuntimeConfig.PLATFORMS;
2822

2923
const VersionSelector = ({
30-
versionList,
31-
snowflakeVersions,
32-
functionObject,
24+
functionObject, // For anchor link to jump back to function upon version change
3325
}) => {
3426
const {
3527
version: versionContext,
@@ -40,8 +32,6 @@ const VersionSelector = ({
4032
const [numericVersion, compatiblePlatform] = getBestNumericVersion(
4133
versionContext,
4234
platformContext,
43-
versionList,
44-
snowflakeVersions,
4535
);
4636

4737
const [widgetPlatform, setWidgetPlatform] = useState(compatiblePlatform);
@@ -54,25 +44,21 @@ const VersionSelector = ({
5444
// versionContext can be DEFAULT_VERSION (null) or "1.40.0" (even if that's the latest).
5545
versionContext,
5646
selectedPlatform,
57-
versionList,
58-
snowflakeVersions,
5947
)
6048
) {
6149
// Navigate to new version and platform.
6250
setVersionAndPlatform({
6351
newVersion: versionContext,
6452
newPlatform: selectedPlatform,
6553
functionName: functionObject ? functionObject.name : "",
66-
versionList,
67-
snowflakeVersions,
6854
});
6955
} else {
7056
// Just set the widget to the selected platform but dont navigate anywhere.
7157
// The user should pick a version first.
7258
setWidgetPlatform(selectedPlatform);
7359
}
7460
},
75-
[functionObject, versionList, snowflakeVersions],
61+
[functionObject],
7662
);
7763

7864
const handleSelectVersion = useCallback(
@@ -81,17 +67,15 @@ const VersionSelector = ({
8167
newVersion: selectedVersion,
8268
newPlatform: widgetPlatform,
8369
functionName: functionObject ? functionObject.name : "",
84-
versionList,
85-
snowflakeVersions,
8670
});
8771
},
88-
[widgetPlatform, functionObject, versionList, snowflakeVersions],
72+
[widgetPlatform, functionObject],
8973
);
9074

9175
const validVersionForWidgetPlatform =
9276
widgetPlatform == DEFAULT_PLATFORM
93-
? versionList
94-
: snowflakeVersions[widgetPlatform];
77+
? VERSIONS_LIST
78+
: PLATFORM_VERSIONS[widgetPlatform];
9579

9680
return (
9781
<Popover.Root>
@@ -115,19 +99,19 @@ const VersionSelector = ({
11599
aria-label="streamlit platform"
116100
onValueChange={handleSelectPlatform}
117101
>
118-
{PLATFORMS.map((platform) => (
119-
<div key={platform.id}>
102+
{Object.keys(PLATFORMS).map((platform) => (
103+
<div key={platform}>
120104
<RadioGroup.Item
121105
className={styles.RadioGroupItem}
122-
value={platform.id}
123-
id={platform.id}
106+
value={platform}
107+
id={platform}
124108
>
125109
<RadioGroup.Indicator
126110
className={styles.RadioGroupIndicator}
127111
/>
128112
</RadioGroup.Item>
129-
<label className={styles.RadioLabel} htmlFor={platform.id}>
130-
{platform.name}
113+
<label className={styles.RadioLabel} htmlFor={platform}>
114+
{PLATFORMS[platform]}
131115
</label>
132116
</div>
133117
))}

context/VersionContext.js

Lines changed: 22 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ const Context = createContext();
99

1010
export const DEFAULT_VERSION = "latest";
1111
export const DEFAULT_PLATFORM = "oss";
12+
const VERSIONS_LIST = publicRuntimeConfig.VERSIONS_LIST;
13+
const LATEST_OSS_VERSION = publicRuntimeConfig.LATEST_OSS_VERSION;
14+
const PLATFORM_VERSIONS = publicRuntimeConfig.PLATFORM_VERSIONS;
15+
const PLATFORM_LATEST_VERSIONS = publicRuntimeConfig.PLATFORM_LATEST_VERSIONS;
16+
const PLATFORMS = publicRuntimeConfig.PLATFORMS;
1217

1318
export function VersionContextProvider({ children }) {
1419
const [initialized, setInitialized] = useState(false);
@@ -19,21 +24,12 @@ export function VersionContextProvider({ children }) {
1924
const router = useRouter();
2025

2126
const initialize = useCallback(
22-
({
23-
newVersion,
24-
newPlatform,
25-
versionList,
26-
snowflakeVersions,
27-
functionName = null,
28-
currMenuItem = null,
29-
}) => {
27+
({ newVersion, newPlatform, functionName = null, currMenuItem = null }) => {
3028
if (initialized) return [version, platform];
3129

3230
setVersionAndPlatform({
3331
newVersion,
3432
newPlatform,
35-
versionList,
36-
snowflakeVersions,
3733
functionName,
3834
currMenuItem,
3935
updateURL: true,
@@ -50,20 +46,11 @@ export function VersionContextProvider({ children }) {
5046
({
5147
newVersion,
5248
newPlatform,
53-
versionList,
54-
snowflakeVersions,
5549
functionName = null,
5650
currMenuItem = null,
5751
updateURL = true,
5852
}) => {
59-
if (
60-
!versionAndPlatformAreCompatible(
61-
newVersion,
62-
newPlatform,
63-
versionList,
64-
snowflakeVersions,
65-
)
66-
) {
53+
if (!versionAndPlatformAreCompatible(newVersion, newPlatform)) {
6754
console.error(
6855
"Incompatible version and platform:",
6956
newVersion,
@@ -76,12 +63,7 @@ export function VersionContextProvider({ children }) {
7663
return;
7764
}
7865

79-
const cleanedVersion = isLatestVersion(
80-
newVersion,
81-
newPlatform,
82-
versionList,
83-
snowflakeVersions,
84-
)
66+
const cleanedVersion = isLatestVersion(newVersion, newPlatform)
8567
? DEFAULT_VERSION
8668
: newVersion;
8769
setVersionState(cleanedVersion);
@@ -131,16 +113,11 @@ export function useVersion() {
131113
return useContext(Context);
132114
}
133115

134-
export function isLatestVersion(
135-
version,
136-
platform,
137-
versionList,
138-
snowflakeVersions,
139-
) {
116+
export function isLatestVersion(version, platform) {
140117
const maxVersion =
141118
platform == DEFAULT_PLATFORM
142-
? getLatest(versionList)
143-
: getLatest(snowflakeVersions[platform]);
119+
? LATEST_OSS_VERSION
120+
: PLATFORM_LATEST_VERSIONS[platform];
144121

145122
return version == DEFAULT_VERSION || version == maxVersion;
146123
}
@@ -239,55 +216,38 @@ export function getVersionAndPlatformFromPathPart(pathPart) {
239216
return [version, cleanedPlatform];
240217
}
241218

242-
export function versionAndPlatformAreCompatible(
243-
version,
244-
platform,
245-
versionList,
246-
snowflakeVersions,
247-
) {
219+
export function versionAndPlatformAreCompatible(version, platform) {
248220
if (version == DEFAULT_VERSION) return true;
249221

250-
if (platform == DEFAULT_PLATFORM && versionList.indexOf(version) >= 0) {
222+
if (platform == DEFAULT_PLATFORM && VERSIONS_LIST.indexOf(version) >= 0) {
251223
return true;
252224
}
253225

254-
return snowflakeVersions[platform].indexOf(version) >= 0;
226+
return PLATFORM_VERSIONS[platform].indexOf(version) >= 0;
255227
}
256228

257-
export function getBestNumericVersion(
258-
version,
259-
platform,
260-
versionList,
261-
snowflakeVersions,
262-
) {
229+
export function getBestNumericVersion(version, platform) {
263230
if (version == DEFAULT_VERSION) {
264-
if (snowflakeVersions[platform]) {
231+
if (PLATFORM_VERSIONS[platform]) {
265232
// This is a valid platform so return the latest version in the platform.
266-
return [getLatest(snowflakeVersions[platform]), platform];
233+
return [PLATFORM_LATEST_VERSIONS[platform], platform];
267234
} else {
268235
// This is an invalid platform so we return the latest version for OSS.
269-
return [getLatest(versionList), DEFAULT_PLATFORM];
236+
return [LATEST_OSS_VERSION, DEFAULT_PLATFORM];
270237
}
271238
} else {
272-
if (
273-
versionAndPlatformAreCompatible(
274-
version,
275-
platform,
276-
versionList,
277-
snowflakeVersions,
278-
)
279-
) {
239+
if (versionAndPlatformAreCompatible(version, platform)) {
280240
// This is a numeric version that is compatible with the platform. Return it all back.
281241
return [version, platform];
282242
} else {
283-
if (snowflakeVersions[platform]) {
243+
if (PLATFORM_VERSIONS[platform]) {
284244
// Version and platform are incompatible, but platform exists. So return the latest
285245
// for the platform.
286-
return [getLatest(snowflakeVersions[platform]), platform];
246+
return [PLATFORM_LATEST_VERSIONS[platform], platform];
287247
} else {
288248
// Version and platform are incompatible, and platform does not exist. So return the
289249
// latest for OSS.
290-
return [getLatest(versionList), DEFAULT_PLATFORM];
250+
return [LATEST_OSS_VERSION, DEFAULT_PLATFORM];
291251
}
292252
}
293253
}

next.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ for (const index in Object.keys(PLATFORM_NOTES)) {
2525
const PLATFORM_VERSIONS = platformVersions;
2626
const PLATFORM_LATEST_VERSIONS = latestPlatformVersion;
2727
const PLATFORMS = {};
28+
PLATFORMS["oss"] = "None";
2829
PLATFORMS["sis"] = "Streamlit in Snowflake";
2930
PLATFORMS["na"] = "Snowflake Native Apps";
3031

pages/[...slug].js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ export default function Article({
135135
router,
136136
newVersion: versionFromStaticLoad,
137137
newPlatform: platformFromStaticLoad,
138-
versionList: versions,
139-
snowflakeVersions,
140138
functionName: null,
141139
currMenuItem,
142140
});

0 commit comments

Comments
 (0)