perf(listings): 优化从详情页返回首页的滚动恢复体验,消除顶部闪跳
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
import {
|
||||
defaultAWRecycleConfig,
|
||||
defaultCooperationFeedbackConfig,
|
||||
defaultHomeAnnouncements,
|
||||
defaultHomeBanners,
|
||||
type AWRecycleConfig,
|
||||
type CooperationFeedbackConfig,
|
||||
type HomeBannerSlide,
|
||||
} from '@/features/listings/api/homeConfig'
|
||||
import {
|
||||
emptyListingPublishOptions,
|
||||
type ListingPublishOptions,
|
||||
} from '@/features/listings/api/listingOptions'
|
||||
import type { Listing } from '@/features/listings/api/listings'
|
||||
|
||||
export interface PcHomeListSnapshot {
|
||||
signature: string
|
||||
listings: Listing[]
|
||||
totalListings: number
|
||||
zoneCounts: Record<string, number>
|
||||
currentPage: number
|
||||
hasMoreListings: boolean
|
||||
}
|
||||
|
||||
export interface PcHomeConfigSnapshot {
|
||||
announcements: string[]
|
||||
banners: HomeBannerSlide[]
|
||||
awRecycle: AWRecycleConfig
|
||||
cooperationFeedback: CooperationFeedbackConfig
|
||||
publishOptions: ListingPublishOptions
|
||||
}
|
||||
|
||||
/** PC端首页列表/配置/滚动缓存:与页面实例解耦,从详情页返回时可无缝秒级恢复,避免回顶闪烁 */
|
||||
export const usePcHomeCacheStore = defineStore('pcHomeCache', {
|
||||
state: () => ({
|
||||
listSignature: '',
|
||||
listings: [] as Listing[],
|
||||
totalListings: 0,
|
||||
zoneCounts: {} as Record<string, number>,
|
||||
currentPage: 1,
|
||||
hasMoreListings: true,
|
||||
scrollTop: 0,
|
||||
announcements: [...defaultHomeAnnouncements] as string[],
|
||||
banners: defaultHomeBanners.map(slide => ({ ...slide })) as HomeBannerSlide[],
|
||||
awRecycle: { ...defaultAWRecycleConfig } as AWRecycleConfig,
|
||||
cooperationFeedback: { ...defaultCooperationFeedbackConfig } as CooperationFeedbackConfig,
|
||||
publishOptions: { ...emptyListingPublishOptions } as ListingPublishOptions,
|
||||
homeConfigLoaded: false,
|
||||
}),
|
||||
getters: {
|
||||
hasListSnapshot(state): boolean {
|
||||
return state.listSignature !== '' && state.listings.length > 0
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
matchList(signature: string): boolean {
|
||||
return (
|
||||
this.listSignature !== '' && this.listSignature === signature && this.listings.length > 0
|
||||
)
|
||||
},
|
||||
saveList(payload: PcHomeListSnapshot) {
|
||||
this.listSignature = payload.signature
|
||||
this.listings = payload.listings
|
||||
this.totalListings = payload.totalListings
|
||||
this.zoneCounts = payload.zoneCounts
|
||||
this.currentPage = payload.currentPage
|
||||
this.hasMoreListings = payload.hasMoreListings
|
||||
},
|
||||
saveScrollTop(top: number) {
|
||||
this.scrollTop = Math.max(0, top)
|
||||
},
|
||||
saveHomeConfig(payload: PcHomeConfigSnapshot) {
|
||||
this.announcements = payload.announcements
|
||||
this.banners = payload.banners
|
||||
this.awRecycle = payload.awRecycle
|
||||
this.cooperationFeedback = payload.cooperationFeedback
|
||||
this.publishOptions = payload.publishOptions
|
||||
this.homeConfigLoaded = true
|
||||
},
|
||||
clearList() {
|
||||
this.listSignature = ''
|
||||
this.listings = []
|
||||
this.totalListings = 0
|
||||
this.zoneCounts = {}
|
||||
this.currentPage = 1
|
||||
this.hasMoreListings = true
|
||||
this.scrollTop = 0
|
||||
},
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user