2023-01-09 15:23:15 +03:00
|
|
|
<script setup lang="ts" generic="T, O, U = T">
|
2022-11-27 09:16:02 +03:00
|
|
|
// @ts-expect-error missing types
|
2022-11-26 22:57:59 +03:00
|
|
|
import { DynamicScroller } from 'vue-virtual-scroller'
|
|
|
|
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'
|
2023-01-06 21:50:31 +03:00
|
|
|
import type { Paginator, WsEvents } from 'masto'
|
2023-05-29 10:16:34 +03:00
|
|
|
import type { UnwrapRef } from 'vue'
|
2022-11-16 19:11:08 +03:00
|
|
|
|
2022-12-28 01:18:16 +03:00
|
|
|
const {
|
|
|
|
paginator,
|
|
|
|
stream,
|
|
|
|
keyProp = 'id',
|
|
|
|
virtualScroller = false,
|
|
|
|
eventType = 'update',
|
|
|
|
preprocess,
|
2023-04-30 18:49:33 +03:00
|
|
|
endMessage = true,
|
2022-12-28 01:18:16 +03:00
|
|
|
} = defineProps<{
|
2023-01-08 10:49:32 +03:00
|
|
|
paginator: Paginator<T[], O>
|
|
|
|
keyProp?: keyof T
|
2022-11-28 16:14:27 +03:00
|
|
|
virtualScroller?: boolean
|
2022-12-29 00:43:46 +03:00
|
|
|
stream?: Promise<WsEvents>
|
2022-12-02 05:21:10 +03:00
|
|
|
eventType?: 'notification' | 'update'
|
2023-01-09 18:39:59 +03:00
|
|
|
preprocess?: (items: (U | T)[]) => U[]
|
2023-04-30 18:49:33 +03:00
|
|
|
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<{
|
2023-05-29 10:16:34 +03:00
|
|
|
default: (props: {
|
2023-01-09 15:23:15 +03:00
|
|
|
items: U[]
|
|
|
|
item: U
|
2023-01-08 11:51:45 +03:00
|
|
|
index: number
|
2022-11-27 11:54:00 +03:00
|
|
|
active?: boolean
|
2023-05-29 10:16:34 +03:00
|
|
|
older: U
|
|
|
|
newer: U // newer is undefined when index === 0
|
|
|
|
}) => void
|
|
|
|
items: (props: {
|
|
|
|
items: UnwrapRef<U[]>
|
|
|
|
}) => void
|
|
|
|
updater: (props: {
|
2022-11-28 14:18:45 +03:00
|
|
|
number: number
|
|
|
|
update: () => void
|
2023-05-29 10:16:34 +03:00
|
|
|
}) => void
|
2023-08-02 13:28:18 +03:00
|
|
|
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
|
|
|
}>()
|
|
|
|
|
2023-01-08 10:49:32 +03:00
|
|
|
const { t } = useI18n()
|
2023-01-31 06:42:46 +03:00
|
|
|
const nuxtApp = useNuxtApp()
|
2023-01-08 10:49:32 +03:00
|
|
|
|
2023-01-15 11:38:02 +03:00
|
|
|
const { items, prevItems, update, state, endAnchor, error } = usePaginator(paginator, $$(stream), eventType, preprocess)
|
2023-01-31 06:42:46 +03:00
|
|
|
|
|
|
|
nuxtApp.hook('elk-logo:click', () => {
|
|
|
|
update()
|
|
|
|
nuxtApp.$scrollToTop()
|
|
|
|
})
|
2023-02-03 00:02:39 +03:00
|
|
|
|
|
|
|
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>
|
2022-11-28 14:18:45 +03:00
|
|
|
<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
|
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"
|
2023-05-29 10:16:34 +03:00
|
|
|
:older="items[index + 1] as U"
|
|
|
|
:newer="items[index - 1] as U"
|
2023-01-08 11:51:45 +03:00
|
|
|
:index="index"
|
2023-05-29 10:16:34 +03:00
|
|
|
: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
|
2022-12-28 02:25:41 +03:00
|
|
|
v-for="item, index of items"
|
2023-05-29 14:46:45 +03:00
|
|
|
v-bind="{ key: item[keyProp as keyof U] }"
|
2023-05-29 10:16:34 +03:00
|
|
|
:item="item as U"
|
|
|
|
:older="items[index + 1] as U"
|
|
|
|
:newer="items[index - 1] as U"
|
2023-01-08 11:51:45 +03:00
|
|
|
:index="index"
|
2023-05-29 10:16:34 +03:00
|
|
|
: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">
|
2022-12-30 18:34:23 +03:00
|
|
|
<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[]">
|
2023-01-06 21:29:44 +03:00
|
|
|
<div p5 text-secondary italic text-center>
|
2023-10-24 16:01:57 +03:00
|
|
|
{{ t(typeof endMessage === 'string' && items.length <= 0 ? endMessage : 'common.end_of_list') }}
|
2023-01-06 21:29:44 +03:00
|
|
|
</div>
|
|
|
|
</slot>
|
2022-11-26 23:41:18 +03:00
|
|
|
<div v-else-if="state === 'error'" p5 text-secondary>
|
2023-01-08 10:49:32 +03:00
|
|
|
{{ t('common.error') }}: {{ error }}
|
2022-11-17 10:35:42 +03:00
|
|
|
</div>
|
2022-11-16 19:11:08 +03:00
|
|
|
</div>
|
|
|
|
</template>
|