Files
BewlyBewly/src/contentScripts/views/Home/components/Following.vue
Hakadao d074844240 perf: video card, closes #760, #762 (#779)
* perf: video card

* refactor: video card

* refactor(ForYou): remove loading icon

* refactor(ForYou): remove unused 'url' property in video card component

* refactor(ForYou): remove unused 'url' property in video card component

* fix(VideoCard): prevent `a` tag bubble

* perf: improve video card performance

* refactor(ForYou): optimize video card loading and rending

* refactor(VideoCard): optimize video card loading and rendering

* perf(ForYou): use another method to generate a unique id

* perf(VideoCard): remove hover animation

* feat(VideoCard): add hover & active effect again...

* feat(VideoCard):  adjust hovering & activating effect

* perf(VideoCard): remove the hover effect

* refactor(VideoCard): optimize lazy loading for images

* perf: optimize scrolling performance

* perf(VideoCard): optimize video preview performance
2024-05-22 00:25:34 +08:00

208 lines
6.0 KiB
Vue

<script setup lang="ts">
import type { Ref } from 'vue'
import Button from '~/components/Button.vue'
import Empty from '~/components/Empty.vue'
import VideoCard from '~/components/VideoCard/VideoCard.vue'
import { useApiClient } from '~/composables/api'
import { useBewlyApp } from '~/composables/useAppProvider'
import type { GridLayout } from '~/logic'
import type { DataItem as MomentItem, MomentResult } from '~/models/moment/moment'
// https://github.com/starknt/BewlyBewly/blob/fad999c2e482095dc3840bb291af53d15ff44130/src/contentScripts/views/Home/components/ForYou.vue#L16
interface VideoElement {
uniqueId: string
item?: MomentItem
}
const props = defineProps<{
gridLayout: GridLayout
}>()
const emit = defineEmits<{
(e: 'beforeLoading'): void
(e: 'afterLoading'): void
}>()
const gridValue = computed((): string => {
if (props.gridLayout === 'adaptive')
return '~ 2xl:cols-5 xl:cols-4 lg:cols-3 md:cols-2 gap-5'
if (props.gridLayout === 'twoColumns')
return '~ cols-1 xl:cols-2 gap-4'
return '~ cols-1 gap-4'
})
const api = useApiClient()
const videoList = ref<VideoElement[]>([])
const isLoading = ref<boolean>(false)
const needToLoginFirst = ref<boolean>(false)
const containerRef = ref<HTMLElement>() as Ref<HTMLElement>
const offset = ref<string>('')
const updateBaseline = ref<string>('')
const noMoreContent = ref<boolean>(false)
const { handleReachBottom, handlePageRefresh } = useBewlyApp()
onMounted(async () => {
initData()
initPageAction()
})
onActivated(() => {
initPageAction()
})
function initPageAction() {
handleReachBottom.value = async () => {
if (isLoading.value)
return
if (noMoreContent.value)
return
getData()
}
handlePageRefresh.value = async () => {
if (isLoading.value)
return
initData()
}
}
async function initData() {
offset.value = ''
updateBaseline.value = ''
videoList.value.length = 0
noMoreContent.value = false
await getData()
}
async function getData() {
for (let i = 0; i < 3; i++)
await getFollowedUsersVideos()
}
async function getFollowedUsersVideos() {
if (noMoreContent.value)
return
if (offset.value === '0') {
noMoreContent.value = true
return
}
emit('beforeLoading')
isLoading.value = true
try {
let i = 0
// https://github.com/starknt/BewlyBewly/blob/fad999c2e482095dc3840bb291af53d15ff44130/src/contentScripts/views/Home/components/ForYou.vue#L208
// When video list is not empty, addthe number of pending videos is half of the page size
// is set to prevent user scrolling the page too fast and causing the page too laggy
const pendingVideos: VideoElement[] = Array.from({ length: videoList.value.length ? 10 : 30 }, () => ({
uniqueId: `unique-id-${(videoList.value.length || 0) + i++})}`,
} satisfies VideoElement))
let lastVideoListLength = videoList.value.length
videoList.value.push(...pendingVideos)
const response: MomentResult = await api.moment.getMoments({
type: 'video',
offset: Number(offset.value),
update_baseline: updateBaseline.value,
})
if (response.code === -101) {
noMoreContent.value = true
needToLoginFirst.value = true
return
}
if (response.code === 0) {
offset.value = response.data.offset
updateBaseline.value = response.data.update_baseline
const resData = [] as MomentItem[]
response.data.items.forEach((item: MomentItem) => {
resData.push(item)
})
// when videoList has length property, it means it is the first time to load
if (!videoList.value.length) {
videoList.value = resData.map(item => ({ uniqueId: `${item.id_str}`, item }))
}
else {
resData.forEach((item) => {
videoList.value[lastVideoListLength++] = {
uniqueId: `${item.id_str}`,
item,
}
})
}
}
else if (response.code === -101) {
needToLoginFirst.value = true
}
}
finally {
videoList.value = videoList.value.filter(video => video.item)
isLoading.value = false
emit('afterLoading')
}
}
function jumpToLoginPage() {
location.href = 'https://passport.bilibili.com/login'
}
defineExpose({ initData })
</script>
<template>
<div>
<!-- By directly using predefined unocss grid properties, it is possible to dynamically set the grid attribute -->
<div hidden grid="~ 2xl:cols-5 xl:cols-4 lg:cols-3 md:cols-2 gap-5" />
<div hidden grid="~ cols-1 xl:cols-2 gap-4" />
<div hidden grid="~ cols-1 gap-4" />
<Empty v-if="needToLoginFirst" mt-6 :description="$t('common.please_log_in_first')">
<Button type="primary" @click="jumpToLoginPage()">
{{ $t('common.login') }}
</Button>
</Empty>
<div
v-else
ref="containerRef"
m="b-0 t-0" relative w-full h-full
:grid="gridValue"
>
<VideoCard
v-for="video in videoList"
:key="video.uniqueId"
:skeleton="!video.item"
:video="video.item ? {
id: Number(video.item.modules.module_dynamic.major.archive?.aid),
durationStr: video.item.modules.module_dynamic.major.archive?.duration_text,
title: `${video.item.modules.module_dynamic.major.archive?.title}`,
cover: `${video.item.modules.module_dynamic.major.archive?.cover}`,
author: video.item.modules.module_author.name,
authorFace: video.item.modules.module_author.face,
mid: video.item.modules.module_author.mid,
viewStr: video.item.modules.module_dynamic.major.archive?.stat.play,
danmakuStr: video.item.modules.module_dynamic.major.archive?.stat.danmaku,
capsuleText: video.item.modules.module_author.pub_time,
bvid: video.item.modules.module_dynamic.major.archive?.bvid,
} : undefined"
show-preview
:horizontal="gridLayout !== 'adaptive'"
/>
</div>
<!-- no more content -->
<Empty v-if="noMoreContent && !needToLoginFirst" class="pb-4" :description="$t('common.no_more_content')" />
</div>
</template>
<style lang="scss" scoped>
</style>