utils/legacyRoutes.jsjavascript
import { PRESENTATION_MODES, entryPathFor } from "../services/presentationModes";
// old route -> current route; keeps old kiosk/iPad bookmarks working
const LEGACY_ROUTES = [["/rad-enablement", "/radenablement-services"]];
/**
* "/rad-enablement/cancer-2" -> "/radenablement-services/cancer-2", or null
* when the path isn't a legacy route.
*
* @param {string} pathname
* @returns {?string}
*/
export const rewriteLegacyPath = (pathname) => {
for (const [from, to] of LEGACY_ROUTES) {
if (pathname === from || pathname.startsWith(`${from}/`)) {
return `${to}${pathname.slice(from.length)}`;
}
}
return null;
};
/**
* "/value-partnerships" -> "/value-partnerships/globe" (see entryPathFor in
* presentationModes.js), or null when the path isn't a mode's bare path
* that redirects elsewhere.
*
* @param {string} pathname
* @returns {?string}
*/
export const rewriteEntryPath = (pathname) => {
const mode = PRESENTATION_MODES.find(({ path }) => path === pathname);
const entry = mode && entryPathFor(mode);
return entry && entry !== pathname ? entry : null;
};
/**
* Rewrites the start URL's hash route (HashRouter) in place: a legacy route
* to its current one, and a mode's bare redirecting path to its entry path.
* Called in index.jsx before the analytics SDK starts, so its first page_view
* already carries the final route - a legacy route looked like a typo of the
* new one in the data, and the bare "/value-partnerships" added a second
* page_view once the app redirected it to the globe. The redirecting <Route>s
* (Routes.jsx, ValuePartnership.jsx) stay as a fallback for in-app navigation.
*/
export const redirectStartHash = () => {
const [path, ...query] = window.location.hash.replace(/^#/, "").split("?");
const legacy = rewriteLegacyPath(path) ?? path;
const next = rewriteEntryPath(legacy) ?? legacy;
if (next === path) return;
const search = query.length ? `?${query.join("?")}` : "";
window.history.replaceState(window.history.state, "", `#${next}${search}`);
};