| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use std::collections::HashMap; |
| 6 | |
| 7 | use topcoat::{ |
| 8 | context::Cx, |
| 9 | router::{query_params, request::uri}, |
| 10 | view::{Child, View, attributes, view}, |
| 11 | }; |
| 12 | |
| 13 | use crate::app::shell::{notebook_name, now_millis, title_of}; |
| 14 | use crate::components::button::{ButtonSize, ButtonVariant, button}; |
| 15 | use crate::components::kbd::kbd; |
| 16 | use crate::i18n::{Lang, Msg, notes_count, relative_time, t}; |
| 17 | use crate::store::{ListFilter, Notebook, NoteMeta, Store}; |
| 18 | |
| 19 | const DAY_MS: i64 = 86_400_000; |
| 20 | |
| 21 | |
| 22 | const DEFAULT_ROWS: usize = 100; |
| 23 | |
| 24 | const MIN_ROWS: usize = 100; |
| 25 | const MAX_ROWS: usize = 5000; |
| 26 | |
| 27 | |
| 28 | #[query_params(error = bad_request)] |
| 29 | pub struct RowsQuery { |
| 30 | pub n: Option<usize>, |
| 31 | } |
| 32 | |
| 33 | |
| 34 | pub fn page_rows(cx: &Cx) -> usize { |
| 35 | query_params::<RowsQuery>(cx) |
| 36 | .ok() |
| 37 | .and_then(|query| query.n) |
| 38 | .unwrap_or(DEFAULT_ROWS) |
| 39 | .clamp(MIN_ROWS, MAX_ROWS) |
| 40 | } |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | pub fn more_href(cx: &Cx, rows: usize, total: usize) -> Option<String> { |
| 46 | if total <= rows || rows >= MAX_ROWS { |
| 47 | return None; |
| 48 | } |
| 49 | let target = uri(cx); |
| 50 | let mut pairs: Vec<String> = target |
| 51 | .query() |
| 52 | .unwrap_or("") |
| 53 | .split('&') |
| 54 | .filter(|pair| !pair.is_empty() && pair.split('=').next() != Some("n")) |
| 55 | .map(str::to_owned) |
| 56 | .collect(); |
| 57 | pairs.push(format!("n={}", (rows * 2).min(MAX_ROWS))); |
| 58 | Some(format!("{}?{}", target.path(), pairs.join("&"))) |
| 59 | } |
| 60 | |
| 61 | |
| 62 | pub struct ListPage { |
| 63 | pub notes: Vec<NoteMeta>, |
| 64 | pub previews: HashMap<String, String>, |
| 65 | |
| 66 | pub total: usize, |
| 67 | |
| 68 | pub more: Option<String>, |
| 69 | } |
| 70 | |
| 71 | |
| 72 | pub async fn load_page(cx: &Cx, store: &Store, filter: ListFilter) -> anyhow::Result<ListPage> { |
| 73 | let rows = page_rows(cx); |
| 74 | let total = store.count_filter(&filter).await?; |
| 75 | let notes = store.list_notes_page(filter, rows, 0).await?; |
| 76 | let ids: Vec<String> = notes.iter().map(|note| note.id.clone()).collect(); |
| 77 | let previews = store.previews_for(&ids).await?; |
| 78 | let more = more_href(cx, rows, total); |
| 79 | Ok(ListPage { |
| 80 | notes, |
| 81 | previews, |
| 82 | total, |
| 83 | more, |
| 84 | }) |
| 85 | } |
| 86 | |
| 87 | |
| 88 | pub const MORE_LINK: &str = "block px-4 py-3 text-sm text-muted-foreground hover:bg-foreground/5"; |
| 89 | |
| 90 | |
| 91 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 92 | pub enum Mobile { |
| 93 | |
| 94 | List, |
| 95 | |
| 96 | Detail, |
| 97 | } |
| 98 | |
| 99 | |
| 100 | pub struct Entry { |
| 101 | pub href: String, |
| 102 | pub title: String, |
| 103 | pub when: String, |
| 104 | pub parts: Vec<(String, bool)>, |
| 105 | pub notebook: Option<String>, |
| 106 | pub selected: bool, |
| 107 | } |
| 108 | |
| 109 | |
| 110 | pub struct Group { |
| 111 | pub label: &'static str, |
| 112 | pub entries: Vec<Entry>, |
| 113 | } |
| 114 | |
| 115 | |
| 116 | pub struct ListSpec { |
| 117 | pub title: String, |
| 118 | |
| 119 | pub heading: bool, |
| 120 | pub count: usize, |
| 121 | |
| 122 | pub notebook_id: String, |
| 123 | pub groups: Vec<Group>, |
| 124 | |
| 125 | pub empty: &'static str, |
| 126 | |
| 127 | pub more: Option<String>, |
| 128 | } |
| 129 | |
| 130 | |
| 131 | const ROW: &str = "flex min-h-14 flex-col justify-center gap-1 px-4 py-2 transition-colors hover:bg-foreground/5"; |
| 132 | const ROW_SELECTED: &str = "marker flex min-h-14 flex-col justify-center gap-1 px-4 py-2"; |
| 133 | const BADGE: &str = "shrink-0 rounded-[4px] border border-border px-1.5 font-mono text-[11px] leading-4 text-muted-foreground"; |
| 134 | |
| 135 | |
| 136 | pub const MENU_LINK: &str = "flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left text-sm whitespace-nowrap hover:bg-foreground/5 focus-visible:bg-foreground/5 active:bg-foreground/10 [&_svg]:size-4 [&_svg]:shrink-0"; |
| 137 | |
| 138 | const LIST_SHOWN: &str = "flex min-h-0 w-full shrink-0 flex-col border-border lg:w-80 lg:border-r"; |
| 139 | const LIST_HIDDEN: &str = "hidden min-h-0 shrink-0 flex-col border-border lg:flex lg:w-80 lg:border-r"; |
| 140 | const DETAIL_SHOWN: &str = "flex min-h-0 min-w-0 flex-1 flex-col"; |
| 141 | const DETAIL_HIDDEN: &str = "hidden min-h-0 min-w-0 flex-1 flex-col lg:flex"; |
| 142 | |
| 143 | |
| 144 | |
| 145 | pub fn split<'a>(cx: &'a Cx, mobile: Mobile, list: Child<'a>, detail: Child<'a>) -> impl View + 'a { |
| 146 | let (list_class, detail_class) = match mobile { |
| 147 | Mobile::List => (LIST_SHOWN, DETAIL_HIDDEN), |
| 148 | Mobile::Detail => (LIST_HIDDEN, DETAIL_SHOWN), |
| 149 | }; |
| 150 | view! { cx => |
| 151 | <div class="flex min-h-0 min-w-0 flex-1"> |
| 152 | <div class=(list_class)>(list)</div> |
| 153 | <div class=(detail_class)>(detail)</div> |
| 154 | </div> |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /// Builds the rows for `notes` (already filtered and ordered, most recent first), grouped |
| 159 | /// by how long ago they were updated: Today, Yesterday, Previous 7 days, Earlier. Groups |
| 160 | /// with no rows are left out. Days are UTC. |
| 161 | pub fn groups( |
| 162 | lang: Lang, |
| 163 | notes: &[NoteMeta], |
| 164 | notebooks: &[Notebook], |
| 165 | previews: &HashMap<String, String>, |
| 166 | selected: Option<&str>, |
| 167 | show_notebook: bool, |
| 168 | ) -> Vec<Group> { |
| 169 | let now = now_millis(); |
| 170 | let today = now.div_euclid(DAY_MS); |
| 171 | let labels = [ |
| 172 | t(lang, Msg::GroupToday), |
| 173 | t(lang, Msg::Yesterday), |
| 174 | t(lang, Msg::GroupPrevious7), |
| 175 | t(lang, Msg::GroupEarlier), |
| 176 | ]; |
| 177 | let mut buckets: [Vec<Entry>; 4] = Default::default(); |
| 178 | for note in notes { |
| 179 | let age_days = today - note.updated_at.div_euclid(DAY_MS); |
| 180 | let bucket = match age_days { |
| 181 | i64::MIN..=0 => 0, |
| 182 | 1 => 1, |
| 183 | 2..=7 => 2, |
| 184 | _ => 3, |
| 185 | }; |
| 186 | let preview = previews.get(¬e.id).cloned().unwrap_or_default(); |
| 187 | buckets[bucket].push(Entry { |
| 188 | href: format!("/notes/{}", note.id), |
| 189 | title: title_of(lang, ¬e.title), |
| 190 | when: relative_time(lang, note.updated_at, now), |
| 191 | parts: vec![(preview, false)], |
| 192 | notebook: if show_notebook { |
| 193 | notebook_name(notebooks, note.notebook_id.as_deref()) |
| 194 | } else { |
| 195 | None |
| 196 | }, |
| 197 | selected: selected == Some(note.id.as_str()), |
| 198 | }); |
| 199 | } |
| 200 | buckets |
| 201 | .into_iter() |
| 202 | .zip(labels) |
| 203 | .filter(|(entries, _)| !entries.is_empty()) |
| 204 | .map(|(entries, label)| Group { label, entries }) |
| 205 | .collect() |
| 206 | } |
| 207 | |
| 208 | /// One note row: title and time on the first line, the excerpt and notebook badge on the |
| 209 | /// second. `two_lines` lets the excerpt wrap to two lines (search results) instead of |
| 210 | /// truncating it to one. |
| 211 | pub fn entry_item<'a>(cx: &'a Cx, entry: Entry, two_lines: bool) -> impl View + 'a { |
| 212 | let Entry { |
| 213 | href, |
| 214 | title, |
| 215 | when, |
| 216 | parts, |
| 217 | notebook, |
| 218 | selected, |
| 219 | } = entry; |
| 220 | let class = if selected { ROW_SELECTED } else { ROW }; |
| 221 | let current = if selected { "true" } else { "false" }; |
| 222 | let excerpt = if two_lines { |
| 223 | "min-w-0 flex-1 line-clamp-2 text-sm text-muted-foreground" |
| 224 | } else { |
| 225 | "min-w-0 flex-1 truncate text-sm text-muted-foreground" |
| 226 | }; |
| 227 | view! { cx => |
| 228 | <li> |
| 229 | <a href=(href) class=(class) aria-current=(current)> |
| 230 | <span class="flex min-w-0 items-baseline gap-2"> |
| 231 | <span class="min-w-0 flex-1 truncate text-sm font-medium">(title)</span> |
| 232 | <span class="shrink-0 font-mono text-xs text-muted-foreground">(when)</span> |
| 233 | </span> |
| 234 | if !parts.is_empty() { |
| 235 | <span class="flex min-w-0 items-center gap-2"> |
| 236 | <span class=(excerpt)> |
| 237 | for (text, matched) in parts { |
| 238 | if matched { |
| 239 | <mark class="rounded-[2px] bg-mark text-foreground">(text)</mark> |
| 240 | } else { |
| 241 | (text) |
| 242 | } |
| 243 | } |
| 244 | </span> |
| 245 | if let Some(name) = notebook { |
| 246 | <span class=(BADGE)>(name)</span> |
| 247 | } |
| 248 | </span> |
| 249 | } |
| 250 | </a> |
| 251 | </li> |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /// The list pane: a header with the title, the count and `actions`, then the rows by |
| 256 | /// date group, or the empty state. "New note" shows here only below 1024px; the sidebar |
| 257 | /// has it on wide screens. |
| 258 | pub fn list_pane<'a>(cx: &'a Cx, lang: Lang, spec: ListSpec, actions: Child<'a>) -> impl View + 'a { |
| 259 | let ListSpec { |
| 260 | title, |
| 261 | heading, |
| 262 | count, |
| 263 | notebook_id, |
| 264 | groups, |
| 265 | empty, |
| 266 | more, |
| 267 | } = spec; |
| 268 | let count_text = notes_count(lang, count); |
| 269 | let new_note = t(lang, Msg::NewNote); |
| 270 | let create_hint = t(lang, Msg::CreateOneHint); |
| 271 | let show_more = t(lang, Msg::ShowMore); |
| 272 | let is_empty = groups.is_empty(); |
| 273 | view! { cx => |
| 274 | <section class="flex min-h-0 min-w-0 flex-1 flex-col"> |
| 275 | <header class="sidebar-lead flex shrink-0 items-center gap-2 border-b border-border px-4 py-3"> |
| 276 | <div class="min-w-0 flex-1"> |
| 277 | if heading { |
| 278 | <h1 class="truncate text-base font-semibold">(title)</h1> |
| 279 | } else { |
| 280 | <h2 class="truncate text-base font-semibold">(title)</h2> |
| 281 | } |
| 282 | <p class="font-mono text-xs tabular-nums text-muted-foreground">(count_text)</p> |
| 283 | </div> |
| 284 | (actions) |
| 285 | <form method="post" action="/notes" class="shrink-0 lg:hidden"> |
| 286 | <input type="hidden" name="notebook_id" value=(notebook_id)> |
| 287 | button( |
| 288 | variant: ButtonVariant::Primary, |
| 289 | size: ButtonSize::Md, |
| 290 | attrs: attributes! { type="submit" }, |
| 291 | keycap: Child::new(view! { kbd("N") }), |
| 292 | (new_note) |
| 293 | ) |
| 294 | </form> |
| 295 | </header> |
| 296 | <div class="min-h-0 flex-1 overflow-y-auto"> |
| 297 | if is_empty { |
| 298 | <div class="flex flex-col items-center gap-2 px-6 py-12 text-center"> |
| 299 | <p class="text-sm font-medium">(empty)</p> |
| 300 | <p class="flex items-center gap-2 text-sm text-muted-foreground"> |
| 301 | kbd("N") |
| 302 | <span>(create_hint)</span> |
| 303 | </p> |
| 304 | </div> |
| 305 | } else { |
| 306 | for group in groups { |
| 307 | <div class="pb-2"> |
| 308 | <p class="section-label px-4 pt-4 pb-1">(group.label)</p> |
| 309 | <ul class="divide-y divide-border"> |
| 310 | for entry in group.entries { |
| 311 | (Child::new(entry_item(cx, entry, false))) |
| 312 | } |
| 313 | </ul> |
| 314 | </div> |
| 315 | } |
| 316 | if let Some(href) = more { |
| 317 | <a href=(href) class=(MORE_LINK)>(show_more)</a> |
| 318 | } |
| 319 | } |
| 320 | </div> |
| 321 | </section> |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /// The detail pane of a list page before a note is chosen: a quiet hint with the |
| 326 | /// shortcuts that work here. |
| 327 | pub fn empty_detail<'a>(cx: &'a Cx, lang: Lang) -> impl View + 'a { |
| 328 | let new_note = t(lang, Msg::NewNote); |
| 329 | let search = t(lang, Msg::Search); |
| 330 | view! { cx => |
| 331 | <div class="flex flex-1 flex-col items-center justify-center gap-3 px-6 py-16 text-center"> |
| 332 | <p class="text-base font-medium">(t(lang, Msg::SelectNote))</p> |
| 333 | <p class="flex flex-wrap items-center justify-center gap-x-5 gap-y-2 text-sm text-muted-foreground"> |
| 334 | <span class="inline-flex items-center gap-2"> |
| 335 | kbd("N") |
| 336 | <span>(new_note)</span> |
| 337 | </span> |
| 338 | <span class="inline-flex items-center gap-2"> |
| 339 | kbd("/") |
| 340 | <span>(search)</span> |
| 341 | </span> |
| 342 | </p> |
| 343 | </div> |
| 344 | } |
| 345 | } |