-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Human readable relative time (#6613)
* human readable date time * lint * feedback * lint
- Loading branch information
1 parent
6cfd7b2
commit dcd553f
Showing
3 changed files
with
84 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,31 @@ | ||
import { DateTime, Duration } from "luxon"; | ||
|
||
function formatUnit(value: number, unit: string): string { | ||
return `${value} ${value === 1 ? unit : unit + "s"} ago`; | ||
} | ||
|
||
export function timeAgo(date: Date): string { | ||
const now = DateTime.now(); | ||
const then = DateTime.fromJSDate(date); | ||
const diff = Duration.fromMillis(now.diff(then).milliseconds); | ||
|
||
if (diff.as("minutes") < 1) return "just now"; | ||
if (diff.as("hours") < 1) return `${Math.floor(diff.as("minutes"))}m ago`; | ||
if (diff.as("days") < 1) return `${Math.floor(diff.as("hours"))}h ago`; | ||
if (diff.as("weeks") < 1) return `${Math.floor(diff.as("days"))}d ago`; | ||
if (diff.as("months") < 1) return `${Math.floor(diff.as("weeks"))}w ago`; | ||
if (diff.as("years") < 1) return `${Math.floor(diff.as("months"))}M ago`; | ||
if (diff.as("minutes") < 1) return "Just now"; | ||
|
||
const minutes = Math.round(diff.as("minutes")); | ||
if (diff.as("hours") < 1) return formatUnit(minutes, "minute"); | ||
|
||
const hours = Math.round(diff.as("hours")); | ||
if (diff.as("days") < 1) return formatUnit(hours, "hour"); | ||
|
||
const days = Math.round(diff.as("days")); | ||
if (diff.as("weeks") < 1) return formatUnit(days, "day"); | ||
|
||
const weeks = Math.round(diff.as("weeks")); | ||
if (diff.as("months") < 1) return formatUnit(weeks, "week"); | ||
|
||
const months = Math.round(diff.as("months")); | ||
if (diff.as("years") < 1) return formatUnit(months, "month"); | ||
|
||
return `${Math.floor(diff.as("years"))}y ago`; | ||
const years = Math.round(diff.as("years")); | ||
return formatUnit(years, "year"); | ||
} |
22 changes: 7 additions & 15 deletions
22
web-admin/src/features/projects/environment-variables/ActivityCell.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
web-admin/src/features/projects/environment-variables/TimeAgo.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<script lang="ts"> | ||
import { onDestroy } from "svelte"; | ||
import { timeAgo } from "../../dashboards/listing/utils"; | ||
import { Duration } from "luxon"; | ||
export let datetime: Date | string; | ||
let formattedTime: string; | ||
let interval: ReturnType<typeof setInterval> | null = null; | ||
let lastInterval: number | null = null; | ||
const INTERVALS = { | ||
SECOND: Duration.fromObject({ seconds: 1 }).toMillis(), | ||
MINUTE: Duration.fromObject({ minutes: 1 }).toMillis(), | ||
FIVE_MINUTES: Duration.fromObject({ minutes: 5 }).toMillis(), | ||
HOUR: Duration.fromObject({ hours: 1 }).toMillis(), | ||
}; | ||
function getInterval(date: Date): number { | ||
const diff = Duration.fromMillis(Date.now() - date.getTime()); | ||
if (diff.as("minutes") < 1) return INTERVALS.SECOND; | ||
if (diff.as("hours") < 1) return INTERVALS.MINUTE; | ||
if (diff.as("days") < 1) return INTERVALS.FIVE_MINUTES; | ||
return INTERVALS.HOUR; | ||
} | ||
function updateTimeAgo(date: Date) { | ||
formattedTime = timeAgo(date); | ||
const newInterval = getInterval(date); | ||
if (newInterval !== lastInterval) { | ||
lastInterval = newInterval; | ||
resetInterval(newInterval); | ||
} | ||
} | ||
function resetInterval(newInterval: number) { | ||
if (interval) clearInterval(interval); | ||
interval = setInterval( | ||
() => updateTimeAgo(new Date(datetime)), | ||
newInterval, | ||
); | ||
} | ||
$: { | ||
const date = new Date(datetime); | ||
updateTimeAgo(date); | ||
} | ||
onDestroy(() => { | ||
if (interval) clearInterval(interval); | ||
}); | ||
</script> | ||
|
||
<time datetime={datetime.toString()}>{formattedTime}</time> |
dcd553f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://ui.rilldata.in as production
🚀 Deployed on https://67aa5b78f0fec00c3d69906c--rill-ui-dev.netlify.app