2019-11-26 09:15:04 -07:00
|
|
|
import {
|
|
|
|
FolderNode,
|
|
|
|
FolderNodeMedial,
|
|
|
|
FolderNodeTerminal,
|
|
|
|
RootFolderNode,
|
2019-12-05 09:24:10 -07:00
|
|
|
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;
|
2019-12-04 13:50:53 -07:00
|
|
|
type IngestFn =
|
2019-12-05 07:54:39 -07:00
|
|
|
(props: IngestFnProps) => RootFolderNode;
|
2019-12-04 13:50:53 -07:00
|
|
|
|
2019-12-05 07:54:39 -07:00
|
|
|
interface IngestFnProps {
|
|
|
|
folders: FolderNode[];
|
2019-12-05 09:24:10 -07:00
|
|
|
localMetaAttributes: Record<number, FolderMeta>;
|
2019-12-05 07:54:39 -07:00
|
|
|
}
|
|
|
|
|
2019-12-05 10:11:40 -07:00
|
|
|
export const ingest: IngestFn = ({ folders, localMetaAttributes }) => {
|
2019-12-04 13:50:53 -07:00
|
|
|
const output: RootFolderNode = {
|
|
|
|
folders: [],
|
2019-12-05 10:11:40 -07:00
|
|
|
noFolder: (localMetaAttributes[PARENTLESS] || {}).sequences || []
|
2019-12-04 13:50:53 -07:00
|
|
|
};
|
2019-12-05 07:54:39 -07:00
|
|
|
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",
|
2019-12-05 10:11:40 -07:00
|
|
|
content: (localMetaAttributes[x.id] || {}).sequences || [],
|
2020-02-26 11:10:59 -07:00
|
|
|
open: false,
|
2019-12-05 14:52:01 -07:00
|
|
|
editing: false,
|
2019-12-06 12:45:18 -07:00
|
|
|
// children: [],
|
2019-12-05 14:52:01 -07:00
|
|
|
...(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,
|
2019-12-05 14:52:01 -07:00
|
|
|
editing: false,
|
2019-11-26 13:45:21 -07:00
|
|
|
children: childrenOf(x.id).map(terminal),
|
2019-12-05 14:52:01 -07:00
|
|
|
content: (localMetaAttributes[x.id] || {}).sequences || [],
|
|
|
|
...(localMetaAttributes[x.id] || {})
|
2019-11-26 13:45:21 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
childrenOf(-1).map((root) => {
|
2019-12-02 10:53:37 -07:00
|
|
|
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,
|
2019-12-05 14:52:01 -07:00
|
|
|
editing: false,
|
2019-11-26 13:45:21 -07:00
|
|
|
children,
|
2019-12-05 14:52:01 -07:00
|
|
|
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
|
|
|
};
|