InboxList.vue
2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<script setup lang="ts">
import type { Mail } from '~/types'
const props = defineProps<{
mails: Mail[]
}>()
const { locale } = useAppI18n()
const mailsRefs = ref<Record<number, Element | null>>({})
const selectedMail = defineModel<Mail | null>()
const dayFormatter = computed(() => {
return new Intl.DateTimeFormat(locale.value === 'zh-CN' ? 'zh-CN' : 'en-US', {
day: '2-digit',
month: 'short'
})
})
const timeFormatter = computed(() => {
return new Intl.DateTimeFormat(locale.value === 'zh-CN' ? 'zh-CN' : 'en-US', {
hour: '2-digit',
minute: '2-digit',
hour12: false
})
})
const formatMailDate = (date: string) => {
const parsed = new Date(date)
const todayStart = new Date()
todayStart.setHours(0, 0, 0, 0)
if (parsed >= todayStart) {
return timeFormatter.value.format(parsed)
}
return dayFormatter.value.format(parsed)
}
watch(selectedMail, () => {
if (!selectedMail.value) {
return
}
const ref = mailsRefs.value[selectedMail.value.id]
if (ref) {
ref.scrollIntoView({ block: 'nearest' })
}
})
defineShortcuts({
arrowdown: () => {
const index = props.mails.findIndex((mail: Mail) => mail.id === selectedMail.value?.id)
if (index === -1) {
selectedMail.value = props.mails[0]
} else if (index < props.mails.length - 1) {
selectedMail.value = props.mails[index + 1]
}
},
arrowup: () => {
const index = props.mails.findIndex((mail: Mail) => mail.id === selectedMail.value?.id)
if (index === -1) {
selectedMail.value = props.mails[props.mails.length - 1]
} else if (index > 0) {
selectedMail.value = props.mails[index - 1]
}
}
})
</script>
<template>
<div class="overflow-y-auto divide-y divide-default">
<div
v-for="(mail, index) in mails"
:key="index"
:ref="(el) => { mailsRefs[mail.id] = el as Element | null }"
>
<div
class="p-4 sm:px-6 text-sm cursor-pointer border-l-2 transition-colors"
:class="[
mail.unread ? 'text-highlighted' : 'text-toned',
selectedMail && selectedMail.id === mail.id
? 'border-primary bg-primary/10'
: 'border-bg hover:border-primary hover:bg-primary/5'
]"
@click="selectedMail = mail"
>
<div class="flex items-center justify-between" :class="[mail.unread && 'font-semibold']">
<div class="flex items-center gap-3">
{{ mail.from.name }}
<UChip v-if="mail.unread" />
</div>
<span>{{ formatMailDate(mail.date) }}</span>
</div>
<p class="truncate" :class="[mail.unread && 'font-semibold']">
{{ mail.subject }}
</p>
<p class="text-dimmed line-clamp-1">
{{ mail.body }}
</p>
</div>
</div>
</div>
</template>