feat: connected replies in home (#572)

This commit is contained in:
patak 2022-12-27 18:47:05 +01:00 committed by GitHub
parent 1c61aef83b
commit 4f3a065927
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 10 deletions

View file

@ -4,18 +4,21 @@ import { DynamicScroller } from 'vue-virtual-scroller'
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css' import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'
import type { Paginator, WsEvents } from 'masto' import type { Paginator, WsEvents } from 'masto'
const { paginator, stream, keyProp = 'id', virtualScroller = false, eventType = 'update' } = defineProps<{ const { paginator, stream, keyProp = 'id', virtualScroller = false, eventType = 'update', preprocess } = defineProps<{
paginator: Paginator<any, any[]> paginator: Paginator<any, any[]>
keyProp?: string keyProp?: string
virtualScroller?: boolean virtualScroller?: boolean
stream?: WsEvents stream?: WsEvents
eventType?: 'notification' | 'update' eventType?: 'notification' | 'update'
preprocess?: (items: any[]) => any[]
}>() }>()
defineSlots<{ defineSlots<{
default: { default: {
item: any item: any
active?: boolean active?: boolean
older?: any
newer?: any
} }
updater: { updater: {
number: number number: number
@ -24,7 +27,7 @@ defineSlots<{
loading: {} loading: {}
}>() }>()
const { items, prevItems, update, state, endAnchor, error } = usePaginator(paginator, stream, eventType) const { items, prevItems, update, state, endAnchor, error } = usePaginator(paginator, stream, eventType, preprocess)
</script> </script>
<template> <template>
@ -44,9 +47,11 @@ const { items, prevItems, update, state, endAnchor, error } = usePaginator(pagin
</template> </template>
<template v-else> <template v-else>
<slot <slot
v-for="item of items" v-for="item, i of items"
:key="item[keyProp]" :key="item[keyProp]"
:item="item" :item="item"
:older="items[i + 1]"
:newer="items[i - 1]"
/> />
</template> </template>
</slot> </slot>

View file

@ -1,12 +1,29 @@
<script setup lang="ts"> <script setup lang="ts">
import type { Status } from 'masto'
const paginator = useMasto().timelines.iterateHome() const paginator = useMasto().timelines.iterateHome()
const stream = await useMasto().stream.streamUser() const stream = await useMasto().stream.streamUser()
onBeforeUnmount(() => stream.disconnect()) onBeforeUnmount(() => stream.disconnect())
const maxDistance = 10
function preprocess(items: Status[]) {
const newItems = [...items]
// TODO: Basic reordering, we should get something more efficient and robust
for (let i = items.length - 1; i > 0; i--) {
for (let k = 1; k <= maxDistance && i - k >= 0; k++) {
if (newItems[i - k].inReplyToId === newItems[i].id) {
const item = newItems.splice(i, 1)[0]
newItems.splice(i - k, 0, item)
k = 1
}
}
}
return newItems
}
</script> </script>
<template> <template>
<div> <div>
<PublishWidget draft-key="home" border="b base" /> <PublishWidget draft-key="home" border="b base" />
<TimelinePaginator v-bind="{ paginator, stream }" context="home" /> <TimelinePaginator v-bind="{ paginator, stream, preprocess }" context="home" />
</div> </div>
</template> </template>

View file

@ -8,26 +8,37 @@ const { paginator, stream } = defineProps<{
paginator: Paginator<any, Status[]> paginator: Paginator<any, Status[]>
stream?: WsEvents stream?: WsEvents
context?: FilterContext context?: FilterContext
preprocess?: (items: any[]) => any[]
}>() }>()
const virtualScroller = $(computedEager(() => useFeatureFlags().experimentalVirtualScroll)) const virtualScroller = $(computedEager(() => useFeatureFlags().experimentalVirtualScroll))
</script> </script>
<template> <template>
<CommonPaginator v-bind="{ paginator, stream }" :virtual-scroller="virtualScroller"> <CommonPaginator v-bind="{ paginator, stream, preprocess }" :virtual-scroller="virtualScroller">
<template #updater="{ number, update }"> <template #updater="{ number, update }">
<button py-4 border="b base" flex="~ col" p-3 w-full text-primary font-bold @click="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) }} {{ $t('timeline.show_new_items', number) }}
</button> </button>
</template> </template>
<template #default="{ item, active }"> <template #default="{ item, older, newer, active }">
<template v-if="virtualScroller"> <template v-if="virtualScroller">
<DynamicScrollerItem :item="item" :active="active" tag="article"> <DynamicScrollerItem :item="item" :active="active" tag="article">
<StatusCard :status="item" border="b base" :context="context" /> <StatusCard
:status="item" :context="context"
:connect-reply="item.id === older?.inReplyToId"
:show-reply-to="!(item.inReplyToId && item.inReplyToId === newer?.id)"
:class="{ 'border-t border-base': !(item.inReplyToId && item.inReplyToId === newer?.id) }"
/>
</DynamicScrollerItem> </DynamicScrollerItem>
</template> </template>
<template v-else> <template v-else>
<StatusCard :status="item" border="b base" :context="context" /> <StatusCard
:status="item" :context="context"
:connect-reply="item.id === older?.inReplyToId"
:show-reply-to="!(item.inReplyToId && item.inReplyToId === newer?.id)"
:class="{ 'border-t border-base': !(item.inReplyToId && item.inReplyToId === newer?.id) }"
/>
</template> </template>
</template> </template>
<template #loading> <template #loading>

View file

@ -2,7 +2,12 @@ import type { Paginator, WsEvents } from 'masto'
import { useDeactivated } from './lifecycle' import { useDeactivated } from './lifecycle'
import type { PaginatorState } from '~/types' import type { PaginatorState } from '~/types'
export function usePaginator<T>(paginator: Paginator<any, T[]>, stream?: WsEvents, eventType: 'notification' | 'update' = 'update') { export function usePaginator<T>(
paginator: Paginator<any, T[]>,
stream?: WsEvents,
eventType: 'notification' | 'update' = 'update',
preprocess: (items: T[]) => T[] = (items: T[]) => items,
) {
const state = ref<PaginatorState>(isMastoInitialised.value ? 'idle' : 'loading') const state = ref<PaginatorState>(isMastoInitialised.value ? 'idle' : 'loading')
const items = ref<T[]>([]) const items = ref<T[]>([])
const nextItems = ref<T[]>([]) const nextItems = ref<T[]>([])
@ -52,7 +57,7 @@ export function usePaginator<T>(paginator: Paginator<any, T[]>, stream?: WsEvent
const result = await paginator.next() const result = await paginator.next()
if (result.value?.length) { if (result.value?.length) {
nextItems.value = result.value nextItems.value = preprocess(result.value) as any
items.value.push(...nextItems.value) items.value.push(...nextItems.value)
state.value = 'idle' state.value = 'idle'
} }