Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / store / open-in-new-tab / open-in-new-tab.actions.ts
index c6462ea139f1ed7d53a463c37164d39ff3cc3c00..83055e32fcbd3750e50b648c4166f6d618b469de 100644 (file)
@@ -2,36 +2,39 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import * as copy from 'copy-to-clipboard';
-import { ResourceKind } from '~/models/resource';
-import { getClipboardUrl } from '~/views-components/context-menu/actions/helpers';
-
-const getUrl = (resource: any) => {
-    let url = null;
-    const { uuid, kind } = resource;
-
-    if (kind === ResourceKind.COLLECTION) {
-        url = `/collections/${uuid}`;
-    }
-    if (kind === ResourceKind.PROJECT) {
-        url = `/projects/${uuid}`;
+import copy from "copy-to-clipboard";
+import { Dispatch } from "redux";
+import { getNavUrl } from "routes/routes";
+import { RootState } from "store/store";
+import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
+
+export const openInNewTabAction = (resource: any) => (dispatch: Dispatch, getState: () => RootState) => {
+    const url = getNavUrl(resource.uuid, getState().auth);
+
+    if (url[0] === "/") {
+        window.open(`${window.location.origin}${url}`, "_blank");
+    } else if (url.length) {
+        window.open(url, "_blank");
     }
-
-    return url;
 };
 
-export const openInNewTabAction = (resource: any) => () => {
-    const url = getUrl(resource);
+export const copyToClipboardAction = (resources: Array<any>) => (dispatch: Dispatch, getState: () => RootState) => {
+    // Copy to clipboard omits token to avoid accidental sharing
 
-    if (url) {
-        window.open(`${window.location.origin}${url}`, '_blank');
-    }
-};
-
-export const copyToClipboardAction = (resource: any) => () => {
-    const url = getUrl(resource);
+    let url = getNavUrl(resources[0].uuid, getState().auth, false);
+    let wasCopied;
 
-    if (url) {
-        copy(getClipboardUrl(url, false));
+    if (url[0] === "/") wasCopied = copy(`${window.location.origin}${url}`);
+    else if (url.length) {
+        wasCopied = copy(url);
     }
-};
\ No newline at end of file
+
+    if (wasCopied)
+        dispatch(
+            snackbarActions.OPEN_SNACKBAR({
+                message: "Copied",
+                hideDuration: 2000,
+                kind: SnackbarKind.SUCCESS,
+            })
+        );
+};