Fix inbox relays, add time functions

This commit is contained in:
Jon Staab
2024-10-02 11:51:14 -07:00
parent b12f4aec4d
commit 7af86f42cf
7 changed files with 126 additions and 10 deletions
+26 -2
View File
@@ -1,6 +1,6 @@
import Fuse from "fuse.js"
import type {IFuseOptions, FuseResult} from "fuse.js"
import {sortBy} from "@welshman/lib"
import {now, int, sortBy, DAY, HOUR, MINUTE} from "@welshman/lib"
export type SearchOptions<V, T> = {
getValue: (item: T) => V
@@ -24,7 +24,7 @@ export const createSearch = <V, T>(options: T[], opts: SearchOptions<V, T>): Sea
const search = (term: string) => {
opts.onSearch?.(term)
let results = term ? fuse.search(term) : options.map(item => ({item, score: 1}) as FuseResult<T>)
let results = term ? fuse.search(term) : options.map(item => ({item}) as FuseResult<T>)
if (opts.sortFn) {
results = sortBy(opts.sortFn, results)
@@ -78,3 +78,27 @@ export const formatTimestampAsTime = (ts: number) => {
return formatter.format(secondsToDate(ts))
}
export const formatTimestampRelative = (ts: number) => {
let unit
let delta = now() - ts
if (delta < int(MINUTE)) {
unit = "second"
} else if (delta < int(HOUR)) {
unit = "minute"
delta = Math.round(delta / int(MINUTE))
} else if (delta < int(DAY, 2)) {
unit = "hour"
delta = Math.round(delta / int(HOUR))
} else {
unit = "day"
delta = Math.round(delta / int(DAY))
}
const locale = new Intl.RelativeTimeFormat().resolvedOptions().locale
const formatter = new Intl.RelativeTimeFormat(locale, {
numeric: "auto",
})
return formatter.format(-delta, unit as Intl.RelativeTimeFormatUnit)
}