-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d55a76f
commit 3939a70
Showing
13 changed files
with
187 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,49 @@ | ||
use std::path::Path; | ||
|
||
use serde::Serialize; | ||
use specta::Type; | ||
|
||
#[derive(Serialize, Type)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct FileEntry { | ||
pub base_path: String, | ||
pub name: String, | ||
pub is_dir: bool, | ||
pub children: Option<Vec<FileEntry>>, | ||
} | ||
|
||
#[tauri::command] | ||
#[specta::specta] | ||
pub fn show_files(input: String) -> Vec<FileEntry> { | ||
let path = std::path::Path::new(&input); | ||
println!("Reading files from: {:?}", path); | ||
fn read_directory(input: String, depth: usize) -> Vec<FileEntry> { | ||
let target_path = Path::new(&input); | ||
|
||
return std::fs::read_dir(path) | ||
std::fs::read_dir(target_path) | ||
.unwrap() | ||
.map(|entry| { | ||
let entry = entry.unwrap(); | ||
.filter_map(|entry| { | ||
let entry = entry.ok()?; | ||
let path = entry.path(); | ||
let is_dir = path.is_dir(); | ||
let name = entry.file_name().into_string().unwrap(); | ||
let name = entry.file_name().into_string().ok()?; | ||
|
||
let children = if is_dir && depth < 3 { | ||
Some(read_directory(path.to_str()?.to_string(), depth + 1)) | ||
} else { | ||
None | ||
}; | ||
|
||
FileEntry { name, is_dir } | ||
Some(FileEntry { | ||
base_path: input.clone(), | ||
name, | ||
is_dir, | ||
children, | ||
}) | ||
}) | ||
.collect(); | ||
.collect() | ||
} | ||
|
||
#[tauri::command] | ||
#[specta::specta] | ||
pub fn show_files(input: String) -> Vec<FileEntry> { | ||
let target_path = Path::new(&input); | ||
println!("Reading files from: {:?}", target_path); | ||
|
||
return read_directory(input, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { join } from "@tauri-apps/api/path"; | ||
import naturalCompare from "natural-compare-lite"; | ||
import { type FileEntry } from "@/types/bindings"; | ||
import { type FileTreeData } from "@/types/file"; | ||
|
||
export function sortFileEntriesNaturally( | ||
fileEntries: FileEntry[], | ||
): FileEntry[] { | ||
const dirs = fileEntries.filter((e) => e.isDir); | ||
const files = fileEntries.filter((e) => !e.isDir); | ||
|
||
const sortedDirs = dirs.sort((a, b) => naturalCompare(a.name, b.name)); | ||
const sortedFiles = files.sort((a, b) => naturalCompare(a.name, b.name)); | ||
|
||
return [...sortedDirs, ...sortedFiles]; | ||
} | ||
|
||
export async function processFileEntries( | ||
entries: FileEntry[], | ||
): Promise<FileTreeData[]> { | ||
return await Promise.all( | ||
sortFileEntriesNaturally(entries).map(async (f) => ({ | ||
...f, | ||
id: await join(f.basePath, f.name), | ||
children: | ||
f.children != null ? await processFileEntries(f.children) : null, | ||
})), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { getCurrent } from "@tauri-apps/api/window"; | ||
import { VStack, styled as p } from "panda/jsx"; | ||
import { useEffect, useState, type ReactElement } from "react"; | ||
import { FileTree } from "@/components/FileTree"; | ||
import { api } from "@/lib/services/api"; | ||
import { type FileEntry } from "@/types/bindings"; | ||
|
||
export default function Page(): ReactElement { | ||
const [fileEntries, setFileEntries] = useState<FileEntry[]>([]); | ||
|
||
useEffect(() => { | ||
const unListen = getCurrent().onDragDropEvent((ev) => { | ||
if (ev.payload.type !== "dropped") { | ||
return; | ||
} | ||
const path = ev.payload.paths.at(0); | ||
if (path == null) throw new Error("Path at 0 is null!"); | ||
void api.showFiles(path).then((result) => { | ||
setFileEntries(result); | ||
}); | ||
}); | ||
|
||
return () => { | ||
void unListen.then((fn) => { | ||
fn(); | ||
}); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<VStack> | ||
<p.div> | ||
<p.code>{fileEntries.length}</p.code> | ||
</p.div> | ||
<FileTree fileEntries={fileEntries} /> | ||
</VStack> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
// Generouted, changes to this file will be overriden | ||
/* eslint-disable */ | ||
|
||
import { components, hooks, utils } from "@generouted/react-router/client"; | ||
import { components, hooks, utils } from '@generouted/react-router/client' | ||
|
||
export type Path = `/` | `/viewer`; | ||
export type Path = | ||
| `/` | ||
| `/license/new` | ||
| `/viewer` | ||
|
||
export type Params = {}; | ||
export type Params = { | ||
|
||
} | ||
|
||
export type ModalPath = never; | ||
export type ModalPath = never | ||
|
||
export const { Link, Navigate } = components<Path, Params>(); | ||
export const { useModals, useNavigate, useParams } = hooks< | ||
Path, | ||
Params, | ||
ModalPath | ||
>(); | ||
export const { redirect } = utils<Path, Params>(); | ||
export const { Link, Navigate } = components<Path, Params>() | ||
export const { useModals, useNavigate, useParams } = hooks<Path, Params, ModalPath>() | ||
export const { redirect } = utils<Path, Params>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { type FileEntry } from "./bindings"; | ||
|
||
export type FileTreeData = FileEntry & { | ||
id: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,18 @@ | ||
export type Override<T, U extends { [Key in keyof T]?: unknown }> = Omit< | ||
T, | ||
keyof U | ||
> & | ||
U; | ||
|
||
export type Entries<T> = Array< | ||
keyof T extends infer U ? (U extends keyof T ? [U, T[U]] : never) : never | ||
>; | ||
|
||
export type ArrayElem<ArrayType extends readonly unknown[]> = | ||
ArrayType extends ReadonlyArray<infer ElementType> ? ElementType : never; | ||
|
||
export type OmitStrict<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>; | ||
|
||
export type Nullable<T> = T | null | undefined; | ||
|
||
export type State<T> = [T, (value: T) => void]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters