//! The two-pane note layout shared by the note pages: the note list pane (header, rows //! grouped by date, empty state), the detail pane, and the split that shows one pane at //! a time below 1024px. use std::collections::HashMap; use topcoat::{ context::Cx, router::{query_params, request::uri}, view::{Child, View, attributes, view}, }; use crate::app::shell::{notebook_name, now_millis, title_of}; use crate::components::button::{ButtonSize, ButtonVariant, button}; use crate::components::kbd::kbd; use crate::i18n::{Lang, Msg, notes_count, relative_time, t}; use crate::store::{ListFilter, Notebook, NoteMeta, Store}; const DAY_MS: i64 = 86_400_000; /// Rows a list page shows when `?n=` is absent or unreadable. const DEFAULT_ROWS: usize = 100; /// Fewest and most rows a list page shows, whatever `?n=` asks for. const MIN_ROWS: usize = 100; const MAX_ROWS: usize = 5000; /// The `n` query parameter of a list page: how many rows to show. #[query_params(error = bad_request)] pub struct RowsQuery { pub n: Option, } /// Rows of the list on this request: `?n=`, clamped to `MIN_ROWS..=MAX_ROWS`. pub fn page_rows(cx: &Cx) -> usize { query_params::(cx) .ok() .and_then(|query| query.n) .unwrap_or(DEFAULT_ROWS) .clamp(MIN_ROWS, MAX_ROWS) } /// The href of the "Show more" link for a list showing `rows` of `total` rows: this page /// with `n` doubled (up to `MAX_ROWS`) and its other query parameters kept. `None` when /// every row is shown, or when the list is already at `MAX_ROWS`. pub fn more_href(cx: &Cx, rows: usize, total: usize) -> Option { if total <= rows || rows >= MAX_ROWS { return None; } let target = uri(cx); let mut pairs: Vec = target .query() .unwrap_or("") .split('&') .filter(|pair| !pair.is_empty() && pair.split('=').next() != Some("n")) .map(str::to_owned) .collect(); pairs.push(format!("n={}", (rows * 2).min(MAX_ROWS))); Some(format!("{}?{}", target.path(), pairs.join("&"))) } /// One page of a list: its rows, their previews, and the list's total count. pub struct ListPage { pub notes: Vec, pub previews: HashMap, /// Notes the list has in all, not only on this page. pub total: usize, /// Href of the "Show more" link, when the list has more rows than this page shows. pub more: Option, } /// Loads the first page of the notes `filter` selects, sized by `?n=` (see `page_rows`). pub async fn load_page(cx: &Cx, store: &Store, filter: ListFilter) -> anyhow::Result { let rows = page_rows(cx); let total = store.count_filter(&filter).await?; let notes = store.list_notes_page(filter, rows, 0).await?; let ids: Vec = notes.iter().map(|note| note.id.clone()).collect(); let previews = store.previews_for(&ids).await?; let more = more_href(cx, rows, total); Ok(ListPage { notes, previews, total, more, }) } /// Classes of the "Show more" link under a list, a quiet full-width row. pub const MORE_LINK: &str = "block px-4 py-3 text-sm text-muted-foreground hover:bg-foreground/5"; /// Which pane shows below 1024px. Above it, both panes show. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Mobile { /// A list page: the list shows, the detail is hidden. List, /// The note page: the detail shows, the list is hidden. Detail, } /// One note row. `parts` is the text of the second line as `(text, highlighted)` runs. pub struct Entry { pub href: String, pub title: String, pub when: String, pub parts: Vec<(String, bool)>, pub notebook: Option, pub selected: bool, } /// A date group of rows, with its section label. pub struct Group { pub label: &'static str, pub entries: Vec, } /// What the list pane shows. pub struct ListSpec { pub title: String, /// True on list pages (the page's one `

`), false on the note page. pub heading: bool, pub count: usize, /// The notebook a "New note" from this list goes to. Empty for none. pub notebook_id: String, pub groups: Vec, /// The headline of the empty state, shown when there are no rows. pub empty: &'static str, /// Href of a "Show more" link after the rows, when the list has more than it shows. pub more: Option, } /// Classes of a note row, and of the selected row (the highlighter swipe). const ROW: &str = "flex min-h-14 flex-col justify-center gap-1 px-4 py-2 transition-colors hover:bg-foreground/5"; const ROW_SELECTED: &str = "marker flex min-h-14 flex-col justify-center gap-1 px-4 py-2"; const BADGE: &str = "shrink-0 rounded-[4px] border border-border px-1.5 font-mono text-[11px] leading-4 text-muted-foreground"; /// Classes of a menu item that is a link, matching the menu items of `dropdown_menu`. 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"; const LIST_SHOWN: &str = "flex min-h-0 w-full shrink-0 flex-col border-border lg:w-80 lg:border-r"; const LIST_HIDDEN: &str = "hidden min-h-0 shrink-0 flex-col border-border lg:flex lg:w-80 lg:border-r"; const DETAIL_SHOWN: &str = "flex min-h-0 min-w-0 flex-1 flex-col"; const DETAIL_HIDDEN: &str = "hidden min-h-0 min-w-0 flex-1 flex-col lg:flex"; /// Splits a page into the list pane and the detail pane. `mobile` picks the pane that /// shows below 1024px; both show above it. pub fn split<'a>(cx: &'a Cx, mobile: Mobile, list: Child<'a>, detail: Child<'a>) -> impl View + 'a { let (list_class, detail_class) = match mobile { Mobile::List => (LIST_SHOWN, DETAIL_HIDDEN), Mobile::Detail => (LIST_HIDDEN, DETAIL_SHOWN), }; view! { cx =>
(list)
(detail)
} } /// Builds the rows for `notes` (already filtered and ordered, most recent first), grouped /// by how long ago they were updated: Today, Yesterday, Previous 7 days, Earlier. Groups /// with no rows are left out. Days are UTC. pub fn groups( lang: Lang, notes: &[NoteMeta], notebooks: &[Notebook], previews: &HashMap, selected: Option<&str>, show_notebook: bool, ) -> Vec { let now = now_millis(); let today = now.div_euclid(DAY_MS); let labels = [ t(lang, Msg::GroupToday), t(lang, Msg::Yesterday), t(lang, Msg::GroupPrevious7), t(lang, Msg::GroupEarlier), ]; let mut buckets: [Vec; 4] = Default::default(); for note in notes { let age_days = today - note.updated_at.div_euclid(DAY_MS); let bucket = match age_days { i64::MIN..=0 => 0, 1 => 1, 2..=7 => 2, _ => 3, }; let preview = previews.get(¬e.id).cloned().unwrap_or_default(); buckets[bucket].push(Entry { href: format!("/notes/{}", note.id), title: title_of(lang, ¬e.title), when: relative_time(lang, note.updated_at, now), parts: vec![(preview, false)], notebook: if show_notebook { notebook_name(notebooks, note.notebook_id.as_deref()) } else { None }, selected: selected == Some(note.id.as_str()), }); } buckets .into_iter() .zip(labels) .filter(|(entries, _)| !entries.is_empty()) .map(|(entries, label)| Group { label, entries }) .collect() } /// One note row: title and time on the first line, the excerpt and notebook badge on the /// second. `two_lines` lets the excerpt wrap to two lines (search results) instead of /// truncating it to one. pub fn entry_item<'a>(cx: &'a Cx, entry: Entry, two_lines: bool) -> impl View + 'a { let Entry { href, title, when, parts, notebook, selected, } = entry; let class = if selected { ROW_SELECTED } else { ROW }; let current = if selected { "true" } else { "false" }; let excerpt = if two_lines { "min-w-0 flex-1 line-clamp-2 text-sm text-muted-foreground" } else { "min-w-0 flex-1 truncate text-sm text-muted-foreground" }; view! { cx =>
  • (title) (when) if !parts.is_empty() { for (text, matched) in parts { if matched { (text) } else { (text) } } if let Some(name) = notebook { (name) } }
  • } } /// The list pane: a header with the title, the count and `actions`, then the rows by /// date group, or the empty state. "New note" shows here only below 1024px; the sidebar /// has it on wide screens. pub fn list_pane<'a>(cx: &'a Cx, lang: Lang, spec: ListSpec, actions: Child<'a>) -> impl View + 'a { let ListSpec { title, heading, count, notebook_id, groups, empty, more, } = spec; let count_text = notes_count(lang, count); let new_note = t(lang, Msg::NewNote); let create_hint = t(lang, Msg::CreateOneHint); let show_more = t(lang, Msg::ShowMore); let is_empty = groups.is_empty(); view! { cx =>
    if is_empty {

    (empty)

    kbd("N") (create_hint)

    } else { for group in groups {
      for entry in group.entries { (Child::new(entry_item(cx, entry, false))) }
    } if let Some(href) = more { (show_more) } }
    } } /// The detail pane of a list page before a note is chosen: a quiet hint with the /// shortcuts that work here. pub fn empty_detail<'a>(cx: &'a Cx, lang: Lang) -> impl View + 'a { let new_note = t(lang, Msg::NewNote); let search = t(lang, Msg::Search); view! { cx =>

    (t(lang, Msg::SelectNote))

    kbd("N") (new_note) kbd("/") (search)

    } }