From d1c99a0a03544ecd95a6c8119144d0b6cc3ff1a7 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Fri, 24 Nov 2023 00:42:50 +0000 Subject: [PATCH] fix nft nulls --- crates/nft/src/asset_source.rs | 19 +++++++---- crates/restricted_actions/src/lib.rs | 48 +++++++++++++++++----------- crates/ui_core/src/dialog.rs | 13 +++----- 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/crates/nft/src/asset_source.rs b/crates/nft/src/asset_source.rs index 205827e5..212f281a 100644 --- a/crates/nft/src/asset_source.rs +++ b/crates/nft/src/asset_source.rs @@ -235,12 +235,12 @@ pub struct NftOwner { #[derive(Asset, TypePath, Deserialize)] pub struct Nft { pub image_url: String, - pub name: String, - pub description: String, - pub permalink: String, - pub creator: NftIdent, + pub name: Option, + pub description: Option, + pub permalink: Option, + pub creator: Option, pub last_sale: Option, - pub top_ownerships: Vec, + pub top_ownerships: Option>, } pub struct NftLoader; @@ -263,8 +263,13 @@ impl AssetLoader for NftLoader { .read_to_end(&mut bytes) .await .map_err(|e| std::io::Error::new(e.kind(), e))?; - serde_json::from_reader(bytes.as_slice()) - .map_err(|e| std::io::Error::new(ErrorKind::InvalidData, e)) + + let res = serde_json::from_reader(bytes.as_slice()) + .map_err(|e| std::io::Error::new(ErrorKind::InvalidData, e)); + if res.is_err() { + debug!("errored nft bytes: {}", String::from_utf8(bytes).unwrap()); + } + res }) } diff --git a/crates/restricted_actions/src/lib.rs b/crates/restricted_actions/src/lib.rs index b7c9454d..06d0799f 100644 --- a/crates/restricted_actions/src/lib.rs +++ b/crates/restricted_actions/src/lib.rs @@ -25,7 +25,7 @@ use dcl_component::proto_components::kernel::comms::rfc4; use ethers_core::types::Address; use ipfs::{ipfs_path::IpfsPath, ChangeRealmEvent, EntityDefinition, ServerAbout}; use isahc::{http::StatusCode, AsyncReadResponseExt}; -use nft::asset_source::Nft; +use nft::asset_source::{Nft, NftIdent}; use scene_runner::{ initialize_scene::{ LiveScenes, PortableScenes, PortableSource, SceneHash, SceneLoading, PARCEL_SIZE, @@ -827,13 +827,18 @@ impl<'a> IntoDialogBody for NftDialog<'a> { ..Default::default() }) .with_children(|c| { - let creator = self.0.creator.get_string(); + let creator = self + .0 + .creator + .as_ref() + .map(NftIdent::get_string) + .unwrap_or("???".to_owned()); c.spawn(TextBundle::from_section( format!("Creator: {creator}"), BODY_TEXT_STYLE.get().unwrap().clone(), )); - if let Some(owner) = self.0.top_ownerships.first() { + if let Some(owner) = self.0.top_ownerships.as_ref().and_then(|v| v.first()) { let owner = owner.owner.get_string(); c.spawn(TextBundle::from_section( format!("Owner: {owner}"), @@ -852,11 +857,9 @@ impl<'a> IntoDialogBody for NftDialog<'a> { BODY_TEXT_STYLE.get().unwrap().clone(), )); - let description: String = if self.0.description.len() < 500 { - self.0.description.clone() - } else { - self.0 - .description + let mut description = self.0.description.clone().unwrap_or("???".to_owned()); + if description.len() > 500 { + description = description .chars() .take(500) .chain(std::iter::repeat('.').take(3)) @@ -884,16 +887,25 @@ fn show_nft_dialog( nft_spawn.response.clone().send(Ok(())); let link = nft.permalink.clone(); - commands.spawn_dialog_two( - nft.name.clone(), - NftDialog(nft, &asset_server), - "Close", - move || {}, - "View on Opensea", - move || { - let _ = opener::open(link.clone()); - }, - ) + if let Some(link) = link { + commands.spawn_dialog_two( + nft.name.clone().unwrap_or("Unnamed Nft".to_owned()), + NftDialog(nft, &asset_server), + "Close", + move || {}, + "View on Opensea", + move || { + let _ = opener::open(link.clone()); + }, + ) + } else { + commands.spawn_dialog( + nft.name.clone().unwrap_or("Unnamed Nft".to_owned()), + NftDialog(nft, &asset_server), + "Close", + move || {}, + ) + } } else if asset_server.load_state(nft_spawn.h_nft.id()) == LoadState::Failed { commands.entity(ent).remove::(); nft_spawn diff --git a/crates/ui_core/src/dialog.rs b/crates/ui_core/src/dialog.rs index 37e430f7..18ccc130 100644 --- a/crates/ui_core/src/dialog.rs +++ b/crates/ui_core/src/dialog.rs @@ -7,10 +7,10 @@ use super::{ }; pub trait SpawnDialog { - fn spawn_dialog( + fn spawn_dialog( &mut self, title: String, - body: String, + body: B, button_one_label: impl Into, button_one_action: impl IntoSystem<(), (), M>, ); @@ -27,10 +27,10 @@ pub trait SpawnDialog { } impl<'w, 's> SpawnDialog for Commands<'w, 's> { - fn spawn_dialog( + fn spawn_dialog( &mut self, title: String, - body: String, + body: B, button_one_label: impl Into, button_one_action: impl IntoSystem<(), (), M>, ) { @@ -90,10 +90,7 @@ impl<'w, 's> SpawnDialog for Commands<'w, 's> { TextBundle::from_section(title, TITLE_TEXT_STYLE.get().unwrap().clone()) .with_text_alignment(TextAlignment::Center), ); - commands.spawn( - TextBundle::from_section(body, BODY_TEXT_STYLE.get().unwrap().clone()) - .with_text_alignment(TextAlignment::Center), - ); + body.body(commands); commands .spawn(NodeBundle { style: Style {