Show how many years ago for "On this day" records

This commit is contained in:
Claudio Ortolina
2025-08-29 13:09:27 +03:00
parent e54dcc50dc
commit 064ece03f3
7 changed files with 40 additions and 9 deletions
+15
View File
@@ -82,6 +82,21 @@ defmodule MusicLibrary.Records.Record do
end end
end end
def released_how_long_ago?(%{release_date: nil}, _current_day), do: nil
def released_how_long_ago?(record, current_day) do
case Date.from_iso8601(record.release_date) do
{:ok, release_date} ->
# approximate calculation of "how many years ago",
# we don't really care about leap years
diff_days = Date.diff(current_day, release_date)
div(diff_days, 365)
_error ->
nil
end
end
def releases(record) do def releases(record) do
record.musicbrainz_data record.musicbrainz_data
|> ReleaseGroup.releases() |> ReleaseGroup.releases()
@@ -85,7 +85,7 @@ defmodule MusicLibraryWeb.StatsComponents do
attr :record_show_path, :any, required: true attr :record_show_path, :any, required: true
attr :records, :list, required: true attr :records, :list, required: true
attr :current_date, Date, required: false, default: nil attr :current_date, Date, required: true
def records_on_this_day(assigns) do def records_on_this_day(assigns) do
~H""" ~H"""
@@ -129,10 +129,11 @@ defmodule MusicLibraryWeb.StatsComponents do
{record.title} {record.title}
</h2> </h2>
<p class="mt-1 text-xs leading-5 text-zinc-500 dark:text-zinc-400"> <p class="mt-1 text-xs leading-5 text-zinc-500 dark:text-zinc-400">
{Records.Record.format_release_date(record.release_date)} {ngettext(
<span :if={@current_date && !Records.Record.released?(record, @current_date)}> "1 year ago",
({gettext("Unreleased")}) "%{count} years ago",
</span> Records.Record.released_how_long_ago?(record, @current_date)
)}
</p> </p>
<p class="sm:hidden mt-1 text-xs leading-5 text-zinc-500 dark:text-zinc-400"> <p class="sm:hidden mt-1 text-xs leading-5 text-zinc-500 dark:text-zinc-400">
{format_label(record.format)} · {type_label(record.type)} {format_label(record.format)} · {type_label(record.type)}
@@ -9,11 +9,12 @@ defmodule MusicLibraryWeb.StatsLive.Index do
alias MusicLibraryWeb.StatsLive.{TopAlbums, TopArtists} alias MusicLibraryWeb.StatsLive.{TopAlbums, TopArtists}
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
current_date = Date.utc_today()
latest_record = Collection.get_latest_record!() latest_record = Collection.get_latest_record!()
recent_tracks = LastFm.get_scrobbled_tracks() recent_tracks = LastFm.get_scrobbled_tracks()
records_by_artists = Collection.count_records_by_artist(limit: 20) records_by_artists = Collection.count_records_by_artist(limit: 20)
records_by_genre = Collection.count_records_by_genre(limit: 20) records_by_genre = Collection.count_records_by_genre(limit: 20)
records_on_this_day = Collection.get_records_on_this_day() records_on_this_day = Collection.get_records_on_this_day(current_date)
if connected?(socket) do if connected?(socket) do
LastFm.subscribe_to_feed() LastFm.subscribe_to_feed()
@@ -32,6 +33,7 @@ defmodule MusicLibraryWeb.StatsLive.Index do
|> assign_counts() |> assign_counts()
|> assign_scrobble_activity(recent_tracks) |> assign_scrobble_activity(recent_tracks)
|> assign( |> assign(
current_date: current_date,
scrobble_activity_mode: "albums", scrobble_activity_mode: "albums",
latest_record: latest_record, latest_record: latest_record,
page_title: gettext("Stats"), page_title: gettext("Stats"),
@@ -493,6 +493,7 @@
{gettext("On This day")} {gettext("On This day")}
</h1> </h1>
<.records_on_this_day <.records_on_this_day
current_date={@current_date}
records={@streams.records_on_this_day} records={@streams.records_on_this_day}
record_show_path={fn record -> ~p"/collection/#{record}" end} record_show_path={fn record -> ~p"/collection/#{record}" end}
/> />
@@ -17,7 +17,7 @@ defmodule MusicLibraryWeb.WishlistLive.Index do
@impl true @impl true
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
current_date = DateTime.utc_now() |> DateTime.to_date() current_date = Date.utc_today()
{:ok, {:ok,
socket socket
+7 -1
View File
@@ -435,7 +435,6 @@ msgid "ID"
msgstr "" msgstr ""
#: lib/music_library_web/components/record_components.ex #: lib/music_library_web/components/record_components.ex
#: lib/music_library_web/components/stats_components.ex
#: lib/music_library_web/live/wishlist_live/show.html.heex #: lib/music_library_web/live/wishlist_live/show.html.heex
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Unreleased" msgid "Unreleased"
@@ -1303,3 +1302,10 @@ msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "On This day" msgid "On This day"
msgstr "" msgstr ""
#: lib/music_library_web/components/stats_components.ex
#, elixir-autogen, elixir-format
msgid "1 year ago"
msgid_plural "%{count} years ago"
msgstr[0] ""
msgstr[1] ""
+7 -1
View File
@@ -435,7 +435,6 @@ msgid "ID"
msgstr "" msgstr ""
#: lib/music_library_web/components/record_components.ex #: lib/music_library_web/components/record_components.ex
#: lib/music_library_web/components/stats_components.ex
#: lib/music_library_web/live/wishlist_live/show.html.heex #: lib/music_library_web/live/wishlist_live/show.html.heex
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Unreleased" msgid "Unreleased"
@@ -1303,3 +1302,10 @@ msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "On This day" msgid "On This day"
msgstr "" msgstr ""
#: lib/music_library_web/components/stats_components.ex
#, elixir-autogen, elixir-format
msgid "1 year ago"
msgid_plural "%{count} years ago"
msgstr[0] ""
msgstr[1] ""