mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
* first version of the pack * single entry point * add custom components to the package * move form components to toolkit * add more utils and hooks to the package * minor fixes * add CI workflow * update readme and package.json * fix ts * fix build of env-validator * fix docker build * fix icons size * test fixes * more test fixes
35 lines
988 B
TypeScript
35 lines
988 B
TypeScript
import type { File, FileTree } from '../types';
|
|
|
|
import { stripLeadingSlash } from 'toolkit/utils/url';
|
|
|
|
import sortFileTree from './sortFileTree';
|
|
|
|
export default function composeFileTree(files: Array<File>) {
|
|
const result: FileTree = [];
|
|
type Level = {
|
|
result: FileTree;
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
} & Record<string, any>;
|
|
const level: Level = { result };
|
|
|
|
files.forEach((file) => {
|
|
const path = stripLeadingSlash(file.file_path);
|
|
const segments = path.split('/');
|
|
|
|
segments.reduce((acc, segment, currentIndex, array) => {
|
|
if (!acc[segment]) {
|
|
acc[segment] = { result: [] };
|
|
acc.result.push({
|
|
name: segment,
|
|
...(currentIndex === array.length - 1 ? file : { children: acc[segment].result }),
|
|
});
|
|
}
|
|
acc.result.sort(sortFileTree);
|
|
|
|
return acc[segment];
|
|
}, level);
|
|
});
|
|
|
|
return result.sort(sortFileTree);
|
|
}
|