refactor: remove isTruncated state and update related logic to use mutable refs

feat/rag-2
twwu 10 months ago
parent 611bc728d0
commit 42fd40500a

@ -17,7 +17,6 @@ type FileListProps = {
handleSelectFile: (file: OnlineDriveFile) => void handleSelectFile: (file: OnlineDriveFile) => void
handleOpenFolder: (file: OnlineDriveFile) => void handleOpenFolder: (file: OnlineDriveFile) => void
isLoading: boolean isLoading: boolean
isTruncated: boolean
getOnlineDriveFiles: (params: { getOnlineDriveFiles: (params: {
prefix?: string[] prefix?: string[]
bucket?: string bucket?: string
@ -39,7 +38,6 @@ const FileList = ({
handleOpenFolder, handleOpenFolder,
isInPipeline, isInPipeline,
isLoading, isLoading,
isTruncated,
getOnlineDriveFiles, getOnlineDriveFiles,
}: FileListProps) => { }: FileListProps) => {
const [inputValue, setInputValue] = useState(keywords) const [inputValue, setInputValue] = useState(keywords)
@ -84,7 +82,6 @@ const FileList = ({
handleSelectFile={handleSelectFile} handleSelectFile={handleSelectFile}
isInPipeline={isInPipeline} isInPipeline={isInPipeline}
isLoading={isLoading} isLoading={isLoading}
isTruncated={isTruncated}
getOnlineDriveFiles={getOnlineDriveFiles} getOnlineDriveFiles={getOnlineDriveFiles}
/> />
</div> </div>

@ -12,7 +12,6 @@ type FileListProps = {
selectedFileList: string[] selectedFileList: string[]
keywords: string keywords: string
isInPipeline: boolean isInPipeline: boolean
isTruncated: boolean
isLoading: boolean isLoading: boolean
handleResetKeywords: () => void handleResetKeywords: () => void
handleSelectFile: (file: OnlineDriveFile) => void handleSelectFile: (file: OnlineDriveFile) => void
@ -34,7 +33,6 @@ const List = ({
handleOpenFolder, handleOpenFolder,
isInPipeline, isInPipeline,
isLoading, isLoading,
isTruncated,
getOnlineDriveFiles, getOnlineDriveFiles,
}: FileListProps) => { }: FileListProps) => {
const anchorRef = useRef<HTMLDivElement>(null) const anchorRef = useRef<HTMLDivElement>(null)
@ -44,9 +42,9 @@ const List = ({
useEffect(() => { useEffect(() => {
if (anchorRef.current) { if (anchorRef.current) {
observerRef.current = new IntersectionObserver((entries) => { observerRef.current = new IntersectionObserver((entries) => {
const { setStartAfter } = dataSourceStore.getState() const { startAfter, isTruncated } = dataSourceStore.getState()
if (entries[0].isIntersecting && isTruncated && !isLoading) { if (entries[0].isIntersecting && isTruncated.current && !isLoading) {
setStartAfter(fileList[fileList.length - 1].key) startAfter.current = fileList[fileList.length - 1].key
getOnlineDriveFiles({ startAfter: fileList[fileList.length - 1].key }) getOnlineDriveFiles({ startAfter: fileList[fileList.length - 1].key })
} }
}, { }, {

@ -27,10 +27,8 @@ const OnlineDrive = ({
const prefix = useDataSourceStoreWithSelector(state => state.prefix) const prefix = useDataSourceStoreWithSelector(state => state.prefix)
const keywords = useDataSourceStoreWithSelector(state => state.keywords) const keywords = useDataSourceStoreWithSelector(state => state.keywords)
const bucket = useDataSourceStoreWithSelector(state => state.bucket) const bucket = useDataSourceStoreWithSelector(state => state.bucket)
const startAfter = useDataSourceStoreWithSelector(state => state.startAfter)
const selectedFileList = useDataSourceStoreWithSelector(state => state.selectedFileList) const selectedFileList = useDataSourceStoreWithSelector(state => state.selectedFileList)
const fileList = useDataSourceStoreWithSelector(state => state.fileList) const fileList = useDataSourceStoreWithSelector(state => state.fileList)
const isTruncated = useDataSourceStoreWithSelector(state => state.isTruncated)
const dataSourceStore = useDataSourceStore() const dataSourceStore = useDataSourceStore()
const [isLoading, setIsLoading] = useState(false) const [isLoading, setIsLoading] = useState(false)
@ -44,9 +42,10 @@ const OnlineDrive = ({
startAfter?: string startAfter?: string
fileList?: OnlineDriveFile[] fileList?: OnlineDriveFile[]
}) => { }) => {
const { startAfter } = dataSourceStore.getState()
const _prefix = params.prefix ?? prefix const _prefix = params.prefix ?? prefix
const _bucket = params.bucket ?? bucket const _bucket = params.bucket ?? bucket
const _startAfter = params.startAfter ?? startAfter const _startAfter = params.startAfter ?? startAfter.current
const _fileList = params.fileList ?? fileList const _fileList = params.fileList ?? fileList
const prefixString = _prefix.length > 0 ? `${_prefix.join('/')}/` : '' const prefixString = _prefix.length > 0 ? `${_prefix.join('/')}/` : ''
setIsLoading(true) setIsLoading(true)
@ -65,10 +64,10 @@ const OnlineDrive = ({
}, },
{ {
onDataSourceNodeCompleted: (documentsData: DataSourceNodeCompletedResponse) => { onDataSourceNodeCompleted: (documentsData: DataSourceNodeCompletedResponse) => {
const { setFileList, setIsTruncated } = dataSourceStore.getState() const { setFileList, isTruncated } = dataSourceStore.getState()
const { fileList: newFileList, isTruncated } = convertOnlineDriveData(documentsData.data, _prefix) const { fileList: newFileList, isTruncated: newIsTruncated } = convertOnlineDriveData(documentsData.data, _prefix, _bucket)
setFileList([..._fileList, ...newFileList]) setFileList([..._fileList, ...newFileList])
setIsTruncated(isTruncated) isTruncated.current = newIsTruncated
setIsLoading(false) setIsLoading(false)
}, },
onDataSourceNodeError: (error: DataSourceNodeErrorResponse) => { onDataSourceNodeError: (error: DataSourceNodeErrorResponse) => {
@ -80,7 +79,7 @@ const OnlineDrive = ({
}, },
}, },
) )
}, [prefix, bucket, startAfter, datasourceNodeRunURL, dataSourceStore, fileList]) }, [prefix, bucket, datasourceNodeRunURL, dataSourceStore, fileList])
useEffect(() => { useEffect(() => {
if (fileList.length > 0) return if (fileList.length > 0) return
@ -99,7 +98,7 @@ const OnlineDrive = ({
setKeywords(keywords) setKeywords(keywords)
}, [dataSourceStore]) }, [dataSourceStore])
const resetPrefix = useCallback(() => { const resetKeywords = useCallback(() => {
const { setKeywords } = dataSourceStore.getState() const { setKeywords } = dataSourceStore.getState()
setKeywords('') setKeywords('')
@ -152,14 +151,13 @@ const OnlineDrive = ({
prefix={prefix} prefix={prefix}
keywords={keywords} keywords={keywords}
bucket={bucket} bucket={bucket}
resetKeywords={resetPrefix} resetKeywords={resetKeywords}
updateKeywords={updateKeywords} updateKeywords={updateKeywords}
searchResultsLength={onlineDriveFileList.length} searchResultsLength={onlineDriveFileList.length}
handleSelectFile={handleSelectFile} handleSelectFile={handleSelectFile}
handleOpenFolder={handleOpenFolder} handleOpenFolder={handleOpenFolder}
isInPipeline={isInPipeline} isInPipeline={isInPipeline}
isLoading={isLoading} isLoading={isLoading}
isTruncated={isTruncated}
getOnlineDriveFiles={getOnlineDriveFiles} getOnlineDriveFiles={getOnlineDriveFiles}
/> />
</div> </div>

@ -7,19 +7,19 @@ export const isFile = (path: string): boolean => {
return filePathRegex.test(path) return filePathRegex.test(path)
} }
export const isBucketListInitiation = (data: OnlineDriveData[], prefix: string[]): boolean => { export const isBucketListInitiation = (data: OnlineDriveData[], prefix: string[], bucket: string): boolean => {
if (prefix.length > 0) return false if (bucket || prefix.length > 0) return false
return data.length > 1 || (data.length === 1 && data[0].files.length === 0) return data.length > 1 || (data.length === 1 && data[0].files.length === 0)
} }
export const convertOnlineDriveData = (data: OnlineDriveData[], prefix: string[]): { fileList: OnlineDriveFile[], isTruncated: boolean } => { export const convertOnlineDriveData = (data: OnlineDriveData[], prefix: string[], bucket: string): { fileList: OnlineDriveFile[], isTruncated: boolean } => {
const fileList: OnlineDriveFile[] = [] const fileList: OnlineDriveFile[] = []
let isTruncated = false let isTruncated = false
if (data.length === 0) if (data.length === 0)
return { fileList, isTruncated } return { fileList, isTruncated }
if (isBucketListInitiation(data, prefix)) { if (isBucketListInitiation(data, prefix, bucket)) {
data.forEach((item) => { data.forEach((item) => {
fileList.push({ fileList.push({
key: item.bucket, key: item.bucket,

@ -6,16 +6,14 @@ export type OnlineDriveSliceShape = {
setPrefix: (prefix: string[]) => void setPrefix: (prefix: string[]) => void
keywords: string keywords: string
setKeywords: (keywords: string) => void setKeywords: (keywords: string) => void
startAfter: string
setStartAfter: (startAfter: string) => void
selectedFileList: string[] selectedFileList: string[]
setSelectedFileList: (selectedFileList: string[]) => void setSelectedFileList: (selectedFileList: string[]) => void
fileList: OnlineDriveFile[] fileList: OnlineDriveFile[]
setFileList: (fileList: OnlineDriveFile[]) => void setFileList: (fileList: OnlineDriveFile[]) => void
bucket: string bucket: string
setBucket: (bucket: string) => void setBucket: (bucket: string) => void
isTruncated: boolean startAfter: React.MutableRefObject<string>
setIsTruncated: (isTruncated: boolean) => void isTruncated: React.MutableRefObject<boolean>
} }
export const createOnlineDriveSlice: StateCreator<OnlineDriveSliceShape> = (set) => { export const createOnlineDriveSlice: StateCreator<OnlineDriveSliceShape> = (set) => {
@ -28,10 +26,7 @@ export const createOnlineDriveSlice: StateCreator<OnlineDriveSliceShape> = (set)
setKeywords: (keywords: string) => set(() => ({ setKeywords: (keywords: string) => set(() => ({
keywords, keywords,
})), })),
startAfter: '', startAfter: { current: '' },
setStartAfter: (startAfter: string) => set(() => ({
startAfter,
})),
selectedFileList: [], selectedFileList: [],
setSelectedFileList: (selectedFileList: string[]) => set(() => ({ setSelectedFileList: (selectedFileList: string[]) => set(() => ({
selectedFileList, selectedFileList,
@ -44,9 +39,6 @@ export const createOnlineDriveSlice: StateCreator<OnlineDriveSliceShape> = (set)
setBucket: (bucket: string) => set(() => ({ setBucket: (bucket: string) => set(() => ({
bucket, bucket,
})), })),
isTruncated: false, isTruncated: { current: false },
setIsTruncated: (isTruncated: boolean) => set(() => ({
isTruncated,
})),
}) })
} }

Loading…
Cancel
Save