From 6f4fe163df408a468b38bea5f35216e942d8cff8 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Tue, 27 Jan 2026 16:16:28 +0000 Subject: [PATCH] Handle toast messages from inside Release component --- lib/music_library_web.ex | 1 + lib/music_library_web/components/release.ex | 64 ++++++++++----------- lib/music_library_web/hooks/show_toast.ex | 18 ++++++ lib/music_library_web/router.ex | 6 +- 4 files changed, 56 insertions(+), 33 deletions(-) create mode 100644 lib/music_library_web/hooks/show_toast.ex diff --git a/lib/music_library_web.ex b/lib/music_library_web.ex index b475c88d..cf3f4dd0 100644 --- a/lib/music_library_web.ex +++ b/lib/music_library_web.ex @@ -71,6 +71,7 @@ defmodule MusicLibraryWeb do def live_component do quote do use Phoenix.LiveComponent + import MusicLibraryWeb.Hooks.ShowToast, only: [put_toast!: 2] unquote(html_helpers()) end diff --git a/lib/music_library_web/components/release.ex b/lib/music_library_web/components/release.ex index 5669ef34..024b0399 100644 --- a/lib/music_library_web/components/release.ex +++ b/lib/music_library_web/components/release.ex @@ -250,26 +250,25 @@ defmodule MusicLibraryWeb.Components.Release do case ScrobbleActivity.scrobble_release(release_with_tracks, finished_at: DateTime.utc_now()) do {:ok, _} -> send_update_after(socket.assigns.myself, %{already_scrobbled: false}, 3000) + put_toast!(:info, gettext("Release scrobbled successfully")) - {:noreply, - socket - |> assign(:already_scrobbled, true) - |> put_toast(:info, gettext("Release scrobbled successfully"))} + {:noreply, socket |> assign(:already_scrobbled, true)} {:error, reason} -> Logger.error("Error scrobbling release: #{inspect(reason)}") - {:noreply, - socket - |> put_toast( - :error, - gettext("Error scrobbling release") <> "," <> inspect(reason) - )} + put_toast!( + :error, + gettext("Error scrobbling release") <> "," <> inspect(reason) + ) + + {:noreply, socket} end end def handle_event("scrobble_release", _params, socket) do - {:noreply, socket |> put_toast(:error, gettext("Error scrobbling release"))} + put_toast!(:error, gettext("Error scrobbling release")) + {:noreply, socket} end def handle_event("scrobble_medium", %{"number" => number}, socket) @@ -282,26 +281,25 @@ defmodule MusicLibraryWeb.Components.Release do ) do {:ok, _} -> send_update_after(socket.assigns.myself, %{already_scrobbled: false}, 3000) + put_toast!(:info, gettext("Disc scrobbled successfully")) - {:noreply, - socket - |> assign(:already_scrobbled, true) - |> put_toast(:info, gettext("Disc scrobbled successfully"))} + {:noreply, socket |> assign(:already_scrobbled, true)} {:error, reason} -> Logger.error("Error scrobbling medium: #{inspect(reason)}") - {:noreply, - socket - |> put_toast( - :error, - gettext("Error scrobbling disc") <> "," <> inspect(reason) - )} + put_toast!( + :error, + gettext("Error scrobbling disc") <> "," <> inspect(reason) + ) + + {:noreply, socket} end end def handle_event("scrobble_medium", _params, socket) do - {:noreply, socket |> put_toast(:error, gettext("Error scrobbling disc"))} + put_toast!(:error, gettext("Error scrobbling disc")) + {:noreply, socket} end def handle_event("toggle_track", %{"track-id" => track_id}, socket) do @@ -344,7 +342,8 @@ defmodule MusicLibraryWeb.Components.Release do selected_track_ids = socket.assigns.selected_tracks if MapSet.size(selected_track_ids) == 0 do - {:noreply, socket |> put_toast(:error, gettext("No tracks selected"))} + put_toast!(:error, gettext("No tracks selected")) + {:noreply, socket} else case ScrobbleActivity.scrobble_tracks( selected_track_ids, @@ -353,28 +352,29 @@ defmodule MusicLibraryWeb.Components.Release do ) do {:ok, _} -> send_update_after(socket.assigns.myself, %{already_scrobbled: false}, 3000) + put_toast!(:info, gettext("Selected tracks scrobbled successfully")) {:noreply, socket |> assign(:already_scrobbled, true) - |> assign(:selected_tracks, MapSet.new()) - |> put_toast(:info, gettext("Selected tracks scrobbled successfully"))} + |> assign(:selected_tracks, MapSet.new())} {:error, reason} -> Logger.error("Error scrobbling tracks: #{inspect(reason)}") - {:noreply, - socket - |> put_toast( - :error, - gettext("Error scrobbling selected tracks") <> "," <> inspect(reason) - )} + put_toast!( + :error, + gettext("Error scrobbling selected tracks") <> "," <> inspect(reason) + ) + + {:noreply, socket} end end end def handle_event("scrobble_selected_tracks", _params, socket) do - {:noreply, socket |> put_toast(:error, gettext("Error scrobbling selected tracks"))} + put_toast!(:error, gettext("Error scrobbling selected tracks")) + {:noreply, socket} end defp medium_selected?(medium, selected_tracks) do diff --git a/lib/music_library_web/hooks/show_toast.ex b/lib/music_library_web/hooks/show_toast.ex new file mode 100644 index 00000000..e96fde67 --- /dev/null +++ b/lib/music_library_web/hooks/show_toast.ex @@ -0,0 +1,18 @@ +defmodule MusicLibraryWeb.Hooks.ShowToast do + import Phoenix.LiveView + import LiveToast, only: [put_toast: 3] + + def put_toast!(type, message) do + send(self(), {:put_toast, type, message}) + end + + def on_mount(_name, _params, _session, socket) do + {:cont, attach_hook(socket, :put_toast, :handle_info, &maybe_put_toast/2)} + end + + defp maybe_put_toast({:put_toast, type, message}, socket) do + {:halt, put_toast(socket, type, message)} + end + + defp maybe_put_toast(_, socket), do: {:cont, socket} +end diff --git a/lib/music_library_web/router.ex b/lib/music_library_web/router.ex index 4ef77635..b5f6f56a 100644 --- a/lib/music_library_web/router.ex +++ b/lib/music_library_web/router.ex @@ -39,7 +39,11 @@ defmodule MusicLibraryWeb.Router do get "/assets/:transform_payload", AssetController, :show live_session :default, - on_mount: [MusicLibraryWeb.Hooks.StaticAssets, MusicLibraryWeb.Hooks.GetTimezone] do + on_mount: [ + MusicLibraryWeb.Hooks.StaticAssets, + MusicLibraryWeb.Hooks.GetTimezone, + MusicLibraryWeb.Hooks.ShowToast + ] do live "/", StatsLive.Index, :index live "/collection", CollectionLive.Index, :index