1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { connect } from "react-redux";
6 import { Tree, TreeProps, TreeItem, TreeItemStatus } from "~/components/tree/tree";
7 import { RootState } from "~/store/store";
8 import { createTreePickerNode, TreePickerNode } from "~/store/tree-picker/tree-picker";
9 import { getNodeValue, getNodeChildrenIds, Tree as Ttree, createTree } from "~/models/tree";
10 import { Dispatch } from "redux";
12 export interface TreePickerProps {
14 toggleItemOpen: (nodeId: string, status: TreeItemStatus, pickerId: string) => void;
15 toggleItemActive: (nodeId: string, status: TreeItemStatus, pickerId: string) => void;
18 const mapStateToProps = (state: RootState, props: TreePickerProps): Pick<TreeProps<any>, 'items'> => {
19 const tree = state.treePicker[props.pickerId] || createTree();
21 items: getNodeChildrenIds('')(tree)
22 .map(treePickerToTreeItems(tree))
26 const mapDispatchToProps = (dispatch: Dispatch, props: TreePickerProps): Pick<TreeProps<any>, 'onContextMenu' | 'toggleItemOpen' | 'toggleItemActive'> => ({
27 onContextMenu: () => { return; },
28 toggleItemActive: (id, status) => props.toggleItemActive(id, status, props.pickerId),
29 toggleItemOpen: (id, status) => props.toggleItemOpen(id, status, props.pickerId)
32 export const TreePicker = connect(mapStateToProps, mapDispatchToProps)(Tree);
34 const treePickerToTreeItems = (tree: Ttree<TreePickerNode>) =>
35 (id: string): TreeItem<any> => {
36 const node: TreePickerNode = getNodeValue(id)(tree) || createTreePickerNode({ nodeId: '', value: 'InvalidNode' });
37 const items = getNodeChildrenIds(node.nodeId)(tree)
38 .map(treePickerToTreeItems(tree));
40 active: node.selected,
43 items: items.length > 0 ? items : undefined,
44 open: !node.collapsed,