| 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 |
use makeover_layout as layout; |
| 48 |
use quasi_declare::declare; |
| 49 |
use quasi_router::screen::{Figure, Tag}; |
| 50 |
use quasi_router::{Node, RegionKind, Slot}; |
| 51 |
use quasi_webview::Webview; |
| 52 |
|
| 53 |
use crate::types::StatCard; |
| 54 |
|
| 55 |
|
| 56 |
pub const REGION: &str = "project-overview"; |
| 57 |
|
| 58 |
|
| 59 |
const TOOLS: &str = "project-overview-tools"; |
| 60 |
|
| 61 |
|
| 62 |
const TOOLS_BODY: &str = "project-overview-tools-body"; |
| 63 |
|
| 64 |
|
| 65 |
struct Step { |
| 66 |
|
| 67 |
label: &'static str, |
| 68 |
|
| 69 |
done: bool, |
| 70 |
|
| 71 |
act: Option<(&'static str, String)>, |
| 72 |
} |
| 73 |
|
| 74 |
impl Step { |
| 75 |
|
| 76 |
fn all(slug: &str, stripe_connected: bool, has_items: bool, has_published: bool) -> Vec<Self> { |
| 77 |
vec![ |
| 78 |
Self { |
| 79 |
label: "Add your first item: upload files, set a price", |
| 80 |
done: has_items, |
| 81 |
act: Some(("New Item", format!("/dashboard/project/{slug}/new-item"))), |
| 82 |
}, |
| 83 |
Self { |
| 84 |
label: "Connect Stripe: required to receive payments (3% processing only)", |
| 85 |
done: stripe_connected, |
| 86 |
act: Some(("Go to Payments", "/dashboard?tab=payments".to_owned())), |
| 87 |
}, |
| 88 |
Self { |
| 89 |
label: "Publish an item: make it visible on your public page", |
| 90 |
done: has_published, |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
act: has_items.then(|| { |
| 95 |
( |
| 96 |
"Go to Content", |
| 97 |
format!("/dashboard/project/{slug}?tab=content"), |
| 98 |
) |
| 99 |
}), |
| 100 |
}, |
| 101 |
] |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
fn all_done(steps: &[Self]) -> bool { |
| 106 |
steps.iter().all(|step| step.done) |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
fn offers_act(&self) -> bool { |
| 116 |
!self.done && self.act.is_some() |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
fn act_label(&self) -> &'static str { |
| 124 |
self.act.as_ref().map_or("", |(label, _)| *label) |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
fn act_href(&self) -> &str { |
| 129 |
self.act.as_ref().map_or("", |(_, href)| href.as_str()) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
declare! { |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
shape step_row(step: &Step) -> Row; |
| 141 |
|
| 142 |
row step.label { |
| 143 |
token Tag::badge("Done").tone(layout::Tone::Success) when step.done; |
| 144 |
act step.act_label() to external step.act_href() when step.offers_act(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
#[must_use] |
| 150 |
pub fn fragment( |
| 151 |
slug: &str, |
| 152 |
stats: &[StatCard], |
| 153 |
stripe_connected: bool, |
| 154 |
has_items: bool, |
| 155 |
has_published: bool, |
| 156 |
) -> String { |
| 157 |
use quasi_axum::Serves as _; |
| 158 |
|
| 159 |
let mut slot = Slot::new(REGION, RegionKind::Pane); |
| 160 |
for node in body(slug, stats, stripe_connected, has_items, has_published) { |
| 161 |
slot = slot.with(node); |
| 162 |
} |
| 163 |
Webview::new().fragment(&Node::Region(slot)) |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
#[must_use] |
| 168 |
pub fn fill( |
| 169 |
slug: &str, |
| 170 |
stats: &[StatCard], |
| 171 |
stripe_connected: bool, |
| 172 |
has_items: bool, |
| 173 |
has_published: bool, |
| 174 |
) -> String { |
| 175 |
use quasi_axum::Serves as _; |
| 176 |
|
| 177 |
let mut out = String::new(); |
| 178 |
for node in body(slug, stats, stripe_connected, has_items, has_published) { |
| 179 |
out.push_str(&Webview::new().fragment(&node)); |
| 180 |
} |
| 181 |
out |
| 182 |
} |
| 183 |
|
| 184 |
declare! { |
| 185 |
|
| 186 |
shape body( |
| 187 |
slug: &str, |
| 188 |
stats: &[StatCard], |
| 189 |
stripe_connected: bool, |
| 190 |
has_items: bool, |
| 191 |
has_published: bool, |
| 192 |
) -> Vec<Node>; |
| 193 |
|
| 194 |
let steps = Step::all(slug, stripe_connected, has_items, has_published); |
| 195 |
include setup(&steps) unless Step::all_done(&steps); |
| 196 |
|
| 197 |
link "Docs: Projects" to get "/docs/projects" navigating; |
| 198 |
include figures(stats); |
| 199 |
section "Quick Actions"; |
| 200 |
for node in quick_actions(slug) { |
| 201 |
include node; |
| 202 |
} |
| 203 |
include tools(); |
| 204 |
} |
| 205 |
|
| 206 |
declare! { |
| 207 |
|
| 208 |
shape setup(steps: &[Step]) -> Node; |
| 209 |
|
| 210 |
region "project-overview-setup" as Pane { |
| 211 |
subsection "Project Setup"; |
| 212 |
list { |
| 213 |
for step in steps.iter() { |
| 214 |
include step_row(step); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
fn change(stat: &StatCard) -> &str { |
| 222 |
stat.change.as_deref().unwrap_or_default() |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
fn delta_tone(stat: &StatCard) -> layout::Tone { |
| 228 |
if stat.is_positive { |
| 229 |
layout::Tone::Success |
| 230 |
} else { |
| 231 |
layout::Tone::Danger |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
declare! { |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
shape figures(stats: &[StatCard]) -> Node; |
| 244 |
|
| 245 |
stats [] { |
| 246 |
for stat in stats.iter() { |
| 247 |
figure Figure::new(stat.value.clone(), stat.label.clone()) |
| 248 |
when stat.change.is_none(); |
| 249 |
figure Figure::new(stat.value.clone(), stat.label.clone()) |
| 250 |
.change(change(stat)) |
| 251 |
.tone(delta_tone(stat)) |
| 252 |
unless stat.change.is_none(); |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
declare! { |
| 258 |
|
| 259 |
shape quick_actions(slug: &str) -> Vec<Node>; |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
act "New Item" to external "/dashboard/project/{slug}/new-item"; |
| 264 |
act "View Public Page" to external "/p/{slug}"; |
| 265 |
|
| 266 |
|
| 267 |
include super::export_act::act("/api/export/projects", "projects.csv"); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
struct Tool { |
| 275 |
|
| 276 |
name: &'static str, |
| 277 |
|
| 278 |
description: &'static str, |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
const TOOLS_LIST: &[Tool] = &[ |
| 283 |
Tool { |
| 284 |
name: "Content", |
| 285 |
description: "Upload items, manage versions, set prices.", |
| 286 |
}, |
| 287 |
Tool { |
| 288 |
name: "Blog", |
| 289 |
description: "Write posts that appear on your project page and RSS feed.", |
| 290 |
}, |
| 291 |
Tool { |
| 292 |
name: "Promo Codes", |
| 293 |
description: "Create discounts, free access codes, or trial periods.", |
| 294 |
}, |
| 295 |
Tool { |
| 296 |
name: "Membership Tiers", |
| 297 |
description: "Recurring subscriptions with gated content access.", |
| 298 |
}, |
| 299 |
Tool { |
| 300 |
name: "Team", |
| 301 |
description: "Add collaborators and split revenue automatically.", |
| 302 |
}, |
| 303 |
Tool { |
| 304 |
name: "Analytics", |
| 305 |
description: "Track sales, revenue, and views over time.", |
| 306 |
}, |
| 307 |
]; |
| 308 |
|
| 309 |
declare! { |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
shape tools() -> Node; |
| 322 |
|
| 323 |
region TOOLS as Group { |
| 324 |
region TOOLS_BODY as Pane { |
| 325 |
label "Explore Your Project Tools"; |
| 326 |
list { |
| 327 |
for tool in TOOLS_LIST { |
| 328 |
row tool.name { |
| 329 |
secondary tool.description; |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
showing_at_most_one None; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
#[cfg(test)] |
| 339 |
mod tests { |
| 340 |
use super::*; |
| 341 |
use quasi_axum::Serves; |
| 342 |
|
| 343 |
fn stat(label: &str, change: Option<&str>, positive: bool) -> StatCard { |
| 344 |
StatCard { |
| 345 |
label: label.into(), |
| 346 |
value: "12".into(), |
| 347 |
change: change.map(Into::into), |
| 348 |
is_positive: positive, |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
fn render(slug: &str, stripe: bool, items: bool, published: bool) -> String { |
| 353 |
let mut out = String::new(); |
| 354 |
for node in &body(slug, &[stat("Items", None, true)], stripe, items, published) { |
| 355 |
out.push_str(&Webview::new().fragment(node)); |
| 356 |
} |
| 357 |
out |
| 358 |
} |
| 359 |
|
| 360 |
#[test] |
| 361 |
fn a_finished_project_is_not_shown_the_setup_checklist() { |
| 362 |
let done = render("an-album", true, true, true); |
| 363 |
assert!(!done.contains("Project Setup"), "{done}"); |
| 364 |
|
| 365 |
let unfinished = render("an-album", false, true, true); |
| 366 |
assert!(unfinished.contains("Project Setup"), "{unfinished}"); |
| 367 |
} |
| 368 |
|
| 369 |
#[test] |
| 370 |
fn a_finished_step_says_done_and_offers_nothing() { |
| 371 |
let html = render("an-album", false, true, false); |
| 372 |
|
| 373 |
|
| 374 |
assert!(html.contains("Done"), "{html}"); |
| 375 |
assert!( |
| 376 |
!html.contains("/dashboard/project/an-album/new-item\">New Item"), |
| 377 |
"a finished step still offers its CTA: {html}" |
| 378 |
); |
| 379 |
|
| 380 |
assert!(html.contains("/dashboard?tab=payments"), "{html}"); |
| 381 |
} |
| 382 |
|
| 383 |
#[test] |
| 384 |
fn publish_offers_content_only_once_there_is_something_to_publish() { |
| 385 |
let empty = render("an-album", false, false, false); |
| 386 |
let stocked = render("an-album", false, true, false); |
| 387 |
|
| 388 |
assert!(!empty.contains("Go to Content"), "{empty}"); |
| 389 |
assert!(stocked.contains("Go to Content"), "{stocked}"); |
| 390 |
} |
| 391 |
|
| 392 |
#[test] |
| 393 |
fn the_tools_disclosure_is_closed_and_holds_all_six() { |
| 394 |
let html = render("an-album", true, true, true); |
| 395 |
|
| 396 |
assert!(html.contains("Explore Your Project Tools"), "{html}"); |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
assert!(html.contains("aria-expanded=\"false\""), "{html}"); |
| 402 |
assert!(!html.contains("data-shows=\"next\""), "{html}"); |
| 403 |
assert!(!html.contains("data-shows=\"previous\""), "{html}"); |
| 404 |
for tool in [ |
| 405 |
"Content", |
| 406 |
"Blog", |
| 407 |
"Promo Codes", |
| 408 |
"Membership Tiers", |
| 409 |
"Team", |
| 410 |
"Analytics", |
| 411 |
] { |
| 412 |
assert!(html.contains(tool), "missing {tool}: {html}"); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
#[test] |
| 417 |
fn the_export_is_the_described_one_and_not_a_sixth_spelling() { |
| 418 |
let html = render("an-album", true, true, true); |
| 419 |
|
| 420 |
assert!(html.contains("data-saves=\"projects.csv\""), "{html}"); |
| 421 |
assert!(html.contains("hx-post=\"/api/export/projects\""), "{html}"); |
| 422 |
|
| 423 |
assert!(!html.contains("hx-swap=\"beforeend\""), "{html}"); |
| 424 |
assert!(!html.contains("data-action"), "{html}"); |
| 425 |
} |
| 426 |
|
| 427 |
#[test] |
| 428 |
fn a_figure_without_a_delta_stays_neutral() { |
| 429 |
let out = Webview::new().fragment(&figures(&[stat("Items", None, true)])); |
| 430 |
assert!(!out.contains("data-tone=\"success\""), "{out}"); |
| 431 |
} |
| 432 |
|
| 433 |
#[test] |
| 434 |
fn the_slug_cannot_smuggle_markup() { |
| 435 |
let html = render("<script>x()</script>", false, false, false); |
| 436 |
assert!(!html.contains("<script>x()"), "{html}"); |
| 437 |
} |
| 438 |
|
| 439 |
#[test] |
| 440 |
fn the_inline_fill_carries_no_region_because_the_strip_draws_one() { |
| 441 |
let inline = fill("an-album", &[], true, true, true); |
| 442 |
assert!(!inline.contains(&format!("id=\"{REGION}\"")), "{inline}"); |
| 443 |
assert!(inline.contains("Quick Actions"), "{inline}"); |
| 444 |
} |
| 445 |
} |
| 446 |
|