lodviz_components 0.3.0

Components for data visualization using lodviz_core
Documentation
use super::draggable_card::CardTransform;
/// Global registry for DraggableCard transforms.
///
/// Each DraggableCard registers its transform with a unique UUID.
/// Child charts can look up their parent card's transform by ID.
use leptos::prelude::*;
use std::collections::HashMap;
use std::sync::LazyLock;

/// Global storage of card transforms by UUID.
/// This provides a reliable way for child components to access their parent card's dimensions
/// without relying on `provide_context`, which has isolation issues between sibling instances.
///
/// Uses std::sync::RwLock instead of RwSignal because this is a static global
/// that doesn't need to be reactive - it's just a lookup table.
pub static CARD_TRANSFORMS: LazyLock<std::sync::RwLock<HashMap<String, CardTransform>>> =
    LazyLock::new(|| std::sync::RwLock::new(HashMap::new()));

/// Register a card's transform in the global registry.
pub fn register_card_transform(id: &str, transform: CardTransform) {
    if let Ok(mut transforms) = CARD_TRANSFORMS.write() {
        transforms.insert(id.to_string(), transform);
    }
}

/// Update a card's transform in the global registry.
pub fn update_card_transform(id: &str, transform: CardTransform) {
    register_card_transform(id, transform); // Same operation
}

/// Look up a card's transform by ID.
pub fn get_card_transform(id: &str) -> Option<CardTransform> {
    CARD_TRANSFORMS.read().ok()?.get(id).copied()
}

/// Remove a card's transform from the registry (cleanup).
pub fn unregister_card_transform(id: &str) {
    if let Ok(mut transforms) = CARD_TRANSFORMS.write() {
        transforms.remove(id);
    }
}

/// Get a reactive Signal for a card's transform by ID.
/// Returns None if the card ID is not registered.
///
/// Note: The returned signal is NOT reactive to updates in the global registry.
/// It captures the value at the time of creation. For true reactivity,
/// consider passing the transform as a prop or using context.
pub fn get_card_transform_signal(id: String) -> Signal<Option<CardTransform>> {
    let value = get_card_transform(&id);
    Signal::stored(value)
}