Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 0e2bab0

Browse files
rodion-arrbenhalverson
authored andcommitted
refactor(Article): remove redundant ifs
1 parent d3f7689 commit 0e2bab0

1 file changed

Lines changed: 36 additions & 42 deletions

File tree

src/components/Article/index.tsx

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,65 +43,59 @@ const Article = ({
4343
};
4444

4545
React.useEffect((): (() => void) => {
46-
let observer: IntersectionObserver;
47-
4846
if (window.history.state && window.history.state.articleScrollTo) {
4947
window.scrollTo({
5048
top: window.history.state.articleScrollTo,
5149
});
5250
}
5351

54-
if (element.current) {
55-
const handleObserverThrottled = throttle(
56-
300,
57-
(entries: IntersectionObserverEntry[]) => {
58-
entries.forEach((entry): void => {
59-
// element is already hidden by the nav
60-
if (entry.boundingClientRect.y < NAV_HEIGHT) {
61-
if (!entry.target.previousElementSibling) {
62-
window.history.replaceState(
63-
{
64-
articleScrollTo: null,
65-
},
66-
'',
67-
null
68-
);
69-
return;
70-
}
71-
52+
const handleObserverThrottled = throttle(
53+
300,
54+
(entries: IntersectionObserverEntry[]) => {
55+
entries.forEach((entry): void => {
56+
// element is already hidden by the nav
57+
if (entry.boundingClientRect.y < NAV_HEIGHT) {
58+
if (!entry.target.previousElementSibling) {
7259
window.history.replaceState(
7360
{
74-
articleScrollTo: document.documentElement.scrollTop,
61+
articleScrollTo: null,
7562
},
7663
'',
7764
null
7865
);
66+
return;
7967
}
80-
});
81-
}
82-
);
8368

84-
observer = new IntersectionObserver(
85-
(entries): void => {
86-
handleObserverThrottled(entries);
87-
},
88-
{
89-
threshold: [0.25, 0.5, 0.75],
90-
rootMargin: `-${NAV_HEIGHT}px 0px 0px 0px`,
91-
}
92-
);
69+
window.history.replaceState(
70+
{
71+
articleScrollTo: document.documentElement.scrollTop,
72+
},
73+
'',
74+
null
75+
);
76+
}
77+
});
78+
}
79+
);
9380

94-
Array.from(element.current.children).forEach((children): void => {
95-
observer.observe(children);
96-
});
97-
}
81+
const observer = new IntersectionObserver(
82+
(entries): void => {
83+
handleObserverThrottled(entries);
84+
},
85+
{
86+
threshold: [0.25, 0.5, 0.75],
87+
rootMargin: `-${NAV_HEIGHT}px 0px 0px 0px`,
88+
}
89+
);
90+
91+
Array.from(element.current.children).forEach((children): void => {
92+
observer.observe(children);
93+
});
9894

9995
return (): void => {
100-
if (observer && element.current) {
101-
Array.from(element.current.children).forEach((children): void => {
102-
observer.unobserve(children);
103-
});
104-
}
96+
Array.from(element.current.children).forEach((children): void => {
97+
observer.unobserve(children);
98+
});
10599
};
106100
}, []);
107101

0 commit comments

Comments
 (0)