aboutsummaryrefslogtreecommitdiff
path: root/static
diff options
context:
space:
mode:
authortdro <tdro@noreply.example.com>2022-07-29 21:24:09 -0400
committertdro <tdro@noreply.example.com>2022-07-29 21:24:09 -0400
commitb163db67a35a10e153a4fa88d1e1f1d8f9a21f8e (patch)
tree984d46e6ddd5edbddc4b0735752c2982c8d36f1c /static
parentcc0f3566fc82fd8bfdeb6d5e4234f052fcd764e9 (diff)
downloadcanory-b163db67a35a10e153a4fa88d1e1f1d8f9a21f8e.tar.gz
canory-b163db67a35a10e153a4fa88d1e1f1d8f9a21f8e.tar.bz2
canory-b163db67a35a10e153a4fa88d1e1f1d8f9a21f8e.zip
static/js: Add relative dates
Diffstat (limited to 'static')
-rw-r--r--static/js/index.ts3
-rw-r--r--static/js/timeago.ts47
2 files changed, 49 insertions, 1 deletions
diff --git a/static/js/index.ts b/static/js/index.ts
index 6000441..52cc678 100644
--- a/static/js/index.ts
+++ b/static/js/index.ts
@@ -2,5 +2,6 @@ import "./pager.ts";
import "./plumber.ts";
import "./instantpage.ts";
import "./fixedsearch.ts";
+import "./timeago.ts";
-console.log("Surface Control: OK");
+console.log("Surface Control: Complete");
diff --git a/static/js/timeago.ts b/static/js/timeago.ts
new file mode 100644
index 0000000..2cd72ca
--- /dev/null
+++ b/static/js/timeago.ts
@@ -0,0 +1,47 @@
+(function () {
+ type second = number;
+ type millisecond = number;
+
+ const relative = new Intl.RelativeTimeFormat("en", {
+ localeMatcher: "best fit",
+ numeric: "always",
+ style: "long",
+ });
+
+ const date = () => {
+ [...document.querySelectorAll("time")]
+ .forEach(
+ (element) => {
+ try {
+ const time: millisecond = new Date(element.dateTime) || new Date();
+ const interval: second = ((new Date().getTime() - time.getTime()) / 1000);
+
+ const seconds: number = Math.floor(interval);
+ const minutes: number = Math.floor(seconds / 60);
+ const hours: number = Math.floor(minutes / 60);
+ const days: number = Math.floor(hours / 24);
+
+ if (seconds <= 60) { return element.textContent = relative.format(-1 * seconds, "second",); }
+ if (minutes <= 120) { return element.textContent = relative.format(-1 * minutes, "minute",); }
+ if (hours <= 48) { return element.textContent = relative.format(-1 * hours, "hour",); }
+ if (days <= 60) { return element.textContent = relative.format(-1 * days, "day",); }
+
+ } catch (err) {
+ console.error(
+ "Error: Unable to resolve relative time format!",
+ err,
+ );
+ }
+ },
+ );
+ }
+
+ const substitution = setInterval(date, 4);
+
+ self.addEventListener("load", () => {
+ setTimeout(() => {
+ clearInterval(substitution);
+ setInterval(date, 1000);
+ }, 1000);
+ });
+})();