Skip to main content

ignite_core/
widget.rs

1//! Dashboard widgets: the stat cards and the latest-records tables a resource
2//! can show on the dashboard, and the panel's own widgets.
3//!
4//! A [`Stat`] is one card, as Filament's `Stat::make($label, $value)` in a stats overview
5//! widget: its label is the card's title, its value the number shown, and an optional
6//! description the line under it. A resource gives its cards with
7//! [`IgniteResource::stats`](crate::IgniteResource::stats) (or a runtime resource's
8//! [`RuntimeData::stats`](crate::RuntimeData::stats)); the panel gives cards of its own with a
9//! [`StatsOverviewWidget`] passed to `Ignite::widget`, as Filament's panel `widgets()`. The
10//! dashboard shows every card in a grid: the panel's widgets first, in the order they were
11//! added, then each resource's, in the order of the navigation's resources.
12
13use orbit_orm::Database;
14
15use crate::policy::ActingUser;
16use crate::runtime_resource::RuntimeFuture;
17
18/// One stat card on the dashboard: a label and a whole-number value.
19#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
20pub struct StatWidget {
21    /// The card's heading, such as "Users".
22    pub label: &'static str,
23    /// The number the card shows.
24    pub value: i64,
25}
26
27/// A dashboard table of the newest records of a resource (added in v1).
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct LatestRecords {
30    /// The table's heading, such as "Latest users".
31    pub title: &'static str,
32    /// The keys of the resource's columns to show, in order. Each key must be a column key.
33    pub columns: &'static [&'static str],
34    /// The most records the table shows, newest first.
35    pub limit: i64,
36}
37
38/// One card of the dashboard, as Filament's `Stat::make($label, $value)`: a title (the label),
39/// the number shown and an optional description under it, such as "3 drafts".
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct Stat {
42    label: String,
43    value: String,
44    description: Option<String>,
45}
46
47impl Stat {
48    /// A card titled `label` showing `value`, such as `Stat::make("Posts", 12)`. The value is
49    /// shown as its text, so it may be formatted already, such as `"1.2k"`.
50    pub fn make(label: impl Into<String>, value: impl ToString) -> Self {
51        Stat {
52            label: label.into(),
53            value: value.to_string(),
54            description: None,
55        }
56    }
57
58    /// The line under the value, as Filament's `description()`.
59    pub fn description(mut self, description: impl Into<String>) -> Self {
60        self.description = Some(description.into());
61        self
62    }
63
64    /// The card's title.
65    pub fn get_label(&self) -> &str {
66        &self.label
67    }
68
69    /// The value, as shown.
70    pub fn get_value(&self) -> &str {
71        &self.value
72    }
73
74    /// The description, if set.
75    pub fn get_description(&self) -> Option<&str> {
76        self.description.as_deref()
77    }
78}
79
80impl From<StatWidget> for Stat {
81    fn from(widget: StatWidget) -> Self {
82        Stat::make(widget.label, widget.value)
83    }
84}
85
86/// A widget of the panel's own on the dashboard, as Filament's `StatsOverviewWidget` with its
87/// `getStats()`: the cards it computes for the signed-in user. Pass it to `Ignite::widget`.
88///
89/// ```ignore
90/// struct SiteStats;
91///
92/// impl StatsOverviewWidget for SiteStats {
93///     fn stats<'a>(&'a self, db: &'a Database, _: &'a ActingUser) -> RuntimeFuture<'a, Vec<Stat>> {
94///         Box::pin(async move {
95///             let comments = count_pending_comments(db).await?;
96///             Ok(vec![Stat::make("Comments", comments).description("awaiting moderation")])
97///         })
98///     }
99/// }
100/// ```
101pub trait StatsOverviewWidget: Send + Sync + 'static {
102    /// The cards, in display order, for `user`.
103    fn stats<'a>(&'a self, db: &'a Database, user: &'a ActingUser) -> RuntimeFuture<'a, Vec<Stat>>;
104
105    /// Whether `user` sees the widget, as Filament's `canView()`. The default is every user.
106    fn can_view(&self, user: &ActingUser) -> bool {
107        let _ = user;
108        true
109    }
110}