Skip to content

Commit

Permalink
Merge pull request #113 from akira/relative-time
Browse files Browse the repository at this point in the history
use relative time format
  • Loading branch information
ananthakumaran authored Feb 26, 2022
2 parents 8c64f56 + 0306fbb commit 7794d52
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 5 deletions.
2 changes: 2 additions & 0 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { LiveSocket } from "phoenix_live_view";
import { Table } from "./hooks/table";
import { RealtimePlot, HistoricalPlot } from "./hooks/plot";
import { Refresh } from "./hooks/refresh";
import { Timestamp } from "./hooks/timestamp";

let csrfToken = document
.querySelector("meta[name='csrf-token']")
Expand All @@ -31,6 +32,7 @@ let liveSocket = new LiveSocket("/live", Socket, {
RealtimePlot: RealtimePlot,
HistoricalPlot: HistoricalPlot,
Refresh: Refresh,
Timestamp: Timestamp,
},
});

Expand Down
40 changes: 40 additions & 0 deletions assets/js/hooks/timestamp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ViewHook } from "phoenix_live_view";

function relativeTime(dateString) {
if (
typeof window.Intl != "object" ||
typeof window.Intl.RelativeTimeFormat != "function"
) {
return dateString;
}

let date = new Date(dateString);
let value = (date - new Date()) / 1000;
const abs = Math.abs(value);
let unit;
if (abs < 60) {
unit = "second";
} else if (abs < 60 * 60) {
unit = "minute";
value = value / 60;
} else if (abs < 60 * 60 * 60) {
unit = "hour";
value = value / (60 * 60);
} else {
unit = "day";
value = value / (60 * 60 * 60);
}

const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
return rtf.format(Math.round(value), unit);
}

/** @type {ViewHook}*/
let Timestamp = {
mounted() {
let dateString = this.el.textContent.trim();
this.el.innerText = relativeTime(dateString);
},
};

export { Timestamp };
2 changes: 1 addition & 1 deletion lib/exq_ui_web/live/dead_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule ExqUIWeb.DeadLive.Index do
%{
header: "Last Failed",
accessor: fn item ->
live_redirect(item.scheduled_at,
live_redirect(human_time(item.scheduled_at),
to: Routes.dead_show_path(socket, item.score, item.id),
class: "nounderline"
)
Expand Down
2 changes: 1 addition & 1 deletion lib/exq_ui_web/live/retry_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule ExqUIWeb.RetryLive.Index do
%{
header: "Next Retry",
accessor: fn item ->
live_redirect(item.scheduled_at,
live_redirect(human_time(item.scheduled_at),
to: Routes.retry_show_path(socket, item.score, item.id),
class: "nounderline"
)
Expand Down
2 changes: 1 addition & 1 deletion lib/exq_ui_web/live/scheduled_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule ExqUIWeb.ScheduledLive.Index do
%{
header: "When",
accessor: fn item ->
live_redirect(item.scheduled_at,
live_redirect(human_time(item.scheduled_at),
to: Routes.scheduled_show_path(socket, item.score, item.id),
class: "nounderline"
)
Expand Down
12 changes: 11 additions & 1 deletion lib/exq_ui_web/views/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@ defmodule ExqUIWeb.Helpers do
""
end

def human_time(%DateTime{} = timestamp) do
formatted = DateTime.to_iso8601(timestamp)

content_tag(:span, formatted,
"phx-hook": "Timestamp",
id: "timestamp-#{:erlang.unique_integer()}",
title: formatted
)
end

def human_time(epoch) when is_number(epoch) do
DateTime.from_unix!(round(epoch))
human_time(DateTime.from_unix!(round(epoch)))
end

def date_selector_class(current, days) do
Expand Down
2 changes: 1 addition & 1 deletion priv/static/js/app.js

Large diffs are not rendered by default.

0 comments on commit 7794d52

Please sign in to comment.