1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-09 10:29:57 +00:00
elk/components/common/CommonPaginator.vue

83 lines
2.1 KiB
Vue
Raw Normal View History

2022-11-16 16:11:08 +00:00
<script setup lang="ts">
2022-11-27 06:16:02 +00:00
// @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'
2022-11-16 16:11:08 +00:00
2022-12-27 22:18:16 +00:00
const {
paginator,
stream,
keyProp = 'id',
virtualScroller = false,
eventType = 'update',
preprocess,
} = defineProps<{
2022-11-17 07:35:42 +00:00
paginator: Paginator<any, any[]>
keyProp?: string
2022-11-28 13:14:27 +00:00
virtualScroller?: boolean
stream?: Promise<WsEvents>
eventType?: 'notification' | 'update'
2022-12-27 17:47:05 +00:00
preprocess?: (items: any[]) => any[]
2022-11-16 16:11:08 +00:00
}>()
2022-11-17 07:35:42 +00:00
2022-11-27 08:54:00 +00:00
defineSlots<{
default: {
item: any
active?: boolean
2022-12-27 17:47:05 +00:00
older?: any
newer?: any // newer is undefined when index === 0
2022-11-27 08:54:00 +00:00
}
updater: {
number: number
update: () => void
}
2022-11-27 08:54:00 +00:00
loading: {}
}>()
2022-12-27 17:47:05 +00:00
const { items, prevItems, update, state, endAnchor, error } = usePaginator(paginator, stream, eventType, preprocess)
2022-11-16 16:11:08 +00:00
</script>
<template>
2022-11-17 07:35:42 +00:00
<div>
<slot v-if="prevItems.length" name="updater" v-bind="{ number: prevItems.length, update }" />
2022-11-30 00:47:54 +00:00
<slot name="items" :items="items">
<template v-if="virtualScroller">
<DynamicScroller
2022-12-27 22:18:16 +00:00
v-slot="{ item, active, index }"
2022-11-30 00:47:54 +00:00
:items="items"
:min-item-size="200"
:key-field="keyProp"
page-mode
>
2022-12-27 22:18:16 +00:00
<slot
:key="item[keyProp]"
2022-12-27 22:18:16 +00:00
:item="item"
:active="active"
:older="items[index + 1]"
:newer="items[index - 1]"
2022-12-27 22:18:16 +00:00
/>
2022-11-30 00:47:54 +00:00
</DynamicScroller>
</template>
<template v-else>
<slot
v-for="item, index of items"
2022-11-30 00:47:54 +00:00
:key="item[keyProp]"
:item="item"
:older="items[index + 1]"
:newer="items[index - 1]"
2022-11-30 00:47:54 +00:00
/>
</template>
</slot>
2022-11-17 07:35:42 +00:00
<div ref="endAnchor" />
2022-11-27 05:02:19 +00:00
<slot v-if="state === 'loading'" name="loading">
<TimelineSkeleton />
2022-11-27 05:02:19 +00:00
</slot>
<div v-else-if="state === 'done'" p5 text-secondary italic text-center>
{{ $t('common.end_of_list') }}
2022-11-17 07:35:42 +00:00
</div>
<div v-else-if="state === 'error'" p5 text-secondary>
{{ $t('common.error') }}: {{ error }}
2022-11-17 07:35:42 +00:00
</div>
2022-11-16 16:11:08 +00:00
</div>
</template>