Skip to content

Commit

Permalink
feat: chat list timestamp (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
agazso authored Sep 21, 2023
1 parent 38208f3 commit e9ff597
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/lib/utils/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { toDecimal, toSignificantString, toBigInt } from './format'
import { toDecimal, toSignificantString, toBigInt, formatTimestamp } from './format'

describe('toDecimal', () => {
const testValues = [
Expand Down Expand Up @@ -55,3 +55,24 @@ describe('toBigInt', () => {
})
})
})

describe('formatTimestamp', () => {
const currentDate = new Date(1695301187859)
const testValues = [
{ timestamp: 1695292194040, expectedOut: '12:29' },
{ timestamp: 1695292052039, expectedOut: '12:27' },
{ timestamp: 1695215231556, expectedOut: 'Wed' },
{ timestamp: 1695213720514, expectedOut: 'Wed' },
{ timestamp: 1695213620936, expectedOut: 'Wed' },
{ timestamp: 1695136556259, expectedOut: 'Tue' },
{ timestamp: 1695062899168, expectedOut: 'Mon' },
{ timestamp: 1694447288453, expectedOut: 'Sep 11' },
{ timestamp: 1694258558099, expectedOut: 'Sep 9' },
]

testValues.forEach(({ timestamp, expectedOut }) => {
it(`with timestamp=${timestamp} should return ${expectedOut}`, () => {
expect(formatTimestamp(timestamp, currentDate)).toEqual(expectedOut)
})
})
})
54 changes: 54 additions & 0 deletions src/lib/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,57 @@ export function formatDateAndTime(timestamp: number) {
const dateTime = timeFormat.format(date)
return `${dateString} at ${dateTime}`
}

export function formatTimestamp(timestamp: number, currentDate = new Date()) {
if (timestamp <= 0) {
return ''
}

const locale = new Intl.DateTimeFormat().resolvedOptions().locale
const date = new Date(timestamp)

// today at 00:00
const today = new Date(
currentDate.getFullYear(),
currentDate.getMonth(),
currentDate.getDate(),
0,
0,
0,
0,
)

// a week before `today`
// this is used for the calculating a day, but it should not include
// the same day twice, hence we only count the last 6 days
const lastWeek = new Date(today.valueOf() - 6 * 24 * 60 * 60 * 1000)

// if it is today, show only the time with a 24 hour clock
if (timestamp >= today.valueOf()) {
const timeFormat = new Intl.DateTimeFormat(locale, {
hour: 'numeric',
minute: 'numeric',
hour12: false,
})
const dateTime = timeFormat.format(date)

return dateTime
}

// if it is last week, show only the day of the week
if (timestamp >= lastWeek.valueOf()) {
const dayFormat = new Intl.DateTimeFormat(locale, {
weekday: 'short',
})

return dayFormat.format(date)
}

// if it is older than a week, show the month and day
const dateFormat = new Intl.DateTimeFormat(locale, {
month: 'short',
day: 'numeric',
})

return dateFormat.format(date)
}
4 changes: 4 additions & 0 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import AuthenticatedOnly from '$lib/components/authenticated-only.svelte'
import Layout from '$lib/components/layout.svelte'
import Events from '$lib/components/icons/events.svelte'
import { formatTimestamp } from '$lib/utils/format'
$: orderedChats = Array.from($chats.chats)
.map(([, chat]) => chat)
Expand Down Expand Up @@ -184,6 +185,9 @@
</Badge>
{/if}
</span>
<span class="timestamp">
{formatTimestamp(lastMessage?.timestamp ?? 0)}
</span>
</div>
<p class={`message text-serif ${myMessage ? 'my-message' : ''}`}>
{#if chat.joined}
Expand Down

1 comment on commit e9ff597

@vercel
Copy link

@vercel vercel bot commented on e9ff597 Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.