You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
3.0 KiB
Rust
91 lines
3.0 KiB
Rust
//! Session storage helpers for persisting quote auth codes within a browser tab.
|
|
//!
|
|
//! Auth codes are stored in `sessionStorage` under the key `auth_code_{quote_id}`.
|
|
//! Session storage is tab-scoped and cleared when the tab closes, making it
|
|
//! appropriate for short-lived auth code persistence.
|
|
//!
|
|
//! This module must only be used from `wasm32-unknown-unknown` targets (the UI binary).
|
|
|
|
/// Persist an auth code for a quote in `sessionStorage`.
|
|
///
|
|
/// The key format is `auth_code_{quote_id}`. Silently no-ops if the browser
|
|
/// `sessionStorage` API is unavailable.
|
|
///
|
|
/// # Arguments
|
|
/// - `quote_id` — The quote's unique ID.
|
|
/// - `auth_code` — The auth code to store.
|
|
pub fn set_auth_code(quote_id: &str, auth_code: &str) {
|
|
if let Some(storage) = web_sys::window()
|
|
.and_then(|w| w.session_storage().ok())
|
|
.flatten()
|
|
{
|
|
let _ = storage.set_item(&format!("auth_code_{quote_id}"), auth_code);
|
|
}
|
|
}
|
|
|
|
/// Retrieve the stored auth code for a quote from `sessionStorage`.
|
|
///
|
|
/// Returns `None` if no code is stored for this quote ID or if `sessionStorage`
|
|
/// is unavailable.
|
|
///
|
|
/// # Arguments
|
|
/// - `quote_id` — The quote's unique ID.
|
|
pub fn get_auth_code(quote_id: &str) -> Option<String> {
|
|
web_sys::window()
|
|
.and_then(|w| w.session_storage().ok())
|
|
.flatten()
|
|
.and_then(|s| s.get_item(&format!("auth_code_{quote_id}")).ok())
|
|
.flatten()
|
|
}
|
|
|
|
/// Remove the stored auth code for a quote from `sessionStorage`.
|
|
///
|
|
/// Silently no-ops if no code is stored or if `sessionStorage` is unavailable.
|
|
///
|
|
/// # Arguments
|
|
/// - `quote_id` — The quote's unique ID.
|
|
pub fn clear_auth_code(quote_id: &str) {
|
|
if let Some(storage) = web_sys::window()
|
|
.and_then(|w| w.session_storage().ok())
|
|
.flatten()
|
|
{
|
|
let _ = storage.remove_item(&format!("auth_code_{quote_id}"));
|
|
}
|
|
}
|
|
|
|
/// Mark a quote as newly created so the quote detail page can show a one-time auth code notice.
|
|
///
|
|
/// Sets `new_quote_{quote_id}` in `sessionStorage`.
|
|
///
|
|
/// # Arguments
|
|
/// - `quote_id` — The quote's unique ID.
|
|
pub fn mark_new_quote(quote_id: &str) {
|
|
if let Some(storage) = web_sys::window()
|
|
.and_then(|w| w.session_storage().ok())
|
|
.flatten()
|
|
{
|
|
let _ = storage.set_item(&format!("new_quote_{quote_id}"), "1");
|
|
}
|
|
}
|
|
|
|
/// Check whether this quote was just created and consume the marker.
|
|
///
|
|
/// Returns `true` and removes the `new_quote_{quote_id}` key if it was present.
|
|
/// This is a one-shot read — a second call for the same ID always returns `false`.
|
|
///
|
|
/// # Arguments
|
|
/// - `quote_id` — The quote's unique ID.
|
|
pub fn take_new_quote(quote_id: &str) -> bool {
|
|
if let Some(storage) = web_sys::window()
|
|
.and_then(|w| w.session_storage().ok())
|
|
.flatten()
|
|
{
|
|
let key = format!("new_quote_{quote_id}");
|
|
if storage.get_item(&key).ok().flatten().is_some() {
|
|
let _ = storage.remove_item(&key);
|
|
return true;
|
|
}
|
|
}
|
|
false
|
|
}
|