diff --git a/CHANGELOG.md b/CHANGELOG.md index dc74b4ef..f6eab7f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ OIDC_REDIRECT_URI=https://your-dawarich-url.com/users/auth/openid_connect/callba - User can create and manage tags for places. - User can enable or disable places layers on the map to show/hide all or just some of their visited places based on tags. - User can define privacy zones around places with specific tags to hide map data within a certain radius. +- If user has a place tagged with a tag named "Home" (case insensitive), and this place doesn't have a privacy zone defined, this place will be used as home location for days with no tracked data. ## Fixed diff --git a/app/controllers/map_controller.rb b/app/controllers/map_controller.rb index bffc5461..622f8112 100644 --- a/app/controllers/map_controller.rb +++ b/app/controllers/map_controller.rb @@ -14,6 +14,7 @@ class MapController < ApplicationController @years = years_range @points_number = points_count @features = DawarichSettings.features + @home_coordinates = current_user.home_place_coordinates end private diff --git a/app/javascript/controllers/maps_controller.js b/app/javascript/controllers/maps_controller.js index 87fa5fb1..4dc958ba 100644 --- a/app/javascript/controllers/maps_controller.js +++ b/app/javascript/controllers/maps_controller.js @@ -117,8 +117,22 @@ export default class extends BaseController { this.markers = []; } - // Set default center (Berlin) if no markers available - this.center = this.markers.length > 0 ? this.markers[this.markers.length - 1] : [52.514568, 13.350111]; + // Set default center based on priority: Home place > last marker > Berlin + let defaultCenter = [52.514568, 13.350111]; // Berlin as final fallback + + // Try to get Home place coordinates + try { + const homeCoords = this.element.dataset.home_coordinates ? + JSON.parse(this.element.dataset.home_coordinates) : null; + if (homeCoords && Array.isArray(homeCoords) && homeCoords.length === 2) { + defaultCenter = homeCoords; + } + } catch (error) { + console.warn('Error parsing home coordinates:', error); + } + + // Use last marker if available, otherwise use default center (Home or Berlin) + this.center = this.markers.length > 0 ? this.markers[this.markers.length - 1] : defaultCenter; this.map = L.map(this.containerTarget).setView([this.center[0], this.center[1]], 14); diff --git a/app/models/user.rb b/app/models/user.rb index 7063ee69..b0b5e865 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -187,4 +187,15 @@ class User < ApplicationRecord # rubocop:disable Metrics/ClassLength Users::MailerSendingJob.set(wait: 9.days).perform_later(id, 'post_trial_reminder_early') Users::MailerSendingJob.set(wait: 14.days).perform_later(id, 'post_trial_reminder_late') end + + def home_place_coordinates + home_tag = tags.find_by('LOWER(name) = ?', 'home') + return nil unless home_tag + return nil if home_tag.privacy_zone? + + home_place = home_tag.places.first + return nil unless home_place + + [home_place.latitude, home_place.longitude] + end end diff --git a/app/views/map/index.html.erb b/app/views/map/index.html.erb index 87a0d425..5db60adf 100644 --- a/app/views/map/index.html.erb +++ b/app/views/map/index.html.erb @@ -90,6 +90,7 @@ data-timezone="<%= Rails.configuration.time_zone %>" data-features='<%= @features.to_json.html_safe %>' data-user_tags='<%= current_user.tags.ordered.select(:id, :name, :icon, :color).as_json.to_json.html_safe %>' + data-home_coordinates='<%= @home_coordinates.to_json.html_safe %>' data-family-members-features-value='<%= @features.to_json.html_safe %>' data-family-members-user-theme-value="<%= current_user&.theme || 'dark' %>">