Bluesky app fork with some witchin' additions 馃挮
witchsky.app
bluesky
fork
client
1import {memo} from 'react'
2import {View} from 'react-native'
3import {Trans, useLingui} from '@lingui/react/macro'
4import {subDays} from 'date-fns'
5
6import {atoms as a, useTheme} from '#/alf'
7import {Text} from '../Typography'
8import {localDateString} from './util'
9
10const timeFormatter = new Intl.DateTimeFormat(undefined, {
11 hour: 'numeric',
12 minute: 'numeric',
13})
14const weekdayFormatter = new Intl.DateTimeFormat(undefined, {
15 weekday: 'long',
16})
17const longDateFormatter = new Intl.DateTimeFormat(undefined, {
18 weekday: 'short',
19 month: 'long',
20 day: 'numeric',
21})
22const longDateFormatterWithYear = new Intl.DateTimeFormat(undefined, {
23 weekday: 'short',
24 month: 'long',
25 day: 'numeric',
26 year: 'numeric',
27})
28
29let DateDivider = ({date: dateStr}: {date: string}): React.ReactNode => {
30 const t = useTheme()
31 const {t: l} = useLingui()
32
33 let date: string
34 const time = timeFormatter.format(new Date(dateStr))
35
36 const timestamp = new Date(dateStr)
37
38 const today = new Date()
39 const yesterday = subDays(today, 1)
40 const oneWeekAgo = subDays(today, 7)
41
42 if (localDateString(today) === localDateString(timestamp)) {
43 date = l`Today`
44 } else if (localDateString(yesterday) === localDateString(timestamp)) {
45 date = l`Yesterday`
46 } else {
47 if (timestamp < oneWeekAgo) {
48 if (timestamp.getFullYear() === today.getFullYear()) {
49 date = longDateFormatter.format(timestamp)
50 } else {
51 date = longDateFormatterWithYear.format(timestamp)
52 }
53 } else {
54 date = weekdayFormatter.format(timestamp)
55 }
56 }
57
58 return (
59 <View style={[a.w_full, a.my_sm]}>
60 <Text
61 style={[
62 a.text_xs,
63 a.text_center,
64 t.atoms.bg,
65 t.atoms.text_contrast_medium,
66 a.px_md,
67 ]}>
68 <Trans>
69 {date} at {time}
70 </Trans>
71 </Text>
72 </View>
73 )
74}
75DateDivider = memo(DateDivider)
76export {DateDivider}