Skip to content

Commit

Permalink
feat: save state when close app
Browse files Browse the repository at this point in the history
  • Loading branch information
reyamir committed Oct 9, 2024
1 parent e04497d commit 9f5b149
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 37 deletions.
19 changes: 6 additions & 13 deletions src-tauri/src/commands/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,31 +577,24 @@ pub async fn get_notifications(state: State<'_, Nostr>) -> Result<Vec<String>, S

#[tauri::command]
#[specta::specta]
pub async fn get_settings(state: State<'_, Nostr>) -> Result<Settings, ()> {
pub async fn get_user_settings(state: State<'_, Nostr>) -> Result<Settings, ()> {
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);
Expand Down
5 changes: 3 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
}
Expand Down
8 changes: 4 additions & 4 deletions src/commands.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,17 @@ async getNotifications() : Promise<Result<string[], string>> {
else return { status: "error", error: e as any };
}
},
async getSettings() : Promise<Result<Settings, null>> {
async getUserSettings() : Promise<Result<Settings, null>> {
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<Result<null, string>> {
async setUserSettings(settings: string) : Promise<Result<null, string>> {
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 };
Expand Down
17 changes: 16 additions & 1 deletion src/routes/$account/_app/home.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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]);

Expand Down
2 changes: 1 addition & 1 deletion src/routes/$account/_settings/general.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down
2 changes: 1 addition & 1 deletion src/routes/$account/_settings/general.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/columns/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
32 changes: 19 additions & 13 deletions src/routes/columns/_layout/launchpad.lazy.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -24,8 +24,8 @@ function Screen() {
className="overflow-hidden size-full"
>
<ScrollArea.Viewport className="relative h-full px-3 pb-3">
<MyGroups />
<MyInterests />
<Groups />
<Interests />
<Core />
</ScrollArea.Viewport>
<ScrollArea.Scrollbar
Expand Down Expand Up @@ -95,10 +95,10 @@ function Core() {
);
}

function MyGroups() {
function Groups() {
const { account } = Route.useSearch();
const { isLoading, data, refetch } = useQuery({
queryKey: ["mygroups", account],
const { isLoading, data, refetch, isRefetching } = useQuery({
queryKey: ["groups", account],
queryFn: async () => {
const res = await commands.getAllGroups();

Expand Down Expand Up @@ -167,12 +167,15 @@ function MyGroups() {
return (
<div className="mb-12 flex flex-col gap-3">
<div className="flex items-center justify-between px-2">
<h3 className="font-semibold">My groups</h3>
<h3 className="font-semibold">Groups</h3>
<div className="inline-flex items-center justify-center gap-2">
<button
type="button"
onClick={() => refetch()}
className="size-7 inline-flex items-center justify-center rounded-full"
className={cn(
"size-7 inline-flex items-center justify-center rounded-full",
isRefetching ? "animate-spin" : "",
)}
>
<ArrowClockwise className="size-4" />
</button>
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -275,12 +278,15 @@ function MyInterests() {
return (
<div className="mb-12 flex flex-col gap-3">
<div className="flex items-center justify-between px-2">
<h3 className="font-semibold">My interests</h3>
<h3 className="font-semibold">Interests</h3>
<div className="inline-flex items-center justify-center gap-2">
<button
type="button"
onClick={() => refetch()}
className="size-7 inline-flex items-center justify-center rounded-full"
className={cn(
"size-7 inline-flex items-center justify-center rounded-full",
isRefetching ? "animate-spin" : "",
)}
>
<ArrowClockwise className="size-4" />
</button>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/index.lazy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9f5b149

Please sign in to comment.