Skip to content

Commit bcde733

Browse files
Remove initialize() from VersionContext and just init in the Context tag directly
1 parent 001fa72 commit bcde733

File tree

3 files changed

+46
-65
lines changed

3 files changed

+46
-65
lines changed

Diff for: context/VersionContext.js

+21-34
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,14 @@ const PLATFORM_VERSIONS = publicRuntimeConfig.PLATFORM_VERSIONS;
1515
const PLATFORM_LATEST_VERSIONS = publicRuntimeConfig.PLATFORM_LATEST_VERSIONS;
1616
const PLATFORMS = publicRuntimeConfig.PLATFORMS;
1717

18-
export function VersionContextProvider({ children }) {
19-
const [initialized, setInitialized] = useState(false);
20-
21-
const [version, setVersionState] = useState(DEFAULT_VERSION);
22-
const [platform, setPlatformState] = useState(DEFAULT_PLATFORM);
23-
// const [isOpen, setIsOpen] = useState(false);
24-
const router = useRouter();
25-
26-
const initialize = useCallback(
27-
({ newVersion, newPlatform, functionName = null, currMenuItem = null }) => {
28-
if (initialized) return [version, platform];
29-
30-
setVersionAndPlatform({
31-
newVersion,
32-
newPlatform,
33-
functionName,
34-
currMenuItem,
35-
updateURL: true,
36-
});
37-
38-
setInitialized(true);
39-
40-
return [newVersion, newPlatform];
41-
},
42-
[initialized, version, platform],
18+
export function VersionContextProvider(props) {
19+
const [platform, setPlatformState] = useState(() =>
20+
getCleanedPlatform(props.platformFromStaticLoad),
21+
);
22+
const [version, setVersionState] = useState(() =>
23+
getCleanedVersion(props.versionFromStaticLoad, platform),
4324
);
25+
const router = useRouter();
4426

4527
const setVersionAndPlatform = useCallback(
4628
({
@@ -63,12 +45,10 @@ export function VersionContextProvider({ children }) {
6345
return;
6446
}
6547

66-
const cleanedVersion = isLatestVersion(newVersion, newPlatform)
67-
? DEFAULT_VERSION
68-
: newVersion;
69-
setVersionState(cleanedVersion);
48+
const cleanedVersion = getCleanedVersion(newVersion, newPlatform);
49+
const cleanedPlatform = getCleanedPlatform(newPlatform);
7050

71-
const cleanedPlatform = newPlatform ?? DEFAULT_PLATFORM;
51+
setVersionState(cleanedVersion);
7252
setPlatformState(cleanedPlatform);
7353

7454
if (updateURL) {
@@ -96,15 +76,14 @@ export function VersionContextProvider({ children }) {
9676
value={{
9777
version,
9878
platform,
99-
initialize,
10079
setVersionAndPlatform,
10180
goToLatest,
10281
goToOpenSource,
10382
// isOpen,
10483
// setIsOpen
10584
}}
10685
>
107-
{children}
86+
{props.children}
10887
</Context.Provider>
10988
);
11089
}
@@ -219,8 +198,8 @@ export function getVersionAndPlatformFromPathPart(pathPart) {
219198
export function versionAndPlatformAreCompatible(version, platform) {
220199
if (version == DEFAULT_VERSION) return true;
221200

222-
if (platform == DEFAULT_PLATFORM && VERSIONS_LIST.indexOf(version) >= 0) {
223-
return true;
201+
if (platform == DEFAULT_PLATFORM) {
202+
return VERSIONS_LIST.indexOf(version) >= 0;
224203
}
225204

226205
return PLATFORM_VERSIONS[platform].indexOf(version) >= 0;
@@ -252,3 +231,11 @@ export function getBestNumericVersion(version, platform) {
252231
}
253232
}
254233
}
234+
235+
function getCleanedVersion(version, platform) {
236+
return isLatestVersion(version, platform) ? DEFAULT_VERSION : version;
237+
}
238+
239+
function getCleanedPlatform(platform) {
240+
return platform ?? DEFAULT_PLATFORM;
241+
}

Diff for: pages/[...slug].js

+19-30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from "fs";
2-
import { join, basename } from "path";
3-
import React, { useState, useCallback } from "react";
2+
import { basename } from "path";
3+
import React, { useState, useCallback, useEffect } from "react";
44
import Link from "next/link";
55
import Head from "next/head";
66
import { serialize } from "next-mdx-remote/serialize";
@@ -71,7 +71,6 @@ import YouTube from "../components/blocks/youTube";
7171
import Cloud from "../components/blocks/cloud";
7272

7373
import styles from "../components/layouts/container.module.css";
74-
import { DotFilledIcon } from "@radix-ui/react-icons";
7574

7675
const VERSIONS_LIST = serverRuntimeConfig.VERSIONS_LIST;
7776
const LATEST_OSS_VERSION = serverRuntimeConfig.LATEST_OSS_VERSION;
@@ -129,34 +128,24 @@ export default function Article({
129128
: "https://github.com/streamlit/docs/tree/main" +
130129
filename.substring(filename.indexOf("/content/"));
131130

132-
const { initialize, goToLatest, goToOpenSource } = useVersion();
133-
134-
const [version, platform] = initialize({
135-
router,
136-
newVersion: versionFromStaticLoad,
137-
newPlatform: platformFromStaticLoad,
138-
functionName: null,
139-
currMenuItem,
140-
});
141-
142-
// If version wasn't specified by hand in the URL, remove version from URL of unversioned pages.
143-
if (currMenuItem && currMenuItem.isVersioned) {
144-
if (
145-
versionFromStaticLoad == DEFAULT_VERSION &&
146-
platformFromStaticLoad == DEFAULT_PLATFORM
147-
) {
131+
const { version, platform, goToLatest, goToOpenSource } = useVersion();
132+
133+
useEffect(
134+
() => {
135+
// If this page isn't versioned, no action needed.
136+
if (!currMenuItem || !currMenuItem.isVersioned) return;
137+
138+
// If the URL already matches VersionContext, no action needed.
148139
if (
149-
versionFromStaticLoad != version ||
150-
platformFromStaticLoad != platform
151-
) {
152-
const versionAndPlatformStr = getVersionAndPlatformStr(
153-
version,
154-
platform,
155-
);
156-
router.push(`/${versionAndPlatformStr}/${slug.join("/")}`);
157-
}
158-
}
159-
}
140+
versionFromStaticLoad == version &&
141+
platformFromStaticLoad == platform
142+
)
143+
return;
144+
145+
const versionAndPlatformStr = getVersionAndPlatformStr(version, platform);
146+
router.replace(`/${versionAndPlatformStr}/${slug.join("/")}`);
147+
} /* Run on every page load */,
148+
);
160149

161150
const components = {
162151
Note,

Diff for: pages/_app.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ function StreamlitDocs({ Component, pageProps }) {
2626

2727
return (
2828
<AppContextProvider>
29-
<VersionContextProvider>
29+
<VersionContextProvider
30+
versionFromStaticLoad={pageProps.versionFromStaticLoad}
31+
platformFromStaticLoad={pageProps.platformFromStaticLoad}
32+
functionName={null}
33+
currMenuItem={pageProps.currMenuItem}
34+
>
3035
<Component {...pageProps} />
3136
</VersionContextProvider>
3237
</AppContextProvider>

0 commit comments

Comments
 (0)