15768: added copytoclipboard snackbar Arvados-DCO-1.1-Signed-off-by: Lisa Knox <lisa...
[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 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
10
11 export const openInNewTabAction = (resource: any) => (dispatch: Dispatch, getState: () => RootState) => {
12     const url = getNavUrl(resource.uuid, getState().auth);
13
14     if (url[0] === "/") {
15         window.open(`${window.location.origin}${url}`, "_blank");
16     } else if (url.length) {
17         window.open(url, "_blank");
18     }
19 };
20
21 export const copyToClipboardAction = (resources: Array<any>) => (dispatch: Dispatch, getState: () => RootState) => {
22     // Copy to clipboard omits token to avoid accidental sharing
23
24     let output = "";
25
26     resources.forEach(resource => {
27         let url = getNavUrl(resource.uuid, getState().auth, false);
28         if (url[0] === "/") url = `${window.location.origin}${url}`;
29         output += output.length ? `, ${url}` : url;
30     });
31
32     if (output.length) {
33         const wasCopied = copy(output);
34         if (wasCopied)
35             dispatch(
36                 snackbarActions.OPEN_SNACKBAR({
37                     message: "Copied",
38                     hideDuration: 2000,
39                     kind: SnackbarKind.SUCCESS,
40                 })
41             );
42     }
43 };