From e86c94e90d643675b9c56295b56b0436bf959a03 Mon Sep 17 00:00:00 2001 From: Thilo Maier Date: Tue, 6 Aug 2024 18:03:27 +0200 Subject: [PATCH 1/3] Wire up Tauri state-changed listener in `onMount` of root layout --- unime/src/lib/stores.ts | 27 +--------- unime/src/routes/+layout.svelte | 95 ++++++++++++++++++++++----------- 2 files changed, 66 insertions(+), 56 deletions(-) diff --git a/unime/src/lib/stores.ts b/unime/src/lib/stores.ts index 4fa59456..31c39418 100644 --- a/unime/src/lib/stores.ts +++ b/unime/src/lib/stores.ts @@ -1,18 +1,9 @@ -import { goto } from '$app/navigation'; -import { setLocale } from '$i18n/i18n-svelte'; -import type { Locales } from '$i18n/i18n-types'; import { writable } from 'svelte/store'; // TODO: run some copy task instead of importing across root to make the frontend independent import type { AppState } from '@bindings/AppState'; import { listen } from '@tauri-apps/api/event'; -import { error as err, info } from '@tauri-apps/plugin-log'; - -interface StateChangedEvent { - event: string; - payload: AppState; - id: number; -} +import { error as err } from '@tauri-apps/plugin-log'; interface ErrorEvent { event: string; @@ -61,21 +52,7 @@ const empty_state: AppState = { * If the frontend intends to change the state, it must dispatch an action to the backend. */ // TODO: make read-only -export const state = writable(empty_state, (set) => { - listen('state-changed', (event: StateChangedEvent) => { - const state = event.payload; - - set(state); - setLocale(state.profile_settings.locale as Locales); - - if (state.current_user_prompt?.type === 'redirect') { - const redirect_target = state.current_user_prompt.target; - info(`Redirecting to: "/${redirect_target}"`); - goto(`/${redirect_target}`); - } - }); - // TODO: unsubscribe from listener! -}); +export const state = writable(empty_state); /** * A store that listens for errors emitted by the Rust backend. diff --git a/unime/src/routes/+layout.svelte b/unime/src/routes/+layout.svelte index fd301f8e..7a2cbe98 100644 --- a/unime/src/routes/+layout.svelte +++ b/unime/src/routes/+layout.svelte @@ -1,14 +1,19 @@