From 355c14dde7b580d32fbc84d37c324576c96d4b75 Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Sun, 7 Jul 2024 20:12:12 -0700 Subject: [PATCH] Extract function for core logic of "staying on the same page when switching versions" We will add unit tests for this functions in the next commit. The function gets its own file because I was unable to get the test runner ("mocha") to work otherwise. See the child commit's description for more details. --- .../integrations/version/correspondingPage.ts | 37 +++++++++++++++++++ .../javascripts/integrations/version/index.ts | 27 ++++++++------ 2 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 src/templates/assets/javascripts/integrations/version/correspondingPage.ts diff --git a/src/templates/assets/javascripts/integrations/version/correspondingPage.ts b/src/templates/assets/javascripts/integrations/version/correspondingPage.ts new file mode 100644 index 00000000000..e001fde3a81 --- /dev/null +++ b/src/templates/assets/javascripts/integrations/version/correspondingPage.ts @@ -0,0 +1,37 @@ +// This is a separate file so that `mocha` can load it without needing +// DOM shims. + +import { Sitemap } from "../sitemap" + +type CorrespondingURLParams = { + selectedVersionSitemap: Sitemap + selectedVersionBaseURL: URL + currentLocation: URL + currentBaseURL: string +} + +/** + * Choose a URL to navigate to when the user chooses a version in the version + * selector. + * + * @param selectedVersionSitemap + * @param selectedVersionBaseURL + * @param currentLocation + * @param currentBaseURL + * @returns the URL to navigate to or null if we can't be sure that the + * corresponding page to the current page exists in the selected version + */ +export function selectedVersionCorrespondingURL( + {selectedVersionSitemap, + selectedVersionBaseURL, + currentLocation, + currentBaseURL}: CorrespondingURLParams +): URL | undefined { + const result = currentLocation.href.replace( + currentBaseURL, + selectedVersionBaseURL.href, + ) + return selectedVersionSitemap.has(result.split("#")[0]) + ? new URL(result) + : undefined +} diff --git a/src/templates/assets/javascripts/integrations/version/index.ts b/src/templates/assets/javascripts/integrations/version/index.ts index 50de29ad461..c4a32e808e6 100644 --- a/src/templates/assets/javascripts/integrations/version/index.ts +++ b/src/templates/assets/javascripts/integrations/version/index.ts @@ -48,6 +48,8 @@ import { import { fetchSitemap } from "../sitemap" +import { selectedVersionCorrespondingURL } from "./correspondingPage" + /* ---------------------------------------------------------------------------- * Helper types * ------------------------------------------------------------------------- */ @@ -122,22 +124,23 @@ export function setupVersionSelector( return EMPTY } ev.preventDefault() - return of(url) + return of(new URL(url)) } } return EMPTY }), - switchMap(url => { - return fetchSitemap(new URL(url)) - .pipe( - map(sitemap => { - const location = getLocation() - const path = location.href.replace(config.base, url) - return sitemap.has(path.split("#")[0]) - ? new URL(path) - : new URL(url) - }) - ) + switchMap(selectedVersionBaseURL => { + return fetchSitemap(selectedVersionBaseURL).pipe( + map( + sitemap => + selectedVersionCorrespondingURL({ + selectedVersionSitemap: sitemap, + selectedVersionBaseURL, + currentLocation: getLocation(), + currentBaseURL: config.base + }) ?? selectedVersionBaseURL, + ), + ) }) ) )