@tecsinapse/cortex-react 1.15.0-beta.7 → 1.15.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,10 +2,32 @@
2
2
 
3
3
  var jsxRuntime = require('react/jsx-runtime');
4
4
  var cortexCore = require('@tecsinapse/cortex-core');
5
+ var React = require('react');
5
6
  var fa6 = require('react-icons/fa6');
6
7
  var md = require('react-icons/md');
7
8
  var ProgressBar = require('../ProgressBar/ProgressBar.js');
8
9
 
10
+ const recursiveCountFolderElements = (node) => {
11
+ let count = 0;
12
+ for (const key in node) {
13
+ count++;
14
+ count += recursiveCountFolderElements(node[key]);
15
+ }
16
+ return count;
17
+ };
18
+ const countFolderElements = (paths, root) => {
19
+ if (!paths.length) return 0;
20
+ const tree = {};
21
+ for (const path of paths) {
22
+ const parts = path.replace(root + "/", "").split("/").filter(Boolean);
23
+ let current = tree;
24
+ for (const part of parts) {
25
+ if (!current[part]) current[part] = {};
26
+ current = current[part];
27
+ }
28
+ }
29
+ return recursiveCountFolderElements(tree);
30
+ };
9
31
  const File = ({
10
32
  file,
11
33
  index,
@@ -63,7 +85,22 @@ const File = ({
63
85
  )
64
86
  ] }, index);
65
87
  };
66
- const Folder = ({ name, size, intent, loading }) => {
88
+ const Folder = ({ name, subItems }) => {
89
+ const size = countFolderElements(
90
+ subItems.map((it) => it.path),
91
+ name
92
+ );
93
+ const loading = React.useMemo(
94
+ () => subItems.some((it) => it.status === "uploading"),
95
+ [subItems]
96
+ );
97
+ const intent = React.useMemo(() => {
98
+ if (loading) return "info";
99
+ if ((subItems ?? []).some((item) => item.status === "error") && (subItems ?? []).some((item) => item.status === "success")) {
100
+ return "warning";
101
+ }
102
+ return "success";
103
+ }, [subItems]);
67
104
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col w-full", children: [
68
105
  /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-between border rounded-t-mili shadow p-mili", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex gap-centi", children: [
69
106
  /* @__PURE__ */ jsxRuntime.jsx("span", { className: "border-2 text-kilo text-primary-medium w-tera h-tera flex items-center justify-center rounded-mili", children: /* @__PURE__ */ jsxRuntime.jsx(fa6.FaRegFolder, {}) }),
@@ -80,79 +117,17 @@ const Folder = ({ name, size, intent, loading }) => {
80
117
  ] });
81
118
  };
82
119
  const FolderList = ({ files }) => {
83
- const paths = /* @__PURE__ */ new Set();
84
- const buildTree = (files2) => {
85
- const root = {
86
- type: "root",
87
- children: /* @__PURE__ */ new Map()
88
- };
89
- for (const file of files2) {
90
- const parts = file.file.relativePath.slice(1).split("/");
91
- parts.map((item) => paths.add(item));
92
- let current = root;
93
- parts.forEach((part, index) => {
94
- const isLast = index === parts.length - 1;
95
- if (isLast) {
96
- current.children.set(part, {
97
- type: "file",
98
- file
99
- });
100
- } else {
101
- if (!current.children.has(part)) {
102
- current.children.set(part, {
103
- type: "folder",
104
- children: /* @__PURE__ */ new Map()
105
- });
106
- }
107
- current = current.children.get(part);
108
- }
109
- });
110
- }
111
- return root;
112
- };
113
- const tree = buildTree(files);
114
- const count = (node) => {
115
- let total = 0;
116
- const files2 = [];
117
- for (const child of node.children.values()) {
118
- if (child.type === "file") {
119
- total += 1;
120
- files2.push(child.file);
121
- } else if (child.type === "folder") {
122
- total += 1;
123
- const nested = count(child);
124
- total += nested.total;
125
- files2.push(...nested.files);
126
- }
127
- }
128
- return { total, files: files2 };
129
- };
130
- const children = [];
131
- for (const [name, node] of tree.children) {
132
- const size = count(node).total;
133
- const files2 = count(node).files;
134
- children.push({
135
- name,
136
- size,
137
- files: files2
120
+ const folders = React.useMemo(() => {
121
+ const segments = {};
122
+ files.forEach((file) => {
123
+ const path = file.file.relativePath.replace(/^\//, "");
124
+ const root = path.split("/")[0];
125
+ const current = Array.from(segments?.[root] ?? []);
126
+ segments[root] = [...current, { path, status: file.status }];
138
127
  });
139
- }
140
- const folders = children.map((folder) => {
141
- const intent = (folder.files ?? []).some((item) => item.status === "success") ? "success" : (files ?? []).some((item) => item.status === "error") ? "error" : "info";
142
- return { ...folder, intent };
143
- });
144
- return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: folders.map(({ intent, name, size, files: files2 }, index) => /* @__PURE__ */ jsxRuntime.jsx(
145
- Folder,
146
- {
147
- name,
148
- size,
149
- intent,
150
- loading: files2.some(
151
- (file) => file.status === "uploading"
152
- )
153
- },
154
- index
155
- )) });
128
+ return segments;
129
+ }, [files]);
130
+ return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: Object.entries(folders).map(([name, children], index) => /* @__PURE__ */ jsxRuntime.jsx(Folder, { name, subItems: children }, index)) });
156
131
  };
157
132
 
158
133
  exports.File = File;
@@ -1,9 +1,31 @@
1
1
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
2
2
  import { button } from '@tecsinapse/cortex-core';
3
+ import { useMemo } from 'react';
3
4
  import { FaRegFileLines, FaRegFolder } from 'react-icons/fa6';
4
5
  import { MdClose } from 'react-icons/md';
5
6
  import { ProgressBar } from '../ProgressBar/ProgressBar.js';
6
7
 
8
+ const recursiveCountFolderElements = (node) => {
9
+ let count = 0;
10
+ for (const key in node) {
11
+ count++;
12
+ count += recursiveCountFolderElements(node[key]);
13
+ }
14
+ return count;
15
+ };
16
+ const countFolderElements = (paths, root) => {
17
+ if (!paths.length) return 0;
18
+ const tree = {};
19
+ for (const path of paths) {
20
+ const parts = path.replace(root + "/", "").split("/").filter(Boolean);
21
+ let current = tree;
22
+ for (const part of parts) {
23
+ if (!current[part]) current[part] = {};
24
+ current = current[part];
25
+ }
26
+ }
27
+ return recursiveCountFolderElements(tree);
28
+ };
7
29
  const File = ({
8
30
  file,
9
31
  index,
@@ -61,7 +83,22 @@ const File = ({
61
83
  )
62
84
  ] }, index);
63
85
  };
64
- const Folder = ({ name, size, intent, loading }) => {
86
+ const Folder = ({ name, subItems }) => {
87
+ const size = countFolderElements(
88
+ subItems.map((it) => it.path),
89
+ name
90
+ );
91
+ const loading = useMemo(
92
+ () => subItems.some((it) => it.status === "uploading"),
93
+ [subItems]
94
+ );
95
+ const intent = useMemo(() => {
96
+ if (loading) return "info";
97
+ if ((subItems ?? []).some((item) => item.status === "error") && (subItems ?? []).some((item) => item.status === "success")) {
98
+ return "warning";
99
+ }
100
+ return "success";
101
+ }, [subItems]);
65
102
  return /* @__PURE__ */ jsxs("div", { className: "flex flex-col w-full", children: [
66
103
  /* @__PURE__ */ jsx("div", { className: "flex items-center justify-between border rounded-t-mili shadow p-mili", children: /* @__PURE__ */ jsxs("div", { className: "flex gap-centi", children: [
67
104
  /* @__PURE__ */ jsx("span", { className: "border-2 text-kilo text-primary-medium w-tera h-tera flex items-center justify-center rounded-mili", children: /* @__PURE__ */ jsx(FaRegFolder, {}) }),
@@ -78,79 +115,17 @@ const Folder = ({ name, size, intent, loading }) => {
78
115
  ] });
79
116
  };
80
117
  const FolderList = ({ files }) => {
81
- const paths = /* @__PURE__ */ new Set();
82
- const buildTree = (files2) => {
83
- const root = {
84
- type: "root",
85
- children: /* @__PURE__ */ new Map()
86
- };
87
- for (const file of files2) {
88
- const parts = file.file.relativePath.slice(1).split("/");
89
- parts.map((item) => paths.add(item));
90
- let current = root;
91
- parts.forEach((part, index) => {
92
- const isLast = index === parts.length - 1;
93
- if (isLast) {
94
- current.children.set(part, {
95
- type: "file",
96
- file
97
- });
98
- } else {
99
- if (!current.children.has(part)) {
100
- current.children.set(part, {
101
- type: "folder",
102
- children: /* @__PURE__ */ new Map()
103
- });
104
- }
105
- current = current.children.get(part);
106
- }
107
- });
108
- }
109
- return root;
110
- };
111
- const tree = buildTree(files);
112
- const count = (node) => {
113
- let total = 0;
114
- const files2 = [];
115
- for (const child of node.children.values()) {
116
- if (child.type === "file") {
117
- total += 1;
118
- files2.push(child.file);
119
- } else if (child.type === "folder") {
120
- total += 1;
121
- const nested = count(child);
122
- total += nested.total;
123
- files2.push(...nested.files);
124
- }
125
- }
126
- return { total, files: files2 };
127
- };
128
- const children = [];
129
- for (const [name, node] of tree.children) {
130
- const size = count(node).total;
131
- const files2 = count(node).files;
132
- children.push({
133
- name,
134
- size,
135
- files: files2
118
+ const folders = useMemo(() => {
119
+ const segments = {};
120
+ files.forEach((file) => {
121
+ const path = file.file.relativePath.replace(/^\//, "");
122
+ const root = path.split("/")[0];
123
+ const current = Array.from(segments?.[root] ?? []);
124
+ segments[root] = [...current, { path, status: file.status }];
136
125
  });
137
- }
138
- const folders = children.map((folder) => {
139
- const intent = (folder.files ?? []).some((item) => item.status === "success") ? "success" : (files ?? []).some((item) => item.status === "error") ? "error" : "info";
140
- return { ...folder, intent };
141
- });
142
- return /* @__PURE__ */ jsx(Fragment, { children: folders.map(({ intent, name, size, files: files2 }, index) => /* @__PURE__ */ jsx(
143
- Folder,
144
- {
145
- name,
146
- size,
147
- intent,
148
- loading: files2.some(
149
- (file) => file.status === "uploading"
150
- )
151
- },
152
- index
153
- )) });
126
+ return segments;
127
+ }, [files]);
128
+ return /* @__PURE__ */ jsx(Fragment, { children: Object.entries(folders).map(([name, children], index) => /* @__PURE__ */ jsx(Folder, { name, subItems: children }, index)) });
154
129
  };
155
130
 
156
131
  export { File, Folder, FolderList };
@@ -1,4 +1,4 @@
1
1
  import { FileProps, FolderListProps, FolderProps } from './types';
2
2
  export declare const File: <T>({ file, index, onDelete, showDelete, }: FileProps<T>) => import("react/jsx-runtime").JSX.Element;
3
- export declare const Folder: <T>({ name, size, intent, loading }: FolderProps) => import("react/jsx-runtime").JSX.Element;
3
+ export declare const Folder: ({ name, subItems }: FolderProps) => import("react/jsx-runtime").JSX.Element;
4
4
  export declare const FolderList: <T>({ files }: FolderListProps<T>) => import("react/jsx-runtime").JSX.Element;
@@ -1,4 +1,10 @@
1
1
  import { DropzoneInputProps, DropzoneRootProps, type FileError, type FileRejection, type FileWithPath } from 'react-dropzone';
2
+ declare global {
3
+ interface File {
4
+ relativePath: string;
5
+ path?: string;
6
+ }
7
+ }
2
8
  export interface FileItem {
3
9
  file: File;
4
10
  loading: 'loading' | 'success' | 'error';
@@ -11,9 +17,10 @@ export interface FileProps<T> {
11
17
  }
12
18
  export interface FolderProps {
13
19
  name: string;
14
- size: number;
15
- intent: 'default' | 'success' | 'warning' | 'info' | 'error';
16
- loading: boolean;
20
+ subItems: {
21
+ status: string;
22
+ path: string;
23
+ }[];
17
24
  }
18
25
  export interface FolderListProps<T> {
19
26
  files: FileUpload<T>[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tecsinapse/cortex-react",
3
- "version": "1.15.0-beta.7",
3
+ "version": "1.15.0-beta.8",
4
4
  "description": "React components based in @tecsinapse/cortex-core",
5
5
  "license": "MIT",
6
6
  "main": "dist/esm/index.js",
@@ -20,7 +20,7 @@
20
20
  "dependencies": {
21
21
  "@floating-ui/react": "^0.26.18",
22
22
  "@internationalized/date": "3.7.0",
23
- "@tecsinapse/cortex-core": "1.2.0-beta.5",
23
+ "@tecsinapse/cortex-core": "1.2.0-beta.6",
24
24
  "clsx": "2.1.1",
25
25
  "currency.js": "2.0.4",
26
26
  "embla-carousel-autoplay": "^8.0.0",
@@ -48,5 +48,5 @@
48
48
  "react-icons": ">=5.2.0",
49
49
  "tailwind": ">=3.3.0"
50
50
  },
51
- "gitHead": "2082dfbefdac0dc3a0773f8876885fdae29a8609"
51
+ "gitHead": "e193bd148328ee510548708de7803cb1832abd6d"
52
52
  }