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';
13 import { OpenIcon } from '~/components/icon/icon';
15 interface FileViewerActionProps {
17 viewers: FileViewerList;
20 const mapStateToProps = (state: RootState): FileViewerActionProps => {
21 const { resource } = state.contextMenu;
23 const file = getNodeValue(resource.uuid)(state.collectionPanelFiles);
25 const fileViewers = getFileViewers(state.properties);
28 viewers: fileViewers.filter(enabledViewers(file)),
38 const enabledViewers = (file: CollectionFile | CollectionDirectory) =>
39 ({ extensions, collections }: FileViewer) => {
40 if (collections && file.type === CollectionFileType.DIRECTORY) {
42 } else if (extensions) {
43 return extensions.some(extension => file.name.endsWith(extension));
49 const fillViewerUrl = (fileUrl: string, { url, filePathParam }: FileViewer) => {
50 const viewerUrl = new URL(url);
51 viewerUrl.searchParams.append(filePathParam, fileUrl);
52 return viewerUrl.href;
55 export const FileViewerActions = connect(mapStateToProps)(
56 ({ fileUrl, viewers, onClick }: FileViewerActionProps & { onClick: () => void }) =>
58 {viewers.map(viewer =>
63 style={{ textDecoration: 'none' }}
64 href={fillViewerUrl(fileUrl, viewer)}
71 <img src={viewer.iconUrl} />