Skip to content

Commit af08110

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 af08110

File tree

2 files changed

+49
-12
lines changed

2 files changed

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

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)