19231: Add smaller page sizes (10 and 20 items) to load faster
[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 { getInlineFileUrl } from 'views-components/context-menu/actions/helpers';
6 import { Config } from './config';
7
8 export const REDIRECT_TO_DOWNLOAD_KEY = 'redirectToDownload';
9 export const REDIRECT_TO_PREVIEW_KEY = 'redirectToPreview';
10
11 const getRedirectKeyFromUrl = (href: string): string | null => {
12     switch (true) {
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;
17         default:
18             return null;
19     }
20 }
21
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;
27     }
28     return null;
29 }
30
31 export const storeRedirects = () => {
32     const { location: { href }, localStorage } = window;
33     const redirectKey = getRedirectKeyFromUrl(href);
34
35     if (localStorage && redirectKey) {
36         localStorage.setItem(redirectKey, href.split(`${redirectKey}=`)[1]);
37     }
38 };
39
40 export const handleRedirects = (token: string, config: Config) => {
41     const { localStorage } = window;
42     const { keepWebServiceUrl, keepWebInlineServiceUrl } = config;
43
44     if (localStorage) {
45         const redirectKey = getRedirectKeyFromStorage(localStorage);
46         const redirectPath = redirectKey ? localStorage.getItem(redirectKey) : '';
47         redirectKey && localStorage.removeItem(redirectKey);
48
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);
54             }
55             window.location.href = redirectUrl;
56         }
57     }
58 };