Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / store / open-in-new-tab / open-in-new-tab.actions.ts
index 42bdc4ccfe5c25de8a33e277b0d34a4c81f24b98..83055e32fcbd3750e50b648c4166f6d618b469de 100644 (file)
@@ -2,27 +2,39 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { Dispatch } from 'redux';
-import { ResourceKind } from '~/models/resource';
-import { unionize, ofType } from '~/common/unionize';
+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 openInNewTabActions = unionize({
-    COPY_STORE: ofType<{}>(),
-    OPEN_COLLECTION_IN_NEW_TAB: ofType<string>(),
-    OPEN_PROJECT_IN_NEW_TAB: ofType<string>()
-});
+export const openInNewTabAction = (resource: any) => (dispatch: Dispatch, getState: () => RootState) => {
+    const url = getNavUrl(resource.uuid, getState().auth);
 
-export const openInNewTabAction = (resource: any) => (dispatch: Dispatch) => {
-    const { uuid, kind } = resource;
+    if (url[0] === "/") {
+        window.open(`${window.location.origin}${url}`, "_blank");
+    } else if (url.length) {
+        window.open(url, "_blank");
+    }
+};
 
-    dispatch(openInNewTabActions.COPY_STORE());
+export const copyToClipboardAction = (resources: Array<any>) => (dispatch: Dispatch, getState: () => RootState) => {
+    // Copy to clipboard omits token to avoid accidental sharing
 
-    if (kind === ResourceKind.COLLECTION) {
-        dispatch(openInNewTabActions.OPEN_COLLECTION_IN_NEW_TAB(uuid));
-    }
-    if (kind === ResourceKind.PROJECT) {
-        dispatch(openInNewTabActions.OPEN_PROJECT_IN_NEW_TAB(uuid));
+    let url = getNavUrl(resources[0].uuid, getState().auth, false);
+    let wasCopied;
+
+    if (url[0] === "/") wasCopied = copy(`${window.location.origin}${url}`);
+    else if (url.length) {
+        wasCopied = copy(url);
     }
 
-    console.log(uuid);
-};
\ No newline at end of file
+    if (wasCopied)
+        dispatch(
+            snackbarActions.OPEN_SNACKBAR({
+                message: "Copied",
+                hideDuration: 2000,
+                kind: SnackbarKind.SUCCESS,
+            })
+        );
+};