defmodule MusicLibraryWeb.StatsComponents do
use MusicLibraryWeb, :live_component
import MusicLibraryWeb.RecordComponents,
only: [
format_label: 1,
type_label: 1,
artist_links: 1,
record_cover: 1,
release_groups_badge: 1,
release_status_tooltip: 1
]
alias MusicLibrary.Records
attr :record, Records.Record, required: false
attr :title, :string, required: true
attr :class, :string
def album_preview(assigns) do
~H"""
<.record_cover
record={@record}
class="w-20 rounded-md shadow-sm md:w-24"
width={192}
/>
{@title}
{@record.title}
<.artist_links artists={@record.artists} joinphrase_class="text-sm md:text-base" />
<.link
:if={!@record}
navigate={~p"/collection/import"}
class={[
"flex items-center rounded-md bg-white px-4 py-5 shadow-sm sm:px-6 sm:pt-6 dark:bg-zinc-800",
"border border-dashed border-zinc-300 dark:border-zinc-600",
"group transition-colors hover:border-zinc-400 dark:hover:border-zinc-500",
@class
]}
>
<.icon
name="hero-plus-circle"
class="size-8 text-zinc-400 group-hover:text-zinc-500 dark:text-zinc-500 dark:group-hover:text-zinc-400"
/>
{gettext("Add a new record")}
"""
end
attr :title, :string, required: true
attr :count, :integer, required: true
attr :path, :string, required: false, default: nil
attr :tooltip, :any, required: false, default: nil
def counter(assigns) do
~H"""
{@title}
<.link
:if={@path}
navigate={@path}
class="block text-center text-2xl font-semibold text-zinc-900 hover:text-zinc-500 sm:text-3xl dark:text-zinc-300 dark:hover:text-zinc-200"
>
{@count}
{@count}
<.tooltip>
<:content>
{@tooltip}
<.link
:if={@path}
navigate={@path}
class="block text-center text-2xl font-semibold text-zinc-900 hover:text-zinc-500 sm:text-3xl dark:text-zinc-300 dark:hover:text-zinc-200"
>
{@count}
{@count}
"""
end
attr :categories_with_counts, :list, required: true
attr :category_format_fn, :any, required: true
attr :category_path_fn, :any, required: true
def counters_by_category(assigns) do
~H"""
-
{@category_format_fn.(category)}
-
<.link
class="text-xl font-semibold hover:text-zinc-500 lg:text-2xl dark:text-zinc-300 dark:hover:text-zinc-200"
navigate={@category_path_fn.(category)}
>
{count}
"""
end
attr :record_show_path, :any, required: true
attr :records, :list, required: true
attr :current_date, Date, required: true
def records_on_this_day(assigns) do
~H"""
-
{gettext("No records released on this day.")}
<%= for entry <- @records do %>
<%= case entry do %>
<% {:single, record} -> %>
<.record_on_this_day_item
record={record}
current_date={@current_date}
record_show_path={@record_show_path}
/>
<% {:group, %{representative: rep, records: records}} -> %>
-
<.record_cover record={rep} width={96} />
<.release_groups_badge record={rep} />
<.artist_links joinphrase_class="text-xs" artists={rep.artists} />
{rep.title}
<.released_how_long_ago record={rep} current_date={@current_date} />
· {ngettext("1 release", "%{count} releases", length(records))}
<.icon
name="hero-chevron-right"
class="size-4 text-zinc-400 transition-transform group-open/details:rotate-90"
/>
-
<.record_cover record={record} width={48} class="rounded-sm" />
<.release_status_tooltip record={record} />
{format_label(record.format)} · {type_label(record.type)}
·
{gettext("Purchased on")}
<.icon
name="hero-banknotes"
class="size-4"
aria-hidden="true"
data-slot="icon"
/>
{Records.Record.format_as_date(record.purchased_at)}
<% end %>
<% end %>
"""
end
attr :record, Records.Record, required: true
attr :current_date, Date, required: true
attr :record_show_path, :any, required: true
defp record_on_this_day_item(assigns) do
~H"""
<.record_cover record={@record} width={96} />
<.release_groups_badge record={@record} />
<.artist_links joinphrase_class="text-xs" artists={@record.artists} />
{@record.title}
<.release_status_tooltip record={@record} />
<.released_how_long_ago record={@record} current_date={@current_date} />
· {format_label(@record.format)} · {type_label(@record.type)}
·
{gettext("Purchased on")}
<.icon
name="hero-banknotes"
class="size-4"
aria-hidden="true"
data-slot="icon"
/>
{Records.Record.format_as_date(@record.purchased_at)}
"""
end
attr :record, Records.Record, required: true
attr :current_date, Date, required: true
defp released_how_long_ago(assigns) do
assigns =
assign(
assigns,
:years,
Records.Record.released_how_long_ago?(assigns.record, assigns.current_date)
)
~H"""
{gettext("Today")}
{ngettext(
"1 year ago",
"%{count} years ago",
@years
)}
"""
end
defp same_year?(year), do: year == 0
defp gold_year?(year), do: rem(year, 10) == 0
defp silver_year?(year), do: rem(year, 5) == 0
defp normal_year?(year), do: !gold_year?(year) && !silver_year?(year)
# 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.
# credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity
defp stats_class(collection) do
case Enum.count(collection) do
1 -> "grid-cols-1"
2 -> "grid-cols-2"
3 -> "grid-cols-3"
4 -> "grid-cols-4"
5 -> "grid-cols-5"
6 -> "grid-cols-6"
7 -> "grid-cols-7"
8 -> "grid-cols-8"
9 -> "grid-cols-9"
_other -> ""
end
end
attr :container_class, :string, default: nil
slot :title, required: true
slot :side_actions
slot :inner_block, required: true
def section(assigns) do
~H"""
{render_slot(@title)}
{render_slot(@side_actions)}
{render_slot(@inner_block)}
"""
end
end