misc: Update relative dates

This commit is contained in:
Xyphyn committed 2023-07-06 13:00:20 -07:00
1 parent 4899accfcf
commit e259ceb6e4
5 files changed
+60 -15

No files matched your search

+6 -2
View File
@@ -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>
+2 -2
View File
@@ -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 -1
View File
@@ -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"
+36 -8
View File
@@ -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)}
+15 -2
View File
@@ -11,6 +11,8 @@
import { onMount } from 'svelte'
import UserLink from '$lib/components/user/UserLink.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import Avatar from '$lib/components/ui/Avatar.svelte'
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
export let data
@@ -100,8 +102,19 @@
<div class="flex flex-col gap-2">
<span class="flex flex-row gap-2 text-sm opacity-80 items-center">
<CommunityLink avatar community={postData.community} />
<span class="opacity-50"><UserLink user={postData.creator} /></span>
<Avatar
url={postData.community.icon}
width={24}
alt={postData.community.name}
/>
<div class="flex flex-col">
<CommunityLink community={postData.community} />
<span class="opacity-50 flex flex-row gap-1 text-sm">
<UserLink user={postData.creator} />
<span>•</span>
<RelativeDate date={new Date(postData.post.published)} />
</span>
</div>
</span>
<h1 class="font-bold text-lg">{post.name}</h1>
{#if post.url}