elk/components/common/CommonPaginator.vue

68 lines
1.9 KiB
Vue
Raw Permalink Normal View History

2022-11-16 19:11:08 +03:00
<script setup lang="ts">
2022-11-27 09:16:02 +03: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 19:11:08 +03:00
const { paginator, stream, keyProp = 'id', virtualScroller = false, eventType = 'update' } = defineProps<{
2022-11-17 10:35:42 +03:00
paginator: Paginator<any, any[]>
keyProp?: string
2022-11-28 16:14:27 +03:00
virtualScroller?: boolean
stream?: WsEvents
eventType?: 'notification' | 'update'
2022-11-16 19:11:08 +03:00
}>()
2022-11-17 10:35:42 +03:00
2022-11-27 11:54:00 +03:00
defineSlots<{
default: {
item: any
active?: boolean
}
updater: {
number: number
update: () => void
}
2022-11-27 11:54:00 +03:00
loading: {}
}>()
const { items, prevItems, update, state, endAnchor, error } = usePaginator(paginator, stream, eventType)
2022-11-16 19:11:08 +03:00
</script>
<template>
2022-11-17 10:35:42 +03:00
<div>
<slot v-if="prevItems.length" name="updater" v-bind="{ number: prevItems.length, update }" />
2022-11-30 03:47:54 +03:00
<slot name="items" :items="items">
<template v-if="virtualScroller">
<DynamicScroller
v-slot="{ item, active }"
:items="items"
:min-item-size="200"
:key-field="keyProp"
page-mode
>
<slot :item="item" :active="active" />
</DynamicScroller>
</template>
<template v-else>
<slot
v-for="item of items"
:key="item[keyProp]"
:item="item"
/>
</template>
</slot>
2022-11-17 10:35:42 +03:00
<div ref="endAnchor" />
2022-11-27 08:02:19 +03:00
<slot v-if="state === 'loading'" name="loading">
<div p5 text-center flex="~ col" items-center animate-pulse>
<div text-secondary i-ri:loader-2-fill animate-spin text-2xl />
2022-11-30 02:25:29 +03:00
<span text-secondary>{{ $t('state.loading') }}</span>
2022-11-27 08:02:19 +03:00
</div>
</slot>
<div v-else-if="state === 'done'" p5 text-secondary italic text-center>
{{ $t('common.end_of_list') }}
2022-11-17 10:35:42 +03:00
</div>
<div v-else-if="state === 'error'" p5 text-secondary>
{{ $t('common.error') }}: {{ error }}
2022-11-17 10:35:42 +03:00
</div>
2022-11-16 19:11:08 +03:00
</div>
</template>