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">
|
<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">
|
<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">
|
<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">
|
<div class="flex flex-col">
|
||||||
<CommunityLink community={postRes.community} />
|
<CommunityLink community={postRes.community} />
|
||||||
<span class="opacity-60 flex flex-row gap-1">
|
<span class="opacity-60 flex flex-row gap-1">
|
||||||
@@ -82,7 +86,7 @@
|
|||||||
<img
|
<img
|
||||||
src={postRes.post.url}
|
src={postRes.post.url}
|
||||||
alt={postRes.post.name}
|
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"
|
loading="lazy"
|
||||||
/>
|
/>
|
||||||
</a>
|
</a>
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
export let url: string
|
export let url: string | undefined
|
||||||
export let alt: string = ''
|
export let alt: string = ''
|
||||||
|
|
||||||
export let width: number
|
export let width: number
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<img
|
<img
|
||||||
src={url}
|
src={url ?? `https://api.dicebear.com/6.x/initials/svg?seed=${alt}`}
|
||||||
{alt}
|
{alt}
|
||||||
{width}
|
{width}
|
||||||
loading="lazy"
|
loading="lazy"
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<div
|
<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
|
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)]
|
flex-col gap-4 p-8 max-w-sm rounded-md h-max max-h-[calc(100vh-6rem)]
|
||||||
overflow-auto w-full"
|
overflow-auto w-full"
|
||||||
|
|||||||
@@ -1,12 +1,40 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
export let date: Date
|
export let date: Date
|
||||||
const rtf = new Intl.RelativeTimeFormat('en', {
|
|
||||||
numeric: 'auto',
|
function formatRelativeDate(date: Date) {
|
||||||
style: 'short',
|
const now = Date.now()
|
||||||
}).format(
|
|
||||||
Math.floor((date.getTime() - Date.now()) / 1000 / 60 / 60 / 24),
|
// Calculate the difference in milliseconds between 'now' and 'date'
|
||||||
'days'
|
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>
|
</script>
|
||||||
|
|
||||||
{rtf}
|
{formatRelativeDate(date)}
|
||||||
@@ -11,6 +11,8 @@
|
|||||||
import { onMount } from 'svelte'
|
import { onMount } from 'svelte'
|
||||||
import UserLink from '$lib/components/user/UserLink.svelte'
|
import UserLink from '$lib/components/user/UserLink.svelte'
|
||||||
import Markdown from '$lib/components/markdown/Markdown.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
|
export let data
|
||||||
|
|
||||||
@@ -100,8 +102,19 @@
|
|||||||
|
|
||||||
<div class="flex flex-col gap-2">
|
<div class="flex flex-col gap-2">
|
||||||
<span class="flex flex-row gap-2 text-sm opacity-80 items-center">
|
<span class="flex flex-row gap-2 text-sm opacity-80 items-center">
|
||||||
<CommunityLink avatar community={postData.community} />
|
<Avatar
|
||||||
<span class="opacity-50"><UserLink user={postData.creator} /></span>
|
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>
|
</span>
|
||||||
<h1 class="font-bold text-lg">{post.name}</h1>
|
<h1 class="font-bold text-lg">{post.name}</h1>
|
||||||
{#if post.url}
|
{#if post.url}
|
||||||
|
|||||||
Reference in new issue
Block a user