Server-synced theme and language, page skeleton sweep
Browse files at this commit9089ea0d5f6afb52c606038c713002998febf225Parent: d3811f1- feat
- server-synced theme and language, page skeleton sweep
16 changed files +534 -119
- crates/in-core/migrations/0003_preferences.sql +8 -0
- crates/in-core/src/store/mod.rs +13 -5
- crates/in-core/src/store/reconcile.rs +19 -7
- crates/in-core/src/store/schema.rs +1 -0
- crates/in-core/src/store/turso_store.rs +14 -3
- crates/in-core/tests/store.rs +46 -16
- crates/in-web/src/drive.rs +1 -1
- crates/in-web/src/i18n.rs +33 -8
- crates/in-web/src/layout.rs +8 -4
- crates/in-web/src/pages.rs +1 -1
- crates/in-web/src/search.rs +12 -6
- crates/in-web/src/server.rs +12 -0
- crates/in-web/src/settings.rs +56 -18
- crates/in-web/src/share.rs +38 -17
- crates/in-web/src/trash.rs +12 -6
- crates/in-web/tests/http.rs +260 -27
crates/in-core/migrations/0003_preferences.sqladded+8 -0
| @@ -0,0 +1,8 @@ | |||
| 1 | +-- The person's theme and language: 'light'/'dark' and 'en'/'tr', read by | ||
| 2 | +-- `root_layout` into `data-theme` and `<html lang>`. The DEFAULTs are the | ||
| 3 | +-- backfill: SQLite fills every existing row with them as each column is | ||
| 4 | +-- added. Databases already carrying the columns never see this file applied | ||
| 5 | +-- piecemeal — they reach the declared shape through `in reconcile`, whose | ||
| 6 | +-- copy map backfills the same values. | ||
| 7 | +ALTER TABLE user ADD COLUMN theme TEXT NOT NULL DEFAULT 'light'; | ||
| 8 | +ALTER TABLE user ADD COLUMN language TEXT NOT NULL DEFAULT 'en'; | ||
crates/in-core/src/store/mod.rsmodified+13 -5
| @@ -96,6 +96,12 @@ pub struct User { | |||
| 96 | /// Display-only, the way `quota_bytes` is data: 'ledger' or 'instrument', | 96 | /// Display-only, the way `quota_bytes` is data: 'ledger' or 'instrument', |
| 97 | /// read by `root_layout` into `data-ui`. Nothing stored depends on it. | 97 | /// read by `root_layout` into `data-ui`. Nothing stored depends on it. |
| 98 | pub ui: String, | 98 | pub ui: String, |
| 99 | + /// Display-only, as [`Self::ui`]: 'light' or 'dark', read by | ||
| 100 | + /// `root_layout` into `data-theme`. Nothing stored depends on it. | ||
| 101 | + pub theme: String, | ||
| 102 | + /// Display-only, as [`Self::ui`]: 'en' or 'tr', read by `root_layout` | ||
| 103 | + /// into `<html lang>` and by the i18n layer. Nothing stored depends on it. | ||
| 104 | + pub language: String, | ||
| 99 | pub created_at: OffsetDateTime, | 105 | pub created_at: OffsetDateTime, |
| 100 | pub last_seen_at: Option<OffsetDateTime>, | 106 | pub last_seen_at: Option<OffsetDateTime>, |
| 101 | } | 107 | } |
| @@ -361,11 +367,13 @@ pub trait Store: 'static + Send + Sync { | |||
| 361 | /// layer reads this and treats the person as signed out. | 367 | /// layer reads this and treats the person as signed out. |
| 362 | async fn set_user_disabled(&self, user_id: &str, disabled: bool) -> Result<()>; | 368 | async fn set_user_disabled(&self, user_id: &str, disabled: bool) -> Result<()>; |
| 363 | 369 | ||
| 364 | - /// Writes the person's UI density: `ledger` or `instrument`, read by | 370 | + /// Writes the person's display preferences — `theme` ('light'/'dark'), |
| 365 | - /// `root_layout` into `data-ui`. Anything else is refused with | 371 | + /// `language` ('en'/'tr') and `ui` ('ledger'/'instrument') — read by |
| 366 | - /// [`StoreError::Corrupt`] — the same refusal a bad enum read from the | 372 | + /// `root_layout` into `data-theme`, `<html lang>` and `data-ui`. A value |
| 367 | - /// row gets, because a bad enum written is a corrupt row. | 373 | + /// no preferences form offers is refused with [`StoreError::Corrupt`] — |
| 368 | - async fn set_user_ui(&self, id: &str, ui: &str) -> Result<()>; | 374 | + /// the same refusal a bad enum read from the row gets, because a bad |
| 375 | + /// enum written is a corrupt row. | ||
| 376 | + async fn set_preferences(&self, id: &str, theme: &str, language: &str, ui: &str) -> Result<()>; | ||
| 369 | 377 | ||
| 370 | // -- folders ----------------------------------------------------------- | 378 | // -- folders ----------------------------------------------------------- |
| 371 | 379 | ||
crates/in-core/src/store/reconcile.rsmodified+19 -7
| @@ -203,11 +203,11 @@ struct TableMap { | |||
| 203 | } | 203 | } |
| 204 | 204 | ||
| 205 | /// Builds the explicit column map. Unchanged tables map every column to its | 205 | /// Builds the explicit column map. Unchanged tables map every column to its |
| 206 | -/// old self; the user table carries the migration-specific expression for | 206 | +/// old self; the user table carries the migration-specific expressions for |
| 207 | -/// `ui`: a database old enough to predate the column starts on the schema's | 207 | +/// `ui`, `theme` and `language`: a database old enough to predate a column |
| 208 | -/// own default rather than on an empty string, which no writer would ever | 208 | +/// starts on the schema's own default rather than on an empty string, which |
| 209 | -/// store and no reader would ever honour. | 209 | +/// no writer would ever store and no reader would ever honour. |
| 210 | -fn build_maps(old_has_ui: bool) -> Vec<TableMap> { | 210 | +fn build_maps(old_has_ui: bool, old_has_theme: bool, old_has_language: bool) -> Vec<TableMap> { |
| 211 | vec![ | 211 | vec![ |
| 212 | TableMap { | 212 | TableMap { |
| 213 | name: "user", | 213 | name: "user", |
| @@ -228,6 +228,16 @@ fn build_maps(old_has_ui: bool) -> Vec<TableMap> { | |||
| 228 | ("ui", "'ledger'".into()) | 228 | ("ui", "'ledger'".into()) |
| 229 | }); | 229 | }); |
| 230 | columns.extend(old_cols(&["created_at", "last_seen_at"])); | 230 | columns.extend(old_cols(&["created_at", "last_seen_at"])); |
| 231 | + columns.push(if old_has_theme { | ||
| 232 | + ("theme", "old.theme".into()) | ||
| 233 | + } else { | ||
| 234 | + ("theme", "'light'".into()) | ||
| 235 | + }); | ||
| 236 | + columns.push(if old_has_language { | ||
| 237 | + ("language", "old.language".into()) | ||
| 238 | + } else { | ||
| 239 | + ("language", "'en'".into()) | ||
| 240 | + }); | ||
| 231 | columns | 241 | columns |
| 232 | }, | 242 | }, |
| 233 | }, | 243 | }, |
| @@ -379,7 +389,9 @@ async fn copy_data(old_conn: &Connection, new_conn: &Connection, path: &str) -> | |||
| 379 | .map_err(|e| StoreError::Backend(e.to_string()))?; | 389 | .map_err(|e| StoreError::Backend(e.to_string()))?; |
| 380 | 390 | ||
| 381 | let old_has_ui = old_has_column(old_conn, "user", "ui").await?; | 391 | let old_has_ui = old_has_column(old_conn, "user", "ui").await?; |
| 382 | - let maps = build_maps(old_has_ui); | 392 | + let old_has_theme = old_has_column(old_conn, "user", "theme").await?; |
| 393 | + let old_has_language = old_has_column(old_conn, "user", "language").await?; | ||
| 394 | + let maps = build_maps(old_has_ui, old_has_theme, old_has_language); | ||
| 383 | validate_maps(new_conn, &maps).await?; | 395 | validate_maps(new_conn, &maps).await?; |
| 384 | 396 | ||
| 385 | // Foreign keys stay off during the copy and are checked by `verify` | 397 | // Foreign keys stay off during the copy and are checked by `verify` |
| @@ -565,7 +577,7 @@ fn backup_name(path: &str) -> Result<String> { | |||
| 565 | } | 577 | } |
| 566 | 578 | ||
| 567 | /// A probe the copy plan asks: whether the old database already carries | 579 | /// A probe the copy plan asks: whether the old database already carries |
| 568 | -/// `column` on `table`. A column the second migration added is absent on a | 580 | +/// `column` on `table`. A column a later migration added is absent on a |
| 569 | /// database old enough to predate it, and the map backfills the default | 581 | /// database old enough to predate it, and the map backfills the default |
| 570 | /// instead of reading a column that is not there. | 582 | /// instead of reading a column that is not there. |
| 571 | async fn old_has_column(conn: &Connection, table: &str, column: &str) -> Result<bool> { | 583 | async fn old_has_column(conn: &Connection, table: &str, column: &str) -> Result<bool> { |
crates/in-core/src/store/schema.rsmodified+1 -0
| @@ -16,6 +16,7 @@ use turso::Connection; | |||
| 16 | pub(crate) const MIGRATIONS: &[&str] = &[ | 16 | pub(crate) const MIGRATIONS: &[&str] = &[ |
| 17 | include_str!("../../migrations/0001_init.sql"), | 17 | include_str!("../../migrations/0001_init.sql"), |
| 18 | include_str!("../../migrations/0002_ui.sql"), | 18 | include_str!("../../migrations/0002_ui.sql"), |
| 19 | + include_str!("../../migrations/0003_preferences.sql"), | ||
| 19 | ]; | 20 | ]; |
| 20 | 21 | ||
| 21 | /// The whole declared schema — every migration applied in order — as one | 22 | /// The whole declared schema — every migration applied in order — as one |
crates/in-core/src/store/turso_store.rsmodified+14 -3
| @@ -36,7 +36,7 @@ const UPLOADS_DIR: &str = "uploads"; | |||
| 36 | const THUMB_SOURCE_CAP: u64 = 64 * 1024 * 1024; | 36 | const THUMB_SOURCE_CAP: u64 = 64 * 1024 * 1024; |
| 37 | 37 | ||
| 38 | const USER_COLUMNS: &str = | 38 | const USER_COLUMNS: &str = |
| 39 | - "id, oidc_sub, email, display_name, admin, disabled, quota_bytes, used_bytes, ui, created_at, last_seen_at"; | 39 | + "id, oidc_sub, email, display_name, admin, disabled, quota_bytes, used_bytes, ui, created_at, last_seen_at, theme, language"; |
| 40 | const FOLDER_COLUMNS: &str = "id, owner_id, parent_id, name, created_at, deleted_at"; | 40 | const FOLDER_COLUMNS: &str = "id, owner_id, parent_id, name, created_at, deleted_at"; |
| 41 | const FILE_COLUMNS: &str = | 41 | const FILE_COLUMNS: &str = |
| 42 | "id, owner_id, folder_id, name, mime, size_bytes, thumb_state, created_at, updated_at, deleted_at"; | 42 | "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<User> { | |||
| 714 | quota_bytes: row.get::<i64>(6).map_err(backend)?.max(0) as u64, | 714 | quota_bytes: row.get::<i64>(6).map_err(backend)?.max(0) as u64, |
| 715 | used_bytes: row.get::<i64>(7).map_err(backend)?.max(0) as u64, | 715 | used_bytes: row.get::<i64>(7).map_err(backend)?.max(0) as u64, |
| 716 | ui: text(row, 8)?, | 716 | ui: text(row, 8)?, |
| 717 | + theme: text(row, 11)?, | ||
| 718 | + language: text(row, 12)?, | ||
| 717 | created_at: parse_stamp(&text(row, 9)?)?, | 719 | created_at: parse_stamp(&text(row, 9)?)?, |
| 718 | last_seen_at: opt_stamp(row, 10)?, | 720 | last_seen_at: opt_stamp(row, 10)?, |
| 719 | }) | 721 | }) |
| @@ -947,13 +949,22 @@ impl Store for TursoStore { | |||
| 947 | Ok(()) | 949 | Ok(()) |
| 948 | } | 950 | } |
| 949 | 951 | ||
| 950 | - async fn set_user_ui(&self, id: &str, ui: &str) -> Result<()> { | 952 | + async fn set_preferences(&self, id: &str, theme: &str, language: &str, ui: &str) -> Result<()> { |
| 953 | + if theme != "light" && theme != "dark" { | ||
| 954 | + return Err(StoreError::Corrupt(format!("theme {theme:?}"))); | ||
| 955 | + } | ||
| 956 | + if language != "en" && language != "tr" { | ||
| 957 | + return Err(StoreError::Corrupt(format!("language {language:?}"))); | ||
| 958 | + } | ||
| 951 | if ui != "ledger" && ui != "instrument" { | 959 | if ui != "ledger" && ui != "instrument" { |
| 952 | return Err(StoreError::Corrupt(format!("ui {ui:?}"))); | 960 | return Err(StoreError::Corrupt(format!("ui {ui:?}"))); |
| 953 | } | 961 | } |
| 954 | let conn = self.conn.lock().await; | 962 | let conn = self.conn.lock().await; |
| 955 | let n = conn | 963 | let n = conn |
| 956 | - .execute("UPDATE user SET ui = ?1 WHERE id = ?2", params![ui, id]) | 964 | + .execute( |
| 965 | + "UPDATE user SET theme = ?1, language = ?2, ui = ?3 WHERE id = ?4", | ||
| 966 | + params![theme, language, ui, id], | ||
| 967 | + ) | ||
| 957 | .await | 968 | .await |
| 958 | .map_err(backend)?; | 969 | .map_err(backend)?; |
| 959 | if n == 0 { | 970 | if n == 0 { |
crates/in-core/tests/store.rsmodified+46 -16
| @@ -1122,39 +1122,59 @@ async fn add_share_user_needs_the_owner() { | |||
| 1122 | } | 1122 | } |
| 1123 | 1123 | ||
| 1124 | #[tokio::test] | 1124 | #[tokio::test] |
| 1125 | -async fn a_provisioned_user_wears_the_ledger_ui() { | 1125 | +async fn a_provisioned_user_wears_the_default_preferences() { |
| 1126 | let scratch = Scratch::open().await; | 1126 | let scratch = Scratch::open().await; |
| 1127 | let user = alice(&scratch.store).await; | 1127 | let user = alice(&scratch.store).await; |
| 1128 | assert_eq!(user.ui, "ledger"); | 1128 | assert_eq!(user.ui, "ledger"); |
| 1129 | + assert_eq!(user.theme, "light"); | ||
| 1130 | + assert_eq!(user.language, "en"); | ||
| 1129 | } | 1131 | } |
| 1130 | 1132 | ||
| 1131 | #[tokio::test] | 1133 | #[tokio::test] |
| 1132 | -async fn the_ui_round_trips_and_refuses_what_is_not_offered() { | 1134 | +async fn preferences_round_trip_and_refuse_what_is_not_offered() { |
| 1133 | let scratch = Scratch::open().await; | 1135 | let scratch = Scratch::open().await; |
| 1134 | let user = alice(&scratch.store).await; | 1136 | let user = alice(&scratch.store).await; |
| 1135 | - scratch.store.set_user_ui(&user.id, "instrument").await.unwrap(); | 1137 | + scratch |
| 1138 | + .store | ||
| 1139 | + .set_preferences(&user.id, "dark", "tr", "instrument") | ||
| 1140 | + .await | ||
| 1141 | + .unwrap(); | ||
| 1136 | let back = scratch.store.user(&user.id).await.unwrap().unwrap(); | 1142 | let back = scratch.store.user(&user.id).await.unwrap().unwrap(); |
| 1143 | + assert_eq!(back.theme, "dark"); | ||
| 1144 | + assert_eq!(back.language, "tr"); | ||
| 1137 | assert_eq!(back.ui, "instrument"); | 1145 | assert_eq!(back.ui, "instrument"); |
| 1138 | - // A value no settings screen offers is refused, and the row keeps what | 1146 | + // A value no preferences form offers is refused, and the row keeps what |
| 1139 | // the last good write left — the same Corrupt the store answers a bad | 1147 | // the last good write left — the same Corrupt the store answers a bad |
| 1140 | // enum it reads. | 1148 | // enum it reads. |
| 1141 | assert!(matches!( | 1149 | assert!(matches!( |
| 1142 | - scratch.store.set_user_ui(&user.id, "dense").await, | 1150 | + scratch.store.set_preferences(&user.id, "dim", "tr", "instrument").await, |
| 1151 | + Err(StoreError::Corrupt(_)) | ||
| 1152 | + )); | ||
| 1153 | + assert!(matches!( | ||
| 1154 | + scratch.store.set_preferences(&user.id, "dark", "xx", "instrument").await, | ||
| 1155 | + Err(StoreError::Corrupt(_)) | ||
| 1156 | + )); | ||
| 1157 | + assert!(matches!( | ||
| 1158 | + scratch.store.set_preferences(&user.id, "dark", "tr", "dense").await, | ||
| 1143 | Err(StoreError::Corrupt(_)) | 1159 | Err(StoreError::Corrupt(_)) |
| 1144 | )); | 1160 | )); |
| 1145 | let kept = scratch.store.user(&user.id).await.unwrap().unwrap(); | 1161 | let kept = scratch.store.user(&user.id).await.unwrap().unwrap(); |
| 1162 | + assert_eq!(kept.theme, "dark"); | ||
| 1163 | + assert_eq!(kept.language, "tr"); | ||
| 1146 | assert_eq!(kept.ui, "instrument"); | 1164 | assert_eq!(kept.ui, "instrument"); |
| 1147 | assert!(matches!( | 1165 | assert!(matches!( |
| 1148 | - scratch.store.set_user_ui("no-such-user", "ledger").await, | 1166 | + scratch.store.set_preferences("no-such-user", "light", "en", "ledger").await, |
| 1149 | Err(StoreError::NotFound) | 1167 | Err(StoreError::NotFound) |
| 1150 | )); | 1168 | )); |
| 1151 | } | 1169 | } |
| 1152 | 1170 | ||
| 1153 | #[tokio::test] | 1171 | #[tokio::test] |
| 1154 | -async fn a_first_migration_database_gains_the_ui_column_on_open() { | 1172 | +async fn a_second_migration_database_gains_the_preference_columns_on_open() { |
| 1155 | - // A database built from 0001 alone — the shape In was deployed with — | 1173 | + // A database built from 0001+0002 — the shape In ran with an interface |
| 1156 | - // carries no `ui` column. Opening it with the current code reconciles it | 1174 | + // picker and no theme or language — carries no `theme` or `language` |
| 1157 | - // onto the declared schema, and its people arrive wearing the default. | 1175 | + // column. Opening it with the current code reconciles it onto the |
| 1176 | + // declared schema: its interface choice is carried across, and the two | ||
| 1177 | + // new preferences arrive wearing their defaults. | ||
| 1158 | let dir = std::env::temp_dir().join(format!("in-test-{}", Ulid::new())); | 1178 | let dir = std::env::temp_dir().join(format!("in-test-{}", Ulid::new())); |
| 1159 | std::fs::create_dir_all(&dir).unwrap(); | 1179 | std::fs::create_dir_all(&dir).unwrap(); |
| 1160 | let path = dir.join("in.db").to_string_lossy().into_owned(); | 1180 | 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() { | |||
| 1164 | conn.execute_batch(include_str!("../migrations/0001_init.sql")) | 1184 | conn.execute_batch(include_str!("../migrations/0001_init.sql")) |
| 1165 | .await | 1185 | .await |
| 1166 | .unwrap(); | 1186 | .unwrap(); |
| 1187 | + conn.execute_batch(include_str!("../migrations/0002_ui.sql")) | ||
| 1188 | + .await | ||
| 1189 | + .unwrap(); | ||
| 1167 | conn.execute( | 1190 | conn.execute( |
| 1168 | "INSERT INTO user (id, oidc_sub, email, display_name, admin, disabled, \ | 1191 | "INSERT INTO user (id, oidc_sub, email, display_name, admin, disabled, \ |
| 1169 | - quota_bytes, used_bytes, created_at, last_seen_at) \ | 1192 | + quota_bytes, used_bytes, ui, created_at, last_seen_at) \ |
| 1170 | VALUES ('u-old', 'sub-old', 'old@example.com', 'Old', 1, 0, 100, 0, \ | 1193 | VALUES ('u-old', 'sub-old', 'old@example.com', 'Old', 1, 0, 100, 0, \ |
| 1171 | - '2026-08-01T00:00:00Z', '2026-08-01T00:00:00Z')", | 1194 | + 'instrument', '2026-08-01T00:00:00Z', '2026-08-01T00:00:00Z')", |
| 1172 | (), | 1195 | (), |
| 1173 | ) | 1196 | ) |
| 1174 | .await | 1197 | .await |
| @@ -1177,11 +1200,18 @@ async fn a_first_migration_database_gains_the_ui_column_on_open() { | |||
| 1177 | let storage = dir.join("storage"); | 1200 | let storage = dir.join("storage"); |
| 1178 | let store = TursoStore::open(&path, Some(&storage)).await.unwrap(); | 1201 | let store = TursoStore::open(&path, Some(&storage)).await.unwrap(); |
| 1179 | let user = store.user_by_oidc_sub("sub-old").await.unwrap().unwrap(); | 1202 | let user = store.user_by_oidc_sub("sub-old").await.unwrap().unwrap(); |
| 1180 | - assert_eq!(user.ui, "ledger"); | 1203 | + assert_eq!(user.ui, "instrument"); |
| 1181 | - // And the carried row takes a new preference like any other. | 1204 | + assert_eq!(user.theme, "light"); |
| 1182 | - store.set_user_ui(&user.id, "instrument").await.unwrap(); | 1205 | + assert_eq!(user.language, "en"); |
| 1206 | + // And the carried row takes new preferences like any other. | ||
| 1207 | + store | ||
| 1208 | + .set_preferences(&user.id, "dark", "tr", "ledger") | ||
| 1209 | + .await | ||
| 1210 | + .unwrap(); | ||
| 1183 | let back = store.user(&user.id).await.unwrap().unwrap(); | 1211 | let back = store.user(&user.id).await.unwrap().unwrap(); |
| 1184 | - assert_eq!(back.ui, "instrument"); | 1212 | + assert_eq!(back.theme, "dark"); |
| 1213 | + assert_eq!(back.language, "tr"); | ||
| 1214 | + assert_eq!(back.ui, "ledger"); | ||
| 1185 | drop(store); | 1215 | drop(store); |
| 1186 | let _ = std::fs::remove_dir_all(&dir); | 1216 | let _ = std::fs::remove_dir_all(&dir); |
| 1187 | } | 1217 | } |
crates/in-web/src/drive.rsmodified+1 -1
| @@ -166,7 +166,7 @@ async fn drive(cx: &Cx) -> Result { | |||
| 166 | }; | 166 | }; |
| 167 | } | 167 | } |
| 168 | }; | 168 | }; |
| 169 | - let language = lang(cx); | 169 | + let language = lang(cx).await; |
| 170 | let store = app(cx).store.clone(); | 170 | let store = app(cx).store.clone(); |
| 171 | 171 | ||
| 172 | let wanted = query_params::<DriveQuery>(cx) | 172 | let wanted = query_params::<DriveQuery>(cx) |
crates/in-web/src/i18n.rsmodified+33 -8
| @@ -1,15 +1,15 @@ | |||
| 1 | //! UI strings in two languages. | 1 | //! UI strings in two languages. |
| 2 | //! | 2 | //! |
| 3 | -//! v1 has no language column on the user row, so the reader's language comes | 3 | +//! A signed-in reader's language comes off their own user row, the way their |
| 4 | -//! off the request: an `Accept-Language` tag starting with `tr` reads | 4 | +//! theme and interface do; everybody else reads `Accept-Language`. An |
| 5 | -//! Turkish, everything else reads English. An unrecognized tag is not a | 5 | +//! unrecognized tag or code is not a refusal, just the default. |
| 6 | -//! refusal, just the default. | ||
| 7 | 6 | ||
| 8 | use topcoat::context::Cx; | 7 | use topcoat::context::Cx; |
| 9 | use topcoat::router::header; | 8 | use topcoat::router::header; |
| 10 | use topcoat::router::request::headers; | 9 | use topcoat::router::request::headers; |
| 11 | 10 | ||
| 12 | -/// English or Turkish, read off the request's `Accept-Language`. | 11 | +/// English or Turkish: the reader's own language when signed in, the |
| 12 | +/// request's `Accept-Language` otherwise. | ||
| 13 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | 13 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 14 | pub enum Lang { | 14 | pub enum Lang { |
| 15 | En, | 15 | En, |
| @@ -44,9 +44,19 @@ impl Lang { | |||
| 44 | } | 44 | } |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | -/// The request's language: `Accept-Language` first, English when it says | 47 | +/// The request's language: the signed-in reader's own row first, |
| 48 | -/// nothing usable. | 48 | +/// `Accept-Language` when signed out or unreadable. |
| 49 | -pub fn lang(cx: &Cx) -> Lang { | 49 | +/// |
| 50 | +/// Cheap per call: [`crate::server::current_user`] is memoized per request, | ||
| 51 | +/// and every signed-in page already resolved the user before asking. | ||
| 52 | +pub async fn lang(cx: &Cx) -> Lang { | ||
| 53 | + let signed_in = match crate::server::current_user(cx).await { | ||
| 54 | + Ok(user) => user.as_ref().map(|user| Lang::from_code(&user.language)), | ||
| 55 | + Err(_) => None, | ||
| 56 | + }; | ||
| 57 | + if let Some(language) = signed_in { | ||
| 58 | + return language; | ||
| 59 | + } | ||
| 50 | headers(cx) | 60 | headers(cx) |
| 51 | .get(header::ACCEPT_LANGUAGE) | 61 | .get(header::ACCEPT_LANGUAGE) |
| 52 | .and_then(|value| value.to_str().ok()) | 62 | .and_then(|value| value.to_str().ok()) |
| @@ -162,6 +172,11 @@ pub enum Key { | |||
| 162 | NoLinks, | 172 | NoLinks, |
| 163 | ManageLinks, | 173 | ManageLinks, |
| 164 | UiLabel, | 174 | UiLabel, |
| 175 | + ThemeLabel, | ||
| 176 | + LightOption, | ||
| 177 | + DarkOption, | ||
| 178 | + LanguageLabel, | ||
| 179 | + Saved, | ||
| 165 | } | 180 | } |
| 166 | 181 | ||
| 167 | pub fn t(lang: Lang, key: Key) -> &'static str { | 182 | pub fn t(lang: Lang, key: Key) -> &'static str { |
| @@ -261,6 +276,11 @@ pub fn t(lang: Lang, key: Key) -> &'static str { | |||
| 261 | NoLinks => "No share links yet.", | 276 | NoLinks => "No share links yet.", |
| 262 | ManageLinks => "Share links", | 277 | ManageLinks => "Share links", |
| 263 | UiLabel => "Interface", | 278 | UiLabel => "Interface", |
| 279 | + ThemeLabel => "THEME", | ||
| 280 | + LightOption => "Light", | ||
| 281 | + DarkOption => "Dark", | ||
| 282 | + LanguageLabel => "LANGUAGE", | ||
| 283 | + Saved => "Saved.", | ||
| 264 | }, | 284 | }, |
| 265 | Lang::Tr => match key { | 285 | Lang::Tr => match key { |
| 266 | Cancel => "Vazgeç", | 286 | Cancel => "Vazgeç", |
| @@ -356,6 +376,11 @@ pub fn t(lang: Lang, key: Key) -> &'static str { | |||
| 356 | NoLinks => "Henüz paylaşım bağlantısı yok.", | 376 | NoLinks => "Henüz paylaşım bağlantısı yok.", |
| 357 | ManageLinks => "Paylaşım bağlantıları", | 377 | ManageLinks => "Paylaşım bağlantıları", |
| 358 | UiLabel => "Arayüz", | 378 | UiLabel => "Arayüz", |
| 379 | + ThemeLabel => "TEMA", | ||
| 380 | + LightOption => "Açık", | ||
| 381 | + DarkOption => "Koyu", | ||
| 382 | + LanguageLabel => "DİL", | ||
| 383 | + Saved => "Kaydedildi.", | ||
| 359 | }, | 384 | }, |
| 360 | } | 385 | } |
| 361 | } | 386 | } |
crates/in-web/src/layout.rsmodified+8 -4
| @@ -850,15 +850,19 @@ const STYLE: Asset = asset!("assets/main.css"); | |||
| 850 | 850 | ||
| 851 | #[layout("/")] | 851 | #[layout("/")] |
| 852 | async fn root_layout(cx: &Cx, slot: Result) -> Result { | 852 | async fn root_layout(cx: &Cx, slot: Result) -> Result { |
| 853 | - // The one per-user chrome knob: the UI density, read off the request's | 853 | + // The per-user chrome knobs, read off the request's own user: the theme |
| 854 | - // own user. Signed-out (or unreadable) renders ledger, the default. | 854 | + // into `data-theme`, the interface into `data-ui`, the language into |
| 855 | + // `<html lang>`. Signed-out (or unreadable) renders light and ledger, | ||
| 856 | + // and the language falls back to `Accept-Language` — both are only set | ||
| 857 | + // when the request's own user has one to read. | ||
| 855 | let me = match current_user(cx).await { | 858 | let me = match current_user(cx).await { |
| 856 | Ok(user) => user.as_ref(), | 859 | Ok(user) => user.as_ref(), |
| 857 | Err(_) => None, | 860 | Err(_) => None, |
| 858 | }; | 861 | }; |
| 859 | let asking = me.is_some(); | 862 | let asking = me.is_some(); |
| 863 | + let dark = me.as_ref().is_some_and(|user| user.theme == "dark"); | ||
| 860 | let ui = me.as_ref().map_or("ledger", |user| user.ui.as_str()); | 864 | let ui = me.as_ref().map_or("ledger", |user| user.ui.as_str()); |
| 861 | - let lang = crate::i18n::lang(cx); | 865 | + let lang = crate::i18n::lang(cx).await; |
| 862 | 866 | ||
| 863 | let content = match slot { | 867 | let content = match slot { |
| 864 | Err(error) if error.downcast_ref::<NotFoundError>().is_some() => view! { | 868 | Err(error) if error.downcast_ref::<NotFoundError>().is_some() => view! { |
| @@ -880,7 +884,7 @@ async fn root_layout(cx: &Cx, slot: Result) -> Result { | |||
| 880 | let build = STYLE.id().as_u64().to_string(); | 884 | let build = STYLE.id().as_u64().to_string(); |
| 881 | view! { | 885 | view! { |
| 882 | <!DOCTYPE html> | 886 | <!DOCTYPE html> |
| 883 | - <html lang=(lang.code()) data-ui=(ui) data-build=(build)> | 887 | + <html lang=(lang.code()) data-theme=(dark.then_some("dark")) data-ui=(ui) data-build=(build)> |
| 884 | <head> | 888 | <head> |
| 885 | <meta charset="utf-8"> | 889 | <meta charset="utf-8"> |
| 886 | <meta name="viewport" content="width=device-width, initial-scale=1"> | 890 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
crates/in-web/src/pages.rsmodified+1 -1
| @@ -44,7 +44,7 @@ async fn landing(cx: &Cx) -> Result { | |||
| 44 | /// The sign-in card for a browser with nobody in it: the wordmark and one | 44 | /// The sign-in card for a browser with nobody in it: the wordmark and one |
| 45 | /// link, which starts the OIDC round-trip at `/auth/login`. | 45 | /// link, which starts the OIDC round-trip at `/auth/login`. |
| 46 | async fn sign_in_card(cx: &Cx) -> Result { | 46 | async fn sign_in_card(cx: &Cx) -> Result { |
| 47 | - let language = lang(cx); | 47 | + let language = lang(cx).await; |
| 48 | view! { | 48 | view! { |
| 49 | cx => | 49 | cx => |
| 50 | <main class="auth-stage"> | 50 | <main class="auth-stage"> |
crates/in-web/src/search.rsmodified+12 -6
| @@ -32,7 +32,7 @@ async fn search(cx: &Cx) -> Result { | |||
| 32 | let user = match require_user(cx).await { | 32 | let user = match require_user(cx).await { |
| 33 | Ok(user) => user, | 33 | Ok(user) => user, |
| 34 | Err(refusal) => { | 34 | Err(refusal) => { |
| 35 | - let language = lang(cx); | 35 | + let language = lang(cx).await; |
| 36 | return view! { | 36 | return view! { |
| 37 | cx => | 37 | cx => |
| 38 | <main class="scaffold-note"> | 38 | <main class="scaffold-note"> |
| @@ -42,7 +42,7 @@ async fn search(cx: &Cx) -> Result { | |||
| 42 | }; | 42 | }; |
| 43 | } | 43 | } |
| 44 | }; | 44 | }; |
| 45 | - let language = lang(cx); | 45 | + let language = lang(cx).await; |
| 46 | let asked = searched(query_value(uri(cx).query().unwrap_or(""), "q").as_deref()); | 46 | let asked = searched(query_value(uri(cx).query().unwrap_or(""), "q").as_deref()); |
| 47 | let hits = match asked.as_deref() { | 47 | let hits = match asked.as_deref() { |
| 48 | Some(query) => Some(app(cx).store.search(&user.id, query, LIMIT).await?), | 48 | Some(query) => Some(app(cx).store.search(&user.id, query, LIMIT).await?), |
| @@ -51,8 +51,8 @@ async fn search(cx: &Cx) -> Result { | |||
| 51 | let box_text = asked.clone().unwrap_or_default(); | 51 | let box_text = asked.clone().unwrap_or_default(); |
| 52 | view! { | 52 | view! { |
| 53 | cx => | 53 | cx => |
| 54 | - <main class="settings-shell"> | 54 | + (topbar(cx, NavPage::Search, &user, language).await?) |
| 55 | - (topbar(cx, NavPage::Search, &user, language).await?) | 55 | + <main class="settings-stage"> |
| 56 | <h1 class="settings-title">(t(language, Key::SearchResults))</h1> | 56 | <h1 class="settings-title">(t(language, Key::SearchResults))</h1> |
| 57 | <form class="field-box-search" method="get" action="/search"> | 57 | <form class="field-box-search" method="get" action="/search"> |
| 58 | <input | 58 | <input |
| @@ -70,7 +70,10 @@ async fn search(cx: &Cx) -> Result { | |||
| 70 | } | 70 | } |
| 71 | if !hits.folders.is_empty() { | 71 | if !hits.folders.is_empty() { |
| 72 | <section class="panel"> | 72 | <section class="panel"> |
| 73 | - <h2 class="panel-title">(t(language, Key::FoldersHeading))</h2> | 73 | + <div class="panel-head"> |
| 74 | + <h2 class="panel-title">(t(language, Key::FoldersHeading))</h2> | ||
| 75 | + <span class="chip">(format!("{}", hits.folders.len()))</span> | ||
| 76 | + </div> | ||
| 74 | <div class="panel-body"> | 77 | <div class="panel-body"> |
| 75 | for folder in &hits.folders { | 78 | for folder in &hits.folders { |
| 76 | <p><a href=(format!("/drive?folder={}", folder.id))>(folder.name.clone())</a></p> | 79 | <p><a href=(format!("/drive?folder={}", folder.id))>(folder.name.clone())</a></p> |
| @@ -80,7 +83,10 @@ async fn search(cx: &Cx) -> Result { | |||
| 80 | } | 83 | } |
| 81 | if !hits.files.is_empty() { | 84 | if !hits.files.is_empty() { |
| 82 | <section class="panel"> | 85 | <section class="panel"> |
| 83 | - <h2 class="panel-title">(t(language, Key::FilesHeading))</h2> | 86 | + <div class="panel-head"> |
| 87 | + <h2 class="panel-title">(t(language, Key::FilesHeading))</h2> | ||
| 88 | + <span class="chip">(format!("{}", hits.files.len()))</span> | ||
| 89 | + </div> | ||
| 84 | <div class="panel-body"> | 90 | <div class="panel-body"> |
| 85 | for file in &hits.files { | 91 | for file in &hits.files { |
| 86 | <p><a href=(format!("/file/{}", file.id))>(file.name.clone())</a></p> | 92 | <p><a href=(format!("/file/{}", file.id))>(file.name.clone())</a></p> |
crates/in-web/src/server.rsmodified+12 -0
| @@ -214,6 +214,10 @@ pub enum Refusal { | |||
| 214 | Unavailable, | 214 | Unavailable, |
| 215 | /// The ui field was not one of the values the form offers. | 215 | /// The ui field was not one of the values the form offers. |
| 216 | BadUi, | 216 | BadUi, |
| 217 | + /// The theme field was not one of the values the form offers. | ||
| 218 | + BadTheme, | ||
| 219 | + /// The language field was not one of the values the form offers. | ||
| 220 | + BadLanguage, | ||
| 217 | } | 221 | } |
| 218 | 222 | ||
| 219 | impl Refusal { | 223 | impl Refusal { |
| @@ -235,6 +239,8 @@ impl Refusal { | |||
| 235 | Refusal::AncestorTrashed => "Restore the folder it is in first.".to_string(), | 239 | Refusal::AncestorTrashed => "Restore the folder it is in first.".to_string(), |
| 236 | Refusal::Unavailable => "Something went wrong.".to_string(), | 240 | Refusal::Unavailable => "Something went wrong.".to_string(), |
| 237 | Refusal::BadUi => "That interface choice is not offered.".to_string(), | 241 | Refusal::BadUi => "That interface choice is not offered.".to_string(), |
| 242 | + Refusal::BadTheme => "That is not a theme.".to_string(), | ||
| 243 | + Refusal::BadLanguage => "That is not a language.".to_string(), | ||
| 238 | } | 244 | } |
| 239 | } | 245 | } |
| 240 | 246 | ||
| @@ -258,6 +264,8 @@ impl Refusal { | |||
| 258 | Refusal::Unavailable => "Bir şeyler ters gitti.".to_string(), | 264 | Refusal::Unavailable => "Bir şeyler ters gitti.".to_string(), |
| 259 | Refusal::AncestorTrashed => "Önce içinde olduğu klasörü geri yükle.".to_string(), | 265 | Refusal::AncestorTrashed => "Önce içinde olduğu klasörü geri yükle.".to_string(), |
| 260 | Refusal::BadUi => "Bu arayüz seçeneği sunulmuyor.".to_string(), | 266 | Refusal::BadUi => "Bu arayüz seçeneği sunulmuyor.".to_string(), |
| 267 | + Refusal::BadTheme => "Bu bir tema değil.".to_string(), | ||
| 268 | + Refusal::BadLanguage => "Bu bir dil değil.".to_string(), | ||
| 261 | } | 269 | } |
| 262 | } | 270 | } |
| 263 | 271 | ||
| @@ -279,6 +287,8 @@ impl Refusal { | |||
| 279 | "ancestor-trashed" => Refusal::AncestorTrashed, | 287 | "ancestor-trashed" => Refusal::AncestorTrashed, |
| 280 | "unavailable" => Refusal::Unavailable, | 288 | "unavailable" => Refusal::Unavailable, |
| 281 | "bad-ui" => Refusal::BadUi, | 289 | "bad-ui" => Refusal::BadUi, |
| 290 | + "bad-theme" => Refusal::BadTheme, | ||
| 291 | + "bad-language" => Refusal::BadLanguage, | ||
| 282 | _ => return None, | 292 | _ => return None, |
| 283 | }) | 293 | }) |
| 284 | } | 294 | } |
| @@ -304,6 +314,8 @@ impl Refusal { | |||
| 304 | Refusal::AncestorTrashed => "ancestor-trashed", | 314 | Refusal::AncestorTrashed => "ancestor-trashed", |
| 305 | Refusal::Unavailable => "unavailable", | 315 | Refusal::Unavailable => "unavailable", |
| 306 | Refusal::BadUi => "bad-ui", | 316 | Refusal::BadUi => "bad-ui", |
| 317 | + Refusal::BadTheme => "bad-theme", | ||
| 318 | + Refusal::BadLanguage => "bad-language", | ||
| 307 | } | 319 | } |
| 308 | } | 320 | } |
| 309 | } | 321 | } |
crates/in-web/src/settings.rsmodified+56 -18
| @@ -51,13 +51,15 @@ fn quota_percent(user: &User) -> u8 { | |||
| 51 | 51 | ||
| 52 | type Redirect = Result<(StatusCode, [(HeaderName, String); 1])>; | 52 | type Redirect = Result<(StatusCode, [(HeaderName, String); 1])>; |
| 53 | 53 | ||
| 54 | -/// Back to settings, the refusal (if any) on the query. | 54 | +/// Back to settings, the refusal (if any) on the query — or `saved=<call>` |
| 55 | +/// when there was nothing to refuse, the way iz's `saved_or_refused` marks | ||
| 56 | +/// a save the page should chip. | ||
| 55 | fn redirect_back(cx: &Cx, call: &str, refusal: Option<Refusal>) -> Redirect { | 57 | fn redirect_back(cx: &Cx, call: &str, refusal: Option<Refusal>) -> Redirect { |
| 56 | let back = back_to(cx, "/settings"); | 58 | let back = back_to(cx, "/settings"); |
| 57 | let separator = if back.contains('?') { '&' } else { '?' }; | 59 | let separator = if back.contains('?') { '&' } else { '?' }; |
| 58 | let location = match refusal { | 60 | let location = match refusal { |
| 59 | Some(refusal) => format!("{back}{separator}refusal={}&on={call}", refusal.code()), | 61 | Some(refusal) => format!("{back}{separator}refusal={}&on={call}", refusal.code()), |
| 60 | - None => back, | 62 | + None => format!("{back}{separator}saved={call}"), |
| 61 | }; | 63 | }; |
| 62 | Ok((StatusCode::SEE_OTHER, [(header::LOCATION, location)])) | 64 | Ok((StatusCode::SEE_OTHER, [(header::LOCATION, location)])) |
| 63 | } | 65 | } |
| @@ -118,25 +120,47 @@ async fn set_disabled(cx: &Cx, Form(input): Form<DisableForm>) -> Redirect { | |||
| 118 | } | 120 | } |
| 119 | 121 | ||
| 120 | #[derive(Deserialize)] | 122 | #[derive(Deserialize)] |
| 121 | -struct UiForm { | 123 | +struct PreferencesForm { |
| 122 | ui: String, | 124 | ui: String, |
| 125 | + theme: String, | ||
| 126 | + language: String, | ||
| 123 | } | 127 | } |
| 124 | 128 | ||
| 125 | -/// Switches the reader's own interface between the ledger and the | 129 | +/// The values the interface field offers. |
| 126 | -/// instrument. Anything else is refused — the form only ever offers the | 130 | +const UI_OPTIONS: [&str; 2] = ["instrument", "ledger"]; |
| 127 | -/// two, so another value is a hand-built post, not a typo. | 131 | + |
| 128 | -#[route(POST "/api/settings/ui")] | 132 | +/// The values the theme field offers. |
| 129 | -async fn set_ui(cx: &Cx, Form(input): Form<UiForm>) -> Redirect { | 133 | +const THEME_OPTIONS: [&str; 2] = ["light", "dark"]; |
| 134 | + | ||
| 135 | +/// The values the language field offers. | ||
| 136 | +const LANGUAGE_OPTIONS: [&str; 2] = ["en", "tr"]; | ||
| 137 | + | ||
| 138 | +/// Writes the reader's own display preferences: interface, theme, language. | ||
| 139 | +/// Each field is checked against what its dropdown offers — another value is | ||
| 140 | +/// a hand-built post, not a typo, and is refused with the field's own | ||
| 141 | +/// refusal, the way iz's `save_profile` answers its preferences block. | ||
| 142 | +#[route(POST "/api/settings/preferences")] | ||
| 143 | +async fn set_preferences(cx: &Cx, Form(input): Form<PreferencesForm>) -> Redirect { | ||
| 130 | let user = match require_user(cx).await { | 144 | let user = match require_user(cx).await { |
| 131 | Ok(user) => user, | 145 | Ok(user) => user, |
| 132 | - Err(refusal) => return redirect_back(cx, "ui", Some(refusal)), | 146 | + Err(refusal) => return redirect_back(cx, "preferences", Some(refusal)), |
| 133 | }; | 147 | }; |
| 134 | - if input.ui != "ledger" && input.ui != "instrument" { | 148 | + if !UI_OPTIONS.contains(&input.ui.as_str()) { |
| 135 | - return redirect_back(cx, "ui", Some(Refusal::BadUi)); | 149 | + return redirect_back(cx, "preferences", Some(Refusal::BadUi)); |
| 150 | + } | ||
| 151 | + if !THEME_OPTIONS.contains(&input.theme.as_str()) { | ||
| 152 | + return redirect_back(cx, "preferences", Some(Refusal::BadTheme)); | ||
| 136 | } | 153 | } |
| 137 | - match app(cx).store.set_user_ui(&user.id, &input.ui).await { | 154 | + if !LANGUAGE_OPTIONS.contains(&input.language.as_str()) { |
| 138 | - Ok(()) => redirect_back(cx, "ui", None), | 155 | + return redirect_back(cx, "preferences", Some(Refusal::BadLanguage)); |
| 139 | - Err(error) => redirect_back(cx, "ui", Some(refusal_of(error))), | 156 | + } |
| 157 | + match app(cx) | ||
| 158 | + .store | ||
| 159 | + .set_preferences(&user.id, &input.theme, &input.language, &input.ui) | ||
| 160 | + .await | ||
| 161 | + { | ||
| 162 | + Ok(()) => redirect_back(cx, "preferences", None), | ||
| 163 | + Err(error) => redirect_back(cx, "preferences", Some(refusal_of(error))), | ||
| 140 | } | 164 | } |
| 141 | } | 165 | } |
| 142 | 166 | ||
| @@ -147,7 +171,7 @@ async fn settings(cx: &Cx) -> Result { | |||
| 147 | let user = match require_user(cx).await { | 171 | let user = match require_user(cx).await { |
| 148 | Ok(user) => user, | 172 | Ok(user) => user, |
| 149 | Err(refusal) => { | 173 | Err(refusal) => { |
| 150 | - let language = lang(cx); | 174 | + let language = lang(cx).await; |
| 151 | return view! { | 175 | return view! { |
| 152 | cx => | 176 | cx => |
| 153 | <main class="scaffold-note"> | 177 | <main class="scaffold-note"> |
| @@ -157,7 +181,7 @@ async fn settings(cx: &Cx) -> Result { | |||
| 157 | }; | 181 | }; |
| 158 | } | 182 | } |
| 159 | }; | 183 | }; |
| 160 | - let language = lang(cx); | 184 | + let language = lang(cx).await; |
| 161 | let store = app(cx).store; | 185 | let store = app(cx).store; |
| 162 | // Re-read the row so the quota bar never shows a stale number. | 186 | // Re-read the row so the quota bar never shows a stale number. |
| 163 | let fresh = store | 187 | let fresh = store |
| @@ -184,7 +208,7 @@ async fn settings(cx: &Cx) -> Result { | |||
| 184 | <div class="settings-shell"> | 208 | <div class="settings-shell"> |
| 185 | <main class="settings-stage"> | 209 | <main class="settings-stage"> |
| 186 | <h1 class="settings-title">(t(language, Key::Settings))</h1> | 210 | <h1 class="settings-title">(t(language, Key::Settings))</h1> |
| 187 | - (refusal_banner(cx, language, &["create", "revoke", "add", "remove", "quota", "disable", "ui"]).await?) | 211 | + (refusal_banner(cx, language, &["create", "revoke", "add", "remove", "quota", "disable", "preferences"]).await?) |
| 188 | if let Some(token) = created { | 212 | if let Some(token) = created { |
| 189 | <section class="panel"> | 213 | <section class="panel"> |
| 190 | <div class="panel-head"> | 214 | <div class="panel-head"> |
| @@ -216,7 +240,7 @@ async fn settings(cx: &Cx) -> Result { | |||
| 216 | <div class="quota-fill" style=(format!("width: {}%", quota_percent(&fresh)))></div> | 240 | <div class="quota-fill" style=(format!("width: {}%", quota_percent(&fresh)))></div> |
| 217 | </div> | 241 | </div> |
| 218 | </div> | 242 | </div> |
| 219 | - <form class="field" method="post" action="/api/settings/ui"> | 243 | + <form class="field" method="post" action="/api/settings/preferences"> |
| 220 | <label class="field"> | 244 | <label class="field"> |
| 221 | <span class="field-label">(t(language, Key::UiLabel))</span> | 245 | <span class="field-label">(t(language, Key::UiLabel))</span> |
| 222 | <select class="field-input" name="ui"> | 246 | <select class="field-input" name="ui"> |
| @@ -224,6 +248,20 @@ async fn settings(cx: &Cx) -> Result { | |||
| 224 | <option value="ledger" selected=(fresh.ui == "ledger")>"Ledger"</option> | 248 | <option value="ledger" selected=(fresh.ui == "ledger")>"Ledger"</option> |
| 225 | </select> | 249 | </select> |
| 226 | </label> | 250 | </label> |
| 251 | + <label class="field"> | ||
| 252 | + <span class="field-label">(t(language, Key::ThemeLabel))</span> | ||
| 253 | + <select class="field-input" name="theme"> | ||
| 254 | + <option value="light" selected=(fresh.theme == "light")>(t(language, Key::LightOption))</option> | ||
| 255 | + <option value="dark" selected=(fresh.theme == "dark")>(t(language, Key::DarkOption))</option> | ||
| 256 | + </select> | ||
| 257 | + </label> | ||
| 258 | + <label class="field"> | ||
| 259 | + <span class="field-label">(t(language, Key::LanguageLabel))</span> | ||
| 260 | + <select class="field-input" name="language"> | ||
| 261 | + <option value="en" selected=(fresh.language == "en")>"English"</option> | ||
| 262 | + <option value="tr" selected=(fresh.language == "tr")>"Türkçe"</option> | ||
| 263 | + </select> | ||
| 264 | + </label> | ||
| 227 | <div class="panel-foot"> | 265 | <div class="panel-foot"> |
| 228 | <button class="primary" type="submit">(t(language, Key::Save))</button> | 266 | <button class="primary" type="submit">(t(language, Key::Save))</button> |
| 229 | </div> | 267 | </div> |
crates/in-web/src/share.rsmodified+38 -17
| @@ -35,20 +35,27 @@ use crate::server::{Refusal, app, back_to, require_user}; | |||
| 35 | path_param!(token); | 35 | path_param!(token); |
| 36 | 36 | ||
| 37 | /// A refusal surfaced as a banner: the `?refusal=<code>&on=<call>` pair the | 37 | /// A refusal surfaced as a banner: the `?refusal=<code>&on=<call>` pair the |
| 38 | -/// mutation redirects carry, rendered only when `on` names one of `calls`. | 38 | +/// mutation redirects carry, rendered only when `on` names one of `calls` — |
| 39 | -/// Shared by the trash and settings pages, which own no banner of their own. | 39 | +/// and the `?saved=<call>` chip a clean save carries, the way iz's |
| 40 | +/// `saved_or_refused` marks one. Shared by the trash and settings pages, | ||
| 41 | +/// which own no banner of their own. | ||
| 40 | pub(crate) async fn refusal_banner(cx: &Cx, language: Lang, calls: &[&str]) -> Result { | 42 | pub(crate) async fn refusal_banner(cx: &Cx, language: Lang, calls: &[&str]) -> Result { |
| 41 | let query = uri(cx).query().unwrap_or(""); | 43 | let query = uri(cx).query().unwrap_or(""); |
| 42 | let refusal = query_value(query, "refusal").and_then(|code| Refusal::from_code(&code)); | 44 | let refusal = query_value(query, "refusal").and_then(|code| Refusal::from_code(&code)); |
| 43 | let on = query_value(query, "on").unwrap_or_default(); | 45 | let on = query_value(query, "on").unwrap_or_default(); |
| 44 | - let mine = calls.iter().any(|call| *call == on); | 46 | + let saved = query_value(query, "saved").unwrap_or_default(); |
| 47 | + let refused = calls.iter().any(|call| *call == on); | ||
| 48 | + let kept = calls.iter().any(|call| *call == saved); | ||
| 45 | view! { | 49 | view! { |
| 46 | cx => | 50 | cx => |
| 47 | - if mine { | 51 | + if refused { |
| 48 | if let Some(refusal) = refusal { | 52 | if let Some(refusal) = refusal { |
| 49 | <p class="field-error" role="alert">(refusal.message_in(language))</p> | 53 | <p class="field-error" role="alert">(refusal.message_in(language))</p> |
| 50 | } | 54 | } |
| 51 | } | 55 | } |
| 56 | + if kept { | ||
| 57 | + <p class="field-note" role="status">(t(language, Key::Saved))</p> | ||
| 58 | + } | ||
| 52 | } | 59 | } |
| 53 | } | 60 | } |
| 54 | 61 | ||
| @@ -389,7 +396,7 @@ fn has_flag(query: &str, key: &str) -> bool { | |||
| 389 | /// target, or a download the link may not open. One answer for all of them — | 396 | /// target, or a download the link may not open. One answer for all of them — |
| 390 | /// a stranger learns nothing about which tokens exist. | 397 | /// a stranger learns nothing about which tokens exist. |
| 391 | async fn dead_link(cx: &Cx) -> Result { | 398 | async fn dead_link(cx: &Cx) -> Result { |
| 392 | - let language = lang(cx); | 399 | + let language = lang(cx).await; |
| 393 | view! { | 400 | view! { |
| 394 | cx => | 401 | cx => |
| 395 | <main class="scaffold-note"> | 402 | <main class="scaffold-note"> |
| @@ -592,7 +599,7 @@ async fn file_card( | |||
| 592 | link: &in_core::store::ShareLink, | 599 | link: &in_core::store::ShareLink, |
| 593 | file: &in_core::store::File, | 600 | file: &in_core::store::File, |
| 594 | ) -> topcoat::Result<topcoat::router::response::Response> { | 601 | ) -> topcoat::Result<topcoat::router::response::Response> { |
| 595 | - let language = lang(cx); | 602 | + let language = lang(cx).await; |
| 596 | let preview = file.mime.starts_with("image/"); | 603 | let preview = file.mime.starts_with("image/"); |
| 597 | let token_path = current_path(cx); | 604 | let token_path = current_path(cx); |
| 598 | view! { | 605 | view! { |
| @@ -626,7 +633,7 @@ async fn folder_card( | |||
| 626 | at: &str, | 633 | at: &str, |
| 627 | token: &str, | 634 | token: &str, |
| 628 | ) -> topcoat::Result<topcoat::router::response::Response> { | 635 | ) -> topcoat::Result<topcoat::router::response::Response> { |
| 629 | - let language = lang(cx); | 636 | + let language = lang(cx).await; |
| 630 | let here = store | 637 | let here = store |
| 631 | .folder(at) | 638 | .folder(at) |
| 632 | .await?; | 639 | .await?; |
| @@ -639,11 +646,14 @@ async fn folder_card( | |||
| 639 | let base = format!("/s/{token}"); | 646 | let base = format!("/s/{token}"); |
| 640 | view! { | 647 | view! { |
| 641 | cx => | 648 | cx => |
| 642 | - <main class="settings-shell"> | 649 | + (wordmark(cx).await?) |
| 643 | - (wordmark(cx).await?) | 650 | + <main class="settings-stage"> |
| 644 | <h1 class="settings-title">(here.name.clone())</h1> | 651 | <h1 class="settings-title">(here.name.clone())</h1> |
| 645 | <section class="panel"> | 652 | <section class="panel"> |
| 646 | - <h2 class="panel-title">(t(language, Key::FoldersHeading))</h2> | 653 | + <div class="panel-head"> |
| 654 | + <h2 class="panel-title">(t(language, Key::FoldersHeading))</h2> | ||
| 655 | + <span class="chip">(format!("{}", listing.folders.len()))</span> | ||
| 656 | + </div> | ||
| 647 | <div class="panel-body"> | 657 | <div class="panel-body"> |
| 648 | if listing.folders.is_empty() { | 658 | if listing.folders.is_empty() { |
| 649 | <p class="field-note">(t(language, Key::EmptyFolder))</p> | 659 | <p class="field-note">(t(language, Key::EmptyFolder))</p> |
| @@ -654,7 +664,10 @@ async fn folder_card( | |||
| 654 | </div> | 664 | </div> |
| 655 | </section> | 665 | </section> |
| 656 | <section class="panel"> | 666 | <section class="panel"> |
| 657 | - <h2 class="panel-title">(t(language, Key::FilesHeading))</h2> | 667 | + <div class="panel-head"> |
| 668 | + <h2 class="panel-title">(t(language, Key::FilesHeading))</h2> | ||
| 669 | + <span class="chip">(format!("{}", listing.files.len()))</span> | ||
| 670 | + </div> | ||
| 658 | <div class="panel-body"> | 671 | <div class="panel-body"> |
| 659 | if listing.files.is_empty() { | 672 | if listing.files.is_empty() { |
| 660 | <p class="field-note">(t(language, Key::EmptyFolder))</p> | 673 | <p class="field-note">(t(language, Key::EmptyFolder))</p> |
| @@ -691,7 +704,7 @@ async fn shared(cx: &Cx) -> Result { | |||
| 691 | let user = match require_user(cx).await { | 704 | let user = match require_user(cx).await { |
| 692 | Ok(user) => user, | 705 | Ok(user) => user, |
| 693 | Err(refusal) => { | 706 | Err(refusal) => { |
| 694 | - let language = lang(cx); | 707 | + let language = lang(cx).await; |
| 695 | return view! { | 708 | return view! { |
| 696 | cx => | 709 | cx => |
| 697 | <main class="scaffold-note"> | 710 | <main class="scaffold-note"> |
| @@ -701,7 +714,7 @@ async fn shared(cx: &Cx) -> Result { | |||
| 701 | }; | 714 | }; |
| 702 | } | 715 | } |
| 703 | }; | 716 | }; |
| 704 | - let language = lang(cx); | 717 | + let language = lang(cx).await; |
| 705 | let store = app(cx).store; | 718 | let store = app(cx).store; |
| 706 | let items = store | 719 | let items = store |
| 707 | .shares_for_user(&user.id) | 720 | .shares_for_user(&user.id) |
| @@ -719,16 +732,21 @@ async fn shared(cx: &Cx) -> Result { | |||
| 719 | owners.insert(item.owner_id.clone(), name); | 732 | owners.insert(item.owner_id.clone(), name); |
| 720 | } | 733 | } |
| 721 | } | 734 | } |
| 735 | + let folder_count = items.iter().filter(|item| item.kind == ShareKind::Folder).count(); | ||
| 736 | + let file_count = items.iter().filter(|item| item.kind == ShareKind::File).count(); | ||
| 722 | view! { | 737 | view! { |
| 723 | cx => | 738 | cx => |
| 724 | - <main class="settings-shell"> | 739 | + (topbar(cx, NavPage::Shared, &user, language).await?) |
| 725 | - (topbar(cx, NavPage::Shared, &user, language).await?) | 740 | + <main class="settings-stage"> |
| 726 | <h1 class="settings-title">(t(language, Key::SharedWithMe))</h1> | 741 | <h1 class="settings-title">(t(language, Key::SharedWithMe))</h1> |
| 727 | if items.is_empty() { | 742 | if items.is_empty() { |
| 728 | <p class="field-note">(t(language, Key::NoSharedItems))</p> | 743 | <p class="field-note">(t(language, Key::NoSharedItems))</p> |
| 729 | } | 744 | } |
| 730 | <section class="panel"> | 745 | <section class="panel"> |
| 731 | - <h2 class="panel-title">(t(language, Key::FoldersHeading))</h2> | 746 | + <div class="panel-head"> |
| 747 | + <h2 class="panel-title">(t(language, Key::FoldersHeading))</h2> | ||
| 748 | + <span class="chip">(format!("{folder_count}"))</span> | ||
| 749 | + </div> | ||
| 732 | <div class="panel-body"> | 750 | <div class="panel-body"> |
| 733 | for item in items.iter().filter(|item| item.kind == ShareKind::Folder) { | 751 | for item in items.iter().filter(|item| item.kind == ShareKind::Folder) { |
| 734 | <p> | 752 | <p> |
| @@ -740,7 +758,10 @@ async fn shared(cx: &Cx) -> Result { | |||
| 740 | </div> | 758 | </div> |
| 741 | </section> | 759 | </section> |
| 742 | <section class="panel"> | 760 | <section class="panel"> |
| 743 | - <h2 class="panel-title">(t(language, Key::FilesHeading))</h2> | 761 | + <div class="panel-head"> |
| 762 | + <h2 class="panel-title">(t(language, Key::FilesHeading))</h2> | ||
| 763 | + <span class="chip">(format!("{file_count}"))</span> | ||
| 764 | + </div> | ||
| 744 | <div class="panel-body"> | 765 | <div class="panel-body"> |
| 745 | for item in items.iter().filter(|item| item.kind == ShareKind::File) { | 766 | for item in items.iter().filter(|item| item.kind == ShareKind::File) { |
| 746 | <p> | 767 | <p> |
crates/in-web/src/trash.rsmodified+12 -6
| @@ -143,7 +143,7 @@ async fn trash(cx: &Cx) -> Result { | |||
| 143 | let user = match require_user(cx).await { | 143 | let user = match require_user(cx).await { |
| 144 | Ok(user) => user, | 144 | Ok(user) => user, |
| 145 | Err(refusal) => { | 145 | Err(refusal) => { |
| 146 | - let language = lang(cx); | 146 | + let language = lang(cx).await; |
| 147 | return view! { | 147 | return view! { |
| 148 | cx => | 148 | cx => |
| 149 | <main class="scaffold-note"> | 149 | <main class="scaffold-note"> |
| @@ -153,15 +153,15 @@ async fn trash(cx: &Cx) -> Result { | |||
| 153 | }; | 153 | }; |
| 154 | } | 154 | } |
| 155 | }; | 155 | }; |
| 156 | - let language = lang(cx); | 156 | + let language = lang(cx).await; |
| 157 | let listing = app(cx) | 157 | let listing = app(cx) |
| 158 | .store | 158 | .store |
| 159 | .list_trash(&user.id) | 159 | .list_trash(&user.id) |
| 160 | .await?; | 160 | .await?; |
| 161 | view! { | 161 | view! { |
| 162 | cx => | 162 | cx => |
| 163 | - <main class="settings-shell"> | 163 | + (topbar(cx, NavPage::Trash, &user, language).await?) |
| 164 | - (topbar(cx, NavPage::Trash, &user, language).await?) | 164 | + <main class="settings-stage"> |
| 165 | <h1 class="settings-title">(t(language, Key::Trash))</h1> | 165 | <h1 class="settings-title">(t(language, Key::Trash))</h1> |
| 166 | (refusal_banner(cx, language, &["restore", "purge", "empty"]).await?) | 166 | (refusal_banner(cx, language, &["restore", "purge", "empty"]).await?) |
| 167 | if listing.folders.is_empty() && listing.files.is_empty() { | 167 | if listing.folders.is_empty() && listing.files.is_empty() { |
| @@ -174,7 +174,10 @@ async fn trash(cx: &Cx) -> Result { | |||
| 174 | } | 174 | } |
| 175 | if !listing.folders.is_empty() { | 175 | if !listing.folders.is_empty() { |
| 176 | <section class="panel"> | 176 | <section class="panel"> |
| 177 | - <h2 class="panel-title">(t(language, Key::FoldersHeading))</h2> | 177 | + <div class="panel-head"> |
| 178 | + <h2 class="panel-title">(t(language, Key::FoldersHeading))</h2> | ||
| 179 | + <span class="chip">(format!("{}", listing.folders.len()))</span> | ||
| 180 | + </div> | ||
| 178 | <div class="panel-body"> | 181 | <div class="panel-body"> |
| 179 | for folder in &listing.folders { | 182 | for folder in &listing.folders { |
| 180 | <div class="member-row"> | 183 | <div class="member-row"> |
| @@ -196,7 +199,10 @@ async fn trash(cx: &Cx) -> Result { | |||
| 196 | } | 199 | } |
| 197 | if !listing.files.is_empty() { | 200 | if !listing.files.is_empty() { |
| 198 | <section class="panel"> | 201 | <section class="panel"> |
| 199 | - <h2 class="panel-title">(t(language, Key::FilesHeading))</h2> | 202 | + <div class="panel-head"> |
| 203 | + <h2 class="panel-title">(t(language, Key::FilesHeading))</h2> | ||
| 204 | + <span class="chip">(format!("{}", listing.files.len()))</span> | ||
| 205 | + </div> | ||
| 200 | <div class="panel-body"> | 206 | <div class="panel-body"> |
| 201 | for file in &listing.files { | 207 | for file in &listing.files { |
| 202 | <div class="member-row"> | 208 | <div class="member-row"> |
crates/in-web/tests/http.rsmodified+260 -27
| @@ -1897,29 +1897,51 @@ async fn a_refused_upload_carries_a_well_formed_query() { | |||
| 1897 | assert!(page.text().contains("already taken"), "{}", page.text()); | 1897 | assert!(page.text().contains("already taken"), "{}", page.text()); |
| 1898 | } | 1898 | } |
| 1899 | 1899 | ||
| 1900 | -// -- settings ui picker -------------------------------------------------------- | 1900 | +// -- settings preferences ------------------------------------------------------ |
| 1901 | 1901 | ||
| 1902 | #[tokio::test] | 1902 | #[tokio::test] |
| 1903 | -async fn settings_ui_picker_round_trips() { | 1903 | +async fn settings_preferences_round_trip() { |
| 1904 | let app = TestApp::build().await; | 1904 | let app = TestApp::build().await; |
| 1905 | - let cookie = app.sign_in("sub-ui", "ui@in.test", "Ui").await; | 1905 | + let cookie = app.sign_in("sub-prefs", "prefs@in.test", "Prefs").await; |
| 1906 | - let me = owner_of(&app, "sub-ui").await; | 1906 | + let me = owner_of(&app, "sub-prefs").await; |
| 1907 | - | 1907 | + |
| 1908 | - // The ledger is the default, worn on `<html>` from the first render. | 1908 | + // Light, English and ledger are the defaults, worn on `<html>` from the |
| 1909 | - assert_eq!(app.store.user(&me).await.unwrap().unwrap().ui, "ledger"); | 1909 | + // first render — and light means no `data-theme` at all. |
| 1910 | + let user = app.store.user(&me).await.unwrap().unwrap(); | ||
| 1911 | + assert_eq!(user.ui, "ledger"); | ||
| 1912 | + assert_eq!(user.theme, "light"); | ||
| 1913 | + assert_eq!(user.language, "en"); | ||
| 1910 | let page = app.get("/settings", Some(&cookie)).await; | 1914 | let page = app.get("/settings", Some(&cookie)).await; |
| 1911 | assert_eq!(page.status, StatusCode::OK, "{}", page.text()); | 1915 | assert_eq!(page.status, StatusCode::OK, "{}", page.text()); |
| 1912 | assert!(page.text().contains("data-ui=\"ledger\""), "{}", page.text()); | 1916 | assert!(page.text().contains("data-ui=\"ledger\""), "{}", page.text()); |
| 1917 | + assert!(!page.text().contains("data-theme="), "{}", page.text()); | ||
| 1918 | + assert!(page.text().contains("lang=\"en\""), "{}", page.text()); | ||
| 1913 | 1919 | ||
| 1914 | - // Picking the instrument writes the row and re-renders the chrome. | 1920 | + // Saving all three writes the row and re-renders the chrome. |
| 1915 | let answer = app | 1921 | let answer = app |
| 1916 | - .post("/api/settings/ui", Some(&cookie), &[("ui", "instrument")]) | 1922 | + .post( |
| 1923 | + "/api/settings/preferences", | ||
| 1924 | + Some(&cookie), | ||
| 1925 | + &[("ui", "instrument"), ("theme", "dark"), ("language", "tr")], | ||
| 1926 | + ) | ||
| 1917 | .await; | 1927 | .await; |
| 1918 | - assert!(answer.accepted(), "ui refused: {:?}", answer.location); | 1928 | + assert!( |
| 1919 | - assert_eq!( | 1929 | + answer.accepted(), |
| 1920 | - app.store.user(&me).await.unwrap().unwrap().ui, | 1930 | + "preferences refused: {:?}", |
| 1921 | - "instrument" | 1931 | + answer.location |
| 1922 | ); | 1932 | ); |
| 1933 | + let user = app.store.user(&me).await.unwrap().unwrap(); | ||
| 1934 | + assert_eq!(user.ui, "instrument"); | ||
| 1935 | + assert_eq!(user.theme, "dark"); | ||
| 1936 | + assert_eq!(user.language, "tr"); | ||
| 1937 | + | ||
| 1938 | + // The redirect carries the saved chip, rendered in the new language. | ||
| 1939 | + let page = app | ||
| 1940 | + .get(answer.location.as_deref().unwrap(), Some(&cookie)) | ||
| 1941 | + .await; | ||
| 1942 | + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); | ||
| 1943 | + assert!(page.text().contains("Kaydedildi."), "{}", page.text()); | ||
| 1944 | + | ||
| 1923 | let page = app.get("/settings", Some(&cookie)).await; | 1945 | let page = app.get("/settings", Some(&cookie)).await; |
| 1924 | assert_eq!(page.status, StatusCode::OK, "{}", page.text()); | 1946 | assert_eq!(page.status, StatusCode::OK, "{}", page.text()); |
| 1925 | assert!( | 1947 | assert!( |
| @@ -1927,44 +1949,160 @@ async fn settings_ui_picker_round_trips() { | |||
| 1927 | "{}", | 1949 | "{}", |
| 1928 | page.text() | 1950 | page.text() |
| 1929 | ); | 1951 | ); |
| 1930 | - assert!(page.text().contains("name=\"ui\""), "{}", page.text()); | 1952 | + assert!( |
| 1953 | + page.text().contains("data-theme=\"dark\""), | ||
| 1954 | + "{}", | ||
| 1955 | + page.text() | ||
| 1956 | + ); | ||
| 1957 | + assert!(page.text().contains("lang=\"tr\""), "{}", page.text()); | ||
| 1958 | + for name in ["name=\"ui\"", "name=\"theme\"", "name=\"language\""] { | ||
| 1959 | + assert!(page.text().contains(name), "{}", page.text()); | ||
| 1960 | + } | ||
| 1931 | 1961 | ||
| 1932 | // And back again. | 1962 | // And back again. |
| 1933 | let answer = app | 1963 | let answer = app |
| 1934 | - .post("/api/settings/ui", Some(&cookie), &[("ui", "ledger")]) | 1964 | + .post( |
| 1965 | + "/api/settings/preferences", | ||
| 1966 | + Some(&cookie), | ||
| 1967 | + &[("ui", "ledger"), ("theme", "light"), ("language", "en")], | ||
| 1968 | + ) | ||
| 1935 | .await; | 1969 | .await; |
| 1936 | - assert!(answer.accepted(), "ui refused: {:?}", answer.location); | 1970 | + assert!( |
| 1937 | - assert_eq!(app.store.user(&me).await.unwrap().unwrap().ui, "ledger"); | 1971 | + answer.accepted(), |
| 1972 | + "preferences refused: {:?}", | ||
| 1973 | + answer.location | ||
| 1974 | + ); | ||
| 1975 | + let user = app.store.user(&me).await.unwrap().unwrap(); | ||
| 1976 | + assert_eq!(user.ui, "ledger"); | ||
| 1977 | + assert_eq!(user.theme, "light"); | ||
| 1978 | + assert_eq!(user.language, "en"); | ||
| 1938 | } | 1979 | } |
| 1939 | 1980 | ||
| 1940 | #[tokio::test] | 1981 | #[tokio::test] |
| 1941 | -async fn settings_ui_rejects_unknown_values() { | 1982 | +async fn settings_preferences_rejects_unknown_values() { |
| 1942 | let app = TestApp::build().await; | 1983 | let app = TestApp::build().await; |
| 1943 | - let cookie = app.sign_in("sub-ui-bad", "uibad@in.test", "UiBad").await; | 1984 | + let cookie = app.sign_in("sub-prefs-bad", "prefsbad@in.test", "PrefsBad").await; |
| 1944 | - let me = owner_of(&app, "sub-ui-bad").await; | 1985 | + let me = owner_of(&app, "sub-prefs-bad").await; |
| 1945 | 1986 | ||
| 1987 | + // Each field is refused with its own code, and the row keeps the last | ||
| 1988 | + // good write. | ||
| 1989 | + let answer = app | ||
| 1990 | + .post( | ||
| 1991 | + "/api/settings/preferences", | ||
| 1992 | + Some(&cookie), | ||
| 1993 | + &[("ui", "mosaic"), ("theme", "light"), ("language", "en")], | ||
| 1994 | + ) | ||
| 1995 | + .await; | ||
| 1996 | + assert!(answer.refused("bad-ui", "preferences"), "{:?}", answer.location); | ||
| 1997 | + let answer = app | ||
| 1998 | + .post( | ||
| 1999 | + "/api/settings/preferences", | ||
| 2000 | + Some(&cookie), | ||
| 2001 | + &[("ui", "ledger"), ("theme", "dim"), ("language", "en")], | ||
| 2002 | + ) | ||
| 2003 | + .await; | ||
| 2004 | + assert!( | ||
| 2005 | + answer.refused("bad-theme", "preferences"), | ||
| 2006 | + "{:?}", | ||
| 2007 | + answer.location | ||
| 2008 | + ); | ||
| 1946 | let answer = app | 2009 | let answer = app |
| 1947 | - .post("/api/settings/ui", Some(&cookie), &[("ui", "mosaic")]) | 2010 | + .post( |
| 2011 | + "/api/settings/preferences", | ||
| 2012 | + Some(&cookie), | ||
| 2013 | + &[("ui", "ledger"), ("theme", "light"), ("language", "xx")], | ||
| 2014 | + ) | ||
| 1948 | .await; | 2015 | .await; |
| 1949 | - assert!(answer.refused("bad-ui", "ui"), "{:?}", answer.location); | 2016 | + assert!( |
| 1950 | - assert_eq!(app.store.user(&me).await.unwrap().unwrap().ui, "ledger"); | 2017 | + answer.refused("bad-language", "preferences"), |
| 2018 | + "{:?}", | ||
| 2019 | + answer.location | ||
| 2020 | + ); | ||
| 2021 | + let user = app.store.user(&me).await.unwrap().unwrap(); | ||
| 2022 | + assert_eq!(user.ui, "ledger"); | ||
| 2023 | + assert_eq!(user.theme, "light"); | ||
| 2024 | + assert_eq!(user.language, "en"); | ||
| 1951 | 2025 | ||
| 1952 | - // The carried refusal renders under the picker form. | 2026 | + // The carried refusal renders on the page, in the reader's language. |
| 1953 | let page = app | 2027 | let page = app |
| 1954 | .get(answer.location.as_deref().unwrap(), Some(&cookie)) | 2028 | .get(answer.location.as_deref().unwrap(), Some(&cookie)) |
| 1955 | .await; | 2029 | .await; |
| 1956 | assert_eq!(page.status, StatusCode::OK, "{}", page.text()); | 2030 | assert_eq!(page.status, StatusCode::OK, "{}", page.text()); |
| 1957 | assert!( | 2031 | assert!( |
| 1958 | - page.text().contains("That interface choice is not offered."), | 2032 | + page.text().contains("That is not a language."), |
| 1959 | "{}", | 2033 | "{}", |
| 1960 | page.text() | 2034 | page.text() |
| 1961 | ); | 2035 | ); |
| 1962 | 2036 | ||
| 1963 | // Signed out, the same post asks to sign in first. | 2037 | // Signed out, the same post asks to sign in first. |
| 1964 | let answer = app | 2038 | let answer = app |
| 1965 | - .post("/api/settings/ui", None, &[("ui", "instrument")]) | 2039 | + .post( |
| 2040 | + "/api/settings/preferences", | ||
| 2041 | + None, | ||
| 2042 | + &[("ui", "instrument"), ("theme", "dark"), ("language", "tr")], | ||
| 2043 | + ) | ||
| 2044 | + .await; | ||
| 2045 | + assert!( | ||
| 2046 | + answer.refused("sign-in-first", "preferences"), | ||
| 2047 | + "{:?}", | ||
| 2048 | + answer.location | ||
| 2049 | + ); | ||
| 2050 | +} | ||
| 2051 | + | ||
| 2052 | +impl TestApp { | ||
| 2053 | + /// Like `get`, but carrying an `Accept-Language` header, for the | ||
| 2054 | + /// signed-out language fallback. | ||
| 2055 | + async fn get_with_lang(&self, path: &str, cookie: Option<&str>, lang: &str) -> Raw { | ||
| 2056 | + let mut request = Request::builder() | ||
| 2057 | + .method("GET") | ||
| 2058 | + .uri(path) | ||
| 2059 | + .header(header::ACCEPT_LANGUAGE, lang); | ||
| 2060 | + if let Some(cookie) = cookie { | ||
| 2061 | + request = request.header(header::COOKIE, HeaderValue::from_str(cookie).unwrap()); | ||
| 2062 | + } | ||
| 2063 | + let response = self | ||
| 2064 | + .router | ||
| 2065 | + .handle(request.body(Body::empty()).unwrap()) | ||
| 2066 | + .await; | ||
| 2067 | + let status = response.status(); | ||
| 2068 | + let bytes = to_bytes(response.into_body(), usize::MAX) | ||
| 2069 | + .await | ||
| 2070 | + .unwrap() | ||
| 2071 | + .to_vec(); | ||
| 2072 | + Raw { | ||
| 2073 | + status, | ||
| 2074 | + content_type: None, | ||
| 2075 | + disposition: None, | ||
| 2076 | + content_range: None, | ||
| 2077 | + accept_ranges: None, | ||
| 2078 | + cache_control: None, | ||
| 2079 | + etag: None, | ||
| 2080 | + location: None, | ||
| 2081 | + bytes, | ||
| 2082 | + } | ||
| 2083 | + } | ||
| 2084 | +} | ||
| 2085 | + | ||
| 2086 | +#[tokio::test] | ||
| 2087 | +async fn signed_out_settings_falls_back_to_accept_language() { | ||
| 2088 | + let app = TestApp::build().await; | ||
| 2089 | + | ||
| 2090 | + // No header: English chrome on a light ledger shell. | ||
| 2091 | + let page = app.get("/settings", None).await; | ||
| 2092 | + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); | ||
| 2093 | + assert!(page.text().contains("lang=\"en\""), "{}", page.text()); | ||
| 2094 | + assert!(!page.text().contains("data-theme="), "{}", page.text()); | ||
| 2095 | + assert!(page.text().contains("data-ui=\"ledger\""), "{}", page.text()); | ||
| 2096 | + assert!(page.text().contains("Sign in first."), "{}", page.text()); | ||
| 2097 | + | ||
| 2098 | + // A Turkish browser is answered in Turkish. | ||
| 2099 | + let page = app | ||
| 2100 | + .get_with_lang("/settings", None, "tr-TR,tr;q=0.9,en;q=0.8") | ||
| 1966 | .await; | 2101 | .await; |
| 1967 | - assert!(answer.refused("sign-in-first", "ui"), "{:?}", answer.location); | 2102 | + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); |
| 2103 | + assert!(page.text().contains("lang=\"tr\""), "{}", page.text()); | ||
| 2104 | + assert!(page.text().contains("Önce oturum aç."), "{}", page.text()); | ||
| 2105 | + assert!(page.text().contains("Sürücüye dön"), "{}", page.text()); | ||
| 1968 | } | 2106 | } |
| 1969 | 2107 | ||
| 1970 | #[tokio::test] | 2108 | #[tokio::test] |
| @@ -1985,3 +2123,98 @@ async fn settings_panels_nest_inside_the_stage() { | |||
| 1985 | // The admin register is a table now: one named column per fact. | 2123 | // The admin register is a table now: one named column per fact. |
| 1986 | assert!(body.contains("<th class=\"member-col-name\""), "{body}"); | 2124 | assert!(body.contains("<th class=\"member-col-name\""), "{body}"); |
| 1987 | } | 2125 | } |
| 2126 | + | ||
| 2127 | +// The /shared, /trash and /search strip fix: content belongs in the stage, | ||
| 2128 | +// never directly in the flex-row shell. Mirrors | ||
| 2129 | +// settings_panels_nest_inside_the_stage. | ||
| 2130 | +fn assert_stage_nests(body: &str, markers: &[&str]) { | ||
| 2131 | + assert!( | ||
| 2132 | + !body.contains("<main class=\"settings-shell\">"), | ||
| 2133 | + "content still sits in the shell: {body}" | ||
| 2134 | + ); | ||
| 2135 | + let stage = body | ||
| 2136 | + .find("<main class=\"settings-stage\">") | ||
| 2137 | + .expect("no settings-stage"); | ||
| 2138 | + let close = body.find("</main>").expect("no main close"); | ||
| 2139 | + assert!(stage < close, "unclosed stage"); | ||
| 2140 | + for marker in markers { | ||
| 2141 | + let at = body.find(marker).unwrap_or_else(|| panic!("no {marker}")); | ||
| 2142 | + assert!(stage < at && at < close, "{marker} escapes the stage"); | ||
| 2143 | + } | ||
| 2144 | +} | ||
| 2145 | + | ||
| 2146 | +#[tokio::test] | ||
| 2147 | +async fn shared_lists_nest_inside_the_stage() { | ||
| 2148 | + let app = TestApp::build().await; | ||
| 2149 | + let admin = app.sign_in("sub-stage-sharer", "sharer@in.test", "Sharer").await; | ||
| 2150 | + let bob = app.sign_in("sub-stage-bob", "stagebob@in.test", "StageBob").await; | ||
| 2151 | + let owner = owner_of(&app, "sub-stage-sharer").await; | ||
| 2152 | + let file = file_id(&app, &owner, "staged.txt", b"staged").await; | ||
| 2153 | + let answer = app | ||
| 2154 | + .post( | ||
| 2155 | + "/api/share/user/add", | ||
| 2156 | + Some(&admin), | ||
| 2157 | + &[ | ||
| 2158 | + ("kind", "file"), | ||
| 2159 | + ("target_id", &file), | ||
| 2160 | + ("email", "stagebob@in.test"), | ||
| 2161 | + ("can_download", "1"), | ||
| 2162 | + ], | ||
| 2163 | + ) | ||
| 2164 | + .await; | ||
| 2165 | + assert!(answer.accepted(), "add refused: {:?}", answer.location); | ||
| 2166 | + let page = app.get("/shared", Some(&bob)).await; | ||
| 2167 | + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); | ||
| 2168 | + assert_stage_nests( | ||
| 2169 | + &page.text(), | ||
| 2170 | + &["panel-head", "panel-title", "staged.txt", "class=\"chip\""], | ||
| 2171 | + ); | ||
| 2172 | +} | ||
| 2173 | + | ||
| 2174 | +#[tokio::test] | ||
| 2175 | +async fn trash_panels_nest_inside_the_stage() { | ||
| 2176 | + let app = TestApp::build().await; | ||
| 2177 | + let admin = app.sign_in("sub-stage-trash", "stagetrash@in.test", "StageTrash").await; | ||
| 2178 | + let owner = owner_of(&app, "sub-stage-trash").await; | ||
| 2179 | + let one = file_id(&app, &owner, "staged-trash.txt", b"staged").await; | ||
| 2180 | + app.store.delete_file(&one).await.unwrap(); | ||
| 2181 | + let page = app.get("/trash", Some(&admin)).await; | ||
| 2182 | + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); | ||
| 2183 | + assert_stage_nests( | ||
| 2184 | + &page.text(), | ||
| 2185 | + &[ | ||
| 2186 | + "panel-head", | ||
| 2187 | + "staged-trash.txt", | ||
| 2188 | + "action=\"/api/trash/restore\"", | ||
| 2189 | + "action=\"/api/trash/empty\"", | ||
| 2190 | + ], | ||
| 2191 | + ); | ||
| 2192 | +} | ||
| 2193 | + | ||
| 2194 | +#[tokio::test] | ||
| 2195 | +async fn search_panels_nest_inside_the_stage() { | ||
| 2196 | + let app = TestApp::build().await; | ||
| 2197 | + let admin = app.sign_in("sub-stage-search", "stagesearch@in.test", "StageSearch").await; | ||
| 2198 | + let owner = owner_of(&app, "sub-stage-search").await; | ||
| 2199 | + file_id(&app, &owner, "staged-report.txt", b"staged").await; | ||
| 2200 | + let page = app.get("/search?q=staged-report", Some(&admin)).await; | ||
| 2201 | + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); | ||
| 2202 | + assert_stage_nests( | ||
| 2203 | + &page.text(), | ||
| 2204 | + &["panel-head", "name=\"q\"", "staged-report.txt"], | ||
| 2205 | + ); | ||
| 2206 | +} | ||
| 2207 | + | ||
| 2208 | +#[tokio::test] | ||
| 2209 | +async fn public_link_dead_card_survives_the_reskin() { | ||
| 2210 | + let app = TestApp::build().await; | ||
| 2211 | + // A never-real token is the dead card, not a stack. | ||
| 2212 | + let page = app.get("/s/never-real-token", None).await; | ||
| 2213 | + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); | ||
| 2214 | + assert!(page.text().contains("no longer works"), "{}", page.text()); | ||
| 2215 | + assert!( | ||
| 2216 | + page.text().contains("class=\"scaffold-note\""), | ||
| 2217 | + "{}", | ||
| 2218 | + page.text() | ||
| 2219 | + ); | ||
| 2220 | +} | ||