Merge branch '16592-renaming-fix'
[arvados-workbench2.git] / src / store / collection-panel / collection-panel-files / collection-panel-files-state.ts
index 953f4c0111492863100f2aecf8b43330ff64a805..aa3bd3057d36601bc9119b4bb721348975e3422d 100644 (file)
@@ -2,14 +2,43 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-export type CollectionPanelFilesState = Array<CollectionPanelFile>;
+import { Tree, TreeNode, mapTreeValues, getNodeValue, getNodeDescendants } from '~/models/tree';
+import { CollectionFile, CollectionDirectory, CollectionFileType } from '~/models/collection-file';
 
-export interface CollectionPanelFile {
-    parentId?: string;
-    id: string;
-    name: string;
-    size?: number;
+export type CollectionPanelFilesState = Tree<CollectionPanelDirectory | CollectionPanelFile>;
+
+export interface CollectionPanelDirectory extends CollectionDirectory {
     collapsed: boolean;
     selected: boolean;
-    type: string;
-}
\ No newline at end of file
+}
+
+export interface CollectionPanelFile extends CollectionFile {
+    selected: boolean;
+}
+
+export const mapCollectionFileToCollectionPanelFile = (node: TreeNode<CollectionDirectory | CollectionFile>): TreeNode<CollectionPanelDirectory | CollectionPanelFile> => {
+    return {
+        ...node,
+        value: node.value.type === CollectionFileType.DIRECTORY
+            ? { ...node.value, selected: false, collapsed: true }
+            : { ...node.value, selected: false }
+    };
+};
+
+export const mergeCollectionPanelFilesStates = (oldState: CollectionPanelFilesState, newState: CollectionPanelFilesState) => {
+    return mapTreeValues((value: CollectionPanelDirectory | CollectionPanelFile) => {
+        const oldValue = getNodeValue(value.id)(oldState);
+        return oldValue
+            ? oldValue.type === CollectionFileType.DIRECTORY
+                ? { ...value, collapsed: oldValue.collapsed, selected: oldValue.selected }
+                : { ...value, selected: oldValue.selected }
+            : value;
+    })(newState);
+};
+
+export const filterCollectionFilesBySelection = (tree: CollectionPanelFilesState, selected: boolean) => {
+    const allFiles = getNodeDescendants('')(tree).map(node => node.value);
+    const selectedDirectories = allFiles.filter(file => file.selected === selected && file.type === CollectionFileType.DIRECTORY);
+    const selectedFiles = allFiles.filter(file => file.selected === selected && !selectedDirectories.some(dir => dir.id === file.path));
+    return [...selectedDirectories, ...selectedFiles];
+};