Extract shared badge/dropdown to ScrobbleComponents

This commit is contained in:
Claudio Ortolina
2026-04-14 16:50:40 +01:00
parent 888b5a68e6
commit 8db324d74a
4 changed files with 13 additions and 75 deletions
@@ -1,6 +1,7 @@
defmodule MusicLibraryWeb.ScrobbleComponents do
@moduledoc """
Universal search modal and related components.
Scrobble activity display components: status badges, import dropdowns,
metadata tooltips, and record-matching UI.
"""
alias LastFm.Track
@@ -153,7 +154,7 @@ defmodule MusicLibraryWeb.ScrobbleComponents do
attr :record, :map, required: true
defp record_dropdown_link(assigns) do
def record_dropdown_link(assigns) do
path =
if assigns.record.purchased_at,
do: ~p"/collection/#{assigns.record.id}",
@@ -171,9 +172,7 @@ defmodule MusicLibraryWeb.ScrobbleComponents do
{gettext("W")}
</.badge>
<span>
{format_label(String.to_existing_atom(@record.format))} · {type_label(
String.to_existing_atom(@record.type)
)}
{format_label(@record.format)} · {type_label(@record.type)}
<span :if={@record.purchased_at} class="text-zinc-500 dark:text-zinc-400">
· {Records.Record.format_as_date(@record.purchased_at)}
</span>
@@ -183,9 +182,9 @@ defmodule MusicLibraryWeb.ScrobbleComponents do
"""
end
defp badge_status([]), do: nil
def badge_status([]), do: nil
defp badge_status(records) do
def badge_status(records) do
all_collected = Enum.all?(records, & &1.purchased_at)
all_wishlisted = Enum.all?(records, &is_nil(&1.purchased_at))
@@ -196,15 +195,15 @@ defmodule MusicLibraryWeb.ScrobbleComponents do
end
end
defp badge_classes(:collected),
def badge_classes(:collected),
do:
"bg-emerald-50 text-emerald-700 ring-1 ring-emerald-600/20 dark:bg-emerald-400/10 dark:text-emerald-400 dark:ring-emerald-400/20"
defp badge_classes(:wishlisted),
def badge_classes(:wishlisted),
do:
"bg-yellow-50 text-yellow-800 ring-1 ring-yellow-600/20 dark:bg-yellow-400/10 dark:text-yellow-500 dark:ring-yellow-400/20"
defp badge_classes(:mixed),
def badge_classes(:mixed),
do:
"bg-yellow-50 text-emerald-700 ring-1 ring-emerald-600/40 dark:bg-yellow-400/10 dark:text-emerald-400 dark:ring-emerald-400/40"
@@ -1,11 +1,11 @@
defmodule MusicLibraryWeb.StatsLive.TopAlbums do
use MusicLibraryWeb, :html
import MusicLibraryWeb.RecordComponents, only: [format_label: 1, type_label: 1]
import MusicLibraryWeb.ScrobbleComponents,
only: [record_dropdown_link: 1, badge_status: 1, badge_classes: 1]
alias MusicLibrary.Assets.Transform
alias MusicLibrary.ListeningStats
alias MusicLibrary.Records
alias MusicLibraryWeb.StatsLive.TopByPeriod
attr :id, :string, required: true
@@ -79,12 +79,12 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbums do
<:toggle>
<span class={[
"inline-flex items-center rounded-md px-2 py-0.5 text-xs font-medium cursor-pointer",
play_count_badge_classes(status)
badge_classes(status)
]}>
{@album.play_count}
</span>
</:toggle>
<.top_album_dropdown_link
<.record_dropdown_link
:for={record <- @album.matching_records}
record={record}
/>
@@ -93,38 +93,6 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbums do
"""
end
attr :record, :map, required: true
defp top_album_dropdown_link(assigns) do
path =
if assigns.record.purchased_at,
do: ~p"/collection/#{assigns.record.id}",
else: ~p"/wishlist/#{assigns.record.id}"
assigns = assign(assigns, :path, path)
~H"""
<.dropdown_link navigate={@path}>
<span class="flex items-center gap-2">
<.badge :if={@record.purchased_at} color="success" size="sm">
{gettext("C")}
</.badge>
<.badge :if={!@record.purchased_at} color="warning" size="sm">
{gettext("W")}
</.badge>
<span>
{format_label(String.to_existing_atom(@record.format))} · {type_label(
String.to_existing_atom(@record.type)
)}
<span :if={@record.purchased_at} class="text-zinc-500 dark:text-zinc-400">
· {Records.Record.format_as_date(@record.purchased_at)}
</span>
</span>
</span>
</.dropdown_link>
"""
end
defp navigate_to_record(%{matching_records: [record]}) do
if record.purchased_at do
JS.navigate(~p"/collection/#{record.id}")
@@ -138,31 +106,6 @@ defmodule MusicLibraryWeb.StatsLive.TopAlbums do
defp navigable?(%{matching_records: [_]}), do: true
defp navigable?(_), do: false
defp badge_status([]), do: nil
defp badge_status(records) do
all_collected = Enum.all?(records, & &1.purchased_at)
all_wishlisted = Enum.all?(records, &is_nil(&1.purchased_at))
cond do
all_collected -> :collected
all_wishlisted -> :wishlisted
true -> :mixed
end
end
defp play_count_badge_classes(:collected),
do:
"bg-emerald-50 text-emerald-700 ring-1 ring-emerald-600/20 dark:bg-emerald-400/10 dark:text-emerald-400 dark:ring-emerald-400/20"
defp play_count_badge_classes(:wishlisted),
do:
"bg-yellow-50 text-yellow-800 ring-1 ring-yellow-600/20 dark:bg-yellow-400/10 dark:text-yellow-500 dark:ring-yellow-400/20"
defp play_count_badge_classes(:mixed),
do:
"bg-yellow-50 text-emerald-700 ring-1 ring-emerald-600/40 dark:bg-yellow-400/10 dark:text-emerald-400 dark:ring-emerald-400/40"
defp cover_url(album) when is_nil(album.cover_hash) do
album.cover_url
end
-2
View File
@@ -2424,13 +2424,11 @@ msgid "Chat"
msgstr ""
#: lib/music_library_web/components/scrobble_components.ex
#: lib/music_library_web/live/stats_live/top_albums.ex
#, elixir-autogen, elixir-format
msgid "C"
msgstr ""
#: lib/music_library_web/components/scrobble_components.ex
#: lib/music_library_web/live/stats_live/top_albums.ex
#, elixir-autogen, elixir-format
msgid "W"
msgstr ""
-2
View File
@@ -2424,13 +2424,11 @@ msgid "Chat"
msgstr ""
#: lib/music_library_web/components/scrobble_components.ex
#: lib/music_library_web/live/stats_live/top_albums.ex
#, elixir-autogen, elixir-format, fuzzy
msgid "C"
msgstr ""
#: lib/music_library_web/components/scrobble_components.ex
#: lib/music_library_web/live/stats_live/top_albums.ex
#, elixir-autogen, elixir-format
msgid "W"
msgstr ""