Skip to content

Commit

Permalink
Add first tauri commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastinez committed Aug 13, 2024
1 parent 7dbb705 commit 218bd9f
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod node;
pub mod repos;
33 changes: 33 additions & 0 deletions src-tauri/src/commands/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use tauri::State;

use radicle::node::address::Store;
use radicle::node::Handle;
use radicle::{Node, Profile};

use crate::error::Error;
use crate::types::NodeInfo;

#[tauri::command]
pub fn node_info(profile: State<Profile>) -> Result<NodeInfo, Error> {
let node = Node::new(profile.socket());
let home = profile.home.database()?;
let agent = home
.get(&profile.public_key)
.unwrap_or_default()
.map(|node| node.agent);
let node_state = if node.is_running() {
"running"
} else {
"stopped"
};

Ok(NodeInfo {
id: profile.public_key,
alias: node.config().ok().map(|c| c.alias),
agent,
state: node_state.to_string(),
avatar_url: profile.config.web.avatar_url.clone(),
banner_url: profile.config.web.banner_url.clone(),
description: profile.config.web.description.clone(),
})
}
74 changes: 74 additions & 0 deletions src-tauri/src/commands/repos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use radicle::issue::cache::Issues;
use radicle::node::routing::Store;
use radicle::patch::cache::Patches;
use radicle::storage::{ReadRepository, ReadStorage};
use radicle::Profile;
use tauri::State;

use crate::types::Author;
use crate::{error::Error, types::RepositoryInfo};

#[tauri::command]
pub fn repo_list(profile: State<Profile>) -> Result<Vec<RepositoryInfo>, Error> {
let storage = &profile.storage;
let db = &profile.database()?;

let mut repos = storage.repositories()?.into_iter().collect::<Vec<_>>();
repos.sort_by_key(|p| p.rid);

let infos = repos
.into_iter()
.filter_map(|info| {
let Ok(repo) = storage.repository(info.rid) else {
return None;
};
let Ok((_, head)) = repo.head() else {
return None;
};
let Ok(commit) = repo.commit(head) else {
return None;
};
let Ok(payload) = info.doc.project() else {
return None;
};
let Ok(issues) = profile.issues(&repo) else {
return None;
};
let Ok(issues) = issues.counts() else {
return None;
};
let Ok(patches) = profile.patches(&repo) else {
return None;
};
let Ok(patches) = patches.counts() else {
return None;
};
let aliases = profile.aliases();
let delegates = info
.doc
.delegates
.into_iter()
.map(|did| Author::new(*did, &aliases))
.collect::<Vec<_>>();
let seeding = db.count(&info.rid).unwrap_or_default();
let last_update = commit.committer().when().seconds();

Some(RepositoryInfo {
name: payload.name().to_owned(),
description: payload.description().to_owned(),
default_branch: payload.default_branch().to_owned(),
delegates,
head,
threshold: info.doc.threshold,
visibility: info.doc.visibility,
issues,
patches,
id: info.rid,
seeding,
last_update,
})
})
.collect::<Vec<_>>();

Ok(infos)
}
15 changes: 10 additions & 5 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
mod commands;
mod error;
mod types;

use commands::{node, repos};
use radicle::Profile;

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
let profile = Profile::load().expect("Not able to load profile");

tauri::Builder::default()
.manage(profile)
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![greet])
.invoke_handler(tauri::generate_handler![repos::repo_list, node::node_info])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

0 comments on commit 218bd9f

Please sign in to comment.