Skip to main content

tablo_core/panel/
banner.rs

1//! [`Banner`]: a strip the app shows above every page of a panel, such as a failed payment.
2
3use std::future::Future;
4
5use topcoat::{
6    Result,
7    context::Cx,
8    view::{BoxView, View, ViewExt, view},
9};
10
11use crate::topcoat_compat::async_page;
12
13/// A strip above every page of a panel, under the bar that names whom a user acts as, installed
14/// with [`Panel::banner`](crate::Panel::banner): what the signed-in user must see wherever they
15/// are, such as a failed payment or an expiring trial.
16///
17/// ```rust
18/// use tablo_core::{Banner, Panel, auth};
19/// use topcoat::{
20///     Result,
21///     context::Cx,
22///     view::{View, view},
23/// };
24///
25/// struct PaymentFailed;
26///
27/// impl Banner for PaymentFailed {
28///     async fn render(cx: &Cx) -> Result<Option<impl View>> {
29///         // A real banner reads the signed-in user's billing state here.
30///         let failed = auth::signed_in(cx) && false;
31///         Ok(failed.then(|| view! { <div role="alert">"Your last payment failed."</div> }))
32///     }
33/// }
34///
35/// Panel::new("app").banner::<PaymentFailed>();
36/// ```
37pub trait Banner: Send + Sync + 'static {
38    /// The strip for this request, or `None` to show nothing. It is asked on every page the
39    /// panel's shell frames, signed in or not, and an error fails the page.
40    fn render(cx: &Cx) -> impl Future<Output = Result<Option<impl View>>> + Send;
41}
42
43/// A [`Banner`] with its type erased: what the shell renders.
44pub(crate) type BannerFn = for<'a> fn(&'a Cx) -> BoxView<'a>;
45
46/// `B`'s strip, or nothing.
47pub(crate) fn erased<B: Banner>(cx: &Cx) -> BoxView<'_> {
48    async_page(async move {
49        Ok(match B::render(cx).await? {
50            Some(strip) => strip.boxed(),
51            None => view! {}.boxed(),
52        })
53    })
54}