1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from "react";
6 import { ListItemText, ListItem, ListItemIcon, Icon } from "@material-ui/core";
7 import { RootState } from '~/store/store';
8 import { getNodeValue } from '~/models/tree';
9 import { CollectionDirectory, CollectionFile, CollectionFileType } from '~/models/collection-file';
10 import { FileViewerList, FileViewer } from '~/models/file-viewers-config';
11 import { getFileViewers } from '~/store/file-viewers/file-viewers-selectors';
12 import { connect } from 'react-redux';
14 interface FileViewerActionProps {
16 viewers: FileViewerList;
19 const mapStateToProps = (state: RootState): FileViewerActionProps => {
20 const { resource } = state.contextMenu;
22 const file = getNodeValue(resource.uuid)(state.collectionPanelFiles);
24 const fileViewers = getFileViewers(state.properties);
27 viewers: fileViewers.filter(enabledViewers(file)),
37 const enabledViewers = (file: CollectionFile | CollectionDirectory) =>
38 ({ extensions, collections }: FileViewer) => {
39 if (collections && file.type === CollectionFileType.DIRECTORY) {
41 } else if (extensions) {
42 return extensions.some(extension => file.name.endsWith(extension));
48 const fillViewerUrl = (fileUrl: string, { url, filePathParam }: FileViewer) => {
49 const viewerUrl = new URL(url);
50 viewerUrl.searchParams.append(filePathParam, fileUrl);
51 return viewerUrl.href;
54 export const FileViewerActions = connect(mapStateToProps)(
55 ({ fileUrl, viewers, onClick }: FileViewerActionProps & { onClick: () => void }) =>
57 {viewers.map(viewer =>
60 style={{ textDecoration: 'none' }}
61 href={fillViewerUrl(fileUrl, viewer)}