From 3348d5a324afa7345b66cfc92132707c99409720 Mon Sep 17 00:00:00 2001 From: Xevion Date: Fri, 17 Feb 2023 00:57:25 -0600 Subject: [PATCH] Add infinite scrolling --- package.json | 1 + src/pages/index.tsx | 52 ++++++++++++++++++--------------------------- yarn.lock | 12 +++++++++++ 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 77e3d77..4fa8332 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "react": "18.2.0", "react-dom": "18.2.0", "react-icons": "^4.7.1", + "react-infinite-scroll-component": "^6.1.0", "react-no-ssr": "^1.1.0", "sass": "^1.58.1", "usehooks-ts": "^2.9.1", diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 3bfbab5..5ba72fb 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -7,21 +7,23 @@ import {useDebounce} from "usehooks-ts"; import {AiOutlineLoading3Quarters} from "react-icons/ai"; import IconCatalog from "@components/render/IconCatalog"; import NoSSR from 'react-no-ssr'; +import InfiniteScroll from "react-infinite-scroll-component"; -const icons = iconList as { id: string, setId: string }[]; +export type IconIdentifierPair = { id: string, setId: string }; +const icons = iconList as IconIdentifierPair[]; const Home: NextPage = () => { const [query, setQuery] = useState(""); const [showLoading, setShowLoading] = useState(false); const [timing, setTiming] = useState(0); - const debouncedQuery = useDebounce(query, 300); + const debouncedQuery = useDebounce(query, 800); useEffect(() => { setShowLoading(query.length != 0); }, [query]); - const filtered = useMemo(() => { + const allFilteredIcons = useMemo(() => { const start = new Date(); const results = debouncedQuery.length == 0 ? [] : icons.filter(({id}) => id.toLowerCase().indexOf(query) !== -1); const end = new Date(); @@ -32,9 +34,12 @@ const Home: NextPage = () => { return results; }, [debouncedQuery]); - const catalog = useMemo(() => { - return i).slice(0, 100)}/>; - }, [filtered]); + useEffect(() => { + setLazyIcons(icons.slice(0, 100)) + }, [allFilteredIcons]); + + + const [lazyIcons, setLazyIcons] = useState([]); return ( <> @@ -56,7 +61,7 @@ const Home: NextPage = () => { {showLoading ? : (query.length == 0 ? icons.length - : filtered.length)} + : allFilteredIcons.length)} { timing !== 0 @@ -66,31 +71,16 @@ const Home: NextPage = () => {
- {catalog} + {useMemo(() => Loading...} + dataLength={lazyIcons.length} + next={() => { + const newLength = Math.min(lazyIcons.length + 300, allFilteredIcons.length) + setLazyIcons(allFilteredIcons.slice(0, newLength)) + }}> + + , [lazyIcons])}
- {/*
    - - - - -
*/} ); diff --git a/yarn.lock b/yarn.lock index 6dfe8d2..c880c4a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2151,6 +2151,13 @@ react-icons@^4.7.1: resolved "https://registry.yarnpkg.com/react-icons/-/react-icons-4.7.1.tgz#0f4b25a5694e6972677cb189d2a72eabea7a8345" integrity sha512-yHd3oKGMgm7zxo3EA7H2n7vxSoiGmHk5t6Ou4bXsfcgWyhfDKMpyKfhHR6Bjnn63c+YXBLBPUql9H4wPJM6sXw== +react-infinite-scroll-component@^6.1.0: + version "6.1.0" + resolved "https://registry.yarnpkg.com/react-infinite-scroll-component/-/react-infinite-scroll-component-6.1.0.tgz#7e511e7aa0f728ac3e51f64a38a6079ac522407f" + integrity sha512-SQu5nCqy8DxQWpnUVLx7V7b7LcA37aM7tvoWjTLZp1dk6EJibM5/4EJKzOnl07/BsM1Y40sKLuqjCwwH/xV0TQ== + dependencies: + throttle-debounce "^2.1.0" + react-is@^16.12.0, react-is@^16.13.1, react-is@^16.7.0: version "16.13.1" resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4" @@ -2457,6 +2464,11 @@ text-table@^0.2.0: resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4" integrity sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw== +throttle-debounce@^2.1.0: + version "2.3.0" + resolved "https://registry.yarnpkg.com/throttle-debounce/-/throttle-debounce-2.3.0.tgz#fd31865e66502071e411817e241465b3e9c372e2" + integrity sha512-H7oLPV0P7+jgvrk+6mwwwBDmxTaxnu9HMXmloNLXwnNO0ZxZ31Orah2n8lU1eMPvsaowP2CX+USCgyovXfdOFQ== + tiny-glob@^0.2.9: version "0.2.9" resolved "https://registry.yarnpkg.com/tiny-glob/-/tiny-glob-0.2.9.tgz#2212d441ac17928033b110f8b3640683129d31e2"