Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"clsx": "^1.1.1",
"facepaint": "^1.2.1",
"file-loader": "^6.2.0",
"intersection-observer": "^0.12.0",
"prism-react-renderer": "^1.2.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
Expand Down
35 changes: 17 additions & 18 deletions src/components/WikiTable/WikiTable.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import React, { useEffect } from 'react';
import React, { useCallback, useEffect, useRef } from 'react';
import { WikiWord } from 'types';

import WikiTableRow from './WikiTableRow';
import useInfiniteScroll from '../../hooks/useInfiniteScroll';

import usePagination from '../../hooks/usePagination';

export default function WikiTable({ words = [] }: { words: string[] }) {
const { onPrevious, onNext, currentPage, result, isLastPage, isFirstPage } =
usePagination({
source: words,
offset: 2,
});
export default function WikiTable({ words = [] }: { words: WikiWord[] }) {
const { result, onNext } = useInfiniteScroll({
source: words,
});
const getLastRow = (index): boolean => {
return result.length - 1 == index;
};

return (
<>
<span>{currentPage}</span>
<button onClick={onPrevious} disabled={isFirstPage}>
Previous
</button>
<button onClick={onNext} disabled={isLastPage}>
Next
</button>
<table width="100%" summary="Web Analytics Handbook 용어사전">
<thead>
<tr style={{ borderBottom: 'none' }}>
Expand All @@ -28,8 +22,13 @@ export default function WikiTable({ words = [] }: { words: string[] }) {
</tr>
</thead>
<tbody>
{result.map(word => (
<WikiTableRow key={word.name} {...word} />
{result.map((word, index) => (
<WikiTableRow
key={word.name}
{...word}
last={getLastRow(index)}
onNext={onNext}
/>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

layer tree가 업데이트 될 때마다 table의 width 값이 계속 바뀌네요 ㅎㅎㅠ 새로 추가되는 콘텐츠에 따라 기존에 있던 table의 너비가 달라지니 당연한 거긴 한데 사용자 경험 측면에서 자연스럽지 않아 보이는군요.. 반응형을 생각했을 때 table 구조가 용어 사전을 보기에 적절한 UI인지도 논의가 필요할 것 같아요

2021-10-08.8.45.00.mov

.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하 영상보고 이해가 되었습니다! 이부분은 적절한 UI로 변경되야될것으로 보이네요!

))}
</tbody>
</table>
Expand Down
30 changes: 24 additions & 6 deletions src/components/WikiTable/WikiTableRow.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import React from 'react';
import useIntersectionObserver from 'hooks/useIntersectionObserver';
import React, { useEffect } from 'react';

import { WikiWord } from '../../types';
type wikiTableRowProps = {
description: string;
name: string;
last: boolean;
onNext: () => void;
};
export default function WikiTableRow({
name,
description,
last,
onNext,
}: wikiTableRowProps) {
const { ref, entry } = useIntersectionObserver({ freezeOnceVisible: true });

export default function WikiTableRow({ name, description }: WikiWord) {
return (
<tr>
useEffect(() => {
if (entry?.isIntersecting) onNext();
}, [entry]);

const content = (
<>
<td>{name}</td>
<td>{description}</td>
</tr>
</>
);

return last ? <tr ref={ref}>{content}</tr> : <tr>{content}</tr>;
}
34 changes: 34 additions & 0 deletions src/hooks/useInfiniteScroll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useEffect, useState } from 'react';

const usePagination = ({ source = [], limit = 10, offset = 0 }) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prop에 대해 interface를 정의하고 각 prop에 대한 설명이 있으면 좋을 것 같아요 :)

interface Props {
 source
 limit
 offset
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분은 생각지 못했는데 감사합니다! 설명!

const getResult = () => {
const startIdx = 0;
const endIdx = currentOffest;
return source.slice(startIdx, endIdx);
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

타입 지정되면 좋을 것 같아요~

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵! 타입지정!


const getMaxOffset = () => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return type도 명시되면 좋을 것 같습니다 ㅎㅎ

Suggested change
const getMaxOffset = () => {
const getMaxOffset = (): number => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵! 타입지정!!!@!

return source.length;
};

const [currentOffest, setCurrentOffest] = useState(limit);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

state generic에는 type 지정을 해주는 게 좋아요 :) 지금은 limit 값으로 초기화를 해줘서 typescript가 setCurrentOffest() 함수를 썼을 때 number 타입 이외에 다른 값이 들어가지 않게 추론을 해주긴 하지만 IDE 환경에 의존해야 하는 부분이 있거든요. 초기화를 안했을 경우에는 어떤 타입으로 할당해야 하는지 추론이 안되구요 ㅎㅎ 그래서 일관성을 위해 다음처럼 작성하면 좋습니당!

Suggested change
const [currentOffest, setCurrentOffest] = useState(limit);
const [currentOffest, setCurrentOffest] = useState<number>(limit);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵! 타입지정을 자꾸 빼먹고 작성을하게 되네용! 꼭 기억하겠습니다!


useEffect(() => {
if (currentOffest !== limit) setCurrentOffest(limit);
else getResult();
}, [source]);

const onNext = () => {
if (currentOffest >= getMaxOffset()) return;
setCurrentOffest(currentOffest + limit);
};

return {
result: getResult(),
currentOffest,
isLastOffset: currentOffest > getMaxOffset(),
onNext,
};
};

export default usePagination;
45 changes: 45 additions & 0 deletions src/hooks/useIntersectionObserver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { RefObject, useEffect, useRef, useState } from 'react';
import 'intersection-observer';
interface Args extends IntersectionObserverInit {
freezeOnceVisible?: boolean;
delay?: number;
}

function useIntersectionObserver({
threshold = 0,
root = null,
rootMargin = '0%',
freezeOnceVisible = false,
delay = 0,
}: Args): {
ref: RefObject<Element> | null;
entry: IntersectionObserverEntry | undefined;
} {
const ref = useRef<HTMLFormElement | null>(null);
const [entry, setEntry] = useState<IntersectionObserverEntry>();

const frozen = entry?.isIntersecting && freezeOnceVisible;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boolean type 지정이 있으면 좋을 것 같아요!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵! 타입지정!

const updateEntry = ([entry]: IntersectionObserverEntry[]): void => {
setTimeout(() => {
setEntry(entry);
}, delay);
};

useEffect(() => {
const node = ref?.current; // DOM Ref
const hasIOSupport = !!window.IntersectionObserver;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intersection-observer 플러그인을 import 해주셔서 폴리필 미지원으로 인한 이슈는 없을 것 같은데 해당 조건이 필요한 케이스가 있을까용?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

없을 것으로 보입니다! 제거하는게 좋아보입니다!

if (!hasIOSupport || frozen || !node) return;

const observerParams = { threshold, root, rootMargin };
const observer = new IntersectionObserver(updateEntry, observerParams);

observer.observe(node);
return () => observer.disconnect();

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ref, threshold, root, rootMargin, frozen]);

return { ref, entry };
}

export default useIntersectionObserver;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so 깔끔한 훅이네요! 굿굿