elk/components/common/CommonPaginator.vue
三咲智子 83db9f0c38
fix: group follow notification
append current group before push a single item
2023-01-09 20:23:15 +08:00

102 lines
2.6 KiB
Vue

<script setup lang="ts" generic="T, O, U = T">
// @ts-expect-error missing types
import { DynamicScroller } from 'vue-virtual-scroller'
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'
import type { Paginator, WsEvents } from 'masto'
const {
paginator,
stream,
keyProp = 'id',
virtualScroller = false,
eventType = 'update',
buffer = 10,
preprocess,
} = defineProps<{
paginator: Paginator<T[], O>
keyProp?: keyof T
virtualScroller?: boolean
stream?: Promise<WsEvents>
eventType?: 'notification' | 'update'
// When preprocess is used, buffer is the number of items that will be hidden
// until the next pagination to avoid border effect between pages when reordering
// and grouping items
buffer?: number
preprocess?: (items: T[]) => U[]
}>()
defineSlots<{
default: {
items: U[]
item: U
index: number
active?: boolean
older?: U
newer?: U // newer is undefined when index === 0
}
items: {
items: U[]
}
updater: {
number: number
update: () => void
}
loading: {}
done: {}
}>()
const { t } = useI18n()
const { items, prevItems, update, state, endAnchor, error } = usePaginator(paginator, stream, eventType, preprocess)
</script>
<template>
<div>
<slot v-if="prevItems.length" name="updater" v-bind="{ number: prevItems.length, update }" />
<slot name="items" :items="items">
<template v-if="virtualScroller">
<DynamicScroller
v-slot="{ item, active, index }"
:items="items"
:min-item-size="200"
:key-field="keyProp"
page-mode
>
<slot
:key="item[keyProp]"
:item="item"
:active="active"
:older="items[index + 1]"
:newer="items[index - 1]"
:index="index"
:items="items"
/>
</DynamicScroller>
</template>
<template v-else>
<slot
v-for="item, index of items"
:key="(item as any)[keyProp]"
:item="item"
:older="items[index + 1]"
:newer="items[index - 1]"
:index="index"
:items="items"
/>
</template>
</slot>
<div ref="endAnchor" />
<slot v-if="state === 'loading'" name="loading">
<TimelineSkeleton />
</slot>
<slot v-else-if="state === 'done'" name="done">
<div p5 text-secondary italic text-center>
{{ t('common.end_of_list') }}
</div>
</slot>
<div v-else-if="state === 'error'" p5 text-secondary>
{{ t('common.error') }}: {{ error }}
</div>
</div>
</template>