diff --git a/app/javascript/controllers/frame_reloader_controller.js b/app/javascript/controllers/frame_reloader_controller.js new file mode 100644 index 000000000..c978293b5 --- /dev/null +++ b/app/javascript/controllers/frame_reloader_controller.js @@ -0,0 +1,21 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + static values = { + reloadInterval: { type: Number, default: 10 * 60 } // 10 minutes + } + + connect() { + this.freshSince = Date.now() + } + + reload() { + const now = Date.now() + const reloadIntervalMs = this.reloadIntervalValue * 1000 + + if ((now - this.freshSince) >= reloadIntervalMs) { + this.freshSince = now + this.element.reload() + } + } +} diff --git a/app/views/notifications/_tray.html.erb b/app/views/notifications/_tray.html.erb index e3fa911ba..02b2d1bfe 100644 --- a/app/views/notifications/_tray.html.erb +++ b/app/views/notifications/_tray.html.erb @@ -7,7 +7,10 @@ data-navigable-list-actionable-items-value="true" data-navigable-list-reverse-navigation-value="true" data-action="keydown->navigable-list#navigate dialog:show@document->navigable-list#reset turbo-visit->navigable-list#reset keydown.esc->dialog#close:stop click@document->dialog#closeOnClickOutside"> - <%= turbo_frame_tag "notifications", src: tray_notifications_path, refresh: "morph", data: { controller: "notifications-tray", notifications_tray_grouped_class: "notification--grouped", action: "turbo:frame-render->notifications-tray#group" } %> + <%= turbo_frame_tag "notifications", src: tray_notifications_path, refresh: "morph", data: { + controller: "notifications-tray frame-reloader", + notifications_tray_grouped_class: "notification--grouped", + action: "turbo:frame-render->notifications-tray#group focus@window->frame-reloader#reload" } %>