Resolve timezone from user browser
This commit is contained in:
+6
-1
@@ -36,7 +36,12 @@ const csrfToken = document
|
||||
.getAttribute("content");
|
||||
const liveSocket = new LiveSocket("/live", Socket, {
|
||||
longPollFallbackMs: 2500,
|
||||
params: { _csrf_token: csrfToken },
|
||||
params: (view) => {
|
||||
return {
|
||||
_csrf_token: csrfToken,
|
||||
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone
|
||||
}
|
||||
},
|
||||
hooks: { ...Hooks, ...colocatedHooks },
|
||||
dom: {
|
||||
onBeforeElUpdated(from, to) {
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@ config :music_library,
|
||||
ecto_repos: [MusicLibrary.BackgroundRepo, MusicLibrary.Repo, MusicLibrary.ErrorRepo],
|
||||
generators: [timestamp_type: :utc_datetime, binary_id: true]
|
||||
|
||||
config :music_library, timezone: "Europe/London"
|
||||
config :music_library, default_timezone: "Europe/London"
|
||||
|
||||
config :music_library, MusicLibraryWeb,
|
||||
login_password: "change me",
|
||||
|
||||
+2
-2
@@ -28,8 +28,8 @@ if personal_access_token = System.get_env("DISCOGS_PERSONAL_ACCESS_TOKEN") do
|
||||
config :music_library, Discogs, personal_access_token: personal_access_token
|
||||
end
|
||||
|
||||
if timezone = System.get_env("TIMEZONE") do
|
||||
config :music_library, :timezone, timezone
|
||||
if default_timezone = System.get_env("DEFAULT_TIMEZONE") do
|
||||
config :music_library, :default_timezone, default_timezone
|
||||
end
|
||||
|
||||
config :music_library, MusicLibrary.Repo,
|
||||
|
||||
@@ -27,5 +27,5 @@ defmodule MusicLibrary do
|
||||
- `MusicLibrary.Secrets` - Encrypted storage for API keys and sensitive configuration
|
||||
"""
|
||||
|
||||
def timezone, do: Application.fetch_env!(:music_library, :timezone)
|
||||
def default_timezone, do: Application.fetch_env!(:music_library, :default_timezone)
|
||||
end
|
||||
|
||||
@@ -214,7 +214,7 @@ defmodule MusicLibrary.ScrobbleActivity do
|
||||
def get_top_albums_by_days(days, opts) do
|
||||
limit = Keyword.get(opts, :limit, 10)
|
||||
current_time = Keyword.get_lazy(opts, :current_time, &DateTime.utc_now/0)
|
||||
timezone = Keyword.get(opts, :timezone, &MusicLibrary.timezone/0)
|
||||
timezone = Keyword.get(opts, :timezone, &MusicLibrary.default_timezone/0)
|
||||
|
||||
cutoff_timestamp =
|
||||
current_time
|
||||
@@ -304,7 +304,7 @@ defmodule MusicLibrary.ScrobbleActivity do
|
||||
def get_top_artists_by_days(days, opts) do
|
||||
limit = Keyword.get(opts, :limit, 10)
|
||||
current_time = Keyword.get_lazy(opts, :current_time, &DateTime.utc_now/0)
|
||||
timezone = Keyword.get(opts, :timezone, &MusicLibrary.timezone/0)
|
||||
timezone = Keyword.get(opts, :timezone, &MusicLibrary.default_timezone/0)
|
||||
|
||||
cutoff_timestamp =
|
||||
current_time
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
defmodule MusicLibraryWeb.Hooks.GetTimezone do
|
||||
use MusicLibraryWeb, :live_component
|
||||
|
||||
def on_mount(:default, _params, _session, socket) do
|
||||
connect_params = get_connect_params(socket)
|
||||
timezone = connect_params["timezone"] || MusicLibrary.default_timezone()
|
||||
|
||||
{:cont, assign(socket, :timezone, timezone)}
|
||||
end
|
||||
end
|
||||
@@ -25,7 +25,6 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(:current_section, :collection)
|
||||
|> assign(:timezone, MusicLibrary.timezone())
|
||||
|> assign(:can_scrobble?, ScrobbleActivity.can_scrobble?())
|
||||
|> assign(:release_with_tracks, nil)}
|
||||
end
|
||||
|
||||
@@ -23,7 +23,6 @@ defmodule MusicLibraryWeb.StatsLive.Index do
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(:timezone, MusicLibrary.timezone())
|
||||
|> stream_configure(:recent_tracks,
|
||||
dom_id: fn track -> "track-#{track.scrobbled_at_uts}" end
|
||||
)
|
||||
|
||||
@@ -40,7 +40,7 @@ defmodule MusicLibraryWeb.Router do
|
||||
get "/artists/:musicbrainz_id/image", ArtistController, :image
|
||||
|
||||
live_session :default,
|
||||
on_mount: MusicLibraryWeb.Hooks.StaticAssets do
|
||||
on_mount: [MusicLibraryWeb.Hooks.StaticAssets, MusicLibraryWeb.Hooks.GetTimezone] do
|
||||
live "/", StatsLive.Index, :index
|
||||
|
||||
live "/collection", CollectionLive.Index, :index
|
||||
|
||||
Reference in New Issue
Block a user