misc: Update relative dates
This commit is contained in:
1 parent
4899accfcf
commit
e259ceb6e4
5 files changed
+60
-15
No files matched your search
@@ -50,7 +50,11 @@
|
||||
<Card class="bg-white flex flex-col overflow-hidden w-full relative">
|
||||
<div class="flex flex-col gap-2 bg-white dark:bg-zinc-900 p-4 rounded-md">
|
||||
<span class="flex flex-row gap-2 text-xs items-center">
|
||||
<Avatar url={postRes.community.icon ?? ''} alt="" width={24} />
|
||||
<Avatar
|
||||
url={postRes.community.icon}
|
||||
alt={postRes.community.title}
|
||||
width={24}
|
||||
/>
|
||||
<div class="flex flex-col">
|
||||
<CommunityLink community={postRes.community} />
|
||||
<span class="opacity-60 flex flex-row gap-1">
|
||||
@@ -82,7 +86,7 @@
|
||||
<img
|
||||
src={postRes.post.url}
|
||||
alt={postRes.post.name}
|
||||
class="rounded-md max-h-64 max-w-full"
|
||||
class="rounded-md max-h-[32rem] w-full max-w-full"
|
||||
loading="lazy"
|
||||
/>
|
||||
</a>
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
<script lang="ts">
|
||||
export let url: string
|
||||
export let url: string | undefined
|
||||
export let alt: string = ''
|
||||
|
||||
export let width: number
|
||||
</script>
|
||||
|
||||
<img
|
||||
src={url}
|
||||
src={url ?? `https://api.dicebear.com/6.x/initials/svg?seed=${alt}`}
|
||||
{alt}
|
||||
{width}
|
||||
loading="lazy"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<div
|
||||
class="sticky top-20 bg-white shadow-md border-slate-200
|
||||
class="sticky top-20 border bg-white shadow-md border-slate-100
|
||||
dark:border-zinc-800 dark:bg-zinc-900 break-words hidden lg:flex
|
||||
flex-col gap-4 p-8 max-w-sm rounded-md h-max max-h-[calc(100vh-6rem)]
|
||||
overflow-auto w-full"
|
||||
|
||||
@@ -1,12 +1,40 @@
|
||||
<script lang="ts">
|
||||
export let date: Date
|
||||
const rtf = new Intl.RelativeTimeFormat('en', {
|
||||
numeric: 'auto',
|
||||
style: 'short',
|
||||
}).format(
|
||||
Math.floor((date.getTime() - Date.now()) / 1000 / 60 / 60 / 24),
|
||||
'days'
|
||||
)
|
||||
|
||||
function formatRelativeDate(date: Date) {
|
||||
const now = Date.now()
|
||||
|
||||
// Calculate the difference in milliseconds between 'now' and 'date'
|
||||
const diffInMillis = now - date.getTime()
|
||||
|
||||
// Define time thresholds for different units (milliseconds)
|
||||
const thresholds = [
|
||||
{ unit: 'second', threshold: 1000 },
|
||||
{ unit: 'minute', threshold: 60 * 1000 },
|
||||
{ unit: 'hour', threshold: 60 * 60 * 1000 },
|
||||
{ unit: 'day', threshold: 24 * 60 * 60 * 1000 },
|
||||
{ unit: 'week', threshold: 7 * 24 * 60 * 60 * 1000 },
|
||||
{ unit: 'month', threshold: 30 * 24 * 60 * 60 * 1000 },
|
||||
{ unit: 'year', threshold: 365 * 24 * 60 * 60 * 1000 },
|
||||
// You can add more units as needed, e.g., week, month, year
|
||||
]
|
||||
|
||||
// Iterate through the thresholds to find the appropriate unit to display
|
||||
for (let i = thresholds.length - 1; i >= 0; i--) {
|
||||
if (diffInMillis >= thresholds[i].threshold) {
|
||||
const value = Math.round(diffInMillis / thresholds[i].threshold)
|
||||
|
||||
// Create a RelativeTimeFormat instance with user's locale and options
|
||||
const rtf = new Intl.RelativeTimeFormat(navigator.language, {
|
||||
numeric: 'auto',
|
||||
})
|
||||
|
||||
return rtf.format(-value, thresholds[i].unit as any)
|
||||
}
|
||||
}
|
||||
|
||||
return 'Now' // If none of the above conditions match defaulting to "Now"
|
||||
}
|
||||
</script>
|
||||
|
||||
{rtf}
|
||||
{formatRelativeDate(date)}
|
||||
Reference in new issue
Block a user