From bb4fce61c7a7af87513e5f22047298b3f59c4fae Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Tue, 13 May 2025 21:03:59 +0100 Subject: [PATCH] Extract StaticAssets hook with a single default live_session --- lib/music_library_web/hooks/static_assets.ex | 14 +++++++++ .../live/collection_live/index.ex | 7 ----- .../live/collection_live/show.ex | 7 ----- .../live/wishlist_live/index.ex | 7 ----- .../live/wishlist_live/show.ex | 7 ----- lib/music_library_web/router.ex | 31 ++++++++++--------- priv/gettext/default.pot | 5 +-- priv/gettext/en/LC_MESSAGES/default.po | 5 +-- 8 files changed, 33 insertions(+), 50 deletions(-) create mode 100644 lib/music_library_web/hooks/static_assets.ex diff --git a/lib/music_library_web/hooks/static_assets.ex b/lib/music_library_web/hooks/static_assets.ex new file mode 100644 index 00000000..32ea207c --- /dev/null +++ b/lib/music_library_web/hooks/static_assets.ex @@ -0,0 +1,14 @@ +defmodule MusicLibraryWeb.Hooks.StaticAssets do + use MusicLibraryWeb, :live_component + + def on_mount(:default, _params, _session, socket) do + socket = + if static_changed?(socket) do + put_flash(socket, :warning, gettext("The application has been updated, please reload.")) + else + socket + end + + {:cont, socket} + end +end diff --git a/lib/music_library_web/live/collection_live/index.ex b/lib/music_library_web/live/collection_live/index.ex index c154e289..9eef2e51 100644 --- a/lib/music_library_web/live/collection_live/index.ex +++ b/lib/music_library_web/live/collection_live/index.ex @@ -17,13 +17,6 @@ defmodule MusicLibraryWeb.CollectionLive.Index do @impl true def mount(_params, _session, socket) do - socket = - if static_changed?(socket) do - put_flash(socket, :warning, gettext("The application has been updated, please reload.")) - else - socket - end - {:ok, assign(socket, :nav_section, :records)} end diff --git a/lib/music_library_web/live/collection_live/show.ex b/lib/music_library_web/live/collection_live/show.ex index 2255c347..9eebcbbd 100644 --- a/lib/music_library_web/live/collection_live/show.ex +++ b/lib/music_library_web/live/collection_live/show.ex @@ -15,13 +15,6 @@ defmodule MusicLibraryWeb.CollectionLive.Show do @impl true def mount(%{"id" => record_id}, _session, socket) do - socket = - if static_changed?(socket) do - put_flash(socket, :warning, gettext("The application has been updated, please reload.")) - else - socket - end - if connected?(socket) do Records.subscribe(record_id) end diff --git a/lib/music_library_web/live/wishlist_live/index.ex b/lib/music_library_web/live/wishlist_live/index.ex index e18257ba..8eff7387 100644 --- a/lib/music_library_web/live/wishlist_live/index.ex +++ b/lib/music_library_web/live/wishlist_live/index.ex @@ -16,13 +16,6 @@ defmodule MusicLibraryWeb.WishlistLive.Index do @impl true def mount(_params, _session, socket) do - socket = - if static_changed?(socket) do - put_flash(socket, :warning, gettext("The application has been updated, please reload.")) - else - socket - end - current_date = DateTime.utc_now() |> DateTime.to_date() {:ok, diff --git a/lib/music_library_web/live/wishlist_live/show.ex b/lib/music_library_web/live/wishlist_live/show.ex index 1db22f8d..4d09e3b2 100644 --- a/lib/music_library_web/live/wishlist_live/show.ex +++ b/lib/music_library_web/live/wishlist_live/show.ex @@ -14,13 +14,6 @@ defmodule MusicLibraryWeb.WishlistLive.Show do @impl true def mount(_params, _session, socket) do - socket = - if static_changed?(socket) do - put_flash(socket, :warning, gettext("The application has been updated, please reload.")) - else - socket - end - current_date = DateTime.utc_now() |> DateTime.to_date() {:ok, diff --git a/lib/music_library_web/router.ex b/lib/music_library_web/router.ex index 3784d993..41b71243 100644 --- a/lib/music_library_web/router.ex +++ b/lib/music_library_web/router.ex @@ -39,25 +39,28 @@ defmodule MusicLibraryWeb.Router do get "/covers/:record_id", CoverController, :show get "/artists/:musicbrainz_id/image", ArtistController, :image - live "/", StatsLive.Index, :index + live_session :default, + on_mount: MusicLibraryWeb.Hooks.StaticAssets do + live "/", StatsLive.Index, :index - live "/collection", CollectionLive.Index, :index - live "/collection/import", CollectionLive.Index, :import - live "/collection/scan", CollectionLive.Index, :barcode_scan - live "/collection/:id/edit", CollectionLive.Index, :edit + live "/collection", CollectionLive.Index, :index + live "/collection/import", CollectionLive.Index, :import + live "/collection/scan", CollectionLive.Index, :barcode_scan + live "/collection/:id/edit", CollectionLive.Index, :edit - live "/collection/:id", CollectionLive.Show, :show - live "/collection/:id/show/edit", CollectionLive.Show, :edit + live "/collection/:id", CollectionLive.Show, :show + live "/collection/:id/show/edit", CollectionLive.Show, :edit - live "/wishlist", WishlistLive.Index, :index - live "/wishlist/import", WishlistLive.Index, :import - live "/wishlist/:id/edit", WishlistLive.Index, :edit + live "/wishlist", WishlistLive.Index, :index + live "/wishlist/import", WishlistLive.Index, :import + live "/wishlist/:id/edit", WishlistLive.Index, :edit - live "/wishlist/:id", WishlistLive.Show, :show - live "/wishlist/:id/show/edit", WishlistLive.Show, :edit + live "/wishlist/:id", WishlistLive.Show, :show + live "/wishlist/:id/show/edit", WishlistLive.Show, :edit - live "/artists/:musicbrainz_id", ArtistLive.Show, :show - live "/artists/:musicbrainz_id/import", ArtistLive.Show, :import + live "/artists/:musicbrainz_id", ArtistLive.Show, :show + live "/artists/:musicbrainz_id/import", ArtistLive.Show, :import + end end end diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index daaba711..cd1af711 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -203,10 +203,7 @@ msgstr "" msgid "Success!" msgstr "" -#: lib/music_library_web/live/collection_live/index.ex -#: lib/music_library_web/live/collection_live/show.ex -#: lib/music_library_web/live/wishlist_live/index.ex -#: lib/music_library_web/live/wishlist_live/show.ex +#: lib/music_library_web/hooks/static_assets.ex #, elixir-autogen, elixir-format msgid "The application has been updated, please reload." msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index 5bd332cd..c7d93c94 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -203,10 +203,7 @@ msgstr "" msgid "Success!" msgstr "" -#: lib/music_library_web/live/collection_live/index.ex -#: lib/music_library_web/live/collection_live/show.ex -#: lib/music_library_web/live/wishlist_live/index.ex -#: lib/music_library_web/live/wishlist_live/show.ex +#: lib/music_library_web/hooks/static_assets.ex #, elixir-autogen, elixir-format msgid "The application has been updated, please reload." msgstr ""