feat: add paginated notification APIs and frontend support

This commit is contained in:
Tim
2025-08-19 19:34:13 +08:00
parent 3f2829cd37
commit c344b5b4ae
5 changed files with 341 additions and 205 deletions

View File

@@ -505,13 +505,16 @@
</div>
</template>
</BaseTimeline>
<div v-if="hasMore" class="load-more">
<button class="load-more-button" @click="loadMore">加载更多</button>
</div>
</div>
</template>
</div>
</template>
<script setup>
import { computed, onMounted, ref } from 'vue'
import { computed, onMounted, ref, watch } from 'vue'
import BasePlaceholder from '~/components/BasePlaceholder.vue'
import BaseTimeline from '~/components/BaseTimeline.vue'
import NotificationContainer from '~/components/NotificationContainer.vue'
@@ -519,12 +522,17 @@ import { toast } from '~/main'
import { authState, getToken } from '~/utils/auth'
import { stripMarkdownLength } from '~/utils/markdown'
import {
fetchNotifications,
fetchAllNotifications,
fetchUnreadNotifications,
fetchUnreadCount,
isLoadingMessage,
isLoadingAll,
isLoadingUnread,
markRead,
notifications,
notificationsAll,
notificationsUnread,
markAllRead,
fetchNotificationPreferences,
updateNotificationPreference,
} from '~/utils/notification'
import TimeManager from '~/utils/time'
@@ -535,8 +543,18 @@ const selectedTab = ref(
['all', 'unread', 'control'].includes(route.query.tab) ? route.query.tab : 'unread',
)
const notificationPrefs = ref([])
const pageAll = ref(0)
const pageUnread = ref(0)
const hasMoreAll = ref(true)
const hasMoreUnread = ref(true)
const filteredNotifications = computed(() =>
selectedTab.value === 'all' ? notifications.value : notifications.value.filter((n) => !n.read),
selectedTab.value === 'all' ? notificationsAll.value : notificationsUnread.value,
)
const isLoadingMessage = computed(() =>
selectedTab.value === 'all' ? isLoadingAll.value : isLoadingUnread.value,
)
const hasMore = computed(() =>
selectedTab.value === 'all' ? hasMoreAll.value : hasMoreUnread.value,
)
const fetchPrefs = async () => {
@@ -547,7 +565,14 @@ const togglePref = async (pref) => {
const ok = await updateNotificationPreference(pref.type, !pref.enabled)
if (ok) {
pref.enabled = !pref.enabled
await fetchNotifications()
pageAll.value = 0
pageUnread.value = 0
const countAll = await fetchAllNotifications(0)
const countUnread = await fetchUnreadNotifications(0)
pageAll.value = 1
pageUnread.value = 1
hasMoreAll.value = countAll === 30
hasMoreUnread.value = countUnread === 30
await fetchUnreadCount()
} else {
toast.error('操作失败')
@@ -627,9 +652,34 @@ const formatType = (t) => {
}
}
onActivated(() => {
fetchNotifications()
fetchPrefs()
const loadMore = async () => {
if (selectedTab.value === 'all') {
const c = await fetchAllNotifications(pageAll.value)
pageAll.value++
if (c < 30) hasMoreAll.value = false
} else {
const c = await fetchUnreadNotifications(pageUnread.value)
pageUnread.value++
if (c < 30) hasMoreUnread.value = false
}
}
onMounted(async () => {
await fetchPrefs()
await loadMore()
await fetchUnreadCount()
})
watch(selectedTab, async (tab) => {
if (tab === 'all' && notificationsAll.value.length === 0) {
pageAll.value = 0
hasMoreAll.value = true
await loadMore()
} else if (tab === 'unread' && notificationsUnread.value.length === 0) {
pageUnread.value = 0
hasMoreUnread.value = true
await loadMore()
}
})
</script>
@@ -691,6 +741,19 @@ onActivated(() => {
height: 100%;
}
.load-more {
text-align: center;
}
.load-more-button {
margin: 10px auto;
padding: 6px 12px;
border: 1px solid var(--normal-border-color);
border-radius: 6px;
background: transparent;
cursor: pointer;
}
.notif-content {
display: flex;
flex-direction: column;