From 9f5b14956a8b73133637053d30d87daf3ed54e07 Mon Sep 17 00:00:00 2001 From: reya Date: Wed, 9 Oct 2024 14:02:13 +0700 Subject: [PATCH] feat: save state when close app --- src-tauri/src/commands/metadata.rs | 19 ++++------- src-tauri/src/main.rs | 5 +-- src/commands.gen.ts | 8 ++--- src/routes/$account/_app/home.lazy.tsx | 17 +++++++++- .../$account/_settings/general.lazy.tsx | 2 +- src/routes/$account/_settings/general.tsx | 2 +- src/routes/columns/_layout.tsx | 2 +- src/routes/columns/_layout/launchpad.lazy.tsx | 32 +++++++++++-------- src/routes/index.lazy.tsx | 2 +- 9 files changed, 52 insertions(+), 37 deletions(-) diff --git a/src-tauri/src/commands/metadata.rs b/src-tauri/src/commands/metadata.rs index 222822a6..f365da45 100644 --- a/src-tauri/src/commands/metadata.rs +++ b/src-tauri/src/commands/metadata.rs @@ -577,31 +577,24 @@ pub async fn get_notifications(state: State<'_, Nostr>) -> Result, S #[tauri::command] #[specta::specta] -pub async fn get_settings(state: State<'_, Nostr>) -> Result { +pub async fn get_user_settings(state: State<'_, Nostr>) -> Result { Ok(state.settings.lock().await.clone()) } #[tauri::command] #[specta::specta] -pub async fn set_settings( - settings: &str, +pub async fn set_user_settings( + settings: String, state: State<'_, Nostr>, handle: tauri::AppHandle, ) -> Result<(), String> { let client = &state.client; - let ident = "lume_v4:settings"; - let signer = client.signer().await.map_err(|e| e.to_string())?; - let public_key = signer.public_key().await.map_err(|e| e.to_string())?; - let encrypted = signer - .nip44_encrypt(&public_key, settings) - .await - .map_err(|e| e.to_string())?; - let tag = Tag::identifier(ident); - let builder = EventBuilder::new(Kind::ApplicationSpecificData, encrypted, vec![tag]); + let tags = vec![Tag::identifier("lume_user_setting")]; + let builder = EventBuilder::new(Kind::ApplicationSpecificData, &settings, tags); match client.send_event_builder(builder).await { Ok(_) => { - let parsed: Settings = serde_json::from_str(settings).map_err(|e| e.to_string())?; + let parsed: Settings = serde_json::from_str(&settings).map_err(|e| e.to_string())?; // Update state state.settings.lock().await.clone_from(&parsed); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index afe7dc67..23e2f644 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -128,8 +128,8 @@ fn main() { zap_event, copy_friend, get_notifications, - get_settings, - set_settings, + get_user_settings, + set_user_settings, verify_nip05, is_trusted_user, get_event_meta, @@ -523,6 +523,7 @@ fn main() { .plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_upload::init()) .plugin(tauri_plugin_updater::Builder::new().build()) + .plugin(tauri_plugin_window_state::Builder::default().build()) .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/src/commands.gen.ts b/src/commands.gen.ts index d268ef9b..cc8b0447 100644 --- a/src/commands.gen.ts +++ b/src/commands.gen.ts @@ -270,17 +270,17 @@ async getNotifications() : Promise> { else return { status: "error", error: e as any }; } }, -async getSettings() : Promise> { +async getUserSettings() : Promise> { try { - return { status: "ok", data: await TAURI_INVOKE("get_settings") }; + return { status: "ok", data: await TAURI_INVOKE("get_user_settings") }; } catch (e) { if(e instanceof Error) throw e; else return { status: "error", error: e as any }; } }, -async setSettings(settings: string) : Promise> { +async setUserSettings(settings: string) : Promise> { try { - return { status: "ok", data: await TAURI_INVOKE("set_settings", { settings }) }; + return { status: "ok", data: await TAURI_INVOKE("set_user_settings", { settings }) }; } catch (e) { if(e instanceof Error) throw e; else return { status: "error", error: e as any }; diff --git a/src/routes/$account/_app/home.lazy.tsx b/src/routes/$account/_app/home.lazy.tsx index 0ecb5464..66b0d31e 100644 --- a/src/routes/$account/_app/home.lazy.tsx +++ b/src/routes/$account/_app/home.lazy.tsx @@ -28,6 +28,7 @@ export const Route = createLazyFileRoute("/$account/_app/home")({ }); function Screen() { + const params = Route.useParams(); const columns = useStore(appColumns, (state) => state); const [emblaRef, emblaApi] = useEmblaCarousel({ @@ -159,7 +160,21 @@ function Screen() { } if (!columns.length) { - getSystemColumns(); + const prevColumns = window.localStorage.getItem( + `${params.account}_columns`, + ); + + if (!prevColumns) { + getSystemColumns(); + } else { + const parsed: LumeColumn[] = JSON.parse(prevColumns); + appColumns.setState(() => parsed); + } + } else { + window.localStorage.setItem( + `${params.account}_columns`, + JSON.stringify(columns), + ); } }, [columns.length]); diff --git a/src/routes/$account/_settings/general.lazy.tsx b/src/routes/$account/_settings/general.lazy.tsx index 7749563d..8f8e033c 100644 --- a/src/routes/$account/_settings/general.lazy.tsx +++ b/src/routes/$account/_settings/general.lazy.tsx @@ -29,7 +29,7 @@ function Screen() { const updateSettings = () => { startTransition(async () => { const newSettings = JSON.stringify(appSettings.state); - const res = await commands.setSettings(newSettings); + const res = await commands.setUserSettings(newSettings); if (res.status === "error") { await message(res.error, { kind: "error" }); diff --git a/src/routes/$account/_settings/general.tsx b/src/routes/$account/_settings/general.tsx index f9b1db31..fbc2049f 100644 --- a/src/routes/$account/_settings/general.tsx +++ b/src/routes/$account/_settings/general.tsx @@ -4,7 +4,7 @@ import { createFileRoute } from "@tanstack/react-router"; export const Route = createFileRoute("/$account/_settings/general")({ beforeLoad: async () => { - const res = await commands.getSettings(); + const res = await commands.getUserSettings(); if (res.status === "ok") { appSettings.setState((state) => { diff --git a/src/routes/columns/_layout.tsx b/src/routes/columns/_layout.tsx index c8c646d8..f1980c45 100644 --- a/src/routes/columns/_layout.tsx +++ b/src/routes/columns/_layout.tsx @@ -12,7 +12,7 @@ export const Route = createFileRoute("/columns/_layout")({ }; }, beforeLoad: async () => { - const res = await commands.getSettings(); + const res = await commands.getUserSettings(); if (res.status === "ok") { appSettings.setState((state) => { diff --git a/src/routes/columns/_layout/launchpad.lazy.tsx b/src/routes/columns/_layout/launchpad.lazy.tsx index 4ff411e4..3f531bec 100644 --- a/src/routes/columns/_layout/launchpad.lazy.tsx +++ b/src/routes/columns/_layout/launchpad.lazy.tsx @@ -1,5 +1,5 @@ import { commands } from "@/commands.gen"; -import { toLumeEvents } from "@/commons"; +import { cn, toLumeEvents } from "@/commons"; import { Spinner, User } from "@/components"; import { LumeWindow } from "@/system"; import type { LumeColumn, NostrEvent } from "@/types"; @@ -24,8 +24,8 @@ function Screen() { className="overflow-hidden size-full" > - - + + { const res = await commands.getAllGroups(); @@ -167,12 +167,15 @@ function MyGroups() { return (
-

My groups

+

Groups

@@ -206,10 +209,10 @@ function MyGroups() { ); } -function MyInterests() { +function Interests() { const { account } = Route.useSearch(); - const { isLoading, data, refetch } = useQuery({ - queryKey: ["myinterests", account], + const { isLoading, data, refetch, isRefetching } = useQuery({ + queryKey: ["interests", account], queryFn: async () => { const res = await commands.getAllInterests(); @@ -275,12 +278,15 @@ function MyInterests() { return (
-

My interests

+

Interests

diff --git a/src/routes/index.lazy.tsx b/src/routes/index.lazy.tsx index 7851ce14..d1f3af4f 100644 --- a/src/routes/index.lazy.tsx +++ b/src/routes/index.lazy.tsx @@ -84,7 +84,7 @@ function Screen() { const res = await commands.login(value, password); if (res.status === "ok") { - const settings = await commands.getSettings(); + const settings = await commands.getUserSettings(); if (settings.status === "ok") { appSettings.setState(() => settings.data);