| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
use quasi_router::layout::{FieldKind, Selector, Tone}; |
| 108 |
use quasi_router::{ |
| 109 |
Act, Action, Choice, Field, Node, RegionKind, Request, Response, RouteError, Router, Screen, |
| 110 |
Slot, |
| 111 |
}; |
| 112 |
|
| 113 |
use super::{State, Status, Subscription, Sync}; |
| 114 |
|
| 115 |
|
| 116 |
const BODY: &str = "sync-body"; |
| 117 |
|
| 118 |
|
| 119 |
use crate::storage_cap::GIB; |
| 120 |
|
| 121 |
|
| 122 |
const CAP: &str = "cap_gib"; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
const INTERVALS: &[u32] = &[5, 15, 30, 60]; |
| 130 |
|
| 131 |
|
| 132 |
pub fn routes(router: Router<super::Panels<'_>>) -> Router<super::Panels<'_>> { |
| 133 |
router |
| 134 |
.get("/sync", index) |
| 135 |
.post("/sync/connect", connect) |
| 136 |
.post("/sync/cancel", cancel) |
| 137 |
.post("/sync/encryption", encryption) |
| 138 |
.post("/sync/now", sync_now) |
| 139 |
.post("/sync/auto", auto) |
| 140 |
.post("/sync/interval", interval) |
| 141 |
.post("/sync/error/clear", clear_error) |
| 142 |
.post("/sync/disconnect", disconnect) |
| 143 |
.post("/sync/subscription/refresh", refresh_subscription) |
| 144 |
.post("/sync/subscribe", subscribe) |
| 145 |
.post("/sync/cap", cap) |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
fn index(state: &super::Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 150 |
Ok(screen(state.sync).into()) |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
fn connect(state: &super::Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 161 |
let address = state |
| 162 |
.sync |
| 163 |
.connect() |
| 164 |
.map_err(|error| RouteError::internal(format!("Sync connect failed: {error}")))?; |
| 165 |
Ok(Response::goto(Action::external(address))) |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
fn cancel(state: &super::Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 170 |
state.sync.cancel(); |
| 171 |
Ok(screen(state.sync).into()) |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
fn encryption(state: &super::Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 180 |
let password = request.payload.get("password").unwrap_or_default(); |
| 181 |
if password.is_empty() { |
| 182 |
return Ok(Response::from(screen(state.sync)) |
| 183 |
.toast(Tone::Danger, "A password is needed to encrypt this vault.")); |
| 184 |
} |
| 185 |
let is_new = matches!( |
| 186 |
state.sync.status().state, |
| 187 |
State::NeedsEncryption { |
| 188 |
has_server_key: false |
| 189 |
} |
| 190 |
); |
| 191 |
state.sync.set_password(password, is_new); |
| 192 |
Ok(screen(state.sync).into()) |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
fn sync_now(state: &super::Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 197 |
state.sync.sync_now(); |
| 198 |
Ok(screen(state.sync).into()) |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
fn auto(state: &super::Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 203 |
let on = !request.payload.get("auto").unwrap_or_default().is_empty(); |
| 204 |
state.sync.set_auto(on); |
| 205 |
Ok(screen(state.sync).into()) |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
fn interval(state: &super::Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 210 |
let minutes: u32 = request |
| 211 |
.payload |
| 212 |
.get(Node::SELECTED) |
| 213 |
.and_then(|value| value.parse().ok()) |
| 214 |
.ok_or_else(|| RouteError::not_found("no such interval"))?; |
| 215 |
if !INTERVALS.contains(&minutes) { |
| 216 |
return Err(RouteError::not_found("no such interval")); |
| 217 |
} |
| 218 |
state.sync.set_interval(minutes); |
| 219 |
Ok(screen(state.sync).into()) |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
fn clear_error(state: &super::Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 224 |
state.sync.clear_error(); |
| 225 |
Ok(screen(state.sync).into()) |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
fn refresh_subscription( |
| 230 |
state: &super::Panels<'_>, |
| 231 |
_request: Request, |
| 232 |
) -> Result<Response, RouteError> { |
| 233 |
state.sync.refresh_subscription(); |
| 234 |
Ok(screen(state.sync).into()) |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
fn subscribe(state: &super::Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 244 |
let cap = cap_from(state, &request)?; |
| 245 |
let annual = request.payload.get("cadence") == Some("annual"); |
| 246 |
state.sync.subscribe(cap, annual); |
| 247 |
Ok(Response::from(screen(state.sync)).toast(Tone::Info, "Opening checkout in your browser.")) |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
fn cap(state: &super::Panels<'_>, request: Request) -> Result<Response, RouteError> { |
| 252 |
let cap = cap_from(state, &request)?; |
| 253 |
state.sync.queue_cap_change(cap); |
| 254 |
Ok(Response::from(screen(state.sync)) |
| 255 |
.toast(Tone::Success, "The cap changes at your next renewal.")) |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
fn cap_from(state: &super::Panels<'_>, request: &Request) -> Result<i64, RouteError> { |
| 264 |
let gib: i64 = request |
| 265 |
.payload |
| 266 |
.get(CAP) |
| 267 |
.and_then(|value| value.parse().ok()) |
| 268 |
.ok_or_else(|| RouteError::not_found("no cap named"))?; |
| 269 |
let bytes = gib.saturating_mul(GIB); |
| 270 |
let pricing = state |
| 271 |
.sync |
| 272 |
.pricing() |
| 273 |
.ok_or_else(|| RouteError::internal("Pricing is not loaded yet."))?; |
| 274 |
if bytes < pricing.min_bytes || bytes > pricing.max_bytes { |
| 275 |
return Err(RouteError::not_found("that cap is not on offer")); |
| 276 |
} |
| 277 |
Ok(bytes) |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
fn disconnect(state: &super::Panels<'_>, _request: Request) -> Result<Response, RouteError> { |
| 282 |
state.sync.disconnect(); |
| 283 |
Ok(screen(state.sync).into()) |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
fn screen(sync: &dyn Sync) -> Screen { |
| 293 |
let status = sync.status(); |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
let mut body = Slot::new(BODY, RegionKind::Pane) |
| 301 |
.live() |
| 302 |
.with(Node::page("Cloud Sync")); |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
if !sync.available() { |
| 311 |
return Screen::sidebar_content("Cloud Sync").with( |
| 312 |
body.with(Node::text("Cloud sync is unavailable.")) |
| 313 |
.with(Node::text("Open a vault to enable sync.")), |
| 314 |
); |
| 315 |
} |
| 316 |
|
| 317 |
body = match status.state { |
| 318 |
State::Disconnected => disconnected(body), |
| 319 |
State::Authenticating => authenticating(body), |
| 320 |
State::NeedsEncryption { has_server_key } => needs_encryption(body, has_server_key), |
| 321 |
State::Ready | State::Syncing => ready(body, &status, sync), |
| 322 |
}; |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
if let Some(error) = &status.last_error { |
| 328 |
body = body.with(Node::Notice { |
| 329 |
kind: quasi_router::layout::Notice::Banner, |
| 330 |
tone: Tone::Danger, |
| 331 |
text: error.clone(), |
| 332 |
}); |
| 333 |
if matches!(status.state, State::Ready | State::Syncing) { |
| 334 |
body = body.with(Node::Act(Act::new("Retry", Action::post("/sync/now")))); |
| 335 |
} |
| 336 |
body = body.with(Node::Act(Act::new( |
| 337 |
"Dismiss", |
| 338 |
Action::post("/sync/error/clear"), |
| 339 |
))); |
| 340 |
} |
| 341 |
|
| 342 |
Screen::sidebar_content("Cloud Sync").with(body) |
| 343 |
} |
| 344 |
|
| 345 |
fn disconnected(body: Slot) -> Slot { |
| 346 |
body.with(Node::text( |
| 347 |
"Connect your audiofiles vault to Makenot.work for cross-device sync.", |
| 348 |
)) |
| 349 |
.with(Node::text( |
| 350 |
"Metadata (tags, vault structure, analysis) syncs automatically. Audio file sync is per-vault opt-in.", |
| 351 |
)) |
| 352 |
.with(Node::Act(Act::new("Connect", Action::post("/sync/connect")))) |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
fn authenticating(body: Slot) -> Slot { |
| 361 |
body.with(Node::text("Waiting for authentication in your browser...")) |
| 362 |
.with(Node::text( |
| 363 |
"The app will update automatically once you sign in.", |
| 364 |
)) |
| 365 |
.with(Node::Act(Act::new("Cancel", Action::post("/sync/cancel")))) |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
fn needs_encryption(body: Slot, has_server_key: bool) -> Slot { |
| 376 |
let says = if has_server_key { |
| 377 |
"This vault is already encrypted. Enter its password to unlock it here." |
| 378 |
} else { |
| 379 |
"Choose a password. It encrypts everything before it leaves this machine, and it cannot be recovered." |
| 380 |
}; |
| 381 |
body.with(Node::text(says)).with(Node::Form { |
| 382 |
fields: vec![ |
| 383 |
Field::new(FieldKind::Secret, "password", "Password") |
| 384 |
.required() |
| 385 |
.hint(if has_server_key { |
| 386 |
"The password this vault was encrypted with." |
| 387 |
} else { |
| 388 |
"Nobody can reset this for you." |
| 389 |
}), |
| 390 |
], |
| 391 |
submit: if has_server_key { |
| 392 |
"Unlock" |
| 393 |
} else { |
| 394 |
"Set password" |
| 395 |
} |
| 396 |
.to_owned(), |
| 397 |
action: Action::post("/sync/encryption"), |
| 398 |
}) |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
fn ready(body: Slot, status: &Status, sync: &dyn Sync) -> Slot { |
| 403 |
let syncing = matches!(status.state, State::Syncing); |
| 404 |
let mut body = body.with(Node::text(if syncing { "Syncing..." } else { "Connected" })); |
| 405 |
|
| 406 |
if let Some(last) = &status.last_sync_at { |
| 407 |
body = body.with(Node::text(format!("Last sync: {last}"))); |
| 408 |
} |
| 409 |
if status.pending_changes > 0 { |
| 410 |
|
| 411 |
|
| 412 |
body = body.with(Node::Figure(quasi_router::Figure::new( |
| 413 |
status.pending_changes.to_string(), |
| 414 |
"pending changes", |
| 415 |
))); |
| 416 |
} |
| 417 |
|
| 418 |
let mut now = Act::new("Sync now", Action::post("/sync/now")); |
| 419 |
if syncing { |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
now = now.disabled(); |
| 424 |
} |
| 425 |
|
| 426 |
body.with(Node::Act(now)) |
| 427 |
.with(Node::section("Auto-sync")) |
| 428 |
.with(Node::Field(Box::new( |
| 429 |
Field::new(FieldKind::Checkbox, "auto", "Sync on a schedule") |
| 430 |
.value(if status.auto_sync_enabled { "on" } else { "" }) |
| 431 |
.changes(Action::post("/sync/auto")), |
| 432 |
))) |
| 433 |
.with(Node::Select { |
| 434 |
kind: Selector::Segmented, |
| 435 |
options: INTERVALS |
| 436 |
.iter() |
| 437 |
.map(|minutes| { |
| 438 |
( |
| 439 |
Choice::new(minutes.to_string(), format!("{minutes} min")), |
| 440 |
None, |
| 441 |
) |
| 442 |
}) |
| 443 |
.collect(), |
| 444 |
chosen: Some(status.sync_interval_minutes.to_string()), |
| 445 |
action: Some(Action::post("/sync/interval")), |
| 446 |
}) |
| 447 |
.with(Node::section("Audio file sync")) |
| 448 |
.with(subscription(sync)) |
| 449 |
.with(Node::Act( |
| 450 |
Act::new("Disconnect", Action::post("/sync/disconnect")) |
| 451 |
.tone(Tone::Danger) |
| 452 |
.confirm("Disconnect this vault from cloud sync?"), |
| 453 |
)) |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
fn subscription(sync: &dyn Sync) -> Node { |
| 463 |
let Some(pricing) = sync.pricing() else { |
| 464 |
return waiting("Loading pricing..."); |
| 465 |
}; |
| 466 |
match sync.subscription() { |
| 467 |
None => waiting("Checking subscription..."), |
| 468 |
Some(sub) if sub.active => subscribed(sync, &sub, &pricing), |
| 469 |
Some(_) => on_offer(sync, &pricing), |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
fn waiting(says: &str) -> Node { |
| 479 |
Node::Region( |
| 480 |
Slot::new("subscription", RegionKind::Pane) |
| 481 |
.pending() |
| 482 |
.with(Node::text(says)) |
| 483 |
.with(Node::Act(Act::new( |
| 484 |
"Retry", |
| 485 |
Action::post("/sync/subscription/refresh"), |
| 486 |
))), |
| 487 |
) |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
fn subscribed(sync: &dyn Sync, sub: &Subscription, pricing: &super::Pricing) -> Node { |
| 492 |
let mut slot = Slot::new("subscription", RegionKind::Pane).with(Node::text(format!( |
| 493 |
"Subscribed: {} ({})", |
| 494 |
gib_of(sub.limit_bytes), |
| 495 |
sub.interval |
| 496 |
))); |
| 497 |
|
| 498 |
if sub.limit_bytes > 0 { |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
slot = slot.with(Node::Meter( |
| 503 |
quasi_router::Meter::new(gib_count(sub.used_bytes), gib_count(sub.limit_bytes).max(1)) |
| 504 |
.label("GiB used") |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
.tone(if nearly_full(sub) { |
| 509 |
Tone::Warning |
| 510 |
} else { |
| 511 |
Tone::Neutral |
| 512 |
}), |
| 513 |
)); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
if let Some(warning) = cap_warning(sync, sub, pricing) { |
| 522 |
slot = slot.with(Node::Notice { |
| 523 |
kind: quasi_router::layout::Notice::Banner, |
| 524 |
tone: if sub.used_bytes >= sub.limit_bytes { |
| 525 |
Tone::Danger |
| 526 |
} else { |
| 527 |
Tone::Warning |
| 528 |
}, |
| 529 |
text: warning, |
| 530 |
}); |
| 531 |
} |
| 532 |
|
| 533 |
if let Some(pending) = sub.pending_limit_bytes { |
| 534 |
slot = slot.with(Node::text(format!( |
| 535 |
"Pending: cap changes to {} at next renewal.", |
| 536 |
gib_of(pending) |
| 537 |
))); |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
let default = if nearly_full(sub) { |
| 546 |
proposed_cap(sync.synced_library_bytes(), pricing).max(sub.limit_bytes) |
| 547 |
} else { |
| 548 |
sub.limit_bytes |
| 549 |
}; |
| 550 |
|
| 551 |
Node::Region( |
| 552 |
slot.with(Node::Form { |
| 553 |
fields: vec![cap_choice(sync, pricing, default)], |
| 554 |
submit: "Update cap".to_owned(), |
| 555 |
action: Action::post("/sync/cap"), |
| 556 |
}) |
| 557 |
.with(exact_cap_form(pricing, default, "/sync/cap")), |
| 558 |
) |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
fn nearly_full(sub: &Subscription) -> bool { |
| 566 |
crate::storage_cap::nearly_full(sub.used_bytes, sub.limit_bytes) |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
fn cap_warning(sync: &dyn Sync, sub: &Subscription, pricing: &super::Pricing) -> Option<String> { |
| 576 |
if !nearly_full(sub) { |
| 577 |
return None; |
| 578 |
} |
| 579 |
|
| 580 |
let annual = sub.interval == "annual"; |
| 581 |
let cadence = if annual { "a year" } else { "a month" }; |
| 582 |
let suggestion = |bytes: i64| { |
| 583 |
format!( |
| 584 |
" {} would hold it, at {} {cadence}.", |
| 585 |
gib_of(bytes), |
| 586 |
money(sync.quote_cents(bytes, annual)) |
| 587 |
) |
| 588 |
}; |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
let bigger = sync |
| 594 |
.synced_library_bytes() |
| 595 |
.map(|need| proposed_cap(Some(need), pricing)) |
| 596 |
.filter(|proposed| *proposed > sub.limit_bytes) |
| 597 |
.map_or_else(String::new, suggestion); |
| 598 |
|
| 599 |
Some(if sub.used_bytes >= sub.limit_bytes { |
| 600 |
format!( |
| 601 |
"Your storage cap is full. New sample files are not uploading; \ |
| 602 |
everything else still syncs.{bigger}" |
| 603 |
) |
| 604 |
} else { |
| 605 |
format!( |
| 606 |
"You are close to your storage cap. When it fills, new sample files \ |
| 607 |
stop uploading and everything else keeps syncing.{bigger}" |
| 608 |
) |
| 609 |
}) |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
fn on_offer(sync: &dyn Sync, pricing: &super::Pricing) -> Node { |
| 625 |
let need = sync.synced_library_bytes(); |
| 626 |
let proposed = proposed_cap(need, pricing); |
| 627 |
|
| 628 |
let mut slot = Slot::new("subscription", RegionKind::Pane); |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
slot = slot.with(Node::text(match need { |
| 633 |
Some(0) => "No vault is set to sync sample files yet, so nothing would upload today. \ |
| 634 |
Turn on file sync for a vault to change that." |
| 635 |
.to_owned(), |
| 636 |
Some(bytes) => format!( |
| 637 |
"Your synced vaults hold {}. That is what would upload.", |
| 638 |
gib_of_exact(bytes) |
| 639 |
), |
| 640 |
None => "Pick a storage cap for audio file sync.".to_owned(), |
| 641 |
})); |
| 642 |
|
| 643 |
if need.is_some_and(|bytes| bytes > 0) { |
| 644 |
slot = slot.with(Node::text(format!( |
| 645 |
"Proposed: {}, which leaves room to grow.", |
| 646 |
gib_of(proposed) |
| 647 |
))); |
| 648 |
} |
| 649 |
|
| 650 |
slot = slot.with(Node::text( |
| 651 |
"Annual is two months free: fewer Stripe fees, and we pass the savings on.", |
| 652 |
)); |
| 653 |
|
| 654 |
Node::Region( |
| 655 |
slot.with(Node::Form { |
| 656 |
fields: vec![ |
| 657 |
cap_choice(sync, pricing, proposed), |
| 658 |
Field::radio( |
| 659 |
"cadence", |
| 660 |
"Billing", |
| 661 |
vec![ |
| 662 |
Choice::new("annual", "Annual"), |
| 663 |
Choice::new("monthly", "Monthly"), |
| 664 |
], |
| 665 |
) |
| 666 |
.value("annual"), |
| 667 |
], |
| 668 |
submit: "Subscribe".to_owned(), |
| 669 |
action: Action::post("/sync/subscribe"), |
| 670 |
}) |
| 671 |
.with(exact_cap_form(pricing, proposed, "/sync/subscribe")), |
| 672 |
) |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
fn cap_choice(sync: &dyn Sync, pricing: &super::Pricing, proposed: i64) -> Field { |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
let mut sizes: Vec<i64> = offered_caps(pricing).collect(); |
| 694 |
if !sizes.contains(&proposed) { |
| 695 |
sizes.push(proposed); |
| 696 |
sizes.sort_unstable(); |
| 697 |
} |
| 698 |
|
| 699 |
let options = sizes |
| 700 |
.into_iter() |
| 701 |
.map(|bytes| { |
| 702 |
Choice::new( |
| 703 |
gib_count(bytes).to_string(), |
| 704 |
format!( |
| 705 |
"{} - {} a month, or {} a year", |
| 706 |
gib_of(bytes), |
| 707 |
money(sync.quote_cents(bytes, false)), |
| 708 |
money(sync.quote_cents(bytes, true)) |
| 709 |
), |
| 710 |
) |
| 711 |
}) |
| 712 |
.collect(); |
| 713 |
|
| 714 |
Field::radio(CAP, "Storage cap", options).value(gib_count(proposed).to_string()) |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
fn exact_cap_form(pricing: &super::Pricing, proposed: i64, action: &str) -> Node { |
| 729 |
let field = Field { |
| 730 |
min: Some(gib_count(pricing.min_bytes).to_string()), |
| 731 |
max: Some(gib_count(pricing.max_bytes).to_string()), |
| 732 |
unit: Some("GiB".to_owned()), |
| 733 |
..Field::new(FieldKind::Number, CAP, "Storage cap") |
| 734 |
} |
| 735 |
.value(gib_count(proposed).to_string()) |
| 736 |
.required() |
| 737 |
.hint(format!( |
| 738 |
"Anything from {} to {}.", |
| 739 |
gib_of(pricing.min_bytes), |
| 740 |
gib_of(pricing.max_bytes) |
| 741 |
)); |
| 742 |
|
| 743 |
Node::Region( |
| 744 |
Slot::new("exact-cap", RegionKind::Pane) |
| 745 |
.with(Node::text("Or set an exact cap.")) |
| 746 |
.with(Node::Form { |
| 747 |
fields: vec![field], |
| 748 |
submit: "Use this cap".to_owned(), |
| 749 |
action: Action::post(action), |
| 750 |
}), |
| 751 |
) |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
fn offered_caps(pricing: &super::Pricing) -> impl Iterator<Item = i64> { |
| 759 |
crate::storage_cap::offered(pricing.min_bytes, pricing.max_bytes) |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
fn proposed_cap(need: Option<i64>, pricing: &super::Pricing) -> i64 { |
| 764 |
crate::storage_cap::proposed(need, pricing.min_bytes, pricing.max_bytes) |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
fn gib_count(bytes: i64) -> u32 { |
| 769 |
u32::try_from(bytes / GIB).unwrap_or(u32::MAX) |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
fn gib_of_exact(bytes: i64) -> String { |
| 779 |
#[expect( |
| 780 |
clippy::cast_precision_loss, |
| 781 |
reason = "a library size in GiB is far inside f64's exact integer range" |
| 782 |
)] |
| 783 |
let gib = bytes as f64 / GIB as f64; |
| 784 |
if gib >= 1024.0 { |
| 785 |
format!("{:.1} TiB", gib / 1024.0) |
| 786 |
} else if gib >= 10.0 { |
| 787 |
format!("{gib:.0} GiB") |
| 788 |
} else { |
| 789 |
format!("{gib:.1} GiB") |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
fn gib_of(bytes: i64) -> String { |
| 795 |
let gib = bytes / GIB; |
| 796 |
if gib >= 1024 { |
| 797 |
#[expect( |
| 798 |
clippy::cast_precision_loss, |
| 799 |
reason = "a cap in TiB is small enough that f64 is exact here" |
| 800 |
)] |
| 801 |
let tib = gib as f64 / 1024.0; |
| 802 |
format!("{tib:.1} TiB") |
| 803 |
} else { |
| 804 |
format!("{gib} GiB") |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
fn money(cents: i64) -> String { |
| 810 |
let dollars = cents / 100; |
| 811 |
let pennies = cents % 100; |
| 812 |
if pennies == 0 { |
| 813 |
format!("${dollars}") |
| 814 |
} else { |
| 815 |
format!("${dollars}.{pennies:02}") |
| 816 |
} |
| 817 |
} |
| 818 |
|