X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/ac09b3b5c8ecd122b778bb9c17f2aef1f00f6b05..3f7e1a8afad27920adf8f03ce82eeb1ae58aa84f:/src/common/redirect-to.ts diff --git a/src/common/redirect-to.ts b/src/common/redirect-to.ts index baf44711..73c94843 100644 --- a/src/common/redirect-to.ts +++ b/src/common/redirect-to.ts @@ -2,48 +2,63 @@ // // SPDX-License-Identifier: AGPL-3.0 +import { getInlineFileUrl } from 'views-components/context-menu/actions/helpers'; import { Config } from './config'; -const REDIRECT_TO_KEY = 'redirectTo'; -export const REDIRECT_TO_APPLY_TO_PATH = 'redirectToApplyToPath'; +export const REDIRECT_TO_DOWNLOAD_KEY = 'redirectToDownload'; +export const REDIRECT_TO_PREVIEW_KEY = 'redirectToPreview'; +export const REDIRECT_TO_KEY = 'redirectTo'; -export const storeRedirects = () => { - let redirectUrl; - const { location: { href }, localStorage } = window; - const applyToPath = href.indexOf(REDIRECT_TO_APPLY_TO_PATH) > -1; +const getRedirectKeyFromUrl = (href: string): string | null => { + switch (true) { + case href.indexOf(REDIRECT_TO_DOWNLOAD_KEY) > -1: + return REDIRECT_TO_DOWNLOAD_KEY; + case href.indexOf(REDIRECT_TO_PREVIEW_KEY) > -1: + return REDIRECT_TO_PREVIEW_KEY; + case href.indexOf(`${REDIRECT_TO_KEY}=`) > -1: + return REDIRECT_TO_KEY; + default: + return null; + } +} - if (href.indexOf(REDIRECT_TO_KEY) > -1) { - redirectUrl = href.split(`${REDIRECT_TO_KEY}=`)[1]; +const getRedirectKeyFromStorage = (localStorage: Storage): string | null => { + if (localStorage.getItem(REDIRECT_TO_DOWNLOAD_KEY)) { + return REDIRECT_TO_DOWNLOAD_KEY; + } else if (localStorage.getItem(REDIRECT_TO_PREVIEW_KEY)) { + return REDIRECT_TO_PREVIEW_KEY; } + return null; +} - if (localStorage && redirectUrl) { - localStorage.setItem(REDIRECT_TO_KEY, redirectUrl); +export const storeRedirects = () => { + const { location: { href }, localStorage } = window; + const redirectKey = getRedirectKeyFromUrl(href); - if (applyToPath) { - localStorage.setItem(REDIRECT_TO_APPLY_TO_PATH, 'true'); - } + // Change old redirectTo -> redirectToPreview when storing redirect + const redirectStoreKey = redirectKey === REDIRECT_TO_KEY ? REDIRECT_TO_PREVIEW_KEY : redirectKey; + + if (localStorage && redirectKey && redirectStoreKey) { + localStorage.setItem(redirectStoreKey, href.split(`${redirectKey}=`)[1]); } }; export const handleRedirects = (token: string, config: Config) => { const { localStorage } = window; - const { keepWebServiceUrl } = config; - - if (localStorage && localStorage.getItem(REDIRECT_TO_KEY)) { - const redirectUrl = localStorage.getItem(REDIRECT_TO_KEY); - localStorage.removeItem(REDIRECT_TO_KEY); - const applyToPath = localStorage.getItem(REDIRECT_TO_APPLY_TO_PATH); - - if (redirectUrl) { - if (applyToPath === 'true') { - localStorage.removeItem(REDIRECT_TO_APPLY_TO_PATH); - setTimeout(() => { - window.location.pathname = redirectUrl; - }, 0); - } else { - const sep = redirectUrl.indexOf("?") > -1 ? "&" : "?"; - window.location.href = `${keepWebServiceUrl}${redirectUrl}${sep}api_token=${token}`; + const { keepWebServiceUrl, keepWebInlineServiceUrl } = config; + + if (localStorage) { + const redirectKey = getRedirectKeyFromStorage(localStorage); + const redirectPath = redirectKey ? localStorage.getItem(redirectKey) : ''; + redirectKey && localStorage.removeItem(redirectKey); + + if (redirectKey && redirectPath) { + const sep = redirectPath.indexOf("?") > -1 ? "&" : "?"; + let redirectUrl = `${keepWebServiceUrl}${redirectPath}${sep}api_token=${token}`; + if (redirectKey === REDIRECT_TO_PREVIEW_KEY) { + redirectUrl = getInlineFileUrl(redirectUrl, keepWebServiceUrl, keepWebInlineServiceUrl); } + window.location.href = redirectUrl; } } };