| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use loro::Frontiers; |
| 4 | use serde::Deserialize; |
| 5 | use topcoat::{ |
| 6 | Result, |
| 7 | context::{Cx, app_context}, |
| 8 | router::{ |
| 9 | content::Form, |
| 10 | error::{RouterErrorExt, SeeOther, bad_request, see_other}, |
| 11 | page, path_param, query_params, route, |
| 12 | }, |
| 13 | view::{View, attributes, view}, |
| 14 | }; |
| 15 | |
| 16 | use super::NoteId; |
| 17 | use crate::app::shell; |
| 18 | use crate::components::button::{ButtonSize, ButtonVariant, button}; |
| 19 | use crate::hub::Hub; |
| 20 | use crate::i18n::{self, Msg, t}; |
| 21 | use crate::store::Store; |
| 22 | |
| 23 | |
| 24 | const ROW: &str = "flex min-h-14 flex-col justify-center gap-1 px-4 py-2 transition-colors hover:bg-foreground/5"; |
| 25 | const ROW_SELECTED: &str = "marker flex min-h-14 flex-col justify-center gap-1 px-4 py-2"; |
| 26 | |
| 27 | #[query_params(error = bad_request)] |
| 28 | pub struct HistoryQuery { |
| 29 | |
| 30 | pub v: Option<String>, |
| 31 | } |
| 32 | |
| 33 | #[derive(Deserialize)] |
| 34 | struct RestoreForm { |
| 35 | at: i64, |
| 36 | } |
| 37 | |
| 38 | |
| 39 | struct Entry { |
| 40 | href: String, |
| 41 | label: String, |
| 42 | |
| 43 | at: Option<i64>, |
| 44 | selected: bool, |
| 45 | } |
| 46 | |
| 47 | |
| 48 | |
| 49 | #[page] |
| 50 | async fn history(cx: &Cx) -> Result<impl View> { |
| 51 | let store = app_context::<Arc<Store>>(cx); |
| 52 | let hub = app_context::<Arc<Hub>>(cx); |
| 53 | let id = path_param::<NoteId>(cx).to_owned(); |
| 54 | let meta = store |
| 55 | .get_note(&id) |
| 56 | .await |
| 57 | .map_err(topcoat::Error::from_anyhow)? |
| 58 | .ok_or_not_found()?; |
| 59 | let versions = store.list_versions(&id).await.map_err(topcoat::Error::from_anyhow)?; |
| 60 | let requested = match query_params::<HistoryQuery>(cx) { |
| 61 | Ok(query) => query.v.clone(), |
| 62 | Err(_) => None, |
| 63 | }; |
| 64 | let lang = i18n::lang(cx); |
| 65 | let trashed = meta.trashed_at.is_some(); |
| 66 | |
| 67 | |
| 68 | let selected: Option<i64> = match requested.as_deref() { |
| 69 | Some("current") => None, |
| 70 | Some(value) => match value.parse::<i64>() { |
| 71 | Ok(at) if versions.iter().any(|(v, _)| *v == at) => Some(at), |
| 72 | _ => versions.first().map(|(at, _)| *at), |
| 73 | }, |
| 74 | None => versions.first().map(|(at, _)| *at), |
| 75 | }; |
| 76 | |
| 77 | let preview = if versions.is_empty() { |
| 78 | String::new() |
| 79 | } else { |
| 80 | let room = hub.room(store, &id).await.map_err(topcoat::Error::from_anyhow)?; |
| 81 | match selected { |
| 82 | None => room.html().await.map_err(topcoat::Error::from_anyhow)?, |
| 83 | Some(at) => { |
| 84 | let bytes = store |
| 85 | .get_version(&id, at) |
| 86 | .await |
| 87 | .map_err(topcoat::Error::from_anyhow)? |
| 88 | .ok_or_not_found()?; |
| 89 | let frontiers = Frontiers::decode(&bytes).map_err(|err| topcoat::Error::from_anyhow(err.into()))?; |
| 90 | room.html_at(&frontiers).await.map_err(topcoat::Error::from_anyhow)? |
| 91 | } |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | let title = t(lang, Msg::VersionHistory); |
| 96 | let back_label = t(lang, Msg::BackToNote); |
| 97 | let current_label = t(lang, Msg::CurrentVersion); |
| 98 | let no_versions = t(lang, Msg::NoVersionsYet); |
| 99 | let restore_label = t(lang, Msg::RestoreVersion); |
| 100 | let note_title = shell::title_of(lang, &meta.title); |
| 101 | let back_href = format!("/notes/{id}"); |
| 102 | let restore_action = format!("/notes/{id}/history/restore"); |
| 103 | let selected_label = match selected { |
| 104 | Some(at) => i18n::date_time(lang, at), |
| 105 | None => current_label.to_owned(), |
| 106 | }; |
| 107 | let restore_at = selected.unwrap_or(0); |
| 108 | |
| 109 | let mut entries = vec![Entry { |
| 110 | href: format!("/notes/{id}/history?v=current"), |
| 111 | label: current_label.to_owned(), |
| 112 | at: None, |
| 113 | selected: selected.is_none(), |
| 114 | }]; |
| 115 | entries.extend(versions.iter().map(|(at, _)| Entry { |
| 116 | href: format!("/notes/{id}/history?v={at}"), |
| 117 | label: i18n::date_time(lang, *at), |
| 118 | at: Some(*at), |
| 119 | selected: selected == Some(*at), |
| 120 | })); |
| 121 | let has_versions = !versions.is_empty(); |
| 122 | let show_restore = has_versions && !trashed && selected.is_some(); |
| 123 | |
| 124 | Ok(view! { |
| 125 | <div class="flex min-h-0 min-w-0 flex-1 flex-col"> |
| 126 | <header class="sticky top-0 z-10 flex h-11 shrink-0 items-center gap-2 border-b border-border bg-background px-3"> |
| 127 | <a |
| 128 | href=(back_href) |
| 129 | aria-label=(back_label) |
| 130 | class="inline-flex size-8 shrink-0 items-center justify-center rounded-control text-muted-foreground transition-colors hover:bg-foreground/5 hover:text-foreground" |
| 131 | > |
| 132 | <svg class="size-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> |
| 133 | <path d="m15 18-6-6 6-6"></path> |
| 134 | </svg> |
| 135 | </a> |
| 136 | <nav aria-label=(title) class="flex min-w-0 flex-1 items-center gap-2 text-sm text-muted-foreground"> |
| 137 | <span class="min-w-0 truncate">(note_title)</span> |
| 138 | <span aria-hidden="true">"›"</span> |
| 139 | <h1 class="shrink-0 text-sm font-medium text-foreground">(title)</h1> |
| 140 | </nav> |
| 141 | </header> |
| 142 | <div class="flex min-h-0 flex-1 flex-col lg:flex-row"> |
| 143 | <ul class="max-h-[40vh] shrink-0 divide-y divide-border overflow-y-auto border-b border-border lg:max-h-none lg:w-72 lg:border-b-0 lg:border-r"> |
| 144 | for entry in entries { |
| 145 | <li> |
| 146 | <a |
| 147 | href=(entry.href) |
| 148 | class=(if entry.selected { ROW_SELECTED } else { ROW }) |
| 149 | aria-current=(if entry.selected { "true" } else { "false" }) |
| 150 | > |
| 151 | if let Some(at) = entry.at { |
| 152 | <time data-ms=(at) class="min-w-0 truncate text-sm font-medium">(entry.label)</time> |
| 153 | } else { |
| 154 | <span class="min-w-0 truncate text-sm font-medium">(entry.label)</span> |
| 155 | } |
| 156 | </a> |
| 157 | </li> |
| 158 | } |
| 159 | </ul> |
| 160 | <section class="min-h-0 min-w-0 flex-1 overflow-y-auto"> |
| 161 | if has_versions { |
| 162 | <div class="flex flex-wrap items-center gap-3 border-b border-border px-4 py-3 text-sm"> |
| 163 | <p class="min-w-0 flex-1 text-xs text-muted-foreground"> |
| 164 | if let Some(at) = selected { |
| 165 | <time data-ms=(at)>(selected_label)</time> |
| 166 | } else { |
| 167 | (selected_label) |
| 168 | } |
| 169 | </p> |
| 170 | if show_restore { |
| 171 | <form method="post" action=(restore_action)> |
| 172 | <input type="hidden" name="at" value=(restore_at)> |
| 173 | button( |
| 174 | variant: ButtonVariant::Outline, |
| 175 | size: ButtonSize::Sm, |
| 176 | attrs: attributes! { type="submit" }, |
| 177 | (restore_label) |
| 178 | ) |
| 179 | </form> |
| 180 | } |
| 181 | </div> |
| 182 | <div id="editor"> |
| 183 | <div class="ProseMirror">(topcoat::view::Unescaped::new_unchecked(preview))</div> |
| 184 | </div> |
| 185 | } else { |
| 186 | <div class="flex flex-col items-center gap-2 px-6 py-16 text-center"> |
| 187 | <p class="max-w-md text-base text-muted-foreground">(no_versions)</p> |
| 188 | </div> |
| 189 | } |
| 190 | </section> |
| 191 | </div> |
| 192 | </div> |
| 193 | }) |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | #[route(POST "./restore")] |
| 199 | async fn restore_version(cx: &Cx, Form(input): Form<RestoreForm>) -> Result<SeeOther> { |
| 200 | let store = app_context::<Arc<Store>>(cx); |
| 201 | let hub = app_context::<Arc<Hub>>(cx); |
| 202 | let id = path_param::<NoteId>(cx).to_owned(); |
| 203 | let meta = store |
| 204 | .get_note(&id) |
| 205 | .await |
| 206 | .map_err(topcoat::Error::from_anyhow)? |
| 207 | .ok_or_not_found()?; |
| 208 | if meta.trashed_at.is_some() { |
| 209 | return Err(bad_request("the note is in the trash; restore it first").into()); |
| 210 | } |
| 211 | let bytes = store |
| 212 | .get_version(&id, input.at) |
| 213 | .await |
| 214 | .map_err(topcoat::Error::from_anyhow)? |
| 215 | .ok_or_not_found()?; |
| 216 | let frontiers = Frontiers::decode(&bytes).map_err(|err| topcoat::Error::from_anyhow(err.into()))?; |
| 217 | let room = hub.room(store, &id).await.map_err(topcoat::Error::from_anyhow)?; |
| 218 | room.restore(store, &frontiers) |
| 219 | .await |
| 220 | .map_err(topcoat::Error::from_anyhow)?; |
| 221 | Ok(see_other(format!("/notes/{id}?toast=version-restored"))) |
| 222 | } |