Skip to content

Commit

Permalink
perf: improve the performance of long lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Clarkkkk committed Jul 31, 2024
1 parent 7595469 commit 60fab9f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 29 deletions.
6 changes: 5 additions & 1 deletion src/components/AddToSonglist.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { ref } from 'vue'
import { onMounted, ref } from 'vue'
import type { ComponentExposed } from 'vue-component-type-helpers'
import { storeToRefs } from 'pinia'
import { Image } from 'components'
Expand All @@ -13,6 +13,10 @@ const { updateSonglists } = useSonglistsStore()
const { createdSonglists } = storeToRefs(useSonglistsStore())
const songIdToAdd = ref(0)
onMounted(() => {
console.log('mounted')
})
async function onClick(songlistId: number) {
if (!songIdToAdd.value || !songlistId) return
await post<ApiPlaylistTracks>('/playlist/tracks', {
Expand Down
13 changes: 2 additions & 11 deletions src/components/ContextMenu/ContextMenu.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { provide, ref, watch } from 'vue'
import { Tippy } from 'vue-tippy'
import { useDeviceType, useIsHovering } from 'services'
import { useDeviceType } from 'services'
import type { Instance, Placement } from 'tippy.js'
import Button from '../Button.vue'
Expand All @@ -26,7 +26,6 @@ const { isPc } = useDeviceType()
const tippy = ref<Instance | null>(null)
const body = document.body
const visible = ref(false)
const { isHovering, onMouseEnter, onMouseLeave } = useIsHovering()
const initialized = ref(false)
function showMenu() {
Expand All @@ -49,21 +48,13 @@ watch(visible, (val) => {
provide('ContextMenu', { hideMenu, visible })
const unwatch = watch(isHovering, (val) => {
if (val) {
initialized.value = true
unwatch()
}
})
defineExpose({ showMenu, hideMenu })
</script>

<template>
<div
class="context-menu"
@mouseenter="onMouseEnter"
@mouseleave="onMouseLeave"
@mouseenter="() => (initialized = true)"
>
<Tippy
v-if="isPc && initialized"
Expand Down
23 changes: 11 additions & 12 deletions src/components/SongItem/SongItem.vue
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
<script setup lang="ts">
import { ref } from 'vue'
import { storeToRefs } from 'pinia'
import { useDeviceType, useIsHovering } from 'services'
import { usePlaylistStore } from 'src/stores'
import Image from '../Image.vue'
import SongItemOption from './SongItemOption.vue'
defineProps<{ song: Song }>()
const { isHovering, onMouseEnter, onMouseLeave } = useIsHovering()
const { isPc } = useDeviceType()
const { currentSong } = storeToRefs(usePlaylistStore())
const shouldRenderOption = ref(false)
</script>

<template>
<li
class="song-item my-2 flex h-14 w-full items-center rounded-lg p-2 transition-all duration-500 @container contain-strict content-auto intrinsic-w-auto-20 intrinsic-h-auto-14 hover:bg-base-200/50"
@mouseenter="onMouseEnter"
@mouseleave="onMouseLeave"
class="song-item group my-2 flex h-14 w-full items-center rounded-lg p-2 transition-all duration-500 @container contain-strict content-auto intrinsic-w-auto-20 intrinsic-h-auto-14 hover:bg-base-200/50"
@mouseenter="() => (shouldRenderOption = true)"
>
<div class="image-container relative mr-2 h-10 w-10 flex-fixed">
<Image
Expand All @@ -27,8 +25,7 @@ const { currentSong } = storeToRefs(usePlaylistStore())
'h-full',
'w-full',
'flex-fixed',
'rounded-lg',
{ hovering: isHovering }
'rounded-lg'
]"
loading="lazy"
:size="40"
Expand Down Expand Up @@ -101,10 +98,12 @@ const { currentSong } = storeToRefs(usePlaylistStore())
</RouterLink>
</div>
<slot />
<SongItemOption
:song="song"
:class="['ml-2', 'transition-all', { 'opacity-0': !isHovering && isPc }]"
/>
<div class="ml-2 h-8 w-8 flex-shrink-0 opacity-0 transition-all md:group-hover:opacity-100">
<SongItemOption
v-if="shouldRenderOption"
:song="song"
/>
</div>
</li>
</template>

Expand Down
2 changes: 1 addition & 1 deletion src/pages/Home/components/NewSongList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const {
list: visibleList,
containerProps,
wrapperProps
} = useVirtualList(listRef, { itemHeight: 72 })
} = useVirtualList(listRef, { itemHeight: 72, overscan: 15 })
</script>

<template>
Expand Down
5 changes: 4 additions & 1 deletion src/pages/UserCenter/components/FavoriteSongs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const props = defineProps<{ id: number }>()
const { songlist, onFullLoad, initSonglist } = useSonglist()
const { switchToThisList } = usePlaylistStore()
const { list, containerProps, wrapperProps } = useVirtualList(songlist, { itemHeight: 72 })
const { list, containerProps, wrapperProps } = useVirtualList(songlist, {
itemHeight: 72,
overscan: 15
})
async function onPlayAll() {
if (songlist.value.length) {
Expand Down
6 changes: 4 additions & 2 deletions src/pages/UserCenter/components/RecentSongs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ const {
containerProps: weekContainerProps,
wrapperProps: weekWrapperProps
} = useVirtualList<RecentSong>(weekList, {
itemHeight: 72
itemHeight: 72,
overscan: 20
})
const {
list: visibleAllList,
containerProps: allContainerProps,
wrapperProps: allWrapperProps
} = useVirtualList<RecentSong>(allList, {
itemHeight: 72
itemHeight: 72,
overscan: 15
})
async function getData(type: 0 | 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const songlistArr = computed(() => {
}
})
})
const { list, containerProps, wrapperProps } = useVirtualList(songlistArr, { itemHeight: 72 })
const { list, containerProps, wrapperProps } = useVirtualList(songlistArr, {
itemHeight: 72,
overscan: 15
})
async function onTabChange() {
currentTab.value = currentTab.value === 'created-list' ? 'collected-list' : 'created-list'
Expand Down

0 comments on commit 60fab9f

Please sign in to comment.