diff --git a/src/components.rs b/src/components.rs index 4d0c8a5..5f7fff7 100644 --- a/src/components.rs +++ b/src/components.rs @@ -1,4 +1,5 @@ pub mod sidebar; pub mod dashboard; pub mod search; -pub mod personal; \ No newline at end of file +pub mod personal; +pub mod dashboard_tile; diff --git a/src/components/dashboard_tile.rs b/src/components/dashboard_tile.rs new file mode 100644 index 0000000..899981f --- /dev/null +++ b/src/components/dashboard_tile.rs @@ -0,0 +1,69 @@ +use leptos::leptos_dom::*; +use leptos::*; +use serde::{Deserialize, Serialize}; +use crate::media_type::MediaType; + +/// Info representing what will be displayed in a dashboard tile +#[derive(Serialize, Deserialize)] +pub struct DashboardTile { + pub image_path: String, + pub title: String, + pub media_type: Option, + pub artist: Option, +} + +impl DashboardTile { + pub fn new(image_path: String, title: String, media_type: Option, artist: Option) -> Self { + Self { + image_path, + title, + media_type, + artist: artist.map(|artist| artist.to_string()), + } + } + + /// Get the description of the dashboard tile + /// Will display the media type, and the artist if it is available and relevant + pub fn description(&self) -> String { + match self.media_type { + Some(MediaType::Song) => { + if let Some(artist) = &self.artist { + format!("{} • {}", MediaType::Song.to_string(), artist) + } else { + MediaType::Song.to_string() + } + }, + Some(MediaType::Album) => { + if let Some(artist) = &self.artist { + format!("{} • {}", MediaType::Album.to_string(), artist) + } else { + MediaType::Album.to_string() + } + }, + Some(MediaType::Artist) => { + MediaType::Artist.to_string() + }, + None => { + if let Some(artist) = &self.artist { + artist.to_string() + } else { + "".to_string() + } + } + } + } +} + +impl IntoView for DashboardTile { + fn into_view(self) -> View { + let description = self.description(); + + view! { +
+ dashboard-tile +

{self.title}

+

{description}

+
+ }.into_view() + } +}