Skip to content

Commit 4254fa5

Browse files
feat: implement scroll position persistence in Cards component (#75)
1 parent ac1698c commit 4254fa5

File tree

1 file changed

+34
-1
lines changed
  • frontend/src/pages/collection/components

1 file changed

+34
-1
lines changed

frontend/src/pages/collection/components/Cards.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Card, CollectionRow } from '@/types'
55
import { type Row, createColumnHelper, getCoreRowModel, getGroupedRowModel, useReactTable } from '@tanstack/react-table'
66
import { useVirtualizer } from '@tanstack/react-virtual'
77
import { ID, type Models } from 'appwrite'
8-
import { type FC, useMemo, useRef } from 'react'
8+
import { type FC, useEffect, useMemo, useRef, useState } from 'react'
99
import FancyCard from '../../../components/FancyCard.tsx'
1010
import type { Card as CardType } from '../../../types'
1111

@@ -88,6 +88,39 @@ export const Cards: FC<Props> = ({ user, ownedCards, setOwnedCards }) => {
8888

8989
const Pack = ({ cards }: { cards: CardType[] }) => {
9090
const parentRef = useRef<HTMLDivElement>(null)
91+
const [scrollPosition, setScrollPosition] = useState(() => {
92+
const savedPosition = localStorage.getItem('scrollPosition')
93+
return savedPosition ? Number.parseInt(savedPosition, 10) : 0
94+
})
95+
96+
useEffect(() => {
97+
const handleScroll = () => {
98+
if (parentRef.current) {
99+
const newPosition = parentRef.current.scrollTop
100+
setScrollPosition(newPosition)
101+
localStorage.setItem('scrollPosition', newPosition.toString())
102+
console.log('handleScroll - scrollPosition:', newPosition)
103+
}
104+
}
105+
106+
const parentElement = parentRef.current
107+
if (parentElement) {
108+
parentElement.addEventListener('scroll', handleScroll)
109+
}
110+
111+
return () => {
112+
if (parentElement) {
113+
parentElement.removeEventListener('scroll', handleScroll)
114+
}
115+
}
116+
}, [])
117+
118+
useEffect(() => {
119+
if (parentRef.current) {
120+
parentRef.current.scrollTop = scrollPosition
121+
console.log('useEffect - scrollPosition set to:', scrollPosition)
122+
}
123+
}, [scrollPosition])
91124

92125
const columns = useMemo(() => {
93126
return [

0 commit comments

Comments
 (0)