Skip to content

Commit b230c4e

Browse files
committed
Extract function for core logic of "staying on the same page when switching versions"
This commit is a no-op refactor to make reviewing the following commit easier.
1 parent c6286de commit b230c4e

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This is a separate file so that `mocha` can load it without needing
2+
// DOM shims.
3+
4+
import { Sitemap } from "../sitemap"
5+
6+
type CorrespondingURLParams = {
7+
selectedVersionSitemap: Sitemap
8+
selectedVersionBaseURL: URL
9+
currentLocation: URL
10+
currentBaseURL: string
11+
}
12+
13+
/**
14+
* Choose a URL to navigate to when the user chooses a version in the version
15+
* selector.
16+
*
17+
* @param selectedVersionSitemap - as obtained by fetchSitemap from `${selectedVersionBaseURL}/sitemap.xml`
18+
* @param selectedVersionBaseURL - usually `${currentBaseURL}/../selectedVersion`
19+
* @param currentLocation - current web browser location
20+
* @param currentBaseURL - as obtained from `config.base`
21+
* @returns the URL to navigate to or null if we can't be sure that the
22+
* corresponding page to the current page exists in the selected version
23+
*/
24+
export function selectedVersionCorrespondingURL(
25+
{selectedVersionSitemap,
26+
selectedVersionBaseURL,
27+
currentLocation,
28+
currentBaseURL}: CorrespondingURLParams
29+
): URL | undefined {
30+
const result = currentLocation.href.replace(
31+
currentBaseURL,
32+
selectedVersionBaseURL.href,
33+
)
34+
return selectedVersionSitemap.has(result.split("#")[0])
35+
? new URL(result)
36+
: undefined
37+
}

src/templates/assets/javascripts/integrations/version/index.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ import {
4848

4949
import { fetchSitemap } from "../sitemap"
5050

51+
import { selectedVersionCorrespondingURL } from "./correspondingPage"
52+
5153
/* ----------------------------------------------------------------------------
5254
* Helper types
5355
* ------------------------------------------------------------------------- */
@@ -122,22 +124,23 @@ export function setupVersionSelector(
122124
return EMPTY
123125
}
124126
ev.preventDefault()
125-
return of(url)
127+
return of(new URL(url))
126128
}
127129
}
128130
return EMPTY
129131
}),
130-
switchMap(url => {
131-
return fetchSitemap(new URL(url))
132-
.pipe(
133-
map(sitemap => {
134-
const location = getLocation()
135-
const path = location.href.replace(config.base, url)
136-
return sitemap.has(path.split("#")[0])
137-
? new URL(path)
138-
: new URL(url)
139-
})
140-
)
132+
switchMap(selectedVersionBaseURL => {
133+
return fetchSitemap(selectedVersionBaseURL).pipe(
134+
map(
135+
sitemap =>
136+
selectedVersionCorrespondingURL({
137+
selectedVersionSitemap: sitemap,
138+
selectedVersionBaseURL,
139+
currentLocation: getLocation(),
140+
currentBaseURL: config.base
141+
}) ?? selectedVersionBaseURL,
142+
),
143+
)
141144
})
142145
)
143146
)

0 commit comments

Comments
 (0)