Skip to content

Commit

Permalink
todo: Optimize redundant requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray-D-Song committed Oct 30, 2024
1 parent 13f66c0 commit d7d11ba
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
33 changes: 33 additions & 0 deletions packages/web/src/hooks/useMediaQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useEffect, useState } from 'react'

export function useMediaQuery(query: string) {
const [matches, setMatches] = useState(false)

useEffect(() => {
const mediaQuery = window.matchMedia(query)
setMatches(mediaQuery.matches)

let timeoutId: NodeJS.Timeout

const handler = (event: MediaQueryListEvent) => {
if (timeoutId) {
clearTimeout(timeoutId)
}

timeoutId = setTimeout(() => {
setMatches(event.matches)
}, 100)
}

mediaQuery.addEventListener('change', handler)

return () => {
mediaQuery.removeEventListener('change', handler)
if (timeoutId) {
clearTimeout(timeoutId)
}
}
}, [query])

return matches
}
46 changes: 38 additions & 8 deletions packages/web/src/pages/(layout)/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,51 @@
import { useRequest } from 'ahooks'
import { memo, useMemo } from 'react'
import { useMediaQuery } from '~/hooks/useMediaQuery'
import { PageCardList } from '~/components/card-view'
import PageDataPieCard from '~/components/page-data-pie-card'
import R2UsageCard from '~/components/r2-usage-card'
import { getRecentSavePage } from '~/data/page'

const MemoizedPageCardList = memo(PageCardList)

function ArchiveHome() {
const { data: pages } = useRequest(getRecentSavePage)
const { data: pages = [] } = useRequest(getRecentSavePage)
const isLargeScreen = useMediaQuery('(min-width: 1024px)')
const isMediumScreen = useMediaQuery('(min-width: 768px)')

const columnCount = useMemo(() => {
if (isLargeScreen)
return 3
if (isMediumScreen)
return 2
return 1
}, [isLargeScreen, isMediumScreen])

const reorganizedPages = useMemo(() => {
const result = Array.from({ length: columnCount }, () => [] as typeof pages)

pages.forEach((page, index) => {
const columnIndex = Math.floor(index / Math.ceil(pages.length / columnCount))
result[columnIndex].push(page)
})

return result
}, [pages, columnCount])

return (
<div className="p-4 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
<PageCardList pages={pages?.filter((_, index) => index % 3 === 0)} onPageDelete={() => {}}>
<MemoizedPageCardList pages={reorganizedPages[0]} onPageDelete={() => {}}>
<PageDataPieCard />
</PageCardList>
<PageCardList pages={pages?.filter((_, index) => index % 3 === 1)} onPageDelete={() => {}}>
<R2UsageCard />
</PageCardList>
<PageCardList pages={pages?.filter((_, index) => index % 3 === 2)} onPageDelete={() => {}}>
</PageCardList>
</MemoizedPageCardList>
{columnCount >= 2 && (
<MemoizedPageCardList pages={reorganizedPages[1]} onPageDelete={() => {}}>
<R2UsageCard />
</MemoizedPageCardList>
)}
{columnCount >= 3 && (
<MemoizedPageCardList pages={reorganizedPages[2]} onPageDelete={() => {}}>
</MemoizedPageCardList>
)}
</div>
)
}
Expand Down

0 comments on commit d7d11ba

Please sign in to comment.