Skip to content

Commit 45c46fd

Browse files
committed
observer scroll debounce, installed lodash.debounce and types
1 parent 56a3505 commit 45c46fd

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

docs/working-notes/todo4.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ extract types
102102

103103
-----
104104
scroll to top, has bug in article mdx layout, bottom position and horizontal scroll
105-
add delay to fetch new page
105+
add delay to fetch new page, debounce
106106
phone tap on backdrop doesnt close lightbox
107107
responsive page size
108+
usehooks, isVisible or observer hook for infinite scroll, thumbnail, scroll to top
108109
```
109110

110111

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"feed": "^4.2.2",
4848
"giscus": "^1.6.0",
4949
"github-slugger": "^2.0.0",
50+
"lodash.debounce": "^4.0.8",
5051
"mdast-util-to-string": "^4.0.0",
5152
"object-treeify": "^5.0.1",
5253
"photoswipe": "^5.4.4",
@@ -66,6 +67,7 @@
6667
"@ianvs/prettier-plugin-sort-imports": "^4.4.1",
6768
"@iconify-json/mdi": "^1.2.3",
6869
"@tailwindcss/typography": "^0.5.16",
70+
"@types/lodash.debounce": "^4.0.9",
6971
"@types/mdast": "^4.0.3",
7072
"@types/react": "^18.3.12",
7173
"@types/react-dom": "^18.3.1",

src/components/react/NewGallery.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { useEffect, useMemo, useRef, useState } from 'react';
22

3+
import debounce from 'lodash.debounce';
34
import PhotoSwipeLightbox from 'photoswipe/lightbox';
45

56
import type { ImageProps } from '@/libs/gallery/transform';
67
import type { FC } from 'react';
78

89
import 'photoswipe/style.css';
910

10-
import { GALLERY_ID } from '@/constants/gallery';
11+
import { GALLERY } from '@/constants/gallery';
1112
import { cn } from '@/utils/styles';
1213

1314
interface Props {
@@ -16,10 +17,7 @@ interface Props {
1617

1718
type LoadedStates = Record<string, boolean>;
1819

19-
// step
20-
const PAGE_SIZE = 3 as const; // Todo: make it responsive
21-
// page dependency in useEffect is more important
22-
const INITIAL_PAGE = 1 as const;
20+
const { PAGE_SIZE, INITIAL_PAGE, OBSERVER_DEBOUNCE, GALLERY_ID } = GALLERY;
2321

2422
const fetchImagesUpToPage = (images: ImageProps[], nextPage: number): ImageProps[] => {
2523
const endIndex = nextPage * PAGE_SIZE;
@@ -55,11 +53,12 @@ const NewGallery: FC<Props> = ({ images }) => {
5553
setPage((prevPage) => prevPage + 1);
5654
}
5755
};
56+
const debouncedCallback = debounce(callback, OBSERVER_DEBOUNCE);
5857
const options: IntersectionObserverInit = { threshold: 1 };
59-
const observer = new IntersectionObserver(callback, options);
6058

61-
const observerRef = observerTarget.current;
59+
const observer = new IntersectionObserver(debouncedCallback, options);
6260

61+
const observerRef = observerTarget.current;
6362
if (observerRef) observer.observe(observerRef);
6463

6564
return () => {

src/constants/gallery.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
export const EXCLUDE_IMAGES = ['avatar1.jpg', 'square-night1.jpg'] as const;
22

3-
export const GALLERY_ID = 'my-gallery' as const;
3+
export const GALLERY = {
4+
GALLERY_ID: 'my-gallery',
5+
// Todo: make it responsive
6+
/** step. */
7+
PAGE_SIZE: 3,
8+
/** page dependency in useEffect is more important */
9+
INITIAL_PAGE: 1,
10+
/** fine tuned for scroll */
11+
OBSERVER_DEBOUNCE: 150,
12+
} as const;

yarn.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,18 @@
14011401
resolved "https://registry.yarnpkg.com/@types/is-empty/-/is-empty-1.2.3.tgz#a2d55ea8a5ec57bf61e411ba2a9e5132fe4f0899"
14021402
integrity sha512-4J1l5d79hoIvsrKh5VUKVRA1aIdsOb10Hu5j3J2VfP/msDnfTdGPmNp2E1Wg+vs97Bktzo+MZePFFXSGoykYJw==
14031403

1404+
"@types/lodash.debounce@^4.0.9":
1405+
version "4.0.9"
1406+
resolved "https://registry.yarnpkg.com/@types/lodash.debounce/-/lodash.debounce-4.0.9.tgz#0f5f21c507bce7521b5e30e7a24440975ac860a5"
1407+
integrity sha512-Ma5JcgTREwpLRwMM+XwBR7DaWe96nC38uCBDFKZWbNKD+osjVzdpnUSwBcqCptrp16sSOLBAUb50Car5I0TCsQ==
1408+
dependencies:
1409+
"@types/lodash" "*"
1410+
1411+
"@types/lodash@*":
1412+
version "4.17.16"
1413+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.17.16.tgz#94ae78fab4a38d73086e962d0b65c30d816bfb0a"
1414+
integrity sha512-HX7Em5NYQAXKW+1T+FiuG27NGwzJfCX3s1GjOa7ujxZa52kjJLOr4FUxT+giF6Tgxv1e+/czV/iTtBw27WTU9g==
1415+
14041416
"@types/mdast@^4.0.0", "@types/mdast@^4.0.3":
14051417
version "4.0.4"
14061418
resolved "https://registry.yarnpkg.com/@types/mdast/-/mdast-4.0.4.tgz#7ccf72edd2f1aa7dd3437e180c64373585804dd6"
@@ -4139,6 +4151,11 @@ lodash.castarray@^4.4.0:
41394151
resolved "https://registry.yarnpkg.com/lodash.castarray/-/lodash.castarray-4.4.0.tgz#c02513515e309daddd4c24c60cfddcf5976d9115"
41404152
integrity sha512-aVx8ztPv7/2ULbArGJ2Y42bG1mEQ5mGjpdvrbJcJFU3TbYybe+QlLS4pst9zV52ymy2in1KpFPiZnAOATxD4+Q==
41414153

4154+
lodash.debounce@^4.0.8:
4155+
version "4.0.8"
4156+
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
4157+
integrity sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow==
4158+
41424159
lodash.isplainobject@^4.0.6:
41434160
version "4.0.6"
41444161
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"

0 commit comments

Comments
 (0)