From 5fabf2f61ac1a2c6fc4c56d03bce0a4a167ed4c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Sat, 22 Apr 2017 00:06:07 -0400 Subject: [PATCH] Make sure friendly date markers are reset at midnight --- client/js/lounge.js | 20 ++++++++++++++++++++ client/views/date-marker.tpl | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/client/js/lounge.js b/client/js/lounge.js index 41505797..ec18b3f1 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -3,6 +3,7 @@ // vendor libraries require("jquery-ui/ui/widgets/sortable"); const $ = require("jquery"); +const moment = require("moment"); const Mousetrap = require("mousetrap"); const URI = require("urijs"); @@ -1593,6 +1594,25 @@ $(function() { } }); + // Compute how many milliseconds are remaining until the next day starts + function msUntilNextDay() { + return moment().add(1, "day").startOf("day") - moment(); + } + + // Go through all Today/Yesterday date markers in the DOM and recompute their + // labels. When done, restart the timer for the next day. + function updateDateMarkers() { + $(".date-marker-text[data-label='Today'], .date-marker-text[data-label='Yesterday']") + .closest(".date-marker-container") + .each(function() { + $(this).replaceWith(templates.date_marker({msgDate: $(this).data("timestamp")})); + }); + + // This should always be 24h later but re-computing exact value just in case + setTimeout(updateDateMarkers, msUntilNextDay()); + } + setTimeout(updateDateMarkers, msUntilNextDay()); + // Only start opening socket.io connection after all events have been registered socket.open(); diff --git a/client/views/date-marker.tpl b/client/views/date-marker.tpl index 3e5ec60d..9e67f09f 100644 --- a/client/views/date-marker.tpl +++ b/client/views/date-marker.tpl @@ -1,4 +1,4 @@ -
+