1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { getInlineFileUrl } from 'views-components/context-menu/actions/helpers';
6 import { Config } from './config';
8 export const REDIRECT_TO_DOWNLOAD_KEY = 'redirectToDownload';
9 export const REDIRECT_TO_PREVIEW_KEY = 'redirectToPreview';
11 const getRedirectKeyFromUrl = (href: string): string | null => {
13 case href.indexOf(REDIRECT_TO_DOWNLOAD_KEY) > -1:
14 return REDIRECT_TO_DOWNLOAD_KEY;
15 case href.indexOf(REDIRECT_TO_PREVIEW_KEY) > -1:
16 return REDIRECT_TO_PREVIEW_KEY;
22 const getRedirectKeyFromStorage = (localStorage: Storage): string | null => {
23 if (localStorage.getItem(REDIRECT_TO_DOWNLOAD_KEY)) {
24 return REDIRECT_TO_DOWNLOAD_KEY;
25 } else if (localStorage.getItem(REDIRECT_TO_PREVIEW_KEY)) {
26 return REDIRECT_TO_PREVIEW_KEY;
31 export const storeRedirects = () => {
32 const { location: { href }, localStorage } = window;
33 const redirectKey = getRedirectKeyFromUrl(href);
35 if (localStorage && redirectKey) {
36 localStorage.setItem(redirectKey, href.split(`${redirectKey}=`)[1]);
40 export const handleRedirects = (token: string, config: Config) => {
41 const { localStorage } = window;
42 const { keepWebServiceUrl, keepWebInlineServiceUrl } = config;
45 const redirectKey = getRedirectKeyFromStorage(localStorage);
46 const redirectPath = redirectKey ? localStorage.getItem(redirectKey) : '';
47 redirectKey && localStorage.removeItem(redirectKey);
49 if (redirectKey && redirectPath) {
50 const sep = redirectPath.indexOf("?") > -1 ? "&" : "?";
51 let redirectUrl = `${keepWebServiceUrl}${redirectPath}${sep}api_token=${token}`;
52 if (redirectKey === REDIRECT_TO_PREVIEW_KEY) {
53 redirectUrl = getInlineFileUrl(redirectUrl, keepWebServiceUrl, keepWebInlineServiceUrl);
55 window.location.href = redirectUrl;