Skip to content

Commit

Permalink
perf: removed unneeded + optimised some rust
Browse files Browse the repository at this point in the history
  • Loading branch information
kul-sudo committed Sep 16, 2023
1 parent 10aaffb commit 25a236d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 80 deletions.
42 changes: 23 additions & 19 deletions lib/useUndoRedo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Reducer, useReducer } from 'react'

const SET_STATE = 0
const UNDO = 1
const REDO = 2
enum ActionTypes {
SET_STATE,
UNDO,
REDO
}

type UndoRedoObject = {
past: string[]
Expand All @@ -11,31 +13,32 @@ type UndoRedoObject = {
}

type ActionObject = {
type: 0 | 1 | 2
type: ActionTypes
data?: string
}

const reducerWithUndoRedo: Reducer<UndoRedoObject, ActionObject> = (state, action) => {
const { past, present, future } = state

switch (action.type) {
case SET_STATE:
case ActionTypes.SET_STATE:
return {
past: [...past, present],
...state,
past: [...state.past, state.present],
present: action.data!,
future: []
}
case UNDO:
case ActionTypes.UNDO:
return {
past: past.slice(0, past.length - 1),
present: past[past.length - 1],
future: [present, ...future]
...state,
past: state.past.slice(0, -1),
present: state.past[state.past.length - 1],
future: [state.present, ...state.future]
}
case REDO:
case ActionTypes.REDO:
return {
past: [...past, present],
present: future[0],
future: future.slice(1)
...state,
past: [...state.past, state.present],
present: state.future[0],
future: state.future.slice(1)
}
default:
throw new Error()
Expand All @@ -51,9 +54,10 @@ const useUndoRedo = (initialState: string) => {

const { past, present, future }: { past: string[], present: string, future: string[] } = state

const setState = (newState: string) => dispatch({ type: SET_STATE, data: newState })
const undo = () => dispatch({ type: UNDO })
const redo = () => dispatch({ type: REDO })
const setState = (newState: string) => dispatch({ type: ActionTypes.SET_STATE, data: newState })
const undo = () => dispatch({ type: ActionTypes.UNDO })
const redo = () => dispatch({ type: ActionTypes.REDO })

const isUndoPossible = past && past.length > 0
const isRedoPossible = future && future.length > 0

Expand Down
102 changes: 50 additions & 52 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type RowProps = {
}

type VolumesListProps = {
is_removable: boolean
kind: 'HDD' | 'SSD'
mountpoint: string
available_gb: number
used_gb: number
Expand Down Expand Up @@ -119,7 +121,7 @@ const Home: FC = () => {
return (
<>
<VStack position="fixed" top="2" right="2">
<Input isDisabled={isSearching} placeholder="Directory" width="10rem" variant="filled" ref={directoryRef} onKeyDown={async event => {
<Input ref={directoryRef} isDisabled={isSearching} placeholder="Directory" width="10rem" variant="filled" onKeyDown={async event => {
if (event.key === 'Enter') {
if (directoryRef.current) {
if (directoryRef.current.value !== currentDirectory) {
Expand Down Expand Up @@ -148,65 +150,61 @@ const Home: FC = () => {

{isSearching && (
<Button variant="outline" onClick={() => {
invoke('set_stop', { value: true })
invoke('stop_finding')
}}>Stop</Button>
)}
</VStack>

<HStack>
<Box height="100vh" position="fixed" top="0" left="0" backgroundColor="blackAlpha.400">
<VStack pt="0.5rem" px="0.5rem">
{Array.from([
{ name: 'Desktop', directory: apiPath?.desktopDir },
{ name: 'Home', directory: apiPath?.homeDir },
{ name: 'Documents', directory: apiPath?.documentDir },
{ name: 'Downloads', directory: apiPath?.downloadDir },
{ name: 'Pictures', directory: apiPath?.pictureDir },
{ name: 'Music', directory: apiPath?.audioDir },
{ name: 'Videos', directory: apiPath?.videoDir }
] as FolderReferencesProps[]).map((section, index) => {
return (
<Button
isDisabled={isSearching}
width="7rem"
rounded="full"
key={index}
onClick={async () => {
setCurrentDirectory(await section.directory())
}}
>{section.name}</Button>
)
})}

<Button width="7rem" variant="outline" onClick={() => {
invoke('get_volumes').then(volumes => {
const volumesWithTypes = volumes as VolumesListProps

setVolumesList(volumesWithTypes)
})
}}>
<HStack>
<Text>Load</Text>
<HardDriveIcon />
</HStack>
</Button>

{volumesList.map((volume, index) => {
<VStack pt="0.5rem" px="0.5rem" height="100vh" position="fixed" top="0" left="0" backgroundColor="blackAlpha.400" overflowY="scroll">
{([
{ name: 'Desktop', directory: apiPath?.desktopDir },
{ name: 'Home', directory: apiPath?.homeDir },
{ name: 'Documents', directory: apiPath?.documentDir },
{ name: 'Downloads', directory: apiPath?.downloadDir },
{ name: 'Pictures', directory: apiPath?.pictureDir },
{ name: 'Music', directory: apiPath?.audioDir },
{ name: 'Videos', directory: apiPath?.videoDir }
] as FolderReferencesProps[]).map((section, index) => {
return (
<VStack key={index}>
<Tooltip label={volume.mountpoint} placement="top">
<Button variant="outline" rounded="full" onClick={() => {
setCurrentDirectory(volume.mountpoint)
}}>
<HardDriveIcon />
</Button>
</Tooltip>
<Progress value={volume.used_gb / volume.total_gb * 100} width="5rem" />
</VStack>
<Button
key={index}
isDisabled={isSearching}
width="7rem"
rounded="full"
onClick={async () => {
setCurrentDirectory(await section.directory())
}}
>{section.name}</Button>
)
})}
</VStack>
</Box>

<Button width="7rem" variant="outline" onClick={() => {
invoke('get_volumes').then(volumes => {
const volumesWithTypes = volumes as VolumesListProps

setVolumesList(volumesWithTypes)
})
}}>
<HStack>
<Text>Load</Text>
<HardDriveIcon />
</HStack>
</Button>

{volumesList.map((volume, index) => (
<VStack key={index}>
<Tooltip label={`${(volume.is_removable ? 'Removable': (volume.kind === 'SSD' ? 'SSD' : 'HDD'))} ${volume.mountpoint}`} placement="top">
<Button variant="outline" rounded="full" onClick={() => {
setCurrentDirectory(volume.mountpoint)
}}>
<HardDriveIcon />
</Button>
</Tooltip>
<Progress value={volume.used_gb / volume.total_gb * 100} width="5rem" />
</VStack>
))}
</VStack>

<VStack alignItems="start" position="relative" left="9rem">
<HStack mt="1rem">
Expand Down
24 changes: 15 additions & 9 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ use sysinfo::{System, SystemExt, Disk, DiskExt};
use serde::Serialize;

lazy_static! {
static ref STOP: Mutex<bool> = Mutex::new(false);
static ref STOP_FINDING: Mutex<bool> = Mutex::new(false);
}

fn bytes_to_gb(bytes: u64) -> u16 {
(bytes / (1e+9 as u64)) as u16
return (bytes / (1e+9 as u64)) as u16
}

#[derive(Serialize)]
pub struct Volume {
is_removable: bool,
kind: String,
mountpoint: PathBuf,
available_gb: u16,
used_gb: u16,
Expand All @@ -33,8 +35,12 @@ impl Volume {
let total_gb = bytes_to_gb(disk.total_space());

let mountpoint = disk.mount_point().to_path_buf();

let kind = format!("{:?}", disk.kind());
let is_removable = disk.is_removable();

Self {
is_removable,
kind,
mountpoint,
available_gb,
used_gb,
Expand All @@ -54,16 +60,16 @@ fn get_volumes() -> Result<Vec<Volume>, ()> {
.map(|disk| {
let volume = Volume::from(disk);

volume
return volume
})
.collect();

Ok(volumes)
}

#[tauri::command(async)]
async fn set_stop(value: bool) {
*STOP.lock().unwrap() = value
async fn stop_finding() {
*STOP_FINDING.lock().unwrap() = true
}

fn remove_extension(full_filename: &str) -> String {
Expand Down Expand Up @@ -107,8 +113,8 @@ async fn find_files_and_folders(app_handle: AppHandle, command: String) {
.into_iter()
.filter_map(|entry: Result<walkdir::DirEntry, walkdir::Error>| entry.ok())
.filter(|entry| *directory != entry.path().to_string_lossy() && remove_extension(entry.file_name().to_str().unwrap()).contains(target_file) && is_not_hidden(entry, include_hidden)) {
if *STOP.lock().unwrap() {
*STOP.lock().unwrap() = false;
if *STOP_FINDING.lock().unwrap() {
*STOP_FINDING.lock().unwrap() = false;
return
}

Expand All @@ -126,7 +132,7 @@ async fn find_files_and_folders(app_handle: AppHandle, command: String) {
#[tokio::main]
async fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![open_file_in_default_application, find_files_and_folders, read_directory, set_stop, get_volumes])
.invoke_handler(tauri::generate_handler![open_file_in_default_application, find_files_and_folders, read_directory, stop_finding, get_volumes])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

0 comments on commit 25a236d

Please sign in to comment.