Skip to content

Commit

Permalink
fix(glowsquid): 🗃️ fix database handling
Browse files Browse the repository at this point in the history
Signed-off-by: Suyashtnt <suyashtnt@gmail.com>
  • Loading branch information
Suya1671 committed Jun 23, 2024
1 parent 5930799 commit 57d0ba4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
14 changes: 14 additions & 0 deletions apps/glowsquid-frontend/src/lib/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ try {
else return { status: "error", error: e as any };
}
},
/**
* Removes an account
*
* # Returns
* returns Ok(()) if the account was removed successfully, otherwise returns Err(())
*/
async removeAccount(accountIndex: number) : Promise<Result<null, null>> {
try {
return { status: "ok", data: await TAURI_INVOKE("remove_account", { accountIndex }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
/**
* Switch to a different account
*
Expand Down
15 changes: 15 additions & 0 deletions apps/glowsquid/src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ pub async fn switch_to_account(
}
}

#[tauri::command]
#[specta::specta]
/// Removes an account
///
/// # Returns
/// returns Ok(()) if the account was removed successfully, otherwise returns Err(())
pub async fn remove_account(
state: tauri::State<'_, state::State>,
account_index: u8,
) -> Result<(), ()> {
state.remove_account(account_index).await;

Ok(())
}

#[tauri::command]
#[specta::specta]
/// Add a new account
Expand Down
1 change: 1 addition & 0 deletions apps/glowsquid/src/auth/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ impl State {
pub async fn remove_account(&self, account_index: u8) {
let mut state = self.frontend_state.lock().await;

// TODO: error handling if account_index is out of bounds
let profile = state.profiles.remove(account_index.into());
let id = profile.id();

Expand Down
11 changes: 3 additions & 8 deletions apps/glowsquid/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fn main() {
.commands(tauri_specta::collect_commands![
greet,
add_account,
remove_account,
switch_to_account
]);

Expand All @@ -34,14 +35,8 @@ fn main() {

tauri::Builder::default()
.setup(|app| {
let db_path = if cfg!(debug_assertions) {
let data_path = app.path().app_data_dir()?;
data_path.join("glowsquid.db3")
} else {
// use db/glowsquid.db3 relative to cwd
let cwd = std::env::current_dir()?;
cwd.join("db/glowsquid.db3")
};
let data_path = app.path().app_data_dir()?;
let db_path = data_path.join("glowsquid.db3");

if !db_path.exists() {
std::fs::File::create(&db_path)?;
Expand Down
2 changes: 1 addition & 1 deletion vitest.workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export default [
'libs/*/vite.config.ts',
'apps/*/vite.config.ts',
'apps/*/vitest.config.ts',
];
]

0 comments on commit 57d0ba4

Please sign in to comment.