From 793810f7370cd3379950890fb50c7fcf08b97802 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 17 Dec 2024 15:05:39 +0100 Subject: [PATCH] Store editor preview preference in cookie --- src/frontend/components/editor.rs | 9 ++++++--- src/frontend/dark_mode.rs | 14 +++----------- src/frontend/mod.rs | 13 ++++++++++++- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/frontend/components/editor.rs b/src/frontend/components/editor.rs index a0d07df..b94ab2e 100644 --- a/src/frontend/components/editor.rs +++ b/src/frontend/components/editor.rs @@ -1,4 +1,4 @@ -use crate::frontend::markdown::render_markdown; +use crate::frontend::{markdown::render_markdown, use_cookie}; use leptos::{ev::beforeunload, html::Textarea, prelude::*}; use leptos_use::{use_event_listener, use_window}; @@ -9,7 +9,8 @@ pub fn EditorView( set_content: WriteSignal, ) -> impl IntoView { let (preview, set_preview) = signal(render_markdown(&content.get_untracked())); - let (show_preview, set_show_preview) = signal(false); + let cookie = use_cookie("editor_preview"); + let show_preview = Signal::derive(move || cookie.0.get().unwrap_or_else(|| true)); // Prevent user from accidentally closing the page while editing. Doesnt prevent navigation // within Ibis. @@ -44,7 +45,9 @@ pub fn EditorView(
diff --git a/src/frontend/dark_mode.rs b/src/frontend/dark_mode.rs index 3d9d0a6..90fc374 100644 --- a/src/frontend/dark_mode.rs +++ b/src/frontend/dark_mode.rs @@ -1,7 +1,6 @@ -use chrono::{Duration, Local}; -use codee::string::FromToStringCodec; +use super::use_cookie; use leptos::prelude::*; -use leptos_use::{use_cookie_with_options, use_preferred_dark, SameSite, UseCookieOptions}; +use leptos_use::use_preferred_dark; #[derive(Debug, Clone)] pub struct DarkMode { @@ -12,14 +11,7 @@ pub struct DarkMode { impl DarkMode { pub fn init() -> Self { - let expires = (Local::now() + Duration::days(356)).timestamp(); - let cookie_options = UseCookieOptions::default() - .path("/") - .expires(expires) - .same_site(SameSite::Strict); - let cookie = - use_cookie_with_options::("dark_mode", cookie_options); - + let cookie = use_cookie("dark_mode"); let is_dark = Signal::derive(move || { let default = || use_preferred_dark().get_untracked(); cookie.0.get().unwrap_or_else(default) diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs index a5df188..8647fbe 100644 --- a/src/frontend/mod.rs +++ b/src/frontend/mod.rs @@ -1,6 +1,8 @@ use crate::common::{utils::extract_domain, DbArticle, DbPerson}; -use chrono::{DateTime, Local, Utc}; +use chrono::{DateTime, Duration, Local, Utc}; +use codee::string::FromToStringCodec; use leptos::prelude::*; +use leptos_use::{use_cookie_with_options, SameSite, UseCookieOptions}; pub mod api; pub mod app; @@ -79,3 +81,12 @@ fn render_date_time(date_time: DateTime) -> String { .format("%Y-%m-%d %H:%M:%S") .to_string() } + +fn use_cookie(name: &str) -> (Signal>, WriteSignal>) { + let expires = (Local::now() + Duration::days(356)).timestamp(); + let cookie_options = UseCookieOptions::default() + .path("/") + .expires(expires) + .same_site(SameSite::Strict); + use_cookie_with_options::(name, cookie_options) +}