From acc5c71bfa549953ce2e4f8468b7a4781f11f614 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Fri, 28 Jun 2024 20:35:18 -0400 Subject: [PATCH] rework error alerts --- src-tauri/src/app.rs | 1 + src-tauri/src/ipc.rs | 8 ++- src/ui/ErrorAlert.svelte | 49 ++++++++++++----- src/ui/PassphraseInput.svelte | 2 +- src/ui/settings/Setting.svelte | 3 +- src/views/Approve.svelte | 33 ++++++------ src/views/Home.svelte | 2 +- src/views/ManageCredentials.svelte | 2 +- src/views/Settings.svelte | 2 +- src/views/Unlock.svelte | 26 +++------ src/views/credentials/AwsCredential.svelte | 32 ++++-------- src/views/passphrase/EnterPassphrase.svelte | 58 ++++++++++++++------- src/views/passphrase/ResetPassphrase.svelte | 29 +++++------ 13 files changed, 135 insertions(+), 112 deletions(-) diff --git a/src-tauri/src/app.rs b/src-tauri/src/app.rs index 5ab5fd7..35eb634 100644 --- a/src-tauri/src/app.rs +++ b/src-tauri/src/app.rs @@ -58,6 +58,7 @@ pub fn run() -> tauri::Result<()> { ipc::save_config, ipc::launch_terminal, ipc::get_setup_errors, + ipc::exit, ]) .setup(|app| { let res = rt::block_on(setup(app)); diff --git a/src-tauri/src/ipc.rs b/src-tauri/src/ipc.rs index 8b8238e..98073ff 100644 --- a/src-tauri/src/ipc.rs +++ b/src-tauri/src/ipc.rs @@ -1,6 +1,6 @@ use serde::{Serialize, Deserialize}; use sqlx::types::Uuid; -use tauri::State; +use tauri::{AppHandle, State}; use crate::config::AppConfig; use crate::credentials::{ @@ -160,3 +160,9 @@ pub async fn launch_terminal(base: bool) -> Result<(), LaunchTerminalError> { pub async fn get_setup_errors(app_state: State<'_, AppState>) -> Result, ()> { Ok(app_state.setup_errors.clone()) } + + +#[tauri::command] +pub fn exit(app_handle: AppHandle) { + app_handle.exit(0) +} diff --git a/src/ui/ErrorAlert.svelte b/src/ui/ErrorAlert.svelte index 38e99ae..aa09adb 100644 --- a/src/ui/ErrorAlert.svelte +++ b/src/ui/ErrorAlert.svelte @@ -7,11 +7,34 @@ export let slideDuration = 150; let animationClass = ""; - export function shake() { + let error = null; + + function shake() { animationClass = 'shake'; window.setTimeout(() => animationClass = "", 400); } + export async function run(fallible) { + try { + const ret = await Promise.resolve(fallible()); + error = null; + return ret; + } + catch (e) { + if (error) shake(); + error = e; + // re-throw so it can be caught by the caller if necessary + throw e; + } + } + + // this is a method rather than a prop so that we can re-shake every time + // the error occurs, even if the error message doesn't change + export function setError(e) { + if (error) shake(); + error = e; + } + @@ -51,15 +74,17 @@ -
- - - - +{#if error} +
+ + + {error.msg || error} + - {#if $$slots.buttons} -
- -
- {/if} -
+ {#if $$slots.buttons} +
+ +
+ {/if} +
+{/if} diff --git a/src/ui/PassphraseInput.svelte b/src/ui/PassphraseInput.svelte index ac7707e..bf7b4a9 100644 --- a/src/ui/PassphraseInput.svelte +++ b/src/ui/PassphraseInput.svelte @@ -23,9 +23,9 @@ value = e.target.value} on:input on:change on:focus on:blur class="input input-bordered flex-grow join-item placeholder:text-gray-500 {classes}" - on:input={e => value = e.target.value} /> - - - - {/if} + + + + + + - + {:else if $appState.sessionStatus === 'locked'} diff --git a/src/views/Home.svelte b/src/views/Home.svelte index ea08ab3..27b741d 100644 --- a/src/views/Home.svelte +++ b/src/views/Home.svelte @@ -56,7 +56,7 @@ - + invoke('exit')}>

Exit

diff --git a/src/views/ManageCredentials.svelte b/src/views/ManageCredentials.svelte index 080ea3e..b33dc55 100644 --- a/src/views/ManageCredentials.svelte +++ b/src/views/ManageCredentials.svelte @@ -36,7 +36,7 @@

Credentials

-
+

AWS Access Keys

diff --git a/src/views/Settings.svelte b/src/views/Settings.svelte index 4bcaa4e..5ec308e 100644 --- a/src/views/Settings.svelte +++ b/src/views/Settings.svelte @@ -5,7 +5,6 @@ import { appState } from '../lib/state.js'; import Nav from '../ui/Nav.svelte'; import Link from '../ui/Link.svelte'; - import ErrorAlert from '../ui/ErrorAlert.svelte'; import SettingsGroup from '../ui/settings/SettingsGroup.svelte'; import Keybind from '../ui/settings/Keybind.svelte'; import { Setting, ToggleSetting, NumericSetting, FileSetting, TextSetting, TimeSetting } from '../ui/settings'; @@ -21,6 +20,7 @@ let error = null; async function save() { try { + throw('wtf'); await invoke('save_config', {config}); $appState.config = await invoke('get_config'); } diff --git a/src/views/Unlock.svelte b/src/views/Unlock.svelte index b0aba93..37372e7 100644 --- a/src/views/Unlock.svelte +++ b/src/views/Unlock.svelte @@ -17,34 +17,22 @@ const dispatch = createEventDispatcher(); - let errorMsg = null; let alert; let passphrase = ''; + let saving = false; async function unlock() { + saving = true; try { - saving = true; - let r = await invoke('unlock', {passphrase}); + await alert.run(async () => invoke('unlock', {passphrase})); $appState.sessionStatus = 'unlocked'; emit('unlocked'); dispatch('unlocked'); } - catch (e) { - const root = getRootCause(e); - if (e.code === 'GetSession' && root.code) { - errorMsg = `Error response from AWS (${root.code}): ${root.msg}`; - } - else { - errorMsg = e.msg; - } - - // if the alert already existed, shake it - if (alert) { - alert.shake(); - } - + finally { saving = false; } + } @@ -61,9 +49,7 @@