| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
use makeover_layout as layout; |
| 49 |
use quasi_declare::declare; |
| 50 |
use quasi_router::screen::{Bar, Chart, Figure}; |
| 51 |
use quasi_router::{Node, RegionKind, Slot}; |
| 52 |
use quasi_webview::Webview; |
| 53 |
|
| 54 |
use crate::types::{ContentItem, RevenueChart, StatCard}; |
| 55 |
|
| 56 |
|
| 57 |
pub const REGION: &str = "project-analytics"; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#[must_use] |
| 62 |
pub fn fragment( |
| 63 |
slug: &str, |
| 64 |
range: &str, |
| 65 |
stats: &[StatCard], |
| 66 |
chart: &RevenueChart, |
| 67 |
items: &[ContentItem], |
| 68 |
) -> String { |
| 69 |
use quasi_axum::Serves as _; |
| 70 |
|
| 71 |
let mut slot = Slot::new(REGION, RegionKind::Pane); |
| 72 |
for node in body(slug, range, stats, chart, items) { |
| 73 |
slot = slot.with(node); |
| 74 |
} |
| 75 |
Webview::new().fragment(&Node::Region(slot)) |
| 76 |
} |
| 77 |
|
| 78 |
declare! { |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
shape body( |
| 84 |
slug: &str, |
| 85 |
range: &str, |
| 86 |
stats: &[StatCard], |
| 87 |
chart: &RevenueChart, |
| 88 |
items: &[ContentItem], |
| 89 |
) -> Vec<Node>; |
| 90 |
|
| 91 |
link "Docs: Analytics" to get "/docs/analytics" navigating; |
| 92 |
link "Export data" to get "/dashboard/export" navigating; |
| 93 |
section super::range_heading(range); |
| 94 |
|
| 95 |
for chip in range_chips(slug, range) { |
| 96 |
include chip; |
| 97 |
} |
| 98 |
|
| 99 |
include figures(stats); |
| 100 |
|
| 101 |
section "Revenue Over Time"; |
| 102 |
empty "No revenue data yet. Revenue will appear here after your first sale. \ |
| 103 |
Publish an item and share it to get started." |
| 104 |
when chart.bars.is_empty(); |
| 105 |
chart Chart::new(chart.most).label("revenue over time") unless chart.bars.is_empty() { |
| 106 |
for bar in chart.bars.iter() { |
| 107 |
bar Bar::at(bar.label.clone()) |
| 108 |
.of(bar.cents) |
| 109 |
.reading(bar.value.clone()) |
| 110 |
.note(super::user_analytics::sales(bar.count)); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
section "Top Performing Items"; |
| 115 |
empty "No sales data yet. Publish and promote your items to see analytics here." |
| 116 |
when items.is_empty(); |
| 117 |
include top_items(items) unless items.is_empty(); |
| 118 |
} |
| 119 |
declare! { |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
shape range_chips(slug: &str, range: &str) -> Vec<Node>; |
| 127 |
|
| 128 |
for window in super::RANGES { |
| 129 |
chip window.value |
| 130 |
to get "/dashboard/project/{slug}/tabs/analytics" carrying "range" window.value { |
| 131 |
latched when super::is_shown(window, range); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
fn change(stat: &StatCard) -> &str { |
| 138 |
stat.change.as_deref().unwrap_or_default() |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
fn delta_tone(stat: &StatCard) -> layout::Tone { |
| 144 |
if stat.is_positive { |
| 145 |
layout::Tone::Success |
| 146 |
} else { |
| 147 |
layout::Tone::Danger |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
declare! { |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
shape figures(stats: &[StatCard]) -> Node; |
| 161 |
|
| 162 |
stats [] { |
| 163 |
for stat in stats.iter() { |
| 164 |
figure Figure::new(stat.value.clone(), stat.label.clone()) |
| 165 |
when stat.change.is_none(); |
| 166 |
figure Figure::new(stat.value.clone(), stat.label.clone()) |
| 167 |
.change(change(stat)) |
| 168 |
.tone(delta_tone(stat)) |
| 169 |
unless stat.change.is_none(); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
declare! { |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
shape top_items(items: &[ContentItem]) -> Node; |
| 187 |
|
| 188 |
table { |
| 189 |
column "Item" { |
| 190 |
width Fill; |
| 191 |
priority Essential; |
| 192 |
} |
| 193 |
column "Revenue" { |
| 194 |
width Content; |
| 195 |
} |
| 196 |
|
| 197 |
for item in items.iter() { |
| 198 |
cells { |
| 199 |
cell item.title.clone(); |
| 200 |
cell item.revenue.clone(); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
#[cfg(test)] |
| 207 |
mod tests { |
| 208 |
use super::*; |
| 209 |
use crate::types::ChartBar; |
| 210 |
use quasi_axum::Serves; |
| 211 |
|
| 212 |
fn stat(label: &str, change: Option<&str>) -> StatCard { |
| 213 |
StatCard { |
| 214 |
label: label.into(), |
| 215 |
value: "$42".into(), |
| 216 |
change: change.map(Into::into), |
| 217 |
is_positive: true, |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
fn bar() -> ChartBar { |
| 222 |
ChartBar { |
| 223 |
label: "Aug 1".into(), |
| 224 |
cents: 1200, |
| 225 |
value: "$12".into(), |
| 226 |
count: 3, |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
fn chart_of(bars: &[ChartBar]) -> RevenueChart { |
| 233 |
RevenueChart { |
| 234 |
most: 2400, |
| 235 |
bars: bars.to_vec(), |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
fn render(range: &str, bars: &[ChartBar], items: &[ContentItem]) -> String { |
| 240 |
let chart = chart_of(bars); |
| 241 |
let nodes = body("an-album", range, &[stat("Revenue", None)], &chart, items); |
| 242 |
let mut out = String::new(); |
| 243 |
let webview = Webview::new(); |
| 244 |
for node in &nodes { |
| 245 |
out.push_str(&webview.fragment(node)); |
| 246 |
} |
| 247 |
out |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
fn the_heading_names_the_window_and_falls_back_to_all_time() { |
| 252 |
assert_eq!(super::super::range_heading("7d"), "Last 7 days"); |
| 253 |
assert_eq!(super::super::range_heading("90d"), "Last 90 days"); |
| 254 |
|
| 255 |
assert_eq!(super::super::range_heading("nonsense"), "All time"); |
| 256 |
} |
| 257 |
|
| 258 |
#[test] |
| 259 |
fn the_showing_range_is_latched_and_the_others_are_not() { |
| 260 |
let html = render("30d", &[bar()], &[]); |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
assert_eq!(html.matches("aria-current=\"true\"").count(), 1, "{html}"); |
| 267 |
assert_eq!(html.matches("class=\"chip latched\"").count(), 1, "{html}"); |
| 268 |
assert_eq!(html.matches("class=\"chip\"").count(), 3, "{html}"); |
| 269 |
} |
| 270 |
|
| 271 |
#[test] |
| 272 |
fn every_range_addresses_this_projects_own_analytics() { |
| 273 |
let html = render("30d", &[bar()], &[]); |
| 274 |
|
| 275 |
for value in ["7d", "30d", "90d", "all"] { |
| 276 |
assert!( |
| 277 |
html.contains(&format!("range={value}")), |
| 278 |
"missing {value}: {html}" |
| 279 |
); |
| 280 |
} |
| 281 |
assert!( |
| 282 |
html.contains("/dashboard/project/an-album/tabs/analytics"), |
| 283 |
"{html}" |
| 284 |
); |
| 285 |
} |
| 286 |
|
| 287 |
#[test] |
| 288 |
fn the_chart_is_drawn_when_there_are_bars_and_stood_in_for_when_there_are_not() { |
| 289 |
let with = render("30d", &[bar()], &[]); |
| 290 |
let without = render("30d", &[], &[]); |
| 291 |
|
| 292 |
assert!(with.contains("class=\"chart-bars\""), "{with}"); |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
assert!(with.contains("--most: 2400"), "{with}"); |
| 297 |
assert!(with.contains("--value: 1200"), "{with}"); |
| 298 |
assert!(!with.contains("--fill"), "{with}"); |
| 299 |
|
| 300 |
assert!(!without.contains("class=\"chart-bars\""), "{without}"); |
| 301 |
assert!(without.contains("No revenue data yet."), "{without}"); |
| 302 |
} |
| 303 |
|
| 304 |
#[test] |
| 305 |
fn the_chart_is_the_same_one_user_analytics_draws() { |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
let mine = render("30d", &[bar()], &[]); |
| 311 |
let theirs = Webview::new().fragment( |
| 312 |
&Node::chart(Chart::new(2400).label("revenue over time")) |
| 313 |
.bar(Bar::at("Aug 1").of(1200).reading("$12").note("3 sales")), |
| 314 |
); |
| 315 |
assert!(mine.contains(&theirs), "{mine}\n\n{theirs}"); |
| 316 |
} |
| 317 |
|
| 318 |
#[test] |
| 319 |
fn a_bar_label_cannot_smuggle_markup() { |
| 320 |
let hostile = ChartBar { |
| 321 |
label: "<script>x()</script>".into(), |
| 322 |
..bar() |
| 323 |
}; |
| 324 |
let html = render("30d", &[hostile], &[]); |
| 325 |
assert!(!html.contains("<script>x()"), "{html}"); |
| 326 |
} |
| 327 |
|
| 328 |
#[test] |
| 329 |
fn an_empty_top_items_says_so_rather_than_drawing_an_empty_table() { |
| 330 |
let html = render("30d", &[bar()], &[]); |
| 331 |
assert!(html.contains("No sales data yet."), "{html}"); |
| 332 |
assert!(!html.contains("role=\"table\""), "{html}"); |
| 333 |
} |
| 334 |
} |
| 335 |
|