mirror of
https://github.com/langgenius/dify.git
synced 2025-09-25 17:15:28 +00:00
refactor: update data source store usage in LocalFile and WebsiteCrawl components
This commit is contained in:
parent
15cd9e0b12
commit
c39746181d
@ -5,7 +5,7 @@ import { useContext } from 'use-context-selector'
|
|||||||
import { RiDeleteBinLine, RiErrorWarningFill, RiUploadCloud2Line } from '@remixicon/react'
|
import { RiDeleteBinLine, RiErrorWarningFill, RiUploadCloud2Line } from '@remixicon/react'
|
||||||
import DocumentFileIcon from '@/app/components/datasets/common/document-file-icon'
|
import DocumentFileIcon from '@/app/components/datasets/common/document-file-icon'
|
||||||
import cn from '@/utils/classnames'
|
import cn from '@/utils/classnames'
|
||||||
import type { DocumentItem, CustomFile as File, FileItem } from '@/models/datasets'
|
import type { CustomFile as File, FileItem } from '@/models/datasets'
|
||||||
import { ToastContext } from '@/app/components/base/toast'
|
import { ToastContext } from '@/app/components/base/toast'
|
||||||
import SimplePieChart from '@/app/components/base/simple-pie-chart'
|
import SimplePieChart from '@/app/components/base/simple-pie-chart'
|
||||||
import { upload } from '@/service/base'
|
import { upload } from '@/service/base'
|
||||||
@ -15,7 +15,7 @@ import { IS_CE_EDITION } from '@/config'
|
|||||||
import { Theme } from '@/types/app'
|
import { Theme } from '@/types/app'
|
||||||
import useTheme from '@/hooks/use-theme'
|
import useTheme from '@/hooks/use-theme'
|
||||||
import { useFileUploadConfig } from '@/service/use-common'
|
import { useFileUploadConfig } from '@/service/use-common'
|
||||||
import { useDataSourceStoreWithSelector } from '../store'
|
import { useDataSourceStore, useDataSourceStoreWithSelector } from '../store'
|
||||||
import produce from 'immer'
|
import produce from 'immer'
|
||||||
|
|
||||||
const FILES_NUMBER_LIMIT = 20
|
const FILES_NUMBER_LIMIT = 20
|
||||||
@ -33,9 +33,7 @@ const LocalFile = ({
|
|||||||
const { notify } = useContext(ToastContext)
|
const { notify } = useContext(ToastContext)
|
||||||
const { locale } = useContext(I18n)
|
const { locale } = useContext(I18n)
|
||||||
const fileList = useDataSourceStoreWithSelector(state => state.localFileList)
|
const fileList = useDataSourceStoreWithSelector(state => state.localFileList)
|
||||||
const setFileList = useDataSourceStoreWithSelector(state => state.setLocalFileList)
|
const dataSourceStore = useDataSourceStore()
|
||||||
const setCurrentFile = useDataSourceStoreWithSelector(state => state.setCurrentLocalFile)
|
|
||||||
const previewFileRef = useDataSourceStoreWithSelector(state => state.previewLocalFileRef)
|
|
||||||
const [dragging, setDragging] = useState(false)
|
const [dragging, setDragging] = useState(false)
|
||||||
|
|
||||||
const dropRef = useRef<HTMLDivElement>(null)
|
const dropRef = useRef<HTMLDivElement>(null)
|
||||||
@ -69,6 +67,7 @@ const LocalFile = ({
|
|||||||
}, [fileUploadConfigResponse])
|
}, [fileUploadConfigResponse])
|
||||||
|
|
||||||
const updateFile = useCallback((fileItem: FileItem, progress: number, list: FileItem[]) => {
|
const updateFile = useCallback((fileItem: FileItem, progress: number, list: FileItem[]) => {
|
||||||
|
const { setLocalFileList } = dataSourceStore.getState()
|
||||||
const newList = produce(list, (draft) => {
|
const newList = produce(list, (draft) => {
|
||||||
const targetIndex = draft.findIndex(file => file.fileID === fileItem.fileID)
|
const targetIndex = draft.findIndex(file => file.fileID === fileItem.fileID)
|
||||||
draft[targetIndex] = {
|
draft[targetIndex] = {
|
||||||
@ -76,18 +75,19 @@ const LocalFile = ({
|
|||||||
progress,
|
progress,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
setFileList(newList)
|
setLocalFileList(newList)
|
||||||
previewFileRef.current = newList[0].file as DocumentItem
|
}, [dataSourceStore])
|
||||||
}, [previewFileRef, setFileList])
|
|
||||||
|
|
||||||
const updateFileList = useCallback((preparedFiles: FileItem[]) => {
|
const updateFileList = useCallback((preparedFiles: FileItem[]) => {
|
||||||
setFileList(preparedFiles)
|
const { setLocalFileList } = dataSourceStore.getState()
|
||||||
}, [setFileList])
|
setLocalFileList(preparedFiles)
|
||||||
|
}, [dataSourceStore])
|
||||||
|
|
||||||
const handlePreview = useCallback((file: File) => {
|
const handlePreview = useCallback((file: File) => {
|
||||||
|
const { setCurrentLocalFile } = dataSourceStore.getState()
|
||||||
if (file.id)
|
if (file.id)
|
||||||
setCurrentFile(file)
|
setCurrentLocalFile(file)
|
||||||
}, [setCurrentFile])
|
}, [dataSourceStore])
|
||||||
|
|
||||||
// utils
|
// utils
|
||||||
const getFileType = (currentFile: File) => {
|
const getFileType = (currentFile: File) => {
|
||||||
|
@ -9,12 +9,16 @@ export type LocalFileSliceShape = {
|
|||||||
previewLocalFileRef: React.MutableRefObject<DocumentItem | undefined>
|
previewLocalFileRef: React.MutableRefObject<DocumentItem | undefined>
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createLocalFileSlice: StateCreator<LocalFileSliceShape> = (set) => {
|
export const createLocalFileSlice: StateCreator<LocalFileSliceShape> = (set, get) => {
|
||||||
return ({
|
return ({
|
||||||
localFileList: [],
|
localFileList: [],
|
||||||
setLocalFileList: (fileList: FileItem[]) => set(() => ({
|
setLocalFileList: (fileList: FileItem[]) => {
|
||||||
localFileList: fileList,
|
set(() => ({
|
||||||
})),
|
localFileList: fileList,
|
||||||
|
}))
|
||||||
|
const { previewLocalFileRef } = get()
|
||||||
|
previewLocalFileRef.current = fileList[0]?.file as DocumentItem
|
||||||
|
},
|
||||||
currentLocalFile: undefined,
|
currentLocalFile: undefined,
|
||||||
setCurrentLocalFile: (file: File | undefined) => set(() => ({
|
setCurrentLocalFile: (file: File | undefined) => set(() => ({
|
||||||
currentLocalFile: file,
|
currentLocalFile: file,
|
||||||
|
@ -91,9 +91,8 @@ const WebsiteCrawl = ({
|
|||||||
: `/rag/pipelines/${pipelineId}/workflows/draft/datasource/nodes/${nodeId}/run`
|
: `/rag/pipelines/${pipelineId}/workflows/draft/datasource/nodes/${nodeId}/run`
|
||||||
|
|
||||||
const handleCheckedCrawlResultChange = useCallback((checkedCrawlResult: CrawlResultItem[]) => {
|
const handleCheckedCrawlResultChange = useCallback((checkedCrawlResult: CrawlResultItem[]) => {
|
||||||
const { setWebsitePages, previewWebsitePageRef } = dataSourceStore.getState()
|
const { setWebsitePages } = dataSourceStore.getState()
|
||||||
setWebsitePages(checkedCrawlResult)
|
setWebsitePages(checkedCrawlResult)
|
||||||
previewWebsitePageRef.current = checkedCrawlResult[0]
|
|
||||||
}, [dataSourceStore])
|
}, [dataSourceStore])
|
||||||
|
|
||||||
const handlePreview = useCallback((website: CrawlResultItem, index: number) => {
|
const handlePreview = useCallback((website: CrawlResultItem, index: number) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user