Skip to content

Commit

Permalink
Fix #9
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitz committed Sep 19, 2024
1 parent 441a9a6 commit f3ca837
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ dist-ssr
*.sln
*.sw?
business-cards-logo-zip-file.zip
business_card_app.cards.json
33 changes: 17 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@mantine/form": "^7.12.1",
"@mantine/hooks": "^7.12.1",
"@mantine/notifications": "^7.12.1",
"@react-hook/window-size": "^3.1.1",
"@stylistic/eslint-plugin": "^2.6.3",
"eslint-plugin-prefer-arrow": "^1.2.3",
"eslint-plugin-react": "^7.35.0",
Expand Down
38 changes: 33 additions & 5 deletions src/pages/Cards.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { NavLink, useLoaderData, useLocation, useRouteLoaderData } from 'react-router-dom'
import { CardsLoaderReturnData } from '../loadersActions'
import { Masonry } from 'masonic'
import { MasonryScroller, useContainerPosition, usePositioner, useResizeObserver } from 'masonic'
import MasonryCard from '../components/cards/MasonryCard'
import { Affix, Button, useMatches } from '@mantine/core'
import { TbPlus } from 'react-icons/tb'
import { useLang } from '../context/lang/useLang'
import { CachedCardResponse, Role } from '../types'
import { Role } from '../types'
import { useRoles } from '../hooks/useRoles'
import { useRef } from 'react'
import { useWindowSize } from '@react-hook/window-size'

const cardCreateRoles: Role[] = ['admin', 'business']

Expand Down Expand Up @@ -34,11 +36,37 @@ const Cards = () => {
0
))

// We only want to use positioner when the collection has gotten smaller, because it causes a flicker
// https://github.com/jaredLunde/masonic/issues/12#issuecomment-1710000167
const lengthRef = useRef<number>(0)

const containerRef = useRef(null)
const [windowWidth, windowHeight] = useWindowSize()
const { offset, width } = useContainerPosition(containerRef, [
windowWidth,
windowHeight
])
const positioner = usePositioner(
{ width, columnWidth, columnGutter },
[cards.length < lengthRef.current]
)
const resizeObserver = useResizeObserver(positioner)

lengthRef.current = cards.length

return (
<>
<Masonry items={[...cards]} render={MasonryCard} key={pathname}
columnWidth={columnWidth} columnGutter={columnGutter}
itemKey={(data, index) => (data as CachedCardResponse | undefined)?._id ?? index} />
<MasonryScroller
positioner={positioner}
resizeObserver={resizeObserver}
containerRef={containerRef}
items={cards}
height={windowHeight}
offset={offset}
overscanBy={6}
render={MasonryCard}
key={pathname}
/>
{
hasRole(...cardCreateRoles) &&
<Affix position={{ top: 80, right: 20 }}>
Expand Down

0 comments on commit f3ca837

Please sign in to comment.