fix row height

This commit is contained in:
Hristo Nedelkov 2023-11-23 12:20:40 +02:00
parent cfea35b4e1
commit 7f436a234f

View File

@ -1,6 +1,6 @@
import { Button, Text } from '@status-im/components' import { Button, Text } from '@status-im/components'
import { FixedSizeList, FixedSizeList as List } from 'react-window' import { VariableSizeList as List } from 'react-window'
import InfiniteLoader from 'react-window-infinite-loader' import InfiniteLoader from 'react-window-infinite-loader'
import './scroller.css' import './scroller.css'
import { Stack, XStack } from 'tamagui' import { Stack, XStack } from 'tamagui'
@ -23,24 +23,24 @@ const LogsTerminal = ({ windowWidth, dropdownMenuItem, timestamps }: LogsTermina
const [isScrolling, setIsScrolling] = useState(false) const [isScrolling, setIsScrolling] = useState(false)
const [loadedIndexes, setLoadedIndexes] = useState<{ [key: number]: boolean }>({}) const [loadedIndexes, setLoadedIndexes] = useState<{ [key: number]: boolean }>({})
const listRef = useRef<List | null>(null) const listRef = useRef<List | null>(null)
const [shouldAutoScroll, setShouldAutoScroll] = useState(true) const [shouldAutoScroll, setShouldAutoScroll] = useState(true)
useEffect(() => { useEffect(() => {
const interval = setInterval(() => { const interval = setInterval(() => {
addNewLog() //addNewLog()
}, 3000) }, 3000)
return () => clearInterval(interval) return () => clearInterval(interval)
}, [isScrolling]) }, [isScrolling])
useEffect(() => { useEffect(() => {
if (listRef.current && shouldAutoScroll) { if (shouldAutoScroll) {
console.log(listRef.current)
setTimeout(() => { setTimeout(() => {
if (listRef.current) listRef.current.scrollToItem(data.length - 1, 'end') listRef.current?.scrollToItem(data.length - 1, 'end')
}, 500) }, 500)
} }
}, [data, shouldAutoScroll]) }, [data.length, shouldAutoScroll])
const loadMoreItems = async (startIndex: number, stopIndex: number) => { const loadMoreItems = async (startIndex: number, stopIndex: number) => {
return new Promise<void>(resolve => { return new Promise<void>(resolve => {
@ -67,22 +67,36 @@ const LogsTerminal = ({ windowWidth, dropdownMenuItem, timestamps }: LogsTermina
const newIndex = data.length const newIndex = data.length
setData(prevData => [...prevData, newLog]) setData(prevData => [...prevData, newLog])
setLoadedIndexes(prev => ({ ...prev, [newIndex]: true })) setLoadedIndexes(prev => ({ ...prev, [newIndex]: true }))
if (shouldAutoScroll) {
listRef.current?.scrollToItem(data.length, 'end')
}
} }
} }
const handleScroll = ({ scrollTop, scrollHeight, clientHeight }: any) => { const handleScroll = ({ scrollTop, scrollHeight, clientHeight }: any) => {
if (scrollTop < scrollHeight - clientHeight) { const isAtBottom = scrollTop + clientHeight >= scrollHeight
setIsScrolling(true)
setShouldAutoScroll(false)
} else {
setIsScrolling(false)
if (!shouldAutoScroll && scrollTop === scrollHeight - clientHeight) { if (isAtBottom) {
setShouldAutoScroll(true) setShouldAutoScroll(true)
} else {
setShouldAutoScroll(false)
} }
} }
} function itemSize(index: number) {
const oneLineCountChars = 165
const descriptionLength = data[index].description.length
const value1 = 76
const value2 = 120
const value3 = 250
if (descriptionLength <= oneLineCountChars) {
return value1
} else if (descriptionLength <= oneLineCountChars * 2) {
return value2
} else {
return value3
}
}
return ( return (
<Stack <Stack
style={{ style={{
@ -97,7 +111,7 @@ const LogsTerminal = ({ windowWidth, dropdownMenuItem, timestamps }: LogsTermina
> >
{({ onItemsRendered, ref }) => ( {({ onItemsRendered, ref }) => (
<List <List
ref={(list: FixedSizeList | null) => { ref={(list: List | null) => {
listRef.current = list listRef.current = list
ref(list) ref(list)
console.log(list) console.log(list)
@ -106,8 +120,8 @@ const LogsTerminal = ({ windowWidth, dropdownMenuItem, timestamps }: LogsTermina
height={650} height={650}
width={windowWidth - 500} width={windowWidth - 500}
itemCount={data.length} itemCount={data.length}
itemSize={150}
itemData={data} itemData={data}
itemSize={itemSize}
onItemsRendered={onItemsRendered} onItemsRendered={onItemsRendered}
onScroll={handleScroll} onScroll={handleScroll}
style={{ style={{