15768: project copy-to-clipboard works Arvados-DCO-1.1-Signed-off-by: Lisa Knox ...
[arvados-workbench2.git] / src / store / open-in-new-tab / open-in-new-tab.actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import copy from 'copy-to-clipboard';
6 import { Dispatch } from 'redux';
7 import { getNavUrl } from 'routes/routes';
8 import { RootState } from 'store/store';
9
10 export const openInNewTabAction = (resource: any) => (dispatch: Dispatch, getState: () => RootState) => {
11     const url = getNavUrl(resource.uuid, getState().auth);
12
13     if (url[0] === '/') {
14         window.open(`${window.location.origin}${url}`, '_blank');
15     } else if (url.length) {
16         window.open(url, '_blank');
17     }
18 };
19
20 export const copyToClipboardAction = (resources: Array<any>) => (dispatch: Dispatch, getState: () => RootState) => {
21     // Copy to clipboard omits token to avoid accidental sharing
22
23     let output = '';
24
25     resources.forEach((resource) => {
26         let url = getNavUrl(resource.uuid, getState().auth, false);
27         if (url[0] === '/') url = `${window.location.origin}${url}`;
28         output += output.length ? `, ${url}` : url;
29     });
30
31     if (output.length) {
32         copy(output);
33     }
34 };