project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching·0 forks·Rust·3.6 MiB·c49f5e7·Tahinli·1 month ago·2 commits
| 1 | //! The trash: trashed notes with Restore and Delete forever, and Empty trash. |
| 2 | |
| 3 | use std::sync::Arc; |
| 4 | |
| 5 | use topcoat::{ |
| 6 | Result, |
| 7 | context::{Cx, app_context}, |
| 8 | router::{ |
| 9 | error::{SeeOther, see_other}, |
| 10 | page, route, |
| 11 | }, |
| 12 | view::{View, attributes, view}, |
| 13 | }; |
| 14 | |
| 15 | use crate::app::pane; |
| 16 | use crate::app::shell::now_millis; |
| 17 | use crate::components::button::{ButtonSize, ButtonVariant, button}; |
| 18 | use crate::components::dialog::{ |
| 19 | dialog, dialog_content, dialog_description, dialog_footer, dialog_header, dialog_title, |
| 20 | }; |
| 21 | use crate::hub::Hub; |
| 22 | use crate::i18n::{self, Msg, forever_count, notes_count, relative_time, t}; |
| 23 | use crate::store::{ListFilter, Store}; |
| 24 | |
| 25 | mod note_id; |
| 26 | |
| 27 | /// GET /trash: trashed notes, most recently trashed first. |
| 28 | #[page] |
| 29 | async fn trash(cx: &Cx) -> Result<impl View> { |
| 30 | let store = app_context::<Arc<Store>>(cx); |
| 31 | let page = pane::load_page(cx, store, ListFilter::Trash) |
| 32 | .await |
| 33 | .map_err(topcoat::Error::from_anyhow)?; |
| 34 | let lang = i18n::lang(cx); |
| 35 | let now = now_millis(); |
| 36 | // The trash's total, not the rows on this page: it decides the empty state and whether |
| 37 | // "Empty trash" shows. |
| 38 | let count = page.total; |
| 39 | let previews = page.previews; |
| 40 | let more = page.more; |
| 41 | let title = t(lang, Msg::Trash); |
| 42 | let show_more = t(lang, Msg::ShowMore); |
| 43 | let count_text = notes_count(lang, count); |
| 44 | let retention = t(lang, Msg::TrashRetention); |
| 45 | let empty_label = t(lang, Msg::EmptyTrashAction); |
| 46 | let empty_title = t(lang, Msg::ConfirmEmptyTrashTitle); |
| 47 | let empty_body = forever_count(lang, count); |
| 48 | let cancel = t(lang, Msg::Cancel); |
| 49 | let restore_label = t(lang, Msg::Restore); |
| 50 | let forever_label = t(lang, Msg::DeleteForever); |
| 51 | let forever_title = t(lang, Msg::ConfirmForeverTitle); |
| 52 | let cannot_undo = t(lang, Msg::CannotUndo); |
| 53 | let trashed_word = t(lang, Msg::Trashed); |
| 54 | |
| 55 | Ok(view! { |
| 56 | <div class="mx-auto flex w-full max-w-3xl flex-col"> |
| 57 | <header class="flex flex-wrap items-start justify-between gap-4 border-b border-border px-4 py-5 lg:px-8"> |
| 58 | <div class="min-w-0"> |
| 59 | <h1 class="text-xl font-semibold tracking-tight">(title)</h1> |
| 60 | <p class="font-mono text-xs tabular-nums text-muted-foreground">(count_text)</p> |
| 61 | <p class="mt-1 text-sm text-muted-foreground">(retention)</p> |
| 62 | </div> |
| 63 | if count > 0 { |
| 64 | button( |
| 65 | variant: ButtonVariant::Destructive, |
| 66 | size: ButtonSize::Sm, |
| 67 | attrs: attributes! { |
| 68 | type="button" |
| 69 | onclick="document.getElementById('empty-trash').showModal()" |
| 70 | }, |
| 71 | (empty_label) |
| 72 | ) |
| 73 | } |
| 74 | </header> |
| 75 | if count == 0 { |
| 76 | <div class="flex flex-col items-center gap-2 px-6 py-16 text-center"> |
| 77 | <p class="text-base font-medium">(t(lang, Msg::EmptyTrash))</p> |
| 78 | </div> |
| 79 | } else { |
| 80 | <ul class="divide-y divide-border"> |
| 81 | for note in page.notes { |
| 82 | <li class="flex flex-wrap items-center gap-3 px-4 py-3 lg:px-8"> |
| 83 | <div class="min-w-0 flex-1"> |
| 84 | <p class="truncate text-sm font-medium">(crate::app::shell::title_of(lang, ¬e.title))</p> |
| 85 | <p class="truncate text-sm text-muted-foreground">(previews.get(¬e.id).cloned().unwrap_or_default())</p> |
| 86 | <p class="font-mono text-xs text-muted-foreground"> |
| 87 | (format!("{trashed_word} · {}", relative_time(lang, note.trashed_at.unwrap_or(note.updated_at), now))) |
| 88 | </p> |
| 89 | </div> |
| 90 | <form method="post" action=(format!("/notes/{}/restore", note.id))> |
| 91 | button( |
| 92 | variant: ButtonVariant::Outline, |
| 93 | size: ButtonSize::Sm, |
| 94 | attrs: attributes! { type="submit" }, |
| 95 | (restore_label) |
| 96 | ) |
| 97 | </form> |
| 98 | button( |
| 99 | variant: ButtonVariant::Destructive, |
| 100 | size: ButtonSize::Sm, |
| 101 | attrs: attributes! { |
| 102 | type="button" |
| 103 | onclick=(format!("document.getElementById('forever-{}').showModal()", note.id)) |
| 104 | }, |
| 105 | (forever_label) |
| 106 | ) |
| 107 | dialog( |
| 108 | open: false, |
| 109 | attrs: attributes! { |
| 110 | id=(format!("forever-{}", note.id)) |
| 111 | aria-labelledby=(format!("forever-title-{}", note.id)) |
| 112 | }, |
| 113 | dialog_content( |
| 114 | <form method="post" action=(format!("/trash/{}/delete", note.id)) class="flex flex-col gap-4"> |
| 115 | dialog_header( |
| 116 | dialog_title(attrs: attributes! { id=(format!("forever-title-{}", note.id)) }, (forever_title)) |
| 117 | dialog_description((cannot_undo)) |
| 118 | ) |
| 119 | dialog_footer( |
| 120 | button( |
| 121 | variant: ButtonVariant::Outline, |
| 122 | attrs: attributes! { type="submit" formmethod="dialog" autofocus=(true) }, |
| 123 | (cancel) |
| 124 | ) |
| 125 | button( |
| 126 | variant: ButtonVariant::Destructive, |
| 127 | attrs: attributes! { type="submit" }, |
| 128 | (forever_label) |
| 129 | ) |
| 130 | ) |
| 131 | </form> |
| 132 | ) |
| 133 | ) |
| 134 | </li> |
| 135 | } |
| 136 | </ul> |
| 137 | if let Some(href) = more { |
| 138 | <a href=(href) class=(pane::MORE_LINK)>(show_more)</a> |
| 139 | } |
| 140 | } |
| 141 | if count > 0 { |
| 142 | dialog( |
| 143 | open: false, |
| 144 | attrs: attributes! { id="empty-trash" aria-labelledby="empty-trash-title" aria-describedby="empty-trash-body" }, |
| 145 | dialog_content( |
| 146 | <form method="post" action="/trash/empty" class="flex flex-col gap-4"> |
| 147 | dialog_header( |
| 148 | dialog_title(attrs: attributes! { id="empty-trash-title" }, (empty_title)) |
| 149 | dialog_description(attrs: attributes! { id="empty-trash-body" }, (empty_body)) |
| 150 | ) |
| 151 | dialog_footer( |
| 152 | button( |
| 153 | variant: ButtonVariant::Outline, |
| 154 | attrs: attributes! { type="submit" formmethod="dialog" autofocus=(true) }, |
| 155 | (cancel) |
| 156 | ) |
| 157 | button( |
| 158 | variant: ButtonVariant::Destructive, |
| 159 | attrs: attributes! { type="submit" }, |
| 160 | (empty_label) |
| 161 | ) |
| 162 | ) |
| 163 | </form> |
| 164 | ) |
| 165 | ) |
| 166 | } |
| 167 | </div> |
| 168 | }) |
| 169 | } |
| 170 | |
| 171 | /// POST /trash/empty: deletes every trashed note for good. |
| 172 | #[route(POST "./empty")] |
| 173 | async fn empty_trash(cx: &Cx) -> Result<SeeOther> { |
| 174 | let store = app_context::<Arc<Store>>(cx); |
| 175 | let hub = app_context::<Arc<Hub>>(cx); |
| 176 | let ids = store.empty_trash().await.map_err(topcoat::Error::from_anyhow)?; |
| 177 | for id in &ids { |
| 178 | hub.forget(id); |
| 179 | } |
| 180 | Ok(see_other("/trash")) |
| 181 | } |