2024-04-28 22:00:56 +07:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import "strings"
|
2024-04-29 21:45:18 +07:00
|
|
|
import "strconv"
|
2024-04-28 22:00:56 +07:00
|
|
|
|
|
|
|
// RelativeTimeText updates the text content of the element to be a relative time text.
|
|
|
|
//
|
|
|
|
// Every second it updates the text content to be the relative time text of the input string.
|
|
|
|
|
2024-04-29 21:45:18 +07:00
|
|
|
script RelativeFromTimeText(id string, time int64, inter int) {
|
2024-04-28 22:00:56 +07:00
|
|
|
const el = document.getElementById(id)
|
2024-04-29 21:45:18 +07:00
|
|
|
el.parentNode.dataset.tip = dayjs.unix(time).tz(dayjs.tz.guess()).format('ddd, D MMM YYYY HH:mm:ss Z')
|
|
|
|
|
|
|
|
const timeText = dayjs.unix(time).fromNow()
|
2024-04-28 22:00:56 +07:00
|
|
|
el.textContent = timeText
|
|
|
|
|
|
|
|
const interval = setInterval(() => {
|
2024-04-29 21:45:18 +07:00
|
|
|
const timeText = dayjs.unix(time).fromNow()
|
2024-04-28 22:00:56 +07:00
|
|
|
el.textContent = timeText
|
2024-04-29 15:18:23 +07:00
|
|
|
}, inter)
|
|
|
|
|
|
|
|
const obs = new MutationObserver((mutations) => {
|
|
|
|
for (const mutation of mutations) {
|
|
|
|
for (const removed of mutation.removedNodes) {
|
|
|
|
if (el === removed) {
|
|
|
|
clearInterval(interval)
|
|
|
|
obs.disconnect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
obs.observe(el.parentNode, { childList: true })
|
|
|
|
}
|
|
|
|
|
2024-04-29 21:45:18 +07:00
|
|
|
templ RelativeTimeNode(id string, time int64, class ...string) {
|
|
|
|
<div class="tooltip z-10" data-tip={ strconv.FormatInt(time, 10) }>
|
|
|
|
<span
|
|
|
|
id={ id }
|
|
|
|
class={ strings.Join(class, " ") }
|
|
|
|
>{ strconv.FormatInt(time, 10) }</span>
|
|
|
|
</div>
|
2024-04-29 15:18:23 +07:00
|
|
|
@RelativeFromTimeText(id, time, 10000)
|
|
|
|
}
|