refactor(HistoryDatabase): optimize card expansion behavior with debounce and animation lock

- Introduced a debounce mechanism to prevent rapid state changes during card expansion and collapse.
- Added an animation lock to avoid conflicts during transitions.
- Adjusted Intersection Observer thresholds and rootMargin for smoother detection and stability.
- Cleaned up resources on component unmounting to prevent memory leaks.
This commit is contained in:
666ghj 2026-01-09 10:46:32 +08:00
parent d90ec77f5d
commit dfab81a6c0

View file

@ -104,6 +104,8 @@ const isExpanded = ref(false)
const hoveringCard = ref(null)
const historyContainer = ref(null)
let observer = null
let isAnimating = false //
let expandDebounceTimer = null //
// -
const CARDS_PER_ROW = 4
@ -290,19 +292,48 @@ onMounted(() => {
loadHistory()
// 使 Intersection Observer /
// 使
observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
isExpanded.value = true
} else {
isExpanded.value = false
//
if (isAnimating) return
const shouldExpand = entry.isIntersecting
//
if (shouldExpand === isExpanded.value) return
//
if (expandDebounceTimer) {
clearTimeout(expandDebounceTimer)
expandDebounceTimer = null
}
// 使
// (50ms)(200ms)
const delay = shouldExpand ? 50 : 200
expandDebounceTimer = setTimeout(() => {
//
if (isAnimating) return
//
isAnimating = true
isExpanded.value = shouldExpand
// 700ms
setTimeout(() => {
isAnimating = false
}, 750)
}, delay)
})
},
{
threshold: [0.5],
rootMargin: '0px 0px -150px 0px'
// 使使
threshold: [0.3, 0.5, 0.7],
// rootMargin使
rootMargin: '0px 0px -100px 0px'
}
)
@ -315,10 +346,16 @@ onMounted(() => {
})
onUnmounted(() => {
// Intersection Observer
if (observer) {
observer.disconnect()
observer = null
}
//
if (expandDebounceTimer) {
clearTimeout(expandDebounceTimer)
expandDebounceTimer = null
}
})
</script>