1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Tree, TreeNode, mapTreeValues, getNodeValue, getNodeDescendants } from '~/models/tree';
6 import { CollectionFile, CollectionDirectory, CollectionFileType } from '~/models/collection-file';
8 export type CollectionPanelFilesState = Tree<CollectionPanelDirectory | CollectionPanelFile>;
10 export interface CollectionPanelDirectory extends CollectionDirectory {
15 export interface CollectionPanelFile extends CollectionFile {
19 export const mapCollectionFileToCollectionPanelFile = (node: TreeNode<CollectionDirectory | CollectionFile>): TreeNode<CollectionPanelDirectory | CollectionPanelFile> => {
22 value: node.value.type === CollectionFileType.DIRECTORY
23 ? { ...node.value, selected: false, collapsed: true }
24 : { ...node.value, selected: false }
28 export const mergeCollectionPanelFilesStates = (oldState: CollectionPanelFilesState, newState: CollectionPanelFilesState) => {
29 return mapTreeValues((value: CollectionPanelDirectory | CollectionPanelFile) => {
30 const oldValue = getNodeValue(value.id)(oldState);
32 ? oldValue.type === CollectionFileType.DIRECTORY
33 ? { ...value, collapsed: oldValue.collapsed, selected: oldValue.selected }
34 : { ...value, selected: oldValue.selected }
39 export const filterCollectionFilesBySelection = (tree: CollectionPanelFilesState, selected: boolean) => {
40 const allFiles = getNodeDescendants('')(tree).map(node => node.value);
42 const selectedDirectories = allFiles.filter(file => file.selected === selected && file.type === CollectionFileType.DIRECTORY);
43 const selectedFiles = allFiles.filter(file => file.selected === selected && !selectedDirectories.some(dir => dir.id === file.path));
44 return [...selectedDirectories, ...selectedFiles];