Skip to main content

tablo_core/
page.rs

1//! [`Page`] — a panel page that is not a resource: a dashboard, a report, a
2//! settings screen, a media library — and the [`PageDef`] it declares.
3
4use std::{any::TypeId, future::Future, sync::Arc};
5
6use topcoat::{
7    Result,
8    context::Cx,
9    icon::IconData,
10    view::{BoxView, View, ViewExt},
11};
12
13pub use self::filters::{PageFilter, PageFilters};
14use crate::{
15    HeaderAction, HeaderActions, Widget,
16    naming::{kebab_case, sentence_case, type_stem},
17    navigation::NavigationItem,
18    widget::WidgetGrid,
19};
20
21/// A panel page that is not a resource, mounted at `{prefix}/{slug}` with a
22/// sidebar entry; the panel owns the route and entry while the page owns its
23/// markup.
24///
25/// ```rust
26/// # use tablo_core::{Page, Panel};
27/// struct ReportsPage;
28///
29/// impl Page for ReportsPage {}
30///
31/// Panel::new("admin").page::<ReportsPage>(); // GET /admin/reports
32/// ```
33///
34/// A page serves one `GET` with a sidebar entry, which [`PageDef::access`] limits
35/// to the users it admits. A form it renders posts to an app-declared route the
36/// auth gate covers; that route asks [`can_open`](crate::can_open) itself.
37pub trait Page: Sized + Send + Sync + 'static {
38    /// The page's slug, sidebar entry, access and header actions. Defaults to
39    /// [`PageDef::new`]: every default.
40    ///
41    /// [`Panel::page_with`](crate::Panel::page_with) adjusts it for one panel.
42    fn declare() -> PageDef {
43        PageDef::new()
44    }
45
46    /// Render the page body. The panel checks for a resolved user and
47    /// [`PageDef::access`] before calling it.
48    ///
49    /// Defaults to the page its [`PageDef`] declares: the [`title`](PageDef::title) and
50    /// [`description`](PageDef::description), the buttons of its
51    /// [`header_action`](PageDef::header_action)s, and the grid of its
52    /// [`widget`](PageDef::widget)s. A page that renders more places those parts itself, through
53    /// [`header_action_buttons`](crate::header_action_buttons) and [`widgets`](crate::widgets):
54    ///
55    /// ```rust
56    /// # use tablo_core::Page;
57    /// # use topcoat::{Result, context::Cx, view::{View, view}};
58    /// struct ReportsPage;
59    ///
60    /// impl Page for ReportsPage {
61    ///     async fn render(cx: &Cx) -> Result<impl View> {
62    ///         Ok(view! {
63    ///             tablo_ui::page(
64    ///                 tablo_ui::page_header(tablo_ui::page_title("Reports"))
65    ///                 tablo_ui::page_content((tablo_core::widgets::<Self>(cx)) "…")
66    ///             )
67    ///         })
68    ///     }
69    /// }
70    /// ```
71    fn render(cx: &Cx) -> impl Future<Output = Result<impl View>> + Send {
72        async move { Ok(declared_body(cx, TypeId::of::<Self>())) }
73    }
74}
75
76/// Renders the page of type `page` as its [`PageDef`] declares it. The panel installs it: a page's
77/// header and grid are the serving layer's markup.
78pub(crate) struct PageBody(pub(crate) for<'a> fn(&'a Cx, TypeId) -> BoxView<'a>);
79
80/// The body [`Page::render`] defaults to; nothing on a router that mounts no panel.
81fn declared_body(cx: &Cx, page: TypeId) -> BoxView<'_> {
82    match topcoat::context::try_context::<PageBody>(cx) {
83        Some(PageBody(render)) => render(cx, page),
84        None => ().boxed(),
85    }
86}
87
88/// Whether the request's user may open a page.
89type AccessFn = dyn Fn(&Cx) -> bool + Send + Sync;
90
91/// What a [`Page`] declares: its slug, sidebar entry, title, access, header actions and widgets.
92///
93/// [`Page::declare`] returns one, and [`Panel::page_with`](crate::Panel::page_with) adjusts it for
94/// one panel. Every setting has a default, so a page sets only what differs:
95///
96/// ```rust
97/// # use tablo_core::{Page, PageDef};
98/// # use topcoat::{Result, context::Cx, view::View};
99/// struct MediaLibraryPage;
100///
101/// impl Page for MediaLibraryPage {
102///     fn declare() -> PageDef {
103///         PageDef::new().slug("media").icon(tablo_ui::icons::IMAGE)
104///     }
105///
106///     async fn render(_cx: &Cx) -> Result<impl View> {
107///         Ok(())
108///     }
109/// }
110/// ```
111///
112/// The panel builds the def once when it mounts and serves the result to every request.
113pub struct PageDef {
114    slug: Option<String>,
115    label: Option<crate::Text>,
116    icon: Option<IconData>,
117    navigation_order: Option<i32>,
118    navigation_group: Option<crate::Text>,
119    navigation: Option<NavigationItem>,
120    navigation_hidden: bool,
121    title: Option<crate::Text>,
122    description: Option<crate::Text>,
123    access: Arc<AccessFn>,
124    access_declared: bool,
125    header_actions: HeaderActions,
126    widgets: WidgetGrid,
127    filters: Vec<PageFilter>,
128}
129
130impl PageDef {
131    /// A def with every default: the slug and label from the page's type name, and access for
132    /// every signed-in user.
133    pub fn new() -> Self {
134        Self::default()
135    }
136
137    /// The URL segment under the panel prefix: `"media"` mounts the page at
138    /// `{panel prefix}/media`.
139    ///
140    /// Defaults to the page type's name without a trailing `Page`, kebab-cased:
141    /// `MediaLibraryPage` → `media-library`. [`Panel::home`](crate::Panel::home) mounts the page
142    /// at the prefix and does not read it.
143    #[must_use]
144    pub fn slug(mut self, slug: impl Into<String>) -> Self {
145        self.slug = Some(slug.into());
146        self
147    }
148
149    /// The sidebar label.
150    ///
151    /// Defaults to the message `page-{name}` of the panel's catalogs, else the page type's name
152    /// without a trailing `Page`, in sentence case: `MediaLibraryPage` reads `page-media-library`,
153    /// else `Media library`.
154    #[must_use]
155    pub fn label(mut self, label: impl Into<crate::Text>) -> Self {
156        self.label = Some(label.into());
157        self
158    }
159
160    /// The icon of the page's sidebar entry, replacing the icon of a
161    /// [`navigation`](Self::navigation) item.
162    #[must_use]
163    pub fn icon(mut self, icon: IconData) -> Self {
164        self.icon = Some(icon);
165        self
166    }
167
168    /// Where the sidebar entry sorts among the panel's entries, lowest first; defaults to `0`, and
169    /// entries of equal order keep their registration order. It replaces the order of a
170    /// [`navigation`](Self::navigation) item.
171    #[must_use]
172    pub fn navigation_order(mut self, order: i32) -> Self {
173        self.navigation_order = Some(order);
174        self
175    }
176
177    /// The labelled sidebar group the entry renders in, such as `"Reports"`; defaults to none,
178    /// above every group. It replaces the group of a [`navigation`](Self::navigation) item.
179    #[must_use]
180    pub fn navigation_group(mut self, group: impl Into<crate::Text>) -> Self {
181        self.navigation_group = Some(group.into());
182        self
183    }
184
185    /// Replaces the sidebar entry, for one that links somewhere other than the page:
186    /// `NavigationItem::at("Exports", "/admin/reports?tab=exports")`. [`icon`](Self::icon),
187    /// [`navigation_order`](Self::navigation_order) and
188    /// [`navigation_group`](Self::navigation_group) still apply to it.
189    #[must_use]
190    pub fn navigation(mut self, item: NavigationItem) -> Self {
191        self.navigation = Some(item);
192        self
193    }
194
195    /// Keeps the page out of the sidebar. It stays mounted at its URL, and
196    /// [`access`](Self::access) still decides who may open it: link it from a
197    /// [user menu item](crate::Panel::user_menu_item), another page or a notification.
198    #[must_use]
199    pub fn without_navigation(mut self) -> Self {
200        self.navigation_hidden = true;
201        self
202    }
203
204    /// The heading the default [`render`](Page::render) shows. Defaults to the
205    /// [`label`](Self::label) when one is given, else the label of the
206    /// [`navigation`](Self::navigation) item, else the label's default.
207    #[must_use]
208    pub fn title(mut self, title: impl Into<crate::Text>) -> Self {
209        self.title = Some(title.into());
210        self
211    }
212
213    /// The line the default [`render`](Page::render) shows under the title; defaults to none.
214    #[must_use]
215    pub fn description(mut self, description: impl Into<crate::Text>) -> Self {
216        self.description = Some(description.into());
217        self
218    }
219
220    /// Whether the request's user may open the page. The panel answers 403 and leaves the page
221    /// out of the sidebar when it answers `false`. Defaults to every signed-in user.
222    ///
223    /// ```rust
224    /// # use tablo_core::{Page, PageDef, auth};
225    /// # use topcoat::{Result, context::Cx, view::{View, view}};
226    /// # #[derive(Clone)]
227    /// # struct Staff { admin: bool }
228    /// # impl tablo_core::PanelUser for Staff {
229    /// #     fn user_id(&self) -> String { String::new() }
230    /// #     fn display_name(&self) -> &str { "" }
231    /// #     fn can_access_panel(&self, _panel: &tablo_core::PanelId) -> bool { true }
232    /// # }
233    /// struct BillingPage;
234    ///
235    /// impl Page for BillingPage {
236    ///     fn declare() -> PageDef {
237    ///         PageDef::new().access(|cx| auth::user::<Staff>(cx).is_some_and(|staff| staff.admin))
238    ///     }
239    ///
240    ///     async fn render(cx: &Cx) -> Result<impl View> {
241    ///         Ok(view! { tablo_ui::page(tablo_ui::page_title("Billing")) })
242    ///     }
243    /// }
244    /// ```
245    #[must_use]
246    pub fn access(mut self, access: impl Fn(&Cx) -> bool + Send + Sync + 'static) -> Self {
247        self.access = Arc::new(access);
248        self.access_declared = true;
249        self
250    }
251
252    /// Adds the [`HeaderAction`] `A` after the ones already declared, served at
253    /// `{url}/-/actions/{NAME}`. The default [`render`](Page::render) shows their buttons in the
254    /// page's header; a page that renders itself places
255    /// [`header_action_buttons`](crate::header_action_buttons). An `A::NAME` that is not a
256    /// single URL path segment does not compile.
257    ///
258    /// The actions run for whoever may open the page: a page has no policy. An action that writes
259    /// a resource's records asks that resource's policy in its own
260    /// [`can_run`](crate::HeaderActionDef::can_run).
261    #[must_use]
262    pub fn header_action<A: HeaderAction>(mut self) -> Self {
263        self.header_actions = self.header_actions.add::<A>();
264        self
265    }
266
267    /// Adds the [`HeaderAction`] `A`, served at `{url}/-/actions/{NAME}` as a
268    /// [`header_action`](Self::header_action) is, but placed nowhere: the page or one of its
269    /// widgets shows its button with [`header_action_button`](crate::header_action_button), such
270    /// as a plan card's Subscribe that posts the plan it offers. It runs for whoever may open the
271    /// page, under its own [`can_run`](crate::HeaderActionDef::can_run).
272    ///
273    /// ```rust
274    /// # use tablo_core::{FormInput, HeaderAction, Page, PageDef, Widget, WidgetDef};
275    /// # use topcoat::{Result, context::Cx, view::{View, view}};
276    /// # #[derive(FormInput)]
277    /// # struct Plan { name: String }
278    /// # struct Subscribe;
279    /// # impl HeaderAction for Subscribe {
280    /// #     type Input = Plan;
281    /// #     const NAME: &'static str = "subscribe";
282    /// #     async fn run(_: &Cx, _: Plan, _: &mut dyn toasty::Executor) -> Result<tablo_core::Outcome, tablo_core::WriteError<Plan>> { Ok(tablo_core::Outcome::done()) }
283    /// # }
284    /// struct PlansPage;
285    ///
286    /// impl Page for PlansPage {
287    ///     fn declare() -> PageDef {
288    ///         PageDef::new().action::<Subscribe>().widget::<ProCard>()
289    ///     }
290    /// }
291    ///
292    /// struct ProCard;
293    ///
294    /// impl Widget for ProCard {
295    ///     fn declare() -> WidgetDef {
296    ///         WidgetDef::new()
297    ///     }
298    ///
299    ///     async fn render(cx: &Cx) -> Result<impl View> {
300    ///         let plan = Plan { name: "pro".into() };
301    ///         Ok(view! {
302    ///             <p>"Pro"</p>
303    ///             (tablo_core::header_action_button::<PlansPage, Subscribe>(cx, &plan))
304    ///         })
305    ///     }
306    /// }
307    /// ```
308    #[must_use]
309    pub fn action<A: HeaderAction>(mut self) -> Self {
310        self.header_actions = self.header_actions.add_unplaced::<A>();
311        self
312    }
313
314    /// Adds the [`Widget`] `W` after the ones already declared, to the grid the default
315    /// [`render`](Page::render) shows under the header; a page that renders itself places
316    /// [`widgets`](crate::widgets).
317    #[must_use]
318    pub fn widget<W: Widget>(mut self) -> Self {
319        self.widgets.push::<W>();
320        self
321    }
322
323    /// Offers `filter` above the widgets, after the filters already declared: a value the page
324    /// keeps in its query string, which its widgets read through
325    /// [`page_filters`](crate::page_filters). The default [`render`](Page::render) shows the
326    /// filters' bar under the header; a page that renders itself places
327    /// [`page_filter_bar`](crate::page_filter_bar). Mounting refuses two filters sharing a key,
328    /// and a key that is not ASCII letters, digits, `-` and `_`.
329    #[must_use]
330    pub fn filter(mut self, filter: PageFilter) -> Self {
331        self.filters.push(filter);
332        self
333    }
334
335    /// How many columns the widget grid has on a wide screen, from 1 to 4; defaults to 2. A
336    /// medium screen shows at most two, a narrow one one.
337    #[must_use]
338    pub fn widget_columns(mut self, columns: u8) -> Self {
339        self.widgets.set_columns(columns);
340        self
341    }
342}
343
344impl Default for PageDef {
345    fn default() -> Self {
346        Self {
347            slug: None,
348            label: None,
349            icon: None,
350            navigation_order: None,
351            navigation_group: None,
352            navigation: None,
353            navigation_hidden: false,
354            title: None,
355            description: None,
356            access: Arc::new(|_| true),
357            access_declared: false,
358            header_actions: HeaderActions::new(),
359            widgets: WidgetGrid::default(),
360            filters: Vec::new(),
361        }
362    }
363}
364
365impl std::fmt::Debug for PageDef {
366    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
367        f.debug_struct("PageDef")
368            .field("slug", &self.slug)
369            .field("label", &self.label)
370            .field("navigation_order", &self.navigation_order)
371            .field("navigation_group", &self.navigation_group)
372            .field("navigation", &self.navigation)
373            .field("navigation_hidden", &self.navigation_hidden)
374            .field("title", &self.title)
375            .field("description", &self.description)
376            .field("header_actions", &self.header_actions)
377            .field("widgets", &self.widgets)
378            .finish_non_exhaustive()
379    }
380}
381
382/// A [`PageDef`] with its defaults filled in for the page `P`, as a panel mounted it.
383pub(crate) struct MountedPage {
384    pub(crate) slug: String,
385    /// The sidebar entry, its target not yet resolved to the page's URL.
386    pub(crate) navigation: NavigationItem,
387    /// Whether the sidebar leaves the page out: [`PageDef::without_navigation`].
388    pub(crate) navigation_hidden: bool,
389    /// The heading the default [`Page::render`] shows.
390    pub(crate) title: crate::Text,
391    pub(crate) description: Option<crate::Text>,
392    access: Arc<AccessFn>,
393    /// Whether the def declared [`PageDef::access`] rather than keep the default.
394    pub(crate) access_declared: bool,
395    pub(crate) header_actions: HeaderActions,
396    pub(crate) widgets: WidgetGrid,
397    /// The [`PageDef::filter`]s, in declaration order.
398    pub(crate) filters: Vec<PageFilter>,
399}
400
401impl MountedPage {
402    pub(crate) fn new<P: Page>(def: PageDef) -> Self {
403        let stem = type_stem::<P>("Page");
404        let noun = sentence_case(stem);
405        let label = crate::Text::derived(
406            crate::naming::derived_id("page", &crate::naming::slug_case(&noun)),
407            noun,
408        );
409        Self::declared(def, kebab_case(stem), label)
410    }
411
412    /// `def` with its defaults filled in: `slug` and `label` where it names none.
413    pub(crate) fn declared(def: PageDef, slug: String, label: crate::Text) -> Self {
414        let title = def
415            .title
416            .or_else(|| def.label.clone())
417            .or_else(|| def.navigation.as_ref().map(|item| item.label.clone()));
418        let label = def.label.unwrap_or(label);
419        let title = title.unwrap_or_else(|| label.clone());
420        let mut navigation = def.navigation.unwrap_or_else(|| NavigationItem {
421            label,
422            ..NavigationItem::default()
423        });
424        if let Some(order) = def.navigation_order {
425            navigation.order = order;
426        }
427        if let Some(icon) = def.icon {
428            navigation.icon = Some(icon);
429        }
430        if let Some(group) = def.navigation_group {
431            navigation.group = Some(group);
432        }
433        Self {
434            slug: def.slug.unwrap_or(slug),
435            navigation,
436            navigation_hidden: def.navigation_hidden,
437            title,
438            description: def.description,
439            access: def.access,
440            access_declared: def.access_declared,
441            header_actions: def.header_actions,
442            widgets: def.widgets,
443            filters: def.filters,
444        }
445    }
446
447    /// Whether the request's user may open the page.
448    pub(crate) fn admits(&self, cx: &Cx) -> bool {
449        (self.access)(cx)
450    }
451
452    /// The page behind `access` in place of its def's: an auth page placed in an area, which
453    /// declares none, opens to whom auth offers it.
454    pub(crate) fn with_access(
455        mut self,
456        access: impl Fn(&Cx) -> bool + Send + Sync + 'static,
457    ) -> Self {
458        self.access = Arc::new(access);
459        self
460    }
461
462    /// The page's access check, shared with its sidebar entry.
463    pub(crate) fn access(&self) -> Arc<AccessFn> {
464        Arc::clone(&self.access)
465    }
466}
467
468/// A page's declaration check, run by
469/// [`RouterBuilderPanelExt::panel`](crate::RouterBuilderPanelExt::panel) as a resource's is.
470pub(crate) type PageCheck = Box<dyn Fn(&Cx, &mut Vec<crate::DeclarationError>) + Send>;
471
472/// Checks the header actions of `page`, the page `P` as the panel mounted it, as a resource's are,
473/// that no two of its widgets share a name, and that its filters' keys are distinct query words.
474pub(crate) fn check_page<P: Page>(page: Arc<MountedPage>) -> PageCheck {
475    Box::new(move |cx, errors| {
476        page.widgets.check::<P>(errors);
477        let mut keys = std::collections::HashSet::new();
478        for filter in &page.filters {
479            if !filter.has_valid_key() || !keys.insert(filter.key()) {
480                errors.push(crate::DeclarationError::of::<P>(
481                    crate::declaration::Site::Registration,
482                    crate::DeclarationErrorKind::MisdeclaredPageFilter {
483                        key: filter.key().to_string(),
484                    },
485                ));
486            }
487        }
488        crate::resource::check_inputs::<P>(
489            cx,
490            page.header_actions
491                .entries()
492                .iter()
493                .map(|action| (action.name, action.input)),
494            errors,
495        );
496    })
497}
498
499pub(crate) mod filters;
500#[cfg(test)]
501mod tests;