forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

notebook_id.rs199 lines, 7.5 KiBHistoryRawDownload
1use std::sync::Arc;
2
3use topcoat::{
4 Result,
5 context::{Cx, app_context},
6 router::{
7 content::Form,
8 error::{RouterErrorExt, SeeOther, see_other},
9 module_param, page, path_param, route,
10 },
11 view::{Child, View, attributes, view},
12};
13
14use crate::app::pane::{self, Mobile};
15use crate::components::button::{ButtonSize, ButtonVariant, button, button_variants};
16use crate::components::dialog::{
17 dialog, dialog_content, dialog_description, dialog_footer, dialog_header, dialog_title,
18};
19use crate::components::dropdown_menu::{
20 dropdown_menu, dropdown_menu_content, dropdown_menu_item, dropdown_menu_separator,
21 dropdown_menu_trigger,
22};
23use crate::components::floating::FloatingAlign;
24use crate::components::input::input;
25use crate::i18n::{self, Msg, t};
26use crate::store::{ListFilter, Store};
27
28use super::{NameForm, clean_name};
29
30module_param!(notebook_id);
31
32/// GET /notebooks/{notebook_id}: the notes in one notebook beside its list, with a menu
33/// to rename or delete the notebook.
34#[page]
35async fn notebook(cx: &Cx) -> Result<impl View> {
36 let store = app_context::<Arc<Store>>(cx);
37 let id = path_param::<NotebookId>(cx).to_owned();
38 let nb = store
39 .get_notebook(&id)
40 .await
41 .map_err(topcoat::Error::from_anyhow)?
42 .ok_or_not_found()?;
43 let notebooks = store.list_notebooks().await.map_err(topcoat::Error::from_anyhow)?;
44 let page = pane::load_page(cx, store, ListFilter::Notebook(id.clone()))
45 .await
46 .map_err(topcoat::Error::from_anyhow)?;
47 let lang = i18n::lang(cx);
48 let groups = pane::groups(lang, &page.notes, &notebooks, &page.previews, None, false);
49 let spec = pane::ListSpec {
50 title: nb.name.clone(),
51 heading: true,
52 count: page.total,
53 notebook_id: id.clone(),
54 groups,
55 empty: t(lang, Msg::EmptyNotebook),
56 more: page.more,
57 };
58
59 let rename_action = format!("/notebooks/{id}/rename");
60 let delete_action = format!("/notebooks/{id}/delete");
61 let options = t(lang, Msg::NotebookOptions);
62 let rename_label = t(lang, Msg::RenameNotebook);
63 let rename_short = t(lang, Msg::Rename);
64 let delete_label = t(lang, Msg::DeleteNotebook);
65 let delete_title = t(lang, Msg::DeleteNotebookTitle);
66 let delete_body = t(lang, Msg::DeleteNotebookBody);
67 let cancel = t(lang, Msg::Cancel);
68 let name_label = t(lang, Msg::NewNotebookLabel);
69
70 let actions = view! {
71 dropdown_menu(
72 dropdown_menu_trigger(
73 target: "notebook-menu",
74 attrs: attributes! {
75 class=(button_variants(ButtonVariant::Ghost, ButtonSize::Icon))
76 aria-label=(options)
77 },
78 <svg class="size-4" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
79 <circle cx="5" cy="12" r="1.75"></circle>
80 <circle cx="12" cy="12" r="1.75"></circle>
81 <circle cx="19" cy="12" r="1.75"></circle>
82 </svg>
83 )
84 dropdown_menu_content(
85 id: "notebook-menu",
86 align: FloatingAlign::End,
87 dropdown_menu_item(
88 close: Some("notebook-menu"),
89 attrs: attributes! { onclick="document.getElementById('rename-notebook').showModal()" },
90 (rename_label)
91 )
92 dropdown_menu_separator()
93 dropdown_menu_item(
94 close: Some("notebook-menu"),
95 attrs: attributes! {
96 class="text-destructive"
97 onclick="document.getElementById('delete-notebook').showModal()"
98 },
99 (delete_label)
100 )
101 )
102 )
103 };
104 let list = pane::list_pane(cx, lang, spec, Child::new(actions));
105 let content = Child::new(pane::split(
106 cx,
107 Mobile::List,
108 Child::new(list),
109 Child::new(pane::empty_detail(cx, lang)),
110 ));
111
112 Ok(view! {
113 (content)
114 dialog(
115 open: false,
116 attrs: attributes! { id="rename-notebook" aria-labelledby="rename-notebook-title" },
117 dialog_content(
118 <form method="post" action=(rename_action) class="flex flex-col gap-4">
119 dialog_header(
120 dialog_title(attrs: attributes! { id="rename-notebook-title" }, (rename_label))
121 )
122 input(attrs: attributes! {
123 type="text"
124 name="name"
125 value=(nb.name.clone())
126 maxlength="120"
127 required=(true)
128 autocomplete="off"
129 aria-label=(name_label)
130 class="w-full"
131 })
132 dialog_footer(
133 button(
134 variant: ButtonVariant::Ghost,
135 attrs: attributes! { type="submit" formmethod="dialog" },
136 (cancel)
137 )
138 button(
139 variant: ButtonVariant::Primary,
140 attrs: attributes! { type="submit" },
141 (rename_short)
142 )
143 )
144 </form>
145 )
146 )
147 dialog(
148 open: false,
149 attrs: attributes! { id="delete-notebook" aria-labelledby="delete-notebook-title" aria-describedby="delete-notebook-body" },
150 dialog_content(
151 <form method="post" action=(delete_action) class="flex flex-col gap-4">
152 dialog_header(
153 dialog_title(attrs: attributes! { id="delete-notebook-title" }, (delete_title))
154 dialog_description(attrs: attributes! { id="delete-notebook-body" }, (delete_body))
155 )
156 dialog_footer(
157 button(
158 variant: ButtonVariant::Outline,
159 attrs: attributes! { type="submit" formmethod="dialog" autofocus=(true) },
160 (cancel)
161 )
162 button(
163 variant: ButtonVariant::Destructive,
164 attrs: attributes! { type="submit" },
165 (delete_label)
166 )
167 )
168 </form>
169 )
170 )
171 })
172}
173
174/// POST /notebooks/{notebook_id}/rename
175#[route(POST "./rename")]
176async fn rename_notebook(cx: &Cx, Form(form): Form<NameForm>) -> Result<SeeOther> {
177 let store = app_context::<Arc<Store>>(cx);
178 let id = path_param::<NotebookId>(cx).to_owned();
179 store.get_notebook(&id).await.map_err(topcoat::Error::from_anyhow)?.ok_or_not_found()?;
180 let name = clean_name(&form.name)?;
181 store
182 .rename_notebook(&id, &name)
183 .await
184 .map_err(topcoat::Error::from_anyhow)?;
185 Ok(see_other(format!("/notebooks/{id}")))
186}
187
188/// POST /notebooks/{notebook_id}/delete: its notes become unfiled.
189#[route(POST "./delete")]
190async fn delete_notebook(cx: &Cx) -> Result<SeeOther> {
191 let store = app_context::<Arc<Store>>(cx);
192 let id = path_param::<NotebookId>(cx).to_owned();
193 store.get_notebook(&id).await.map_err(topcoat::Error::from_anyhow)?.ok_or_not_found()?;
194 store
195 .delete_notebook(&id)
196 .await
197 .map_err(topcoat::Error::from_anyhow)?;
198 Ok(see_other("/?toast=notebook_deleted"))
199}