16812: Reverted changes in download action, fixed keep links
[arvados-workbench2.git] / src / common / redirect-to.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Config } from './config';
6
7 const REDIRECT_TO_KEY = 'redirectTo';
8
9 export const storeRedirects = () => {
10     if (window.location.href.indexOf(REDIRECT_TO_KEY) > -1) {
11         const { location: { href }, localStorage } = window;
12         const redirectUrl = href.split(`${REDIRECT_TO_KEY}=`)[1];
13
14         if (localStorage) {
15             localStorage.setItem(REDIRECT_TO_KEY, redirectUrl);
16         }
17     }
18 };
19
20 export const handleRedirects = (token: string, config: Config) => {
21     const { localStorage } = window;
22     const { keepWebServiceUrl } = config;
23
24     if (localStorage && localStorage.getItem(REDIRECT_TO_KEY)) {
25         const redirectUrl = localStorage.getItem(REDIRECT_TO_KEY);
26         localStorage.removeItem(REDIRECT_TO_KEY);
27         if (redirectUrl) {
28             const sep = redirectUrl.indexOf("?") > -1 ? "&" : "?";
29             window.location.href = `${keepWebServiceUrl}${redirectUrl}${sep}api_token=${token}`;
30         }
31     }
32 };