From 9089ea0d5f6afb52c606038c713002998febf225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 4 Sep 2026 22:12:17 +0300 Subject: [PATCH] feat(ui): server-synced theme and language, page skeleton sweep MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Preferences follow iz's model exactly: user.theme (light/dark) and user.language (en/tr) join user.ui in the row (migration 0003), one preferences form on the settings page saves all three through set_preferences, and the layout emits data-theme/lang/data-ui from the signed-in user (Accept-Language fallback signed-out). set_user_ui and /api/settings/ui are gone — one route, one form, like iz. Skeleton defect class swept: /shared, /trash, /search and the public folder link all put content straight into settings-shell (a flex row meant for rail+stage), rendering as an overlapping strip — user screenshots. All four now nest in settings-stage with panel structure; a test per page pins the wrapper. --- .../in-core/migrations/0003_preferences.sql | 8 + crates/in-core/src/store/mod.rs | 18 +- crates/in-core/src/store/reconcile.rs | 26 +- crates/in-core/src/store/schema.rs | 1 + crates/in-core/src/store/turso_store.rs | 17 +- crates/in-core/tests/store.rs | 62 +++- crates/in-web/src/drive.rs | 2 +- crates/in-web/src/i18n.rs | 41 ++- crates/in-web/src/layout.rs | 12 +- crates/in-web/src/pages.rs | 2 +- crates/in-web/src/search.rs | 18 +- crates/in-web/src/server.rs | 12 + crates/in-web/src/settings.rs | 74 +++-- crates/in-web/src/share.rs | 55 ++-- crates/in-web/src/trash.rs | 18 +- crates/in-web/tests/http.rs | 287 ++++++++++++++++-- 16 files changed, 534 insertions(+), 119 deletions(-) create mode 100644 crates/in-core/migrations/0003_preferences.sql diff --git a/crates/in-core/migrations/0003_preferences.sql b/crates/in-core/migrations/0003_preferences.sql new file mode 100644 index 0000000..0890783 --- /dev/null +++ b/crates/in-core/migrations/0003_preferences.sql @@ -0,0 +1,8 @@ +-- The person's theme and language: 'light'/'dark' and 'en'/'tr', read by +-- `root_layout` into `data-theme` and ``. The DEFAULTs are the +-- backfill: SQLite fills every existing row with them as each column is +-- added. Databases already carrying the columns never see this file applied +-- piecemeal — they reach the declared shape through `in reconcile`, whose +-- copy map backfills the same values. +ALTER TABLE user ADD COLUMN theme TEXT NOT NULL DEFAULT 'light'; +ALTER TABLE user ADD COLUMN language TEXT NOT NULL DEFAULT 'en'; diff --git a/crates/in-core/src/store/mod.rs b/crates/in-core/src/store/mod.rs index e460f32..a9439c9 100644 --- a/crates/in-core/src/store/mod.rs +++ b/crates/in-core/src/store/mod.rs @@ -96,6 +96,12 @@ pub struct User { /// Display-only, the way `quota_bytes` is data: 'ledger' or 'instrument', /// read by `root_layout` into `data-ui`. Nothing stored depends on it. pub ui: String, + /// Display-only, as [`Self::ui`]: 'light' or 'dark', read by + /// `root_layout` into `data-theme`. Nothing stored depends on it. + pub theme: String, + /// Display-only, as [`Self::ui`]: 'en' or 'tr', read by `root_layout` + /// into `` and by the i18n layer. Nothing stored depends on it. + pub language: String, pub created_at: OffsetDateTime, pub last_seen_at: Option, } @@ -361,11 +367,13 @@ pub trait Store: 'static + Send + Sync { /// layer reads this and treats the person as signed out. async fn set_user_disabled(&self, user_id: &str, disabled: bool) -> Result<()>; - /// Writes the person's UI density: `ledger` or `instrument`, read by - /// `root_layout` into `data-ui`. Anything else is refused with - /// [`StoreError::Corrupt`] — the same refusal a bad enum read from the - /// row gets, because a bad enum written is a corrupt row. - async fn set_user_ui(&self, id: &str, ui: &str) -> Result<()>; + /// Writes the person's display preferences — `theme` ('light'/'dark'), + /// `language` ('en'/'tr') and `ui` ('ledger'/'instrument') — read by + /// `root_layout` into `data-theme`, `` and `data-ui`. A value + /// no preferences form offers is refused with [`StoreError::Corrupt`] — + /// the same refusal a bad enum read from the row gets, because a bad + /// enum written is a corrupt row. + async fn set_preferences(&self, id: &str, theme: &str, language: &str, ui: &str) -> Result<()>; // -- folders ----------------------------------------------------------- diff --git a/crates/in-core/src/store/reconcile.rs b/crates/in-core/src/store/reconcile.rs index 5a8ebb1..c58871d 100644 --- a/crates/in-core/src/store/reconcile.rs +++ b/crates/in-core/src/store/reconcile.rs @@ -203,11 +203,11 @@ struct TableMap { } /// Builds the explicit column map. Unchanged tables map every column to its -/// old self; the user table carries the migration-specific expression for -/// `ui`: a database old enough to predate the column starts on the schema's -/// own default rather than on an empty string, which no writer would ever -/// store and no reader would ever honour. -fn build_maps(old_has_ui: bool) -> Vec { +/// old self; the user table carries the migration-specific expressions for +/// `ui`, `theme` and `language`: a database old enough to predate a column +/// starts on the schema's own default rather than on an empty string, which +/// no writer would ever store and no reader would ever honour. +fn build_maps(old_has_ui: bool, old_has_theme: bool, old_has_language: bool) -> Vec { vec![ TableMap { name: "user", @@ -228,6 +228,16 @@ fn build_maps(old_has_ui: bool) -> Vec { ("ui", "'ledger'".into()) }); columns.extend(old_cols(&["created_at", "last_seen_at"])); + columns.push(if old_has_theme { + ("theme", "old.theme".into()) + } else { + ("theme", "'light'".into()) + }); + columns.push(if old_has_language { + ("language", "old.language".into()) + } else { + ("language", "'en'".into()) + }); columns }, }, @@ -379,7 +389,9 @@ async fn copy_data(old_conn: &Connection, new_conn: &Connection, path: &str) -> .map_err(|e| StoreError::Backend(e.to_string()))?; let old_has_ui = old_has_column(old_conn, "user", "ui").await?; - let maps = build_maps(old_has_ui); + let old_has_theme = old_has_column(old_conn, "user", "theme").await?; + let old_has_language = old_has_column(old_conn, "user", "language").await?; + let maps = build_maps(old_has_ui, old_has_theme, old_has_language); validate_maps(new_conn, &maps).await?; // Foreign keys stay off during the copy and are checked by `verify` @@ -565,7 +577,7 @@ fn backup_name(path: &str) -> Result { } /// A probe the copy plan asks: whether the old database already carries -/// `column` on `table`. A column the second migration added is absent on a +/// `column` on `table`. A column a later migration added is absent on a /// database old enough to predate it, and the map backfills the default /// instead of reading a column that is not there. async fn old_has_column(conn: &Connection, table: &str, column: &str) -> Result { diff --git a/crates/in-core/src/store/schema.rs b/crates/in-core/src/store/schema.rs index f5fcf18..e0db0f8 100644 --- a/crates/in-core/src/store/schema.rs +++ b/crates/in-core/src/store/schema.rs @@ -16,6 +16,7 @@ use turso::Connection; pub(crate) const MIGRATIONS: &[&str] = &[ include_str!("../../migrations/0001_init.sql"), include_str!("../../migrations/0002_ui.sql"), + include_str!("../../migrations/0003_preferences.sql"), ]; /// The whole declared schema — every migration applied in order — as one diff --git a/crates/in-core/src/store/turso_store.rs b/crates/in-core/src/store/turso_store.rs index d972772..d068db8 100644 --- a/crates/in-core/src/store/turso_store.rs +++ b/crates/in-core/src/store/turso_store.rs @@ -36,7 +36,7 @@ const UPLOADS_DIR: &str = "uploads"; const THUMB_SOURCE_CAP: u64 = 64 * 1024 * 1024; const USER_COLUMNS: &str = - "id, oidc_sub, email, display_name, admin, disabled, quota_bytes, used_bytes, ui, created_at, last_seen_at"; + "id, oidc_sub, email, display_name, admin, disabled, quota_bytes, used_bytes, ui, created_at, last_seen_at, theme, language"; const FOLDER_COLUMNS: &str = "id, owner_id, parent_id, name, created_at, deleted_at"; const FILE_COLUMNS: &str = "id, owner_id, folder_id, name, mime, size_bytes, thumb_state, created_at, updated_at, deleted_at"; @@ -714,6 +714,8 @@ fn user_from(row: &Row) -> Result { quota_bytes: row.get::(6).map_err(backend)?.max(0) as u64, used_bytes: row.get::(7).map_err(backend)?.max(0) as u64, ui: text(row, 8)?, + theme: text(row, 11)?, + language: text(row, 12)?, created_at: parse_stamp(&text(row, 9)?)?, last_seen_at: opt_stamp(row, 10)?, }) @@ -947,13 +949,22 @@ impl Store for TursoStore { Ok(()) } - async fn set_user_ui(&self, id: &str, ui: &str) -> Result<()> { + async fn set_preferences(&self, id: &str, theme: &str, language: &str, ui: &str) -> Result<()> { + if theme != "light" && theme != "dark" { + return Err(StoreError::Corrupt(format!("theme {theme:?}"))); + } + if language != "en" && language != "tr" { + return Err(StoreError::Corrupt(format!("language {language:?}"))); + } if ui != "ledger" && ui != "instrument" { return Err(StoreError::Corrupt(format!("ui {ui:?}"))); } let conn = self.conn.lock().await; let n = conn - .execute("UPDATE user SET ui = ?1 WHERE id = ?2", params![ui, id]) + .execute( + "UPDATE user SET theme = ?1, language = ?2, ui = ?3 WHERE id = ?4", + params![theme, language, ui, id], + ) .await .map_err(backend)?; if n == 0 { diff --git a/crates/in-core/tests/store.rs b/crates/in-core/tests/store.rs index ec21607..4683489 100644 --- a/crates/in-core/tests/store.rs +++ b/crates/in-core/tests/store.rs @@ -1122,39 +1122,59 @@ async fn add_share_user_needs_the_owner() { } #[tokio::test] -async fn a_provisioned_user_wears_the_ledger_ui() { +async fn a_provisioned_user_wears_the_default_preferences() { let scratch = Scratch::open().await; let user = alice(&scratch.store).await; assert_eq!(user.ui, "ledger"); + assert_eq!(user.theme, "light"); + assert_eq!(user.language, "en"); } #[tokio::test] -async fn the_ui_round_trips_and_refuses_what_is_not_offered() { +async fn preferences_round_trip_and_refuse_what_is_not_offered() { let scratch = Scratch::open().await; let user = alice(&scratch.store).await; - scratch.store.set_user_ui(&user.id, "instrument").await.unwrap(); + scratch + .store + .set_preferences(&user.id, "dark", "tr", "instrument") + .await + .unwrap(); let back = scratch.store.user(&user.id).await.unwrap().unwrap(); + assert_eq!(back.theme, "dark"); + assert_eq!(back.language, "tr"); assert_eq!(back.ui, "instrument"); - // A value no settings screen offers is refused, and the row keeps what + // A value no preferences form offers is refused, and the row keeps what // the last good write left — the same Corrupt the store answers a bad // enum it reads. assert!(matches!( - scratch.store.set_user_ui(&user.id, "dense").await, + scratch.store.set_preferences(&user.id, "dim", "tr", "instrument").await, + Err(StoreError::Corrupt(_)) + )); + assert!(matches!( + scratch.store.set_preferences(&user.id, "dark", "xx", "instrument").await, + Err(StoreError::Corrupt(_)) + )); + assert!(matches!( + scratch.store.set_preferences(&user.id, "dark", "tr", "dense").await, Err(StoreError::Corrupt(_)) )); let kept = scratch.store.user(&user.id).await.unwrap().unwrap(); + assert_eq!(kept.theme, "dark"); + assert_eq!(kept.language, "tr"); assert_eq!(kept.ui, "instrument"); assert!(matches!( - scratch.store.set_user_ui("no-such-user", "ledger").await, + scratch.store.set_preferences("no-such-user", "light", "en", "ledger").await, Err(StoreError::NotFound) )); } #[tokio::test] -async fn a_first_migration_database_gains_the_ui_column_on_open() { - // A database built from 0001 alone — the shape In was deployed with — - // carries no `ui` column. Opening it with the current code reconciles it - // onto the declared schema, and its people arrive wearing the default. +async fn a_second_migration_database_gains_the_preference_columns_on_open() { + // A database built from 0001+0002 — the shape In ran with an interface + // picker and no theme or language — carries no `theme` or `language` + // column. Opening it with the current code reconciles it onto the + // declared schema: its interface choice is carried across, and the two + // new preferences arrive wearing their defaults. let dir = std::env::temp_dir().join(format!("in-test-{}", Ulid::new())); std::fs::create_dir_all(&dir).unwrap(); let path = dir.join("in.db").to_string_lossy().into_owned(); @@ -1164,11 +1184,14 @@ async fn a_first_migration_database_gains_the_ui_column_on_open() { conn.execute_batch(include_str!("../migrations/0001_init.sql")) .await .unwrap(); + conn.execute_batch(include_str!("../migrations/0002_ui.sql")) + .await + .unwrap(); conn.execute( "INSERT INTO user (id, oidc_sub, email, display_name, admin, disabled, \ - quota_bytes, used_bytes, created_at, last_seen_at) \ + quota_bytes, used_bytes, ui, created_at, last_seen_at) \ VALUES ('u-old', 'sub-old', 'old@example.com', 'Old', 1, 0, 100, 0, \ - '2026-08-01T00:00:00Z', '2026-08-01T00:00:00Z')", + 'instrument', '2026-08-01T00:00:00Z', '2026-08-01T00:00:00Z')", (), ) .await @@ -1177,11 +1200,18 @@ async fn a_first_migration_database_gains_the_ui_column_on_open() { let storage = dir.join("storage"); let store = TursoStore::open(&path, Some(&storage)).await.unwrap(); let user = store.user_by_oidc_sub("sub-old").await.unwrap().unwrap(); - assert_eq!(user.ui, "ledger"); - // And the carried row takes a new preference like any other. - store.set_user_ui(&user.id, "instrument").await.unwrap(); + assert_eq!(user.ui, "instrument"); + assert_eq!(user.theme, "light"); + assert_eq!(user.language, "en"); + // And the carried row takes new preferences like any other. + store + .set_preferences(&user.id, "dark", "tr", "ledger") + .await + .unwrap(); let back = store.user(&user.id).await.unwrap().unwrap(); - assert_eq!(back.ui, "instrument"); + assert_eq!(back.theme, "dark"); + assert_eq!(back.language, "tr"); + assert_eq!(back.ui, "ledger"); drop(store); let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/in-web/src/drive.rs b/crates/in-web/src/drive.rs index ed653ee..39701e2 100644 --- a/crates/in-web/src/drive.rs +++ b/crates/in-web/src/drive.rs @@ -166,7 +166,7 @@ async fn drive(cx: &Cx) -> Result { }; } }; - let language = lang(cx); + let language = lang(cx).await; let store = app(cx).store.clone(); let wanted = query_params::(cx) diff --git a/crates/in-web/src/i18n.rs b/crates/in-web/src/i18n.rs index 66cda81..429e82f 100644 --- a/crates/in-web/src/i18n.rs +++ b/crates/in-web/src/i18n.rs @@ -1,15 +1,15 @@ //! UI strings in two languages. //! -//! v1 has no language column on the user row, so the reader's language comes -//! off the request: an `Accept-Language` tag starting with `tr` reads -//! Turkish, everything else reads English. An unrecognized tag is not a -//! refusal, just the default. +//! A signed-in reader's language comes off their own user row, the way their +//! theme and interface do; everybody else reads `Accept-Language`. An +//! unrecognized tag or code is not a refusal, just the default. use topcoat::context::Cx; use topcoat::router::header; use topcoat::router::request::headers; -/// English or Turkish, read off the request's `Accept-Language`. +/// English or Turkish: the reader's own language when signed in, the +/// request's `Accept-Language` otherwise. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Lang { En, @@ -44,9 +44,19 @@ impl Lang { } } -/// The request's language: `Accept-Language` first, English when it says -/// nothing usable. -pub fn lang(cx: &Cx) -> Lang { +/// The request's language: the signed-in reader's own row first, +/// `Accept-Language` when signed out or unreadable. +/// +/// Cheap per call: [`crate::server::current_user`] is memoized per request, +/// and every signed-in page already resolved the user before asking. +pub async fn lang(cx: &Cx) -> Lang { + let signed_in = match crate::server::current_user(cx).await { + Ok(user) => user.as_ref().map(|user| Lang::from_code(&user.language)), + Err(_) => None, + }; + if let Some(language) = signed_in { + return language; + } headers(cx) .get(header::ACCEPT_LANGUAGE) .and_then(|value| value.to_str().ok()) @@ -162,6 +172,11 @@ pub enum Key { NoLinks, ManageLinks, UiLabel, + ThemeLabel, + LightOption, + DarkOption, + LanguageLabel, + Saved, } pub fn t(lang: Lang, key: Key) -> &'static str { @@ -261,6 +276,11 @@ pub fn t(lang: Lang, key: Key) -> &'static str { NoLinks => "No share links yet.", ManageLinks => "Share links", UiLabel => "Interface", + ThemeLabel => "THEME", + LightOption => "Light", + DarkOption => "Dark", + LanguageLabel => "LANGUAGE", + Saved => "Saved.", }, Lang::Tr => match key { Cancel => "Vazgeç", @@ -356,6 +376,11 @@ pub fn t(lang: Lang, key: Key) -> &'static str { NoLinks => "Henüz paylaşım bağlantısı yok.", ManageLinks => "Paylaşım bağlantıları", UiLabel => "Arayüz", + ThemeLabel => "TEMA", + LightOption => "Açık", + DarkOption => "Koyu", + LanguageLabel => "DİL", + Saved => "Kaydedildi.", }, } } diff --git a/crates/in-web/src/layout.rs b/crates/in-web/src/layout.rs index 68f38ef..ddc64db 100644 --- a/crates/in-web/src/layout.rs +++ b/crates/in-web/src/layout.rs @@ -850,15 +850,19 @@ const STYLE: Asset = asset!("assets/main.css"); #[layout("/")] async fn root_layout(cx: &Cx, slot: Result) -> Result { - // The one per-user chrome knob: the UI density, read off the request's - // own user. Signed-out (or unreadable) renders ledger, the default. + // The per-user chrome knobs, read off the request's own user: the theme + // into `data-theme`, the interface into `data-ui`, the language into + // ``. Signed-out (or unreadable) renders light and ledger, + // and the language falls back to `Accept-Language` — both are only set + // when the request's own user has one to read. let me = match current_user(cx).await { Ok(user) => user.as_ref(), Err(_) => None, }; let asking = me.is_some(); + let dark = me.as_ref().is_some_and(|user| user.theme == "dark"); let ui = me.as_ref().map_or("ledger", |user| user.ui.as_str()); - let lang = crate::i18n::lang(cx); + let lang = crate::i18n::lang(cx).await; let content = match slot { Err(error) if error.downcast_ref::().is_some() => view! { @@ -880,7 +884,7 @@ async fn root_layout(cx: &Cx, slot: Result) -> Result { let build = STYLE.id().as_u64().to_string(); view! { - + diff --git a/crates/in-web/src/pages.rs b/crates/in-web/src/pages.rs index 212436c..5680e41 100644 --- a/crates/in-web/src/pages.rs +++ b/crates/in-web/src/pages.rs @@ -44,7 +44,7 @@ async fn landing(cx: &Cx) -> Result { /// The sign-in card for a browser with nobody in it: the wordmark and one /// link, which starts the OIDC round-trip at `/auth/login`. async fn sign_in_card(cx: &Cx) -> Result { - let language = lang(cx); + let language = lang(cx).await; view! { cx =>
diff --git a/crates/in-web/src/search.rs b/crates/in-web/src/search.rs index 80c9f2a..5fd4075 100644 --- a/crates/in-web/src/search.rs +++ b/crates/in-web/src/search.rs @@ -32,7 +32,7 @@ async fn search(cx: &Cx) -> Result { let user = match require_user(cx).await { Ok(user) => user, Err(refusal) => { - let language = lang(cx); + let language = lang(cx).await; return view! { cx =>
@@ -42,7 +42,7 @@ async fn search(cx: &Cx) -> Result { }; } }; - let language = lang(cx); + let language = lang(cx).await; let asked = searched(query_value(uri(cx).query().unwrap_or(""), "q").as_deref()); let hits = match asked.as_deref() { Some(query) => Some(app(cx).store.search(&user.id, query, LIMIT).await?), @@ -51,8 +51,8 @@ async fn search(cx: &Cx) -> Result { let box_text = asked.clone().unwrap_or_default(); view! { cx => -
- (topbar(cx, NavPage::Search, &user, language).await?) + (topbar(cx, NavPage::Search, &user, language).await?) +

(t(language, Key::SearchResults))