use leptos::*; use leptos_meta::*; use crate::components::{item_form::ItemForm, items_list::ItemsList, review_form::ReviewForm, reviews_list::ReviewsList}; use crate::models::{item::Item, review::Review}; // Ensure Review is imported use crate::nostr::NostrClient; use tokio::sync::mpsc; use uuid::Uuid; use leptos::spawn_local; use nostr_sdk::serde_json; #[component] pub fn App() -> impl IntoView { provide_meta_context(); // Signal to store and update the list of items. let (items_signal, set_items) = create_signal(Vec::::new()); // Signal to store the ID of the current item for reviews let (current_item_id, set_current_item_id) = create_signal(String::new()); // Signal to store reviews for the current item let (reviews_signal, set_reviews) = create_signal(Vec::::new()); let (tx, mut rx) = mpsc::channel::(100); spawn_local(async move { // Initialize Nostr client let nostr_client = NostrClient::new("wss://relay.damus.io").await.unwrap(); nostr_client.subscribe_to_items(tx.clone()).await.unwrap(); // Handle incoming events while let Some(content) = rx.recv().await { if let Ok(item) = serde_json::from_str::(&content) { set_items.update(|items| items.push(item)); } } }); // Function to handle adding a new item to the list. let add_item = move |name: String, description: String, tags: Vec<(String, String)>| { let new_id = Uuid::new_v4().to_string(); // Generate a new UUID for the item set_current_item_id.set(new_id.clone()); // Update the current item ID set_items.update(|items| { let item = Item { id: new_id, name: name.clone(), description: description.clone(), tags: tags.clone(), reviews: vec![], }; items.push(item); }); spawn_local(async move { let nostr_client = NostrClient::new("wss://relay.example.com").await.unwrap(); nostr_client.publish_item(name, description, tags).await.unwrap(); }); }; // Handle review submission let submit_review = { let current_item_id = current_item_id.clone(); // Clone the current item ID let user_id = "some_user_id".to_string(); // Replace with actual user ID logic move |review_content: String| { // Create a new review and add it to the reviews list let new_review = Review { content: review_content.clone(), item_id: current_item_id.get().clone(), // Use the current item ID user_id: user_id.clone(), // Use the user ID }; set_reviews.update(|reviews| reviews.push(new_review)); println!("Review submitted: {}", review_content); println!("Current reviews: {:?}", reviews_signal.get()); } }; view! { <>

{ "CompareWare" }

// Form component for adding new items. // Reviews form // Component to display the list of items. // Component to display the list of reviews for the current item.
} }