From 064ece03f3c78b659020f25f7089c91406220e0e Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Fri, 29 Aug 2025 13:09:27 +0300 Subject: [PATCH] Show how many years ago for "On this day" records --- lib/music_library/records/record.ex | 15 +++++++++++++++ .../components/stats_components.ex | 11 ++++++----- lib/music_library_web/live/stats_live/index.ex | 4 +++- .../live/stats_live/index.html.heex | 1 + lib/music_library_web/live/wishlist_live/index.ex | 2 +- priv/gettext/default.pot | 8 +++++++- priv/gettext/en/LC_MESSAGES/default.po | 8 +++++++- 7 files changed, 40 insertions(+), 9 deletions(-) diff --git a/lib/music_library/records/record.ex b/lib/music_library/records/record.ex index bad7f517..0972e9d4 100644 --- a/lib/music_library/records/record.ex +++ b/lib/music_library/records/record.ex @@ -82,6 +82,21 @@ defmodule MusicLibrary.Records.Record do 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 record.musicbrainz_data |> ReleaseGroup.releases() diff --git a/lib/music_library_web/components/stats_components.ex b/lib/music_library_web/components/stats_components.ex index b091454b..01cc11e6 100644 --- a/lib/music_library_web/components/stats_components.ex +++ b/lib/music_library_web/components/stats_components.ex @@ -85,7 +85,7 @@ defmodule MusicLibraryWeb.StatsComponents do attr :record_show_path, :any, 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 ~H""" @@ -129,10 +129,11 @@ defmodule MusicLibraryWeb.StatsComponents do {record.title}

- {Records.Record.format_release_date(record.release_date)} - - ({gettext("Unreleased")}) - + {ngettext( + "1 year ago", + "%{count} years ago", + Records.Record.released_how_long_ago?(record, @current_date) + )}

{format_label(record.format)} ยท {type_label(record.type)} diff --git a/lib/music_library_web/live/stats_live/index.ex b/lib/music_library_web/live/stats_live/index.ex index 33631c0a..8cf02d85 100644 --- a/lib/music_library_web/live/stats_live/index.ex +++ b/lib/music_library_web/live/stats_live/index.ex @@ -9,11 +9,12 @@ defmodule MusicLibraryWeb.StatsLive.Index do alias MusicLibraryWeb.StatsLive.{TopAlbums, TopArtists} def mount(_params, _session, socket) do + current_date = Date.utc_today() latest_record = Collection.get_latest_record!() recent_tracks = LastFm.get_scrobbled_tracks() records_by_artists = Collection.count_records_by_artist(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 LastFm.subscribe_to_feed() @@ -32,6 +33,7 @@ defmodule MusicLibraryWeb.StatsLive.Index do |> assign_counts() |> assign_scrobble_activity(recent_tracks) |> assign( + current_date: current_date, scrobble_activity_mode: "albums", latest_record: latest_record, page_title: gettext("Stats"), diff --git a/lib/music_library_web/live/stats_live/index.html.heex b/lib/music_library_web/live/stats_live/index.html.heex index 15d585db..486ca6f1 100644 --- a/lib/music_library_web/live/stats_live/index.html.heex +++ b/lib/music_library_web/live/stats_live/index.html.heex @@ -493,6 +493,7 @@ {gettext("On This day")} <.records_on_this_day + current_date={@current_date} records={@streams.records_on_this_day} record_show_path={fn record -> ~p"/collection/#{record}" end} /> diff --git a/lib/music_library_web/live/wishlist_live/index.ex b/lib/music_library_web/live/wishlist_live/index.ex index 767c9564..fdb96b5e 100644 --- a/lib/music_library_web/live/wishlist_live/index.ex +++ b/lib/music_library_web/live/wishlist_live/index.ex @@ -17,7 +17,7 @@ defmodule MusicLibraryWeb.WishlistLive.Index do @impl true def mount(_params, _session, socket) do - current_date = DateTime.utc_now() |> DateTime.to_date() + current_date = Date.utc_today() {:ok, socket diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 0cf3f82c..6846ce12 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -435,7 +435,6 @@ msgid "ID" msgstr "" #: 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 #, elixir-autogen, elixir-format msgid "Unreleased" @@ -1303,3 +1302,10 @@ msgstr "" #, elixir-autogen, elixir-format msgid "On This day" 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] "" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index 214a09dd..bdf9adc1 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -435,7 +435,6 @@ msgid "ID" msgstr "" #: 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 #, elixir-autogen, elixir-format msgid "Unreleased" @@ -1303,3 +1302,10 @@ msgstr "" #, elixir-autogen, elixir-format msgid "On This day" 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] ""