20455: Use noopener everywhere on links and window.open
[arvados.git] / services / workbench2 / 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", "noopener");
16     } else if (url.length) {
17         window.open(url, "_blank", "noopener");
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 url = getNavUrl(resources[0].uuid, getState().auth, false);
25     let wasCopied;
26
27     if (url[0] === "/") wasCopied = copy(`${window.location.origin}${url}`);
28     else if (url.length) {
29         wasCopied = copy(url);
30     }
31
32     if (wasCopied)
33         dispatch(
34             snackbarActions.OPEN_SNACKBAR({
35                 message: "Copied",
36                 hideDuration: 2000,
37                 kind: SnackbarKind.SUCCESS,
38             })
39         );
40 };