2022-11-14 18:48:51 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import classNames from 'classnames';
|
2023-01-04 00:53:22 +00:00
|
|
|
import React, { useEffect, useRef, useCallback, useMemo } from 'react';
|
2022-11-14 18:48:51 +00:00
|
|
|
import type { Index, ListRowRenderer } from 'react-virtualized';
|
|
|
|
import { List } from 'react-virtualized';
|
|
|
|
import { ScrollBehavior } from '../types/Util';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
rowCount: number;
|
|
|
|
calculateRowHeight: (index: number) => number;
|
|
|
|
rowRenderer: ListRowRenderer;
|
|
|
|
scrollToIndex?: number;
|
|
|
|
scrollable?: boolean;
|
|
|
|
className?: string;
|
|
|
|
shouldRecomputeRowHeights?: boolean;
|
|
|
|
scrollBehavior?: ScrollBehavior;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Thin wrapper around react-virtualized List. Simplified API and provides common
|
|
|
|
* defaults.
|
|
|
|
*/
|
2022-11-18 00:45:19 +00:00
|
|
|
export function ListView({
|
2022-11-14 18:48:51 +00:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
rowCount,
|
|
|
|
calculateRowHeight,
|
|
|
|
rowRenderer,
|
|
|
|
scrollToIndex,
|
|
|
|
className,
|
|
|
|
scrollable = true,
|
|
|
|
shouldRecomputeRowHeights = false,
|
|
|
|
scrollBehavior = ScrollBehavior.Default,
|
2022-11-18 00:45:19 +00:00
|
|
|
}: Props): JSX.Element {
|
2022-11-14 18:48:51 +00:00
|
|
|
const listRef = useRef<null | List>(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const list = listRef.current;
|
|
|
|
if (shouldRecomputeRowHeights && list) {
|
|
|
|
list.recomputeRowHeights();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-04 00:53:22 +00:00
|
|
|
const rowHeight = useCallback(
|
|
|
|
(index: Index) => calculateRowHeight(index.index),
|
|
|
|
[calculateRowHeight]
|
|
|
|
);
|
|
|
|
|
2023-04-20 17:03:43 +00:00
|
|
|
const style: React.CSSProperties = useMemo(() => {
|
2023-01-04 00:53:22 +00:00
|
|
|
return {
|
|
|
|
// See `<Timeline>` for an explanation of this `any` cast.
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
overflowY: scrollable ? ('overlay' as any) : 'hidden',
|
2023-04-20 17:03:43 +00:00
|
|
|
direction: 'inherit',
|
2023-01-04 00:53:22 +00:00
|
|
|
};
|
|
|
|
}, [scrollable]);
|
|
|
|
|
2022-11-14 18:48:51 +00:00
|
|
|
return (
|
|
|
|
<List
|
|
|
|
className={classNames(
|
|
|
|
'ListView',
|
|
|
|
`ListView--scroll-behavior-${scrollBehavior}`,
|
|
|
|
className
|
|
|
|
)}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
ref={listRef}
|
|
|
|
rowCount={rowCount}
|
2023-01-04 00:53:22 +00:00
|
|
|
rowHeight={rowHeight}
|
2022-11-14 18:48:51 +00:00
|
|
|
rowRenderer={rowRenderer}
|
|
|
|
scrollToIndex={scrollToIndex}
|
2023-01-04 00:53:22 +00:00
|
|
|
style={style}
|
2022-11-14 18:48:51 +00:00
|
|
|
tabIndex={-1}
|
|
|
|
/>
|
|
|
|
);
|
2022-11-18 00:45:19 +00:00
|
|
|
}
|