move app state to store

This commit is contained in:
Joseph Montanaro 2023-04-25 08:49:00 -07:00
parent 865b7fd5c4
commit 6f9cd6b471
11 changed files with 63 additions and 65 deletions

View File

@ -2,25 +2,17 @@
import { emit, listen } from '@tauri-apps/api/event'; import { emit, listen } from '@tauri-apps/api/event';
import { invoke } from '@tauri-apps/api/tauri'; import { invoke } from '@tauri-apps/api/tauri';
import { appState } from './lib/state.js';
import { currentView } from './lib/routing.js'; import { currentView } from './lib/routing.js';
import queue from './lib/queue.js';
const VIEWS = import.meta.glob('./views/*.svelte', {eager: true});
var appState = {
currentRequest: null,
pendingRequests: queue(),
credentialStatus: 'locked',
}
listen('credentials-request', (tauriEvent) => { listen('credentials-request', (tauriEvent) => {
appState.pendingRequests.put(tauriEvent.payload); $appState.pendingRequests.put(tauriEvent.payload);
}); });
</script> </script>
<svelte:component <svelte:component
this={VIEWS[`./views/${$currentView}.svelte`].default} this="{$currentView}"
bind:appState={appState}
/> />
<!-- <svelte:component this="{VIEWS['./views/ShowApproved.svelte'].default}" bind:appState="{appState}" /> --> <!-- <svelte:component this="{VIEWS['./views/ShowApproved.svelte'].default}" bind:appState="{appState}" /> -->

View File

@ -1,8 +1,13 @@
import { writable } from 'svelte/store'; import { writable, derived } from 'svelte/store';
const VIEWS = import.meta.glob('../views/*.svelte', {eager: true});
export const currentView = writable('Home'); export let currentView = writable();
export function navigate(viewName) { export function navigate(viewName) {
currentView.set(viewName); let view = VIEWS[`../views/${viewName}.svelte`].default;
currentView.set(view);
} }
navigate('Home');

9
src/lib/state.js Normal file
View File

@ -0,0 +1,9 @@
import { writable } from 'svelte/store';
import queue from './queue.js';
export let appState = writable({
currentRequest: null,
pendingRequests: queue(),
credentialStatus: 'locked',
});

View File

@ -1,9 +1,9 @@
<script> <script>
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { currentView } from '../lib/routing.js'; import { navigate } from '../lib/routing.js';
export let target = $currentView; export let target;
export let hotkey = null; export let hotkey = null;
export let ctrl = false export let ctrl = false
export let alt = false; export let alt = false;
@ -11,7 +11,7 @@
function click() { function click() {
if (typeof target === 'string') { if (typeof target === 'string') {
$currentView = target; navigate(target);
} }
else if (typeof target === 'function') { else if (typeof target === 'function') {
target(); target();

View File

@ -2,11 +2,10 @@
import { invoke } from '@tauri-apps/api/tauri'; import { invoke } from '@tauri-apps/api/tauri';
import { navigate } from '../lib/routing.js'; import { navigate } from '../lib/routing.js';
import { appState } from '../lib/state.js';
import Link from '../ui/Link.svelte'; import Link from '../ui/Link.svelte';
import Icon from '../ui/Icon.svelte'; import Icon from '../ui/Icon.svelte';
export let appState;
async function approve() { async function approve() {
let status = await invoke('get_session_status'); let status = await invoke('get_session_status');
@ -22,8 +21,8 @@
} }
var appName = null; var appName = null;
if (appState.currentRequest.clients.length === 1) { if ($appState.currentRequest.clients.length === 1) {
let path = appState.currentRequest.clients[0].exe; let path = $appState.currentRequest.clients[0].exe;
let m = path.match(/\/([^/]+?$)|\\([^\\]+?$)/); let m = path.match(/\/([^/]+?$)|\\([^\\]+?$)/);
appName = m[1] || m[2]; appName = m[1] || m[2];
} }
@ -34,7 +33,7 @@
<!-- <div class="p-4 rounded-box border-2 border-neutral-content"> --> <!-- <div class="p-4 rounded-box border-2 border-neutral-content"> -->
<div class="space-y-1 mb-4"> <div class="space-y-1 mb-4">
<h2 class="text-xl font-bold">{appName ? `"${appName}"` : 'An appplication'} would like to access your AWS credentials.</h2> <h2 class="text-xl font-bold">{appName ? `"${appName}"` : 'An appplication'} would like to access your AWS credentials.</h2>
{#each appState.currentRequest.clients as client} {#each $appState.currentRequest.clients as client}
<p>Path: {client ? client.exe : 'Unknown'}</p> <p>Path: {client ? client.exe : 'Unknown'}</p>
<p>PID: {client ? client.pid : 'Unknown'}</p> <p>PID: {client ? client.pid : 'Unknown'}</p>
{/each} {/each}

View File

@ -3,11 +3,11 @@
import { invoke } from '@tauri-apps/api/tauri'; import { invoke } from '@tauri-apps/api/tauri';
import { getRootCause } from '../lib/errors.js'; import { getRootCause } from '../lib/errors.js';
import { appState } from '../lib/state.js';
import { navigate } from '../lib/routing.js'; import { navigate } from '../lib/routing.js';
import Link from '../ui/Link.svelte'; import Link from '../ui/Link.svelte';
import ErrorAlert from '../ui/ErrorAlert.svelte'; import ErrorAlert from '../ui/ErrorAlert.svelte';
export let appState;
let errorMsg = null; let errorMsg = null;
let alert; let alert;
@ -19,7 +19,7 @@
try { try {
await invoke('save_credentials', {credentials, passphrase}); await invoke('save_credentials', {credentials, passphrase});
if (appState.currentRequest) { if ($appState.currentRequest) {
navigate('ShowApproved'); navigate('ShowApproved');
} }
else { else {

View File

@ -1,18 +1,18 @@
<script> <script>
import { onMount, createEventDispatcher } from 'svelte'; import { onMount } from 'svelte';
import { invoke } from '@tauri-apps/api/tauri'; import { invoke } from '@tauri-apps/api/tauri';
import { appState } from '../lib/state.js';
import { navigate } from '../lib/routing.js'; import { navigate } from '../lib/routing.js';
import Nav from '../ui/Nav.svelte'; import Nav from '../ui/Nav.svelte';
import Icon from '../ui/Icon.svelte'; import Icon from '../ui/Icon.svelte';
import Link from '../ui/Link.svelte';
export let appState;
const dispatch = createEventDispatcher();
onMount(async () => { onMount(async () => {
// will block until a request comes in // will block until a request comes in
let req = await appState.pendingRequests.get(); let req = await $appState.pendingRequests.get();
appState.currentRequest = req; $appState.currentRequest = req;
navigate('Approve'); navigate('Approve');
}); });

View File

@ -1,8 +1,6 @@
<script> <script>
import Nav from '../ui/Nav.svelte'; import Nav from '../ui/Nav.svelte';
import Link from '../ui/Link.svelte'; import Link from '../ui/Link.svelte';
export let appState;
</script> </script>

View File

@ -1,28 +1,28 @@
<script> <script>
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { fly, draw, fade } from 'svelte/transition'; import { draw, fade } from 'svelte/transition';
import { expoIn, expoOut, expoInOut, linear } from 'svelte/easing';
import { emit } from '@tauri-apps/api/event'; import { emit } from '@tauri-apps/api/event';
import { invoke } from '@tauri-apps/api/tauri'; import { invoke } from '@tauri-apps/api/tauri';
import { appState } from '../lib/state.js';
import { navigate } from '../lib/routing.js'; import { navigate } from '../lib/routing.js';
import ErrorAlert from '../ui/ErrorAlert.svelte'; import ErrorAlert from '../ui/ErrorAlert.svelte';
import Icon from '../ui/Icon.svelte'; import Icon from '../ui/Icon.svelte';
import Link from '../ui/Link.svelte';
export let appState;
let success = false; let success = false;
let error = null; let error = null;
async function respond() { async function respond() {
let response = { let response = {
id: appState.currentRequest.id, id: $appState.currentRequest.id,
approval: 'Approved', approval: 'Approved',
}; };
try { try {
await invoke('respond', {response}); await invoke('respond', {response});
success = true; success = true;
appState.currentRequest = null; $appState.currentRequest = null;
window.setTimeout(() => navigate('Home'), 1000); window.setTimeout(() => navigate('Home'), 1000);
} }
catch (e) { catch (e) {
@ -44,20 +44,18 @@
<div class="flex flex-col h-screen items-center justify-center m-auto max-w-lg"> <div class="flex flex-col h-screen items-center justify-center m-auto max-w-lg">
<ErrorAlert> <ErrorAlert>
{error} {error}
<button <Link target="Home">
slot="buttons" <button
class="btn btn-sm bg-transparent hover:bg-[#cd5a5a] border border-error-content text-error-content" slot="buttons"
on:click="{() => navigate('Home')}" class="btn btn-sm bg-transparent hover:bg-[#cd5a5a] border border-error-content text-error-content"
> >
Ok Ok
</button> </button>
</Link>
</ErrorAlert> </ErrorAlert>
</div> </div>
{:else if success} {:else if success}
<!-- <div on:click="{() => {console.log('redraw'); k += 1}}" in:fly="{{x: '60vw', duration: 400, easing: expoOut}}" class="flex flex-col h-screen items-center justify-center max-w-max m-auto"> -->
<div class="flex flex-col h-screen items-center justify-center max-w-max m-auto"> <div class="flex flex-col h-screen items-center justify-center max-w-max m-auto">
<!-- <Icon name="check-circle" class="w-36 h-36" /> -->
<svg xmlns="http://www.w3.org/2000/svg" class="w-36 h-36" fill="none" viewBox="0 0 24 24" stroke-width="1" stroke="currentColor"> <svg xmlns="http://www.w3.org/2000/svg" class="w-36 h-36" fill="none" viewBox="0 0 24 24" stroke-width="1" stroke="currentColor">
<path in:draw="{{duration: 500}}" stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /> <path in:draw="{{duration: 500}}" stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg> </svg>

View File

@ -1,26 +1,26 @@
<script> <script>
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { fade, draw } from 'svelte/transition'; import { draw, fade } from 'svelte/transition';
import { expoOut } from 'svelte/easing';
import { emit } from '@tauri-apps/api/event'; import { emit } from '@tauri-apps/api/event';
import { invoke } from '@tauri-apps/api/tauri'; import { invoke } from '@tauri-apps/api/tauri';
import { appState } from '../lib/state.js';
import { navigate } from '../lib/routing.js'; import { navigate } from '../lib/routing.js';
import ErrorAlert from '../ui/ErrorAlert.svelte'; import ErrorAlert from '../ui/ErrorAlert.svelte';
import Icon from '../ui/Icon.svelte'; import Icon from '../ui/Icon.svelte';
import Link from '../ui/Link.svelte';
export let appState;
let error = null; let error = null;
async function respond() { async function respond() {
let response = { let response = {
id: appState.currentRequest.id, id: $appState.currentRequest.id,
approval: 'Denied', approval: 'Denied',
} }
try { try {
await invoke('respond', {response}); await invoke('respond', {response});
appState.currentRequest = null; $appState.currentRequest = null;
window.setTimeout(() => navigate('Home'), 1000); window.setTimeout(() => navigate('Home'), 1000);
} }
catch (e) { catch (e) {
@ -36,20 +36,18 @@
<div class="flex flex-col h-screen items-center justify-center m-auto max-w-lg"> <div class="flex flex-col h-screen items-center justify-center m-auto max-w-lg">
<ErrorAlert> <ErrorAlert>
{error} {error}
<button <Link target="Home">
slot="buttons" <button
class="btn btn-sm bg-transparent hover:bg-[#cd5a5a] border border-error-content text-error-content" slot="buttons"
on:click="{() => dispatch('navigate', {target: 'Home'})}" class="btn btn-sm bg-transparent hover:bg-[#cd5a5a] border border-error-content text-error-content"
> >
Ok Ok
</button> </button>
</Link>
</ErrorAlert> </ErrorAlert>
</div> </div>
{:else} {:else}
<!-- <div in:fly="{{x: '60vw', duration: 400, easing: expoOut}}" class="flex flex-col items-center justify-center h-screen max-w-max m-auto"> -->
<div class="flex flex-col items-center justify-center h-screen max-w-max m-auto"> <div class="flex flex-col items-center justify-center h-screen max-w-max m-auto">
<!-- <Icon name="x-circle" class="w-36 h-36" /> -->
<svg xmlns="http://www.w3.org/2000/svg" class="w-36 h-36" fill="none" viewBox="0 0 24 24" stroke-width="1" stroke="currentColor"> <svg xmlns="http://www.w3.org/2000/svg" class="w-36 h-36" fill="none" viewBox="0 0 24 24" stroke-width="1" stroke="currentColor">
<path in:draw="{{duration: 500}}" stroke-linecap="round" stroke-linejoin="round" d="M9.75 9.75l4.5 4.5m0-4.5l-4.5 4.5M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /> <path in:draw="{{duration: 500}}" stroke-linecap="round" stroke-linejoin="round" d="M9.75 9.75l4.5 4.5m0-4.5l-4.5 4.5M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg> </svg>

View File

@ -1,13 +1,12 @@
<script> <script>
import { invoke } from '@tauri-apps/api/tauri'; import { invoke } from '@tauri-apps/api/tauri';
import { appState } from '../lib/state.js';
import { navigate } from '../lib/routing.js'; import { navigate } from '../lib/routing.js';
import { getRootCause } from '../lib/errors.js'; import { getRootCause } from '../lib/errors.js';
import ErrorAlert from '../ui/ErrorAlert.svelte'; import ErrorAlert from '../ui/ErrorAlert.svelte';
import Link from '../ui/Link.svelte'; import Link from '../ui/Link.svelte';
export let appState;
let errorMsg = null; let errorMsg = null;
let alert; let alert;
@ -15,8 +14,8 @@
async function unlock() { async function unlock() {
try { try {
let r = await invoke('unlock', {passphrase}); let r = await invoke('unlock', {passphrase});
appState.credentialStatus = 'unlocked'; $appState.credentialStatus = 'unlocked';
if (appState.currentRequest) { if ($appState.currentRequest) {
navigate('ShowApproved'); navigate('ShowApproved');
} }
else { else {