7d25f1cfc18a00d0f380cf4e2480cb18f34fd7c8
[arvados-workbench2.git] / src / views-components / context-menu / actions / collection-file-viewer-action.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { connect } from "react-redux";
6 import { RootState } from "../../../store/store";
7 import { FileViewerAction } from '~/views-components/context-menu/actions/file-viewer-action';
8 import { getNodeValue } from "~/models/tree";
9 import { ContextMenuKind } from '~/views-components/context-menu/context-menu';
10 import { getInlineFileUrl, sanitizeToken } from "./helpers";
11
12 const mapStateToProps = (state: RootState) => {
13     const { resource } = state.contextMenu;
14     const currentCollectionUuid = state.collectionPanel.item ? state.collectionPanel.item.uuid : '';
15     if (resource && [
16         ContextMenuKind.COLLECTION_FILE_ITEM,
17         ContextMenuKind.READONLY_COLLECTION_FILE_ITEM,
18         ContextMenuKind.COLLECTION_DIRECTORY_ITEM,
19         ContextMenuKind.READONLY_COLLECTION_DIRECTORY_ITEM ].indexOf(resource.menuKind as ContextMenuKind) > -1) {
20         const file = getNodeValue(resource.uuid)(state.collectionPanelFiles);
21         if (file) {
22             const fileUrl = sanitizeToken(getInlineFileUrl(
23                 file.url,
24                 state.auth.config.keepWebServiceUrl,
25                 state.auth.config.keepWebInlineServiceUrl), true);
26             return {
27                 href: fileUrl,
28                 kind: 'file',
29                 currentCollectionUuid
30             };
31         }
32     }
33     return {};
34 };
35
36 export const CollectionFileViewerAction = connect(mapStateToProps)(FileViewerAction);