Farmbot-Web-App/frontend/folders/data_transfer.ts

81 lines
2.3 KiB
TypeScript
Raw Permalink Normal View History

2019-11-26 09:15:04 -07:00
import {
FolderNode,
FolderNodeMedial,
FolderNodeTerminal,
RootFolderNode,
FolderMeta,
2020-01-03 13:13:49 -07:00
} from "./interfaces";
2019-11-26 13:45:21 -07:00
import { sortBy } from "lodash";
2019-11-25 09:08:51 -07:00
2019-11-25 19:56:02 -07:00
type FoldersIndexedByParentId = Record<number, FolderNode[]>;
2019-11-25 15:23:14 -07:00
2019-11-25 19:56:02 -07:00
/** Set empty `parent_id` to -1 to increase index simplicity. */
const setDefaultParentId = (input: FolderNode): Required<FolderNode> => {
return { ...input, parent_id: input.parent_id || -1 };
};
2019-11-25 15:23:14 -07:00
2019-11-27 06:01:39 -07:00
type AddToIndex = (a: FoldersIndexedByParentId, i: Required<FolderNode>) =>
Record<number, FolderNode[] | undefined>;
const addToIndex: AddToIndex = (accumulator, item) => {
const key = item.parent_id;
const lastValue: FolderNode[] = accumulator[key] || [];
const nextValue: FolderNode[] = [...lastValue, item];
return { ...accumulator, [key]: nextValue };
};
2019-11-25 15:23:14 -07:00
2019-11-25 19:56:02 -07:00
const emptyIndex: FoldersIndexedByParentId = {};
2019-11-25 15:23:14 -07:00
2019-12-09 16:35:29 -07:00
export const PARENTLESS = -1;
type IngestFn =
(props: IngestFnProps) => RootFolderNode;
interface IngestFnProps {
folders: FolderNode[];
localMetaAttributes: Record<number, FolderMeta>;
}
export const ingest: IngestFn = ({ folders, localMetaAttributes }) => {
const output: RootFolderNode = {
folders: [],
noFolder: (localMetaAttributes[PARENTLESS] || {}).sequences || []
};
const index = folders.map(setDefaultParentId).reduce(addToIndex, emptyIndex);
2020-02-26 11:10:59 -07:00
const childrenOf = (i: number) =>
sortBy(index[i] || [], (x) => x.name.toLowerCase());
2019-11-26 09:15:04 -07:00
2019-11-26 13:45:21 -07:00
const terminal = (x: FolderNode): FolderNodeTerminal => ({
...x,
kind: "terminal",
content: (localMetaAttributes[x.id] || {}).sequences || [],
2020-02-26 11:10:59 -07:00
open: false,
editing: false,
2019-12-06 12:45:18 -07:00
// children: [],
...(localMetaAttributes[x.id] || {})
2019-11-26 13:45:21 -07:00
});
const medial = (x: FolderNode): FolderNodeMedial => ({
...x,
kind: "medial",
2020-02-26 11:10:59 -07:00
open: false,
editing: false,
2019-11-26 13:45:21 -07:00
children: childrenOf(x.id).map(terminal),
content: (localMetaAttributes[x.id] || {}).sequences || [],
...(localMetaAttributes[x.id] || {})
2019-11-26 13:45:21 -07:00
});
childrenOf(-1).map((root) => {
const children = childrenOf(root.id).map(medial);
2019-11-26 09:15:04 -07:00
return output.folders.push({
2019-11-26 13:45:21 -07:00
...root,
2019-11-26 09:15:04 -07:00
kind: "initial",
2020-02-26 11:10:59 -07:00
open: false,
editing: false,
2019-11-26 13:45:21 -07:00
children,
content: (localMetaAttributes[root.id] || {}).sequences || [],
...(localMetaAttributes[root.id] || {})
2019-11-26 09:15:04 -07:00
});
});
2019-11-25 11:18:22 -07:00
return output;
2019-12-05 09:12:14 -07:00
};