Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/js/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import styled from 'styled-components';
import { useEffect } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
import { EpisodeSearch } from './EpisodesSearch';
import { UnderConstructionBanner } from './UnderConstructionBanner';
import { EpisodeSearchFallback } from './EpisodeSearchFallback';
import { useObserver } from './hooks/useObserver';

const Wrapper = styled.div`
display: flex;
Expand Down Expand Up @@ -57,6 +59,12 @@ const Wrapper = styled.div`
`;

export const App = () => {
const { entry, ref } = useObserver();

useEffect(() => {
console.info(entry);
}, [entry]);

return (
<Wrapper>
<header>
Expand All @@ -71,6 +79,7 @@ export const App = () => {
<h1>transcript.fish</h1>
<UnderConstructionBanner />
<img
ref={ref}
className="nstaaf-logo"
src="https://media.transcript.fish/images/logo.jpg"
/>
Expand Down
36 changes: 36 additions & 0 deletions src/js/hooks/useObserver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useEffect, useRef, useState } from 'react';

const nArray = (n: number) => [...Array(n), 1].map((_, i) => i / n);

const options = {
root: null,
threshold: nArray(4),
};

export const useObserver = () => {
const ref = useRef(null);
const [entry, setEntry] = useState<IntersectionObserverEntry>();

useEffect(() => {
const observer = new IntersectionObserver(([entry]) => {
setEntry(entry);
}, options);

const { current } = ref;

if (current) {
observer.observe(current);
}

return () => {
if (current) {
observer.unobserve(current);
}
};
}, []);

return {
entry,
ref,
};
};