Skip to content

Commit

Permalink
refactor(tauri): minor changes (#1255)
Browse files Browse the repository at this point in the history
  • Loading branch information
doums authored Oct 7, 2024
1 parent 12fb73e commit 48722dc
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 38 deletions.
4 changes: 1 addition & 3 deletions nym-vpn-app/src-tauri/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ use anyhow::{anyhow, Result};
use clap::{Parser, Subcommand};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::path::PathBuf;
use std::str::FromStr;
use std::{path::PathBuf, sync::Arc};
use tauri::PackageInfo;
use tracing::{error, info};

pub type ManagedCli = Arc<Cli>;

#[derive(Parser, Serialize, Deserialize, Debug, Clone)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
Expand Down
7 changes: 2 additions & 5 deletions nym-vpn-app/src-tauri/src/commands/cli.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use tauri::State;
use tracing::{debug, instrument};

use crate::{
cli::{Cli, ManagedCli},
error::BackendError,
};
use crate::{cli::Cli, error::BackendError};

#[instrument(skip_all)]
#[tauri::command]
pub fn cli_args(cli: State<'_, ManagedCli>) -> Result<&Cli, BackendError> {
pub fn cli_args(cli: State<'_, Cli>) -> Result<&Cli, BackendError> {
debug!("cli_args");
Ok(cli.inner())
}
7 changes: 3 additions & 4 deletions nym-vpn-app/src-tauri/src/commands/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use nym_vpn_proto::entry_node::EntryNodeEnum;
use nym_vpn_proto::exit_node::ExitNodeEnum;
use nym_vpn_proto::{EntryNode, ExitNode, Location};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tauri::State;
use tracing::{debug, error, info, instrument};
use ts_rs::TS;
Expand All @@ -31,7 +30,7 @@ pub struct ConnectionStateResponse {
#[tauri::command]
pub async fn get_connection_state(
state: State<'_, SharedAppState>,
grpc: State<'_, Arc<GrpcClient>>,
grpc: State<'_, GrpcClient>,
) -> Result<ConnectionStateResponse, BackendError> {
debug!("get_connection_state");
let res = grpc.vpn_status().await?;
Expand All @@ -50,7 +49,7 @@ pub async fn get_connection_state(
pub async fn connect(
app: tauri::AppHandle,
state: State<'_, SharedAppState>,
grpc: State<'_, Arc<GrpcClient>>,
grpc: State<'_, GrpcClient>,
entry: NodeLocation,
exit: NodeLocation,
) -> Result<ConnectionState, BackendError> {
Expand Down Expand Up @@ -168,7 +167,7 @@ pub async fn connect(
pub async fn disconnect(
app: tauri::AppHandle,
state: State<'_, SharedAppState>,
grpc: State<'_, Arc<GrpcClient>>,
grpc: State<'_, GrpcClient>,
) -> Result<ConnectionState, BackendError> {
debug!("disconnect");
let mut app_state = state.lock().await;
Expand Down
3 changes: 1 addition & 2 deletions nym-vpn-app/src-tauri/src/commands/country.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use nym_vpn_proto::GatewayType;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tauri::State;
use tracing::{debug, instrument};
use ts_rs::TS;
Expand All @@ -23,7 +22,7 @@ pub enum NodeType {
pub async fn get_countries(
vpn_mode: VpnMode,
node_type: Option<NodeType>,
grpc: State<'_, Arc<GrpcClient>>,
grpc: State<'_, GrpcClient>,
) -> Result<Vec<Country>, BackendError> {
debug!("get_countries");
let gw_type = match vpn_mode {
Expand Down
4 changes: 1 addition & 3 deletions nym-vpn-app/src-tauri/src/commands/credential.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use tauri::State;
use tracing::{debug, info, instrument, warn};

Expand All @@ -12,7 +10,7 @@ use crate::{
#[tauri::command]
pub async fn add_credential(
credential: String,
grpc: State<'_, Arc<GrpcClient>>,
grpc: State<'_, GrpcClient>,
) -> Result<Option<i64>, BackendError> {
debug!("add_credential");

Expand Down
7 changes: 2 additions & 5 deletions nym-vpn-app/src-tauri/src/commands/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::error::BackendError;
use crate::grpc::client::{GrpcClient, VpndStatus};
use crate::states::SharedAppState;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tauri::State;
use tracing::{debug, instrument, warn};
use ts_rs::TS;
Expand All @@ -18,7 +17,7 @@ pub struct DaemonInfo {
#[tauri::command]
pub async fn daemon_status(
app_state: State<'_, SharedAppState>,
grpc_client: State<'_, Arc<GrpcClient>>,
grpc_client: State<'_, GrpcClient>,
) -> Result<VpndStatus, BackendError> {
debug!("daemon_status");
let status = grpc_client
Expand All @@ -34,9 +33,7 @@ pub async fn daemon_status(

#[instrument(skip_all)]
#[tauri::command]
pub async fn daemon_info(
grpc_client: State<'_, Arc<GrpcClient>>,
) -> Result<DaemonInfo, BackendError> {
pub async fn daemon_info(grpc_client: State<'_, GrpcClient>) -> Result<DaemonInfo, BackendError> {
debug!("daemon_info");
let res = grpc_client.vpnd_info().await.inspect_err(|e| {
warn!("failed to get daemon info: {:?}", e);
Expand Down
9 changes: 4 additions & 5 deletions nym-vpn-app/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use std::path::PathBuf;
use std::process::exit;
use std::sync::Arc;
use std::time::Duration;

use crate::cli::{db_command, Commands};
Expand Down Expand Up @@ -150,11 +149,11 @@ async fn main() -> Result<()> {
.plugin(tauri_plugin_updater::Builder::new().build())
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_shell::init())
.manage(Arc::new(Mutex::new(app_state)))
.manage(Arc::new(app_config))
.manage(Arc::new(cli.clone()))
.manage(Mutex::new(app_state))
.manage(app_config)
.manage(cli.clone())
.manage(db.clone())
.manage(Arc::new(grpc.clone()))
.manage(grpc.clone())
.setup(move |app| {
info!("app setup");

Expand Down
3 changes: 1 addition & 2 deletions nym-vpn-app/src-tauri/src/states/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::sync::Arc;
use tokio::sync::Mutex;

pub mod app;

pub type SharedAppState = Arc<Mutex<app::AppState>>;
pub type SharedAppState = Mutex<app::AppState>;
4 changes: 1 addition & 3 deletions nym-vpn-app/src-tauri/src/tray.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use anyhow::Result;
use strum::AsRefStr;
use tauri::image::Image;
Expand Down Expand Up @@ -57,7 +55,7 @@ fn on_menu_event(app: &AppHandle, event: MenuEvent) {
let c_app = app.clone();
tokio::spawn(async move {
let state = c_app.state::<SharedAppState>();
let grpc = c_app.state::<Arc<GrpcClient>>();
let grpc = c_app.state::<GrpcClient>();

let app_state = state.lock().await;
if let ConnectionState::Connected = app_state.state {
Expand Down
7 changes: 2 additions & 5 deletions nym-vpn-app/src/pages/home/ConnectionBadge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import clsx from 'clsx';
import { useTranslation } from 'react-i18next';
import { ConnectionState } from '../../types';
import { AnimateIn } from '../../ui';
import { type } from '@tauri-apps/plugin-os';

const os = type();

function ConnectionBadge({ state }: { state: ConnectionState }) {
const { t } = useTranslation('home');
Expand Down Expand Up @@ -70,14 +67,14 @@ function ConnectionBadge({ state }: { state: ConnectionState }) {
'relative flex justify-center items-center',
// use static pixel sizes for animated elements to avoid glitches
// with the different UI scaling factors
os === 'windows' ? 'h-[12px] w-[12px]' : 'h-[14px] w-[14px]',
'h-[12px] w-[12px]',
])}
>
<div className="animate-ping absolute h-full w-full rounded-full bg-cornflower opacity-75" />
<div
className={clsx([
'relative rounded-full bg-cornflower',
os === 'windows' ? 'h-[8px] w-[8px]' : 'h-[10px] w-[10px]',
'h-[8px] w-[8px]',
])}
/>
</div>
Expand Down
2 changes: 1 addition & 1 deletion nym-vpn-app/src/ui/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function Spinner() {
<span
className={clsx([
'loader',
os === 'linux' ? 'h-[32px] w-[32px]' : 'h-[22px] w-[22px] border-4',
os === 'linux' ? 'h-[28px] w-[28px]' : 'h-[22px] w-[22px] border-4',
'border:white dark:border-[#2c2b2e] border-b-transparent dark:border-b-transparent',
])}
></span>
Expand Down

0 comments on commit 48722dc

Please sign in to comment.