1
0
mirror of https://github.com/elk-zone/elk synced 2024-11-24 07:06:08 +09:00
elk/components/common/CommonPaginator.vue

122 lines
3.1 KiB
Vue
Raw Normal View History

<script setup lang="ts" generic="T, O, U = T">
2022-11-27 15:16:02 +09:00
// @ts-expect-error missing types
import { DynamicScroller } from 'vue-virtual-scroller'
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'
2023-01-07 03:50:31 +09:00
import type { Paginator, WsEvents } from 'masto'
2022-11-17 01:11:08 +09:00
2022-12-28 07:18:16 +09:00
const {
paginator,
stream,
keyProp = 'id',
virtualScroller = false,
eventType = 'update',
preprocess,
endMessage = true,
2022-12-28 07:18:16 +09:00
} = defineProps<{
paginator: Paginator<T[], O>
keyProp?: keyof T
2022-11-28 22:14:27 +09:00
virtualScroller?: boolean
stream?: Promise<WsEvents>
eventType?: 'notification' | 'update'
preprocess?: (items: (U | T)[]) => U[]
endMessage?: boolean | string
2022-11-17 01:11:08 +09:00
}>()
2022-11-17 16:35:42 +09:00
2022-11-27 17:54:00 +09:00
defineSlots<{
default: {
items: U[]
item: U
index: number
2022-11-27 17:54:00 +09:00
active?: boolean
older?: U
newer?: U // newer is undefined when index === 0
}
items: {
items: U[]
2022-11-27 17:54:00 +09:00
}
updater: {
number: number
update: () => void
}
2022-11-27 17:54:00 +09:00
loading: {}
done: {}
2022-11-27 17:54:00 +09:00
}>()
const { t } = useI18n()
const nuxtApp = useNuxtApp()
2023-01-15 17:38:02 +09:00
const { items, prevItems, update, state, endAnchor, error } = usePaginator(paginator, $$(stream), eventType, preprocess)
nuxtApp.hook('elk-logo:click', () => {
update()
nuxtApp.$scrollToTop()
})
function createEntry(item: any) {
items.value = [...items.value, preprocess?.([item]) ?? item]
}
function updateEntry(item: any) {
const id = item[keyProp]
const index = items.value.findIndex(i => (i as any)[keyProp] === id)
if (index > -1)
items.value = [...items.value.slice(0, index), preprocess?.([item]) ?? item, ...items.value.slice(index + 1)]
}
function removeEntry(entryId: any) {
items.value = items.value.filter(i => (i as any)[keyProp] !== entryId)
}
defineExpose({ createEntry, removeEntry, updateEntry })
2022-11-17 01:11:08 +09:00
</script>
<template>
2022-11-17 16:35:42 +09:00
<div>
<slot v-if="prevItems.length" name="updater" v-bind="{ number: prevItems.length, update }" />
2022-11-30 09:47:54 +09:00
<slot name="items" :items="items">
<template v-if="virtualScroller">
<DynamicScroller
2022-12-28 07:18:16 +09:00
v-slot="{ item, active, index }"
2022-11-30 09:47:54 +09:00
:items="items"
:min-item-size="200"
:key-field="keyProp"
page-mode
>
2022-12-28 07:18:16 +09:00
<slot
:key="item[keyProp]"
2022-12-28 07:18:16 +09:00
:item="item"
:active="active"
:older="items[index + 1]"
:newer="items[index - 1]"
:index="index"
:items="items"
2022-12-28 07:18:16 +09:00
/>
2022-11-30 09:47:54 +09:00
</DynamicScroller>
</template>
<template v-else>
<slot
v-for="item, index of items"
:key="(item as any)[keyProp]"
2022-11-30 09:47:54 +09:00
:item="item"
:older="items[index + 1]"
:newer="items[index - 1]"
:index="index"
:items="items"
2022-11-30 09:47:54 +09:00
/>
</template>
</slot>
2022-11-17 16:35:42 +09:00
<div ref="endAnchor" />
2022-11-27 14:02:19 +09:00
<slot v-if="state === 'loading'" name="loading">
<TimelineSkeleton />
2022-11-27 14:02:19 +09:00
</slot>
<slot v-else-if="state === 'done' && endMessage !== false" name="done">
<div p5 text-secondary italic text-center>
{{ t(typeof endMessage === 'string' ? endMessage : 'common.end_of_list') }}
</div>
</slot>
<div v-else-if="state === 'error'" p5 text-secondary>
{{ t('common.error') }}: {{ error }}
2022-11-17 16:35:42 +09:00
</div>
2022-11-17 01:11:08 +09:00
</div>
</template>