From 82e9d3c5a7ea424421804ea1bc5c0e5570e0d8c7 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Sun, 29 Jun 2025 08:49:12 +0100 Subject: [PATCH] Extract markup from stats live to separate set of components --- .../components/stats_components.ex | 206 ++++++++++++++++++ .../live/stats_live/index.ex | 204 +---------------- priv/gettext/default.pot | 2 +- priv/gettext/en/LC_MESSAGES/default.po | 2 +- 4 files changed, 209 insertions(+), 205 deletions(-) create mode 100644 lib/music_library_web/components/stats_components.ex diff --git a/lib/music_library_web/components/stats_components.ex b/lib/music_library_web/components/stats_components.ex new file mode 100644 index 00000000..ac76e17c --- /dev/null +++ b/lib/music_library_web/components/stats_components.ex @@ -0,0 +1,206 @@ +defmodule MusicLibraryWeb.StatsComponents do + use MusicLibraryWeb, :live_component + + attr :record, MusicLibrary.Records.Record, required: true + attr :title, :string, required: true + attr :class, :string + + def album_preview(assigns) do + ~H""" +
+
+ {@record.title} +

+ {@title} +

+
+
+

+ + {@record.title} + + <.link + :for={artist <- @record.artists} + class="text-sm md:text-base text-zinc-600 dark:text-zinc-200 hover:text-zinc-500 dark:text-zinc-300 dark:hover:text-zinc-200" + navigate={~p"/artists/#{artist.musicbrainz_id}"} + > + {artist.name} + +

+
+
+ """ + end + + attr :title, :string, required: true + attr :count, :integer, required: true + attr :path, :string, required: true + + def counter(assigns) do + ~H""" +
+
+

+ {@title} +

+
+
+ <.link + navigate={@path} + class="block text-2xl sm:text-3xl font-semibold text-center text-zinc-900 hover:text-zinc-500 dark:text-zinc-300 dark:hover:text-zinc-200" + > + {@count} + +
+
+ """ + end + + attr :albums, :list, required: true + attr :collected_releases, :list, required: true + attr :wishlisted_releases, :list, required: true + + def top_albums_by_period(assigns) do + ~H""" +
+
+
+ {album.album_title} +
+ <.link + class="text-xs text-zinc-700 hover:text-zinc-500 dark:text-zinc-400 dark:hover:text-zinc-300 truncate" + navigate={~p"/artists/#{album.artist_musicbrainz_id}"} + > + {album.artist_name} + +

+ {album.album_title} +

+
+ <.badge :if={album.album_musicbrainz_id == ""}> + {album.play_count} + + <.badge :if={ + album.album_musicbrainz_id !== "" and + !tracked_record?( + @collected_releases ++ @wishlisted_releases, + album.album_musicbrainz_id + ) + }> + {album.play_count} + + <.badge :if={tracked_record?(@collected_releases, album.album_musicbrainz_id)} color="green"> + {album.play_count} + + <.badge + :if={tracked_record?(@wishlisted_releases, album.album_musicbrainz_id)} + color="yellow" + > + {album.play_count} + +
+
+
+ """ + end + + attr :artists, :list, required: true + + def top_artists_by_period(assigns) do + ~H""" +
+
+
+ {artist.artist_name} ~p"/images/cover-not-found.png" <> "';"} + /> +
+ <.icon name="hero-user" class="w-6 h-6 text-zinc-400" /> +
+
+ <.link + :if={artist.artist_musicbrainz_id != ""} + class="text-sm font-medium text-zinc-900 dark:text-zinc-300 hover:text-zinc-700 dark:hover:text-zinc-400 truncate" + navigate={~p"/artists/#{artist.artist_musicbrainz_id}"} + > + {artist.artist_name} + +

+ {artist.artist_name} +

+
+ <.badge> + {artist.play_count} + +
+
+
+ """ + end + + def refresh_lastfm_feed_button(assigns) do + ~H""" + + """ + end + + def tracked_record?(tracked_releases, release_id) do + Enum.find_value(tracked_releases, fn tracked_release -> + if tracked_release.release_id == release_id, do: tracked_release.record_id + end) + end + + defp navigate_to_record(collected_releases, wishlisted_releases, musicbrainz_id) do + cond do + record_id = tracked_record?(collected_releases, musicbrainz_id) -> + JS.navigate(~p"/collection/#{record_id}") + + record_id = tracked_record?(wishlisted_releases, musicbrainz_id) -> + JS.navigate(~p"/wishlist/#{record_id}") + + true -> + nil + end + end +end diff --git a/lib/music_library_web/live/stats_live/index.ex b/lib/music_library_web/live/stats_live/index.ex index 9604e28b..4428f55c 100644 --- a/lib/music_library_web/live/stats_live/index.ex +++ b/lib/music_library_web/live/stats_live/index.ex @@ -3,206 +3,10 @@ defmodule MusicLibraryWeb.StatsLive.Index do import MusicLibraryWeb.ChartComponents import MusicLibraryWeb.RecordComponents, only: [format_label: 1, type_label: 1] + import MusicLibraryWeb.StatsComponents alias MusicLibrary.{Collection, Records, ScrobbleActivity, Wishlist} - attr :record, MusicLibrary.Records.Record, required: true - attr :title, :string, required: true - attr :class, :string - - defp album_preview(assigns) do - ~H""" -
-
- {@record.title} -

- {@title} -

-
-
-

- - {@record.title} - - <.link - :for={artist <- @record.artists} - class="text-sm md:text-base text-zinc-600 dark:text-zinc-200 hover:text-zinc-500 dark:text-zinc-300 dark:hover:text-zinc-200" - navigate={~p"/artists/#{artist.musicbrainz_id}"} - > - {artist.name} - -

-
-
- """ - end - - attr :title, :string, required: true - attr :count, :integer, required: true - attr :path, :string, required: true - - defp counter(assigns) do - ~H""" -
-
-

- {@title} -

-
-
- <.link - navigate={@path} - class="block text-2xl sm:text-3xl font-semibold text-center text-zinc-900 hover:text-zinc-500 dark:text-zinc-300 dark:hover:text-zinc-200" - > - {@count} - -
-
- """ - end - - defp navigate_to_record(collected_releases, wishlisted_releases, musicbrainz_id) do - cond do - record_id = tracked_record?(collected_releases, musicbrainz_id) -> - JS.navigate(~p"/collection/#{record_id}") - - record_id = tracked_record?(wishlisted_releases, musicbrainz_id) -> - JS.navigate(~p"/wishlist/#{record_id}") - - true -> - nil - end - end - - attr :albums, :list, required: true - attr :collected_releases, :list, required: true - attr :wishlisted_releases, :list, required: true - - def top_albums_by_period(assigns) do - ~H""" -
-
-
- {album.album_title} -
- <.link - class="text-xs text-zinc-700 hover:text-zinc-500 dark:text-zinc-400 dark:hover:text-zinc-300 truncate" - navigate={~p"/artists/#{album.artist_musicbrainz_id}"} - > - {album.artist_name} - -

- {album.album_title} -

-
- <.badge :if={album.album_musicbrainz_id == ""}> - {album.play_count} - - <.badge :if={ - album.album_musicbrainz_id !== "" and - !tracked_record?( - @collected_releases ++ @wishlisted_releases, - album.album_musicbrainz_id - ) - }> - {album.play_count} - - <.badge :if={tracked_record?(@collected_releases, album.album_musicbrainz_id)} color="green"> - {album.play_count} - - <.badge - :if={tracked_record?(@wishlisted_releases, album.album_musicbrainz_id)} - color="yellow" - > - {album.play_count} - -
-
-
- """ - end - - attr :artists, :list, required: true - - def top_artists_by_period(assigns) do - ~H""" -
-
-
- {artist.artist_name} ~p"/images/cover-not-found.png" <> "';"} - /> -
- <.icon name="hero-user" class="w-6 h-6 text-zinc-400" /> -
-
- <.link - :if={artist.artist_musicbrainz_id != ""} - class="text-sm font-medium text-zinc-900 dark:text-zinc-300 hover:text-zinc-700 dark:hover:text-zinc-400 truncate" - navigate={~p"/artists/#{artist.artist_musicbrainz_id}"} - > - {artist.artist_name} - -

- {artist.artist_name} -

-
- <.badge> - {artist.play_count} - -
-
-
- """ - end - - defp refresh_lastfm_feed_button(assigns) do - ~H""" - - """ - end - def mount(_params, _session, socket) do latest_record = Collection.get_latest_record!() recent_tracks = LastFm.get_scrobbled_tracks() @@ -356,12 +160,6 @@ defmodule MusicLibraryWeb.StatsLive.Index do assign(socket, top_artists: top_artists) end - defp tracked_record?(tracked_releases, release_id) do - Enum.find_value(tracked_releases, fn tracked_release -> - if tracked_release.release_id == release_id, do: tracked_release.record_id - end) - end - # The Tailwind build step requires all needed classes to be explicitly referenced # in the source code, and not dynamically generated. This implies that one cannot # (for example) interpolate a number in a class name. diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index d9d1bb76..02c42f88 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -281,7 +281,7 @@ msgstr "" msgid "MusicBrainz data refreshed successfully" msgstr "" -#: lib/music_library_web/live/stats_live/index.ex +#: lib/music_library_web/components/stats_components.ex #, elixir-autogen, elixir-format msgid "Refresh LastFm Feed" msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index 4974228d..367e3afc 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -281,7 +281,7 @@ msgstr "" msgid "MusicBrainz data refreshed successfully" msgstr "" -#: lib/music_library_web/live/stats_live/index.ex +#: lib/music_library_web/components/stats_components.ex #, elixir-autogen, elixir-format msgid "Refresh LastFm Feed" msgstr ""