feat: implement paginator context

This commit is contained in:
Ayo 2023-01-10 20:08:36 +01:00
parent 1cd6448bc5
commit a2a76651a5
3 changed files with 10 additions and 7 deletions

View file

@ -2,7 +2,7 @@
// @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'
import type { Paginator, WsEvents, mastodon } from 'masto'
const {
paginator,
@ -11,13 +11,15 @@ const {
virtualScroller = false,
eventType = 'update',
preprocess,
context = 'public',
} = defineProps<{
paginator: Paginator<T[], O>
keyProp?: keyof T
virtualScroller?: boolean
stream?: Promise<WsEvents>
eventType?: 'notification' | 'update'
preprocess?: (items: (U | T)[]) => U[]
preprocess?: (items: (U | T)[], context?: mastodon.v2.FilterContext) => U[]
context?: mastodon.v2.FilterContext
}>()
defineSlots<{
@ -42,7 +44,7 @@ defineSlots<{
const { t } = useI18n()
const { items, prevItems, update, state, endAnchor, error } = usePaginator(paginator, stream, eventType, preprocess)
const { items, prevItems, update, state, endAnchor, error } = usePaginator(paginator, stream, eventType, preprocess, context)
</script>
<template>

View file

@ -22,7 +22,7 @@ const showOriginSite = $computed(() =>
</script>
<template>
<CommonPaginator v-bind="{ paginator, stream, preprocess, buffer }" :virtual-scroller="virtualScroller">
<CommonPaginator v-bind="{ paginator, stream, preprocess, buffer }" :virtual-scroller="virtualScroller" :context="context">
<template #updater="{ number, update }">
<button py-4 border="b base" flex="~ col" p-3 w-full text-primary font-bold @click="update">
{{ $t('timeline.show_new_items', number, { named: { v: formatNumber(number) } }) }}

View file

@ -5,7 +5,8 @@ export function usePaginator<T, P, U = T>(
_paginator: Paginator<T[], P>,
stream?: Promise<WsEvents>,
eventType: 'notification' | 'update' = 'update',
preprocess: (items: (T | U)[]) => U[] = items => items as unknown as U[],
preprocess: (items: (T | U)[], context?: mastodon.v2.FilterContext) => U[] = items => items as unknown as U[],
context?: mastodon.v2.FilterContext,
buffer = 10,
) {
// called `next` method will mutate the internal state of the variable,
@ -25,7 +26,7 @@ export function usePaginator<T, P, U = T>(
const deactivated = useDeactivated()
async function update() {
(items.value as U[]).unshift(...preprocess(prevItems.value as T[]))
(items.value as U[]).unshift(...preprocess(prevItems.value as T[], context))
prevItems.value = []
}
@ -70,7 +71,7 @@ export function usePaginator<T, P, U = T>(
const result = await paginator.next()
if (!result.done && result.value.length) {
const preprocessedItems = preprocess([...nextItems.value, ...result.value] as (U | T)[])
const preprocessedItems = preprocess([...nextItems.value, ...result.value] as (U | T)[], context)
const itemsToShowCount
= preprocessedItems.length <= buffer
? preprocessedItems.length