2022-10-17 11:28:01 +00:00
|
|
|
import { getLocationContextRoot } from '../base/util/uri';
|
2022-03-17 14:13:58 +00:00
|
|
|
|
|
|
|
import { addTrackStateToURL } from './functions.any';
|
2022-10-17 11:28:01 +00:00
|
|
|
import { IStore } from './types';
|
2022-03-17 14:13:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirects to another page generated by replacing the path in the original URL
|
|
|
|
* with the given path.
|
|
|
|
*
|
|
|
|
* @param {(string)} pathname - The path to navigate to.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function redirectWithStoredParams(pathname: string) {
|
2022-10-17 11:28:01 +00:00
|
|
|
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
|
2022-03-17 14:13:58 +00:00
|
|
|
const { locationURL } = getState()['features/base/connection'];
|
2022-10-17 11:28:01 +00:00
|
|
|
const newLocationURL = new URL(locationURL?.href ?? '');
|
2022-03-17 14:13:58 +00:00
|
|
|
|
|
|
|
newLocationURL.pathname = pathname;
|
|
|
|
window.location.assign(newLocationURL.toString());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assigns a specific pathname to window.location.pathname taking into account
|
|
|
|
* the context root of the Web app.
|
|
|
|
*
|
|
|
|
* @param {string} pathname - The pathname to assign to
|
|
|
|
* window.location.pathname. If the specified pathname is relative, the context
|
|
|
|
* root of the Web app will be prepended to the specified pathname before
|
|
|
|
* assigning it to window.location.pathname.
|
|
|
|
* @param {string} hashParam - Optional hash param to assign to
|
|
|
|
* window.location.hash.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
2022-10-17 11:28:01 +00:00
|
|
|
export function redirectToStaticPage(pathname: string, hashParam?: string) {
|
2022-03-17 14:13:58 +00:00
|
|
|
return () => {
|
|
|
|
const windowLocation = window.location;
|
|
|
|
let newPathname = pathname;
|
|
|
|
|
|
|
|
if (!newPathname.startsWith('/')) {
|
|
|
|
// A pathname equal to ./ specifies the current directory. It will be
|
|
|
|
// fine but pointless to include it because contextRoot is the current
|
|
|
|
// directory.
|
|
|
|
newPathname.startsWith('./')
|
|
|
|
&& (newPathname = newPathname.substring(2));
|
|
|
|
newPathname = getLocationContextRoot(windowLocation) + newPathname;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hashParam) {
|
|
|
|
windowLocation.hash = hashParam;
|
|
|
|
}
|
|
|
|
|
|
|
|
windowLocation.pathname = newPathname;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reloads the page by restoring the original URL.
|
|
|
|
*
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function reloadWithStoredParams() {
|
2022-10-17 11:28:01 +00:00
|
|
|
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
|
2022-03-17 14:13:58 +00:00
|
|
|
const state = getState();
|
|
|
|
const { locationURL } = state['features/base/connection'];
|
|
|
|
|
|
|
|
// Preserve the local tracks muted states.
|
2022-10-17 11:28:01 +00:00
|
|
|
// @ts-ignore
|
2022-03-17 14:13:58 +00:00
|
|
|
const newURL = addTrackStateToURL(locationURL, state);
|
|
|
|
const windowLocation = window.location;
|
|
|
|
const oldSearchString = windowLocation.search;
|
|
|
|
|
|
|
|
windowLocation.replace(newURL.toString());
|
|
|
|
|
|
|
|
if (newURL.search === oldSearchString) {
|
|
|
|
// NOTE: Assuming that only the hash or search part of the URL will
|
|
|
|
// be changed!
|
|
|
|
// location.replace will not trigger redirect/reload when
|
|
|
|
// only the hash params are changed. That's why we need to call
|
|
|
|
// reload in addition to replace.
|
|
|
|
windowLocation.reload();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|