diff --git a/src/components/FileList.tsx b/src/components/FileList.tsx index 976fb95..326bd2f 100644 --- a/src/components/FileList.tsx +++ b/src/components/FileList.tsx @@ -1,15 +1,20 @@ //@ts-nocheck import { Box, Center, Text } from '@mantine/core'; import EventEmitter from 'events'; -import { includes, isEmpty, map, not, pipe, sort } from 'ramda'; -import { FC, useCallback, useContext } from 'react'; +import { head, includes, indexOf, isEmpty, length, mergeLeft, not, pluck } from 'ramda'; +import { FC, useCallback, useContext, useLayoutEffect, useMemo, useRef } from 'react'; +import { useMeasure } from 'react-use'; +import { FixedSizeList } from 'react-window'; import { EventBusContext } from '../EventBus'; -import { useFileListStore } from '../states/files'; +import { sortedFilesSelector, useFileListStore } from '../states/files'; export const FileList: FC = () => { - const files = useFileListStore.use.files(); + const files = useFileListStore(sortedFilesSelector()); const activeFiles = useFileListStore.use.actives(); const ebus = useContext(EventBusContext); + const filesCount = useMemo(() => length(files), [files]); + const [parentRef, { height: parentHeight }] = useMeasure(); + const listRef = useRef(); const handleFileSelectAction = useCallback( (filename: string) => { ebus.emit('navigate_offset', { filename }); @@ -17,26 +22,43 @@ export const FileList: FC = () => { [ebus] ); + useLayoutEffect(() => { + let firstActived = head(activeFiles); + let firstActivedIndex = indexOf(firstActived, pluck('filename', files)); + listRef.current?.scrollToItem(firstActivedIndex, 'smart'); + }, [activeFiles]); + return ( - - {pipe( - sort((fa, fb) => fa.sort - fb.sort), - map(item => ( - handleFileSelectAction(item.filename)} - sx={theme => ({ - cursor: 'pointer', - '&:hover': { color: not(includes(item.filename, activeFiles)) && theme.colors.red } - })} - > - {item.filename} - - )) - )(files)} + + {!isEmpty(files) && ( + + {({ index, style }) => ( + handleFileSelectAction(files[index].filename)} + sx={theme => + mergeLeft(style, { + cursor: 'pointer', + '&:hover': { + color: not(includes(files[index].filename, activeFiles)) && theme.colors.red + } + }) + } + > + {files[index].filename} + + )} + + )} {isEmpty(files) && (
请先打开一个文件夹。