Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private _handleRouteChange = (): void => {
const { pagePath: prevPagePath, platform } = this.state;
const { siteDefinition } = this.props;
const { platforms } = siteDefinition;
const newPagePath = removeAnchorLink(location.hash);
if (prevPagePath === newPagePath) {
// Must have been a change to the anchor only (not the route).
// Don't do a full update, just jump to the anchor.
this._jumpToAnchor(extractAnchorLink(location.hash));
return;
}
const platformKeys = platforms && (Object.keys(platforms) as TPlatforms[]);
if (platformKeys && platformKeys.length > 0) {
// Test if the platform has changed on each hashchange to avoid costly forEach below.
const currentPlatformRegex = new RegExp(`/${platform}\\b`);
if (!currentPlatformRegex.test(newPagePath)) {
for (const key of platformKeys) {
// If the user navigates directly to a platform specific page, set the active platform to that of the new page.
const isNewPlatform = new RegExp(`/${key}`, 'gi');
if (isNewPlatform.test(newPagePath)) {
this._onPlatformChanged(key);
break;
}
public componentDidUpdate(prevProps: ISiteProps, prevState: ISiteState): void {
if (prevState.pagePath !== this.state.pagePath) {
this._jumpToAnchor(extractAnchorLink(location.hash));
}
const { activePages, pagePlatforms } = this.state;
const { siteDefinition } = this.props;
// If current page doesn't have pages for the active platform, switch to its first platform.
if (Object.keys(pagePlatforms).length > 0 && activePages.length === 0) {
const firstPlatform = getPageFirstPlatform(getSiteArea(siteDefinition.pages), siteDefinition);
this._onPlatformChanged(firstPlatform);
}
}