Skip to content

Commit

Permalink
Merge pull request #125 from pacholoamit/feat/implement-ts-rs
Browse files Browse the repository at this point in the history
feat(types): implement ts-rs
  • Loading branch information
pacholoamit authored May 16, 2024
2 parents 8fbdb3f + 5b4f7fb commit bfe7a45
Show file tree
Hide file tree
Showing 19 changed files with 142 additions and 117 deletions.
52 changes: 52 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", br
tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tauri-plugin-autostart = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
log = "^0.4"

tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread"] }
ts-rs = "8.1"
# surrealdb = { version = "1.5.0" }



Expand Down
57 changes: 34 additions & 23 deletions src-tauri/src/models.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use ts_rs::TS;

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, TS)]
#[ts(export, export_to = "../../src/lib/bindings/")]
pub struct Timestamp(pub i64);

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "../../src/lib/bindings/")]
pub struct Memory {
#[ts(type = "number")]
pub free: u64,
#[ts(type = "number")]
pub total: u64,
#[ts(type = "number")]
pub used: u64,
#[ts(type = "number")]
pub used_percentage: f64,
pub timestamp: Timestamp,
}
Expand All @@ -18,8 +25,9 @@ pub trait MemoryTrait {
fn get_memory(&mut self) -> Memory;
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "../../src/lib/bindings/")]
pub struct GlobalCpu {
pub usage: f32,
pub brand: String,
Expand All @@ -33,8 +41,9 @@ pub trait GlobalCpuTrait {
fn get_global_cpu(&mut self) -> GlobalCpu;
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "../../src/lib/bindings/")]
pub struct Cpu {
pub name: String,
pub usage: f64,
Expand All @@ -45,12 +54,17 @@ pub trait CpuTrait {
fn get_cpus(&mut self) -> Vec<Cpu>;
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "../../src/lib/bindings/")]
pub struct Swap {
#[ts(type = "number")]
pub free: u64,
#[ts(type = "number")]
pub total: u64,
#[ts(type = "number")]
pub used: u64,
#[ts(type = "number")]
pub used_percentage: f64,
pub timestamp: Timestamp,
}
Expand All @@ -59,8 +73,9 @@ pub trait SwapTrait {
fn get_swap(&mut self) -> Swap;
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "../../src/lib/bindings/")]
pub struct SysInfo {
pub kernel_version: String,
pub os_version: String,
Expand All @@ -73,11 +88,14 @@ pub trait SystemInformationTrait {
fn get_system_information(&mut self) -> SysInfo;
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "../../src/lib/bindings/")]
pub struct Network {
pub name: String,
#[ts(type = "number")]
pub received: u64,
#[ts(type = "number")]
pub transmitted: u64,
pub timestamp: Timestamp,
}
Expand All @@ -86,12 +104,16 @@ pub trait NetworkTrait {
fn get_networks(&mut self) -> Vec<Network>;
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "../../src/lib/bindings/")]
pub struct Disk {
pub name: String,
#[ts(type = "number")]
pub free: u64,
#[ts(type = "number")]
pub total: u64,
#[ts(type = "number")]
pub used: u64,
pub mount_point: PathBuf,
pub file_system: String,
Expand All @@ -104,12 +126,15 @@ pub trait DisksTrait {
fn get_disks(&mut self) -> Vec<Disk>;
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "../../src/lib/bindings/")]
pub struct Process {
pub name: String,
pub pid: String,
#[ts(type = "number")]
pub cpu_usage: f32,
#[ts(type = "number")]
pub memory_usage: u64,
pub status: String,
}
Expand All @@ -118,17 +143,3 @@ pub trait ProcessesTrait {
fn get_processes(&mut self) -> Vec<Process>;
fn kill_process(&mut self, pid: &str) -> bool;
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Config {
pub user: User,
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct User {
pub email: String,
pub first_name: String,
pub last_name: String,
pub skipped_setup: bool,
pub user_hash: String,
}
2 changes: 1 addition & 1 deletion src/features/metrics/components/disks/disk.area-chart.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import formatBytes from "@/features/metrics/utils/format-bytes";
import { Enumerable } from "@/hooks/useServerEventsEnumerableStore";
import { Disk } from "@/lib/types";
import { Disk } from "@/lib";
import AreaChart, { useAreaChartState } from "@/components/area-chart";
import { useEffect } from "react";

Expand Down
13 changes: 3 additions & 10 deletions src/features/processes/components/processes.kill-verification.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Process } from "@/lib/types";
import { Process } from "@/lib";
import { Button, Modal, Text, Group, Space } from "@mantine/core";
import useKillProcess from "@/features/processes/hooks/useKillProcess";

Expand All @@ -14,9 +14,7 @@ const KillProcessVerification: React.FC<KillProcessVerificationProps> = ({
}) => {
const [kill] = useKillProcess({
onKill() {
setRecords((records) =>
records.filter((r) => r.pid !== selectedProcess?.pid)
);
setRecords((records) => records.filter((r) => r.pid !== selectedProcess?.pid));
},
});

Expand All @@ -29,12 +27,7 @@ const KillProcessVerification: React.FC<KillProcessVerificationProps> = ({
};

return (
<Modal
title="Are you sure?"
onClose={onCancel}
opened={!!selectedProcess}
centered
>
<Modal title="Are you sure?" onClose={onCancel} opened={!!selectedProcess} centered>
<Space h="md" />
<Text> Are you sure you want to kill {selectedProcess?.name}? </Text>
<Space h="xl" />
Expand Down
2 changes: 1 addition & 1 deletion src/features/processes/components/processes.table.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataTable, DataTableSortStatus } from "mantine-datatable";
import { Process } from "@/lib/types";
import { Process } from "@/lib";
import { memo } from "react";
import { Button } from "@mantine/core";

Expand Down
13 changes: 3 additions & 10 deletions src/features/processes/hooks/useKillProcess.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command, invoke } from "@/lib";
import { KillProcessOpts, Process } from "@/lib/types";
import { KillProcessOpts, Process } from "@/lib";
import notification from "@/utils/notification";
import { useCallback } from "react";

Expand All @@ -8,11 +8,7 @@ interface UseKillProcessOpts {
onFailure?: () => void;
}

const killProcess = async (
process: Process,
onKill?: () => void,
onFailure?: () => void
) => {
const killProcess = async (process: Process, onKill?: () => void, onFailure?: () => void) => {
const cmd = Command.KillProcess;
const isKilled = await invoke<KillProcessOpts, boolean>(cmd, {
pid: process.pid,
Expand All @@ -36,10 +32,7 @@ const killProcess = async (
const useKillProcess = (opts: UseKillProcessOpts) => {
const { onKill, onFailure } = opts;

const kill = useCallback(
(process: Process) => killProcess(process, onKill, onFailure),
[onKill, onFailure]
);
const kill = useCallback((process: Process) => killProcess(process, onKill, onFailure), [onKill, onFailure]);

return [kill] as const;
};
Expand Down
2 changes: 1 addition & 1 deletion src/features/processes/pages/processes.page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DataTableSortStatus } from "mantine-datatable";
import { TextInput } from "@mantine/core";
import { IconSearch } from "@tabler/icons-react";
import { Process } from "@/lib/types";
import { Process } from "@/lib";
import { useState, useEffect, useMemo } from "react";

import sortBy from "lodash.sortby";
Expand Down
4 changes: 4 additions & 0 deletions src/lib/bindings/Cpu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Timestamp } from "./Timestamp";

export type Cpu = { name: string, usage: number, timestamp: Timestamp, };
4 changes: 4 additions & 0 deletions src/lib/bindings/Disk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Timestamp } from "./Timestamp";

export type Disk = { name: string, free: number, total: number, used: number, mountPoint: string, fileSystem: string, diskType: string, isRemovable: boolean, timestamp: Timestamp, };
4 changes: 4 additions & 0 deletions src/lib/bindings/GlobalCpu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Timestamp } from "./Timestamp";

export type GlobalCpu = { usage: number, brand: string, frequency: bigint, name: string, vendor: string, timestamp: Timestamp, };
4 changes: 4 additions & 0 deletions src/lib/bindings/Memory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Timestamp } from "./Timestamp";

export type Memory = { free: number, total: number, used: number, usedPercentage: number, timestamp: Timestamp, };
4 changes: 4 additions & 0 deletions src/lib/bindings/Network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Timestamp } from "./Timestamp";

export type Network = { name: string, received: number, transmitted: number, timestamp: Timestamp, };
3 changes: 3 additions & 0 deletions src/lib/bindings/Process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type Process = { name: string, pid: string, cpuUsage: number, memoryUsage: number, status: string, };
4 changes: 4 additions & 0 deletions src/lib/bindings/Swap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Timestamp } from "./Timestamp";

export type Swap = { free: number, total: number, used: number, usedPercentage: number, timestamp: Timestamp, };
4 changes: 4 additions & 0 deletions src/lib/bindings/SysInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Timestamp } from "./Timestamp";

export type SysInfo = { kernelVersion: string, osVersion: string, hostname: string, coreCount: string, timestamp: Timestamp, };
3 changes: 3 additions & 0 deletions src/lib/bindings/Timestamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type Timestamp = bigint;
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "@/lib/types";
export * from "@/lib/store";
export * from "@/lib/autostart";
export * from "@/lib/commands";

Loading

0 comments on commit bfe7a45

Please sign in to comment.