fux
This commit is contained in:
parent
4af77f8dd0
commit
35ff74de90
@ -1,63 +1,73 @@
|
|||||||
"use client"
|
"use client";
|
||||||
|
|
||||||
import React, {useEffect} from 'react';
|
|
||||||
import TreeNode from '../TreeNode';
|
|
||||||
import { } from "@tauri-apps/api";
|
|
||||||
import {invoke} from "@tauri-apps/api/core";
|
|
||||||
import { listen } from '@tauri-apps/api/event';
|
|
||||||
|
|
||||||
|
import React, { useEffect } from "react";
|
||||||
|
import TreeNode from "../TreeNode";
|
||||||
|
import { invoke } from "@tauri-apps/api/core";
|
||||||
|
import { listen } from "@tauri-apps/api/event";
|
||||||
|
|
||||||
interface TreeNodeData {
|
interface TreeNodeData {
|
||||||
name: string;
|
name: string;
|
||||||
path: string;
|
path: string;
|
||||||
type: "folder" | "file"; // Enforce specific values for 'type'
|
type: "folder" | "file"; // Specific values for 'type'
|
||||||
totalSize: number;
|
totalSize: number;
|
||||||
subfolderCount: number;
|
subfolderCount: number;
|
||||||
fileCount: number;
|
fileCount: number;
|
||||||
children?: TreeNodeData[];
|
children?: TreeNodeData[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
interface TreeProps {
|
interface TreeProps {
|
||||||
data: TreeNodeData[];
|
data: TreeNodeData[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const Tree: React.FC<TreeProps> = () => {
|
const Tree: React.FC<TreeProps> = () => {
|
||||||
const [folders, setFolders] = React.useState<TreeNodeData>();
|
const [rootNode, setRootNode] = React.useState<TreeNodeData | null>(null);
|
||||||
|
|
||||||
function countTotalFolderChildren(root: TreeNodeData): number {
|
// Helper function to recursively render TreeNodeData
|
||||||
let count = 0;
|
const renderTreeNode = (node: TreeNodeData, rootSize: number) => {
|
||||||
|
return (
|
||||||
|
<TreeNode key={node.path} node={node} rootSize={rootSize}>
|
||||||
|
{node.children &&
|
||||||
|
node.children.map((child) => renderTreeNode(child, rootSize))}
|
||||||
|
</TreeNode>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const countTotalSizeFolderChildren = (root: TreeNodeData): number => {
|
||||||
|
let size = 0;
|
||||||
|
|
||||||
if (root.children && root.children.length > 0) {
|
if (root.children && root.children.length > 0) {
|
||||||
for (const child of root.children) {
|
for (const child of root.children) {
|
||||||
if (child.type === 'folder') {
|
if (child.type === "folder") {
|
||||||
count += child.subfolderCount + countTotalFolderChildren(child);
|
size += child.totalSize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return size;
|
||||||
}
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
listen<any>('folders-loaded', (event) => {
|
listen<any>("folders-loaded", (event) => {
|
||||||
setFolders(event.payload)
|
setRootNode(event.payload);
|
||||||
});
|
});
|
||||||
|
|
||||||
setInterval(async () => {
|
setInterval(async () => {
|
||||||
invoke('load_folders')
|
invoke("load_folders")
|
||||||
.then(() => console.log("Folders loaded"))
|
.then(() => console.log("Folders loaded"))
|
||||||
.catch(console.error)
|
.catch(console.error);
|
||||||
}, 5000)
|
}, 5000);
|
||||||
}, [])
|
}, []);
|
||||||
|
|
||||||
|
if (!rootNode) {
|
||||||
|
return <div>Loading...</div>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const rootSize = countTotalSizeFolderChildren(rootNode);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="tree">
|
<div className="tree">
|
||||||
{folders.map((node) => (
|
{renderTreeNode(rootNode, rootSize)}
|
||||||
<TreeNode key={node.path} node={node} rootSize={countTotalFolderChildren(folders)} />
|
</div>
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -3,13 +3,13 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
|
|
||||||
interface TreeNodeData {
|
interface TreeNodeData {
|
||||||
name: string;
|
name: string;
|
||||||
type: string;
|
path: string;
|
||||||
path: string;
|
type: "folder" | "file"; // Specific values for 'type'
|
||||||
subfolders: number;
|
totalSize: number;
|
||||||
size: number;
|
subfolderCount: number;
|
||||||
files: number;
|
fileCount: number;
|
||||||
children?: TreeNodeData[];
|
children?: TreeNodeData[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TreeNodeProps {
|
interface TreeNodeProps {
|
||||||
@ -17,6 +17,16 @@ interface TreeNodeProps {
|
|||||||
rootSize: number;
|
rootSize: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function beautifyBytes(bytes: number): string {
|
||||||
|
if (bytes === 0) return "0 Bytes";
|
||||||
|
|
||||||
|
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
||||||
|
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
||||||
|
const readableSize = (bytes / Math.pow(1024, i)).toFixed(2);
|
||||||
|
|
||||||
|
return `${readableSize} ${sizes[i]}`;
|
||||||
|
}
|
||||||
|
|
||||||
const TreeNode: React.FC<TreeNodeProps> = ({ node, rootSize }) => {
|
const TreeNode: React.FC<TreeNodeProps> = ({ node, rootSize }) => {
|
||||||
const [isExpanded, setIsExpanded] = useState(false);
|
const [isExpanded, setIsExpanded] = useState(false);
|
||||||
|
|
||||||
@ -24,14 +34,15 @@ const TreeNode: React.FC<TreeNodeProps> = ({ node, rootSize }) => {
|
|||||||
setIsExpanded(!isExpanded);
|
setIsExpanded(!isExpanded);
|
||||||
};
|
};
|
||||||
|
|
||||||
const percentage = (node.size / rootSize) * 100;
|
const percentage = (node.totalSize / rootSize) * 100;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="treeNode">
|
<div className="treeNode">
|
||||||
<div className="nodeHeader" onClick={toggleExpand}>
|
<div className="nodeHeader" onClick={toggleExpand}>
|
||||||
<span className="nodeName">{node.name}</span>
|
<span className="nodeName">{node.name}</span>
|
||||||
<span className="subfolderBadge">Subfolders: {node.subfolders}</span>
|
<span className="subfolderBadge">Subfolders: {node.subfolderCount}</span>
|
||||||
<span className="fileBadge">Files: {node.files}</span>
|
<span className="fileBadge">Files: {node.fileCount}</span>
|
||||||
|
<span className="fileBadge">Size: {beautifyBytes(node.totalSize)}</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="progressBarContainer">
|
<div className="progressBarContainer">
|
||||||
<div
|
<div
|
||||||
|
@ -89,4 +89,4 @@ body {
|
|||||||
|
|
||||||
.children {
|
.children {
|
||||||
margin-left: 10px;
|
margin-left: 10px;
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,10 @@ pub fn load_folder(path : PathBuf) -> FolderData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for child in &children {
|
||||||
|
total_size += child.total_size;
|
||||||
|
}
|
||||||
|
|
||||||
FolderData {
|
FolderData {
|
||||||
name: path.file_name().unwrap().to_str().unwrap().to_string(),
|
name: path.file_name().unwrap().to_str().unwrap().to_string(),
|
||||||
path: path.to_str().unwrap().to_string(),
|
path: path.to_str().unwrap().to_string(),
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
)]
|
)]
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
mod folder_crawler;
|
mod folder_crawler;
|
||||||
use tauri::{AppHandle, Emitter, EventTarget};
|
use tauri::{AppHandle, Emitter};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user