Commit c48a0a63 authored by Robert Knight's avatar Robert Knight

Add missing types for ThreadCard, ThreadList

parent 62b149a4
...@@ -42,6 +42,7 @@ function ThreadCard({ frameSync, thread }) { ...@@ -42,6 +42,7 @@ function ThreadCard({ frameSync, thread }) {
); );
const scrollToAnnotation = useCallback( const scrollToAnnotation = useCallback(
/** @param {string} tag */
tag => { tag => {
frameSync.scrollToAnnotation(tag); frameSync.scrollToAnnotation(tag);
}, },
...@@ -72,11 +73,14 @@ function ThreadCard({ frameSync, thread }) { ...@@ -72,11 +73,14 @@ function ThreadCard({ frameSync, thread }) {
onClick={e => { onClick={e => {
// Prevent click events intended for another action from // Prevent click events intended for another action from
// triggering a page scroll. // triggering a page scroll.
if (!isFromButtonOrLink(/** @type {Element} */ (e.target))) { if (
!isFromButtonOrLink(/** @type {Element} */ (e.target)) &&
threadTag
) {
scrollToAnnotation(threadTag); scrollToAnnotation(threadTag);
} }
}} }}
onMouseEnter={() => focusThreadAnnotation(threadTag)} onMouseEnter={() => focusThreadAnnotation(threadTag ?? null)}
onMouseLeave={() => focusThreadAnnotation(null)} onMouseLeave={() => focusThreadAnnotation(null)}
key={thread.id} key={thread.id}
> >
......
...@@ -87,7 +87,7 @@ function ThreadList({ threads }) { ...@@ -87,7 +87,7 @@ function ThreadList({ threads }) {
}, []); }, []);
// Map of thread ID to measured height of thread. // Map of thread ID to measured height of thread.
const [threadHeights, setThreadHeights] = useState({}); const [threadHeights, setThreadHeights] = useState(() => new Map());
// ID of thread to scroll to after the next render. If the thread is not // ID of thread to scroll to after the next render. If the thread is not
// present, the value persists until it can be "consumed". // present, the value persists until it can be "consumed".
...@@ -151,8 +151,9 @@ function ThreadList({ threads }) { ...@@ -151,8 +151,9 @@ function ThreadList({ threads }) {
// Clear `scrollToId` so we don't scroll again after the next render. // Clear `scrollToId` so we don't scroll again after the next render.
setScrollToId(null); setScrollToId(null);
/** @param {Thread} thread */
const getThreadHeight = thread => const getThreadHeight = thread =>
threadHeights[thread.id] || THREAD_DIMENSION_DEFAULTS.defaultHeight; threadHeights.get(thread.id) || THREAD_DIMENSION_DEFAULTS.defaultHeight;
const yOffset = topLevelThreads const yOffset = topLevelThreads
.slice(0, threadIndex) .slice(0, threadIndex)
...@@ -166,7 +167,7 @@ function ThreadList({ threads }) { ...@@ -166,7 +167,7 @@ function ThreadList({ threads }) {
// heights of thread cards and update `threadHeights` state if there are changes. // heights of thread cards and update `threadHeights` state if there are changes.
useEffect(() => { useEffect(() => {
setThreadHeights(prevHeights => { setThreadHeights(prevHeights => {
const changedHeights = {}; const changedHeights = new Map();
for (let { id } of visibleThreads) { for (let { id } of visibleThreads) {
const threadElement = /** @type {HTMLElement} */ ( const threadElement = /** @type {HTMLElement} */ (
document.getElementById(id) document.getElementById(id)
...@@ -186,18 +187,18 @@ function ThreadList({ threads }) { ...@@ -186,18 +187,18 @@ function ThreadList({ threads }) {
} }
const height = getElementHeightWithMargins(threadElement); const height = getElementHeightWithMargins(threadElement);
if (height !== prevHeights[id]) { if (height !== prevHeights.get(id)) {
changedHeights[id] = height; changedHeights.set(id, height);
} }
} }
// Skip update if no heights changed from previous measured values // Skip update if no heights changed from previous measured values
// (or defaults). // (or defaults).
if (Object.keys(changedHeights).length === 0) { if (changedHeights.size === 0) {
return prevHeights; return prevHeights;
} }
return { ...prevHeights, ...changedHeights }; return new Map([...prevHeights, ...changedHeights]);
}); });
}, [visibleThreads]); }, [visibleThreads]);
......
...@@ -19,7 +19,7 @@ describe('sidebar/helpers/visible-threads', () => { ...@@ -19,7 +19,7 @@ describe('sidebar/helpers/visible-threads', () => {
{ id: 't9' }, { id: 't9' },
{ id: 't10' }, { id: 't10' },
]; ];
fakeThreadHeights = {}; fakeThreadHeights = new Map();
fakeWindowHeight = 100; fakeWindowHeight = 100;
fakeDefaultDimensions = { fakeDefaultDimensions = {
defaultHeight: 200, defaultHeight: 200,
......
...@@ -18,7 +18,7 @@ export const THREAD_DIMENSION_DEFAULTS = { ...@@ -18,7 +18,7 @@ export const THREAD_DIMENSION_DEFAULTS = {
* estimating which of the threads are within or near the viewport. * estimating which of the threads are within or near the viewport.
* *
* @param {Thread[]} threads - List of threads in the order they appear * @param {Thread[]} threads - List of threads in the order they appear
* @param {Record<string, number>} threadHeights - Map of thread ID to measured height * @param {Map<string, number>} threadHeights - Map of thread ID to measured height
* @param {number} scrollPos - Vertical scroll offset of scrollable container * @param {number} scrollPos - Vertical scroll offset of scrollable container
* @param {number} windowHeight - * @param {number} windowHeight -
* Height of the visible area of the scrollable container. * Height of the visible area of the scrollable container.
...@@ -43,7 +43,7 @@ export function calculateVisibleThreads( ...@@ -43,7 +43,7 @@ export function calculateVisibleThreads(
let offscreenLowerHeight = 0; let offscreenLowerHeight = 0;
threads.forEach(thread => { threads.forEach(thread => {
const threadHeight = threadHeights[thread.id] || defaultHeight; const threadHeight = threadHeights.get(thread.id) || defaultHeight;
const threadIsAboveViewport = const threadIsAboveViewport =
totalHeight + threadHeight < scrollPos - marginAbove; totalHeight + threadHeight < scrollPos - marginAbove;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment