use std::collections::BTreeMap;
use std::future::Future;
use std::marker::PhantomData;
use futures_util::future::BoxFuture;
use laravel::http::Request;
use crate::protocol::{
BadgeColor, ChartPointDto, ChartStyle, ColumnAlign, DashboardWidgetDto, TableWidgetColumnDto, TableWidgetRowDto,
TrendDirection,
};
pub trait DashboardWidget: Send + Sync + 'static {
fn resolve(&self, request: &Request) -> impl Future<Output = laravel::Result<DashboardWidgetDto>> + Send;
}
pub(crate) trait ErasedWidget: Send + Sync {
fn resolve<'a>(&'a self, request: &'a Request) -> BoxFuture<'a, laravel::Result<DashboardWidgetDto>>;
}
impl<W: DashboardWidget> ErasedWidget for W {
fn resolve<'a>(&'a self, request: &'a Request) -> BoxFuture<'a, laravel::Result<DashboardWidgetDto>> {
Box::pin(DashboardWidget::resolve(self, request))
}
}
pub(crate) struct FnWidget<F>(pub(crate) F);
impl<F, Fut, W> DashboardWidget for FnWidget<F>
where
F: Fn() -> Fut + Send + Sync + 'static,
Fut: Future<Output = laravel::Result<W>> + Send,
W: Into<DashboardWidgetDto>,
{
async fn resolve(&self, _request: &Request) -> laravel::Result<DashboardWidgetDto> {
Ok((self.0)().await?.into())
}
}
pub(crate) struct RequestWidget<F, W>(pub(crate) F, pub(crate) PhantomData<fn() -> W>);
impl<F, W> DashboardWidget for RequestWidget<F, W>
where
F: for<'a> crate::panel::RequestFn<'a, W>,
W: Into<DashboardWidgetDto> + 'static,
{
async fn resolve(&self, request: &Request) -> laravel::Result<DashboardWidgetDto> {
Ok(self.0.call(request).await?.into())
}
}
#[derive(Debug, Clone)]
pub struct StatWidget {
id: String,
label: String,
value: String,
description: Option<String>,
trend: Option<String>,
trend_direction: Option<TrendDirection>,
icon: Option<String>,
color: BadgeColor,
sparkline: Vec<f64>,
href: Option<String>,
col_span: u8,
}
impl StatWidget {
#[must_use]
pub fn new(id: impl Into<String>, label: impl Into<String>, value: impl Into<String>) -> Self {
Self {
id: id.into(),
label: label.into(),
value: value.into(),
description: None,
trend: None,
trend_direction: None,
icon: None,
color: BadgeColor::Zinc,
sparkline: Vec::new(),
href: None,
col_span: 1,
}
}
#[must_use]
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
#[must_use]
pub fn trend(mut self, trend: impl Into<String>, direction: TrendDirection) -> Self {
self.trend = Some(trend.into());
self.trend_direction = Some(direction);
self
}
#[must_use]
pub fn icon(mut self, icon: impl Into<String>) -> Self {
self.icon = Some(icon.into());
self
}
#[must_use]
pub fn color(mut self, color: BadgeColor) -> Self {
self.color = color;
self
}
#[must_use]
pub fn sparkline(mut self, points: impl IntoIterator<Item = f64>) -> Self {
self.sparkline = points.into_iter().collect();
self
}
#[must_use]
pub fn href(mut self, href: impl Into<String>) -> Self {
self.href = Some(href.into());
self
}
#[must_use]
pub fn col_span(mut self, span: u8) -> Self {
self.col_span = span.clamp(1, 4);
self
}
#[must_use]
pub fn build(self) -> DashboardWidgetDto {
DashboardWidgetDto::Stat {
id: self.id,
label: self.label,
value: self.value,
description: self.description,
trend: self.trend,
trend_direction: self.trend_direction,
icon: self.icon,
color: self.color,
sparkline: self.sparkline,
href: self.href,
col_span: self.col_span,
}
}
}
impl From<StatWidget> for DashboardWidgetDto {
fn from(widget: StatWidget) -> Self {
widget.build()
}
}
#[derive(Debug, Clone)]
pub struct ChartWidget {
id: String,
title: String,
subtitle: Option<String>,
style: ChartStyle,
points: Vec<ChartPointDto>,
value_prefix: Option<String>,
value_suffix: Option<String>,
col_span: u8,
}
impl ChartWidget {
#[must_use]
pub fn new(id: impl Into<String>, title: impl Into<String>, style: ChartStyle) -> Self {
Self {
id: id.into(),
title: title.into(),
subtitle: None,
style,
points: Vec::new(),
value_prefix: None,
value_suffix: None,
col_span: 2,
}
}
#[must_use]
pub fn subtitle(mut self, subtitle: impl Into<String>) -> Self {
self.subtitle = Some(subtitle.into());
self
}
#[must_use]
pub fn point(mut self, label: impl Into<String>, value: f64) -> Self {
self.points.push(ChartPointDto {
label: label.into(),
value,
color: None,
});
self
}
#[must_use]
pub fn colored_point(mut self, label: impl Into<String>, value: f64, color: BadgeColor) -> Self {
self.points.push(ChartPointDto {
label: label.into(),
value,
color: Some(color),
});
self
}
#[must_use]
pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
self.value_prefix = Some(prefix.into());
self
}
#[must_use]
pub fn suffix(mut self, suffix: impl Into<String>) -> Self {
self.value_suffix = Some(suffix.into());
self
}
#[must_use]
pub fn col_span(mut self, span: u8) -> Self {
self.col_span = span.clamp(1, 4);
self
}
#[must_use]
pub fn build(self) -> DashboardWidgetDto {
DashboardWidgetDto::Chart {
id: self.id,
title: self.title,
subtitle: self.subtitle,
style: self.style,
points: self.points,
value_prefix: self.value_prefix,
value_suffix: self.value_suffix,
col_span: self.col_span,
}
}
}
impl From<ChartWidget> for DashboardWidgetDto {
fn from(widget: ChartWidget) -> Self {
widget.build()
}
}
#[derive(Debug, Clone)]
pub struct TableWidget {
id: String,
title: String,
subtitle: Option<String>,
action_label: Option<String>,
action_href: Option<String>,
columns: Vec<TableWidgetColumnDto>,
rows: Vec<TableWidgetRowDto>,
col_span: u8,
}
impl TableWidget {
#[must_use]
pub fn new(id: impl Into<String>, title: impl Into<String>) -> Self {
Self {
id: id.into(),
title: title.into(),
subtitle: None,
action_label: None,
action_href: None,
columns: Vec::new(),
rows: Vec::new(),
col_span: 4,
}
}
#[must_use]
pub fn subtitle(mut self, subtitle: impl Into<String>) -> Self {
self.subtitle = Some(subtitle.into());
self
}
#[must_use]
pub fn action(mut self, label: impl Into<String>, href: impl Into<String>) -> Self {
self.action_label = Some(label.into());
self.action_href = Some(href.into());
self
}
#[must_use]
pub fn column(mut self, key: impl Into<String>, label: impl Into<String>, align: ColumnAlign) -> Self {
self.columns.push(TableWidgetColumnDto {
key: key.into(),
label: label.into(),
align,
badge_colors: BTreeMap::new(),
});
self
}
#[must_use]
pub fn badge_column(
mut self,
key: impl Into<String>,
label: impl Into<String>,
colors: impl IntoIterator<Item = (String, BadgeColor)>,
) -> Self {
self.columns.push(TableWidgetColumnDto {
key: key.into(),
label: label.into(),
align: ColumnAlign::Left,
badge_colors: colors.into_iter().collect(),
});
self
}
#[must_use]
pub fn row(
mut self,
key: impl Into<String>,
href: Option<String>,
cells: BTreeMap<String, serde_json::Value>,
) -> Self {
self.rows.push(TableWidgetRowDto {
key: key.into(),
href,
cells,
});
self
}
#[must_use]
pub fn col_span(mut self, span: u8) -> Self {
self.col_span = span.clamp(1, 4);
self
}
#[must_use]
pub fn build(self) -> DashboardWidgetDto {
DashboardWidgetDto::Table {
id: self.id,
title: self.title,
subtitle: self.subtitle,
action_label: self.action_label,
action_href: self.action_href,
columns: self.columns,
rows: self.rows,
col_span: self.col_span,
}
}
}
impl From<TableWidget> for DashboardWidgetDto {
fn from(widget: TableWidget) -> Self {
widget.build()
}
}