defmodule MusicLibraryWeb.ArtistLive.Show do use MusicLibraryWeb, :live_view alias MusicLibrary.{Artists, Records} import MusicLibraryWeb.RecordComponents, only: [record_grid: 1] @impl true def mount(_params, _session, socket) do {:ok, socket} end @impl true def handle_params(%{"musicbrainz_id" => musicbrainz_id}, _, socket) do artist = Artists.get_artist!(musicbrainz_id) %{collection: collection_records, wishlist: wishlist_records} = musicbrainz_id |> Records.get_artist_records() |> group_and_sort() {:noreply, socket |> assign(:nav_section, :artists) |> assign(:artist, artist) |> stream(:collection_records, collection_records, reset: true) |> stream(:wishlist_records, wishlist_records, reset: true) |> assign(:collection_records_count, Enum.count(collection_records)) |> assign(:wishlist_records_count, Enum.count(wishlist_records)) |> assign_async(:artist_info, fn -> with {:ok, artist_info} <- LastFm.get_artist_info(artist.musicbrainz_id, artist.name) do {:ok, %{artist_info: artist_info}} end end) |> assign_async(:similar_artists, fn -> with {:ok, similar_artists} <- Artists.get_similar_artists(artist) do {:ok, %{similar_artists: similar_artists}} end end) |> assign(:page_title, page_title(socket.assigns.live_action, artist))} end defp page_title(:show, artist) do Enum.join( [ artist.name, "ยท", gettext("Details") ], " " ) end defp group_and_sort(records) do {collection, wishlist} = Enum.split_with(records, fn r -> r.purchased_at end) %{ collection: Enum.sort_by(collection, fn r -> r.release end, :desc), wishlist: Enum.sort_by(wishlist, fn r -> r.release end, :desc) } end # Bios start with text, then a link to read more on Last.fm, followed by a license text. # We split the bio at the read more link in order to render the license separately. @last_fm_link_regex ~r/\.*\s*/ defp render_bio(bio) do case String.split(bio, @last_fm_link_regex, include_captures: true) do [text, link, ""] -> reformatted_bio = Enum.join( [ text, ~s(

#{link}

) ], "" ) PhoenixHTMLHelpers.Format.text_to_html(reformatted_bio, escape: false, attributes: [class: "mt-2 text-sm/7"] ) [text, link, license] -> reformatted_bio = Enum.join( [ text, ~s(

#{link}

), ~s(

#{license}

) ], "" ) PhoenixHTMLHelpers.Format.text_to_html(reformatted_bio, escape: false, attributes: [class: "mt-2 text-sm/7"] ) other -> PhoenixHTMLHelpers.Format.text_to_html(other, escape: false, attributes: [class: "mt-2 text-sm/7"] ) end end end