elk/components/common/CommonPaginator.vue

122 lines
3.3 KiB
Vue
Raw Permalink Normal View History

<script setup lang="ts" generic="T, O, U = T">
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'
2024-01-09 11:56:15 +03:00
import type { mastodon } from 'masto'
2022-11-16 19:11:08 +03:00
2022-12-28 01:18:16 +03:00
const {
paginator,
stream,
eventType,
2022-12-28 01:18:16 +03:00
keyProp = 'id',
virtualScroller = false,
preprocess,
endMessage = true,
2022-12-28 01:18:16 +03:00
} = defineProps<{
2024-01-09 11:56:15 +03:00
paginator: mastodon.Paginator<T[], O>
keyProp?: keyof T
2022-11-28 16:14:27 +03:00
virtualScroller?: boolean
2024-01-09 11:56:15 +03:00
stream?: mastodon.streaming.Subscription
eventType?: 'update' | 'notification'
preprocess?: (items: (U | T)[]) => U[]
endMessage?: boolean | string
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: (props: {
items: U[]
item: U
index: number
2022-11-27 11:54:00 +03:00
active?: boolean
older: U
newer: U // newer is undefined when index === 0
}) => void
items: (props: {
items: U[]
}) => void
updater: (props: {
number: number
update: () => void
}) => void
loading: (props: object) => void
2023-07-05 09:33:41 +03:00
done: (props: { items: U[] }) => void
2022-11-27 11:54:00 +03:00
}>()
const { t } = useI18n()
const nuxtApp = useNuxtApp()
const { items, prevItems, update, state, endAnchor, error } = usePaginator(paginator, toRef(() => 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-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 }" />
<slot name="items" :items="items as U[]">
2022-11-30 03:47:54 +03:00
<template v-if="virtualScroller">
<DynamicScroller
2022-12-28 01:18:16 +03:00
v-slot="{ item, active, index }"
2022-11-30 03:47:54 +03:00
:items="items"
:min-item-size="200"
:key-field="keyProp"
page-mode
>
2022-12-28 01:18:16 +03:00
<slot
2023-05-29 14:46:45 +03:00
v-bind="{ key: item[keyProp] }"
2022-12-28 01:18:16 +03:00
:item="item"
:active="active"
:older="items[index + 1] as U"
:newer="items[index - 1] as U"
:index="index"
:items="items as U[]"
2022-12-28 01:18:16 +03:00
/>
2022-11-30 03:47:54 +03:00
</DynamicScroller>
</template>
<template v-else>
<slot
v-for="(item, index) of items"
v-bind="{ key: (item as U)[keyProp as keyof U] }"
:item="item as U"
:older="items[index + 1] as U"
:newer="items[index - 1] as U"
:index="index"
:items="items as U[]"
2022-11-30 03:47:54 +03:00
/>
</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">
<TimelineSkeleton />
2022-11-27 08:02:19 +03:00
</slot>
2023-07-05 09:33:41 +03:00
<slot v-else-if="state === 'done' && endMessage !== false" name="done" :items="items as U[]">
<div p5 text-secondary italic text-center>
{{ t(typeof endMessage === 'string' && items.length <= 0 ? endMessage : 'common.end_of_list') }}
</div>
</slot>
<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>