mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-30 07:32:44 +08:00
refactor(useFiles): 移除multipleSelections参数并重构文件选择逻辑
将multipleSelections从组件props移动到onSelectFile方法参数中,简化组件接口 重构文件选择逻辑,移除不必要的useMemo,提升代码可维护性
This commit is contained in:
parent
d304782143
commit
32024b3f50
@ -1,66 +1,66 @@
|
||||
import { FileMetadata } from '@renderer/types'
|
||||
import { filterSupportedFiles } from '@renderer/utils'
|
||||
import { useCallback, useMemo, useState } from 'react'
|
||||
import { useCallback, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
type Props = {
|
||||
/** 支持选择的扩展名 */
|
||||
extensions?: string[]
|
||||
multipleSelections?: boolean
|
||||
}
|
||||
|
||||
export const useFiles = ({ extensions, multipleSelections = true }: Props) => {
|
||||
export const useFiles = ({ extensions }: Props) => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const [files, setFiles] = useState<FileMetadata[]>([])
|
||||
const [selecting, setSelecting] = useState<boolean>(false)
|
||||
|
||||
const selectProps: Electron.OpenDialogOptions['properties'] = useMemo(
|
||||
() => (multipleSelections ? ['openFile', 'multiSelections'] : ['openFile']),
|
||||
[multipleSelections]
|
||||
)
|
||||
|
||||
const onSelectFile = useCallback(async () => {
|
||||
if (selecting) {
|
||||
return
|
||||
}
|
||||
const supportedExtensions = extensions ?? ['*']
|
||||
|
||||
// when the number of extensions is greater than 20, use *.* to avoid selecting window lag
|
||||
const useAllFiles = supportedExtensions.length > 20
|
||||
|
||||
setSelecting(true)
|
||||
const _files: FileMetadata[] = await window.api.file.select({
|
||||
properties: selectProps,
|
||||
filters: [
|
||||
{
|
||||
name: 'Files',
|
||||
extensions: useAllFiles ? ['*'] : supportedExtensions.map((i) => i.replace('.', ''))
|
||||
}
|
||||
]
|
||||
})
|
||||
setSelecting(false)
|
||||
|
||||
if (_files) {
|
||||
if (!useAllFiles) {
|
||||
setFiles([...files, ..._files])
|
||||
const onSelectFile = useCallback(
|
||||
async (multipleSelections: boolean = true) => {
|
||||
if (selecting) {
|
||||
return
|
||||
}
|
||||
const supportedFiles = await filterSupportedFiles(_files, supportedExtensions)
|
||||
if (supportedFiles.length > 0) {
|
||||
setFiles([...files, ...supportedFiles])
|
||||
}
|
||||
const supportedExtensions = extensions ?? ['*']
|
||||
const selectProps: Electron.OpenDialogOptions['properties'] = multipleSelections
|
||||
? ['openFile', 'multiSelections']
|
||||
: ['openFile']
|
||||
|
||||
if (supportedFiles.length !== _files.length) {
|
||||
window.message.info({
|
||||
key: 'file_not_supported',
|
||||
content: t('chat.input.file_not_supported_count', {
|
||||
count: _files.length - supportedFiles.length
|
||||
// when the number of extensions is greater than 20, use *.* to avoid selecting window lag
|
||||
const useAllFiles = supportedExtensions.length > 20
|
||||
|
||||
setSelecting(true)
|
||||
const _files: FileMetadata[] = await window.api.file.select({
|
||||
properties: selectProps,
|
||||
filters: [
|
||||
{
|
||||
name: 'Files',
|
||||
extensions: useAllFiles ? ['*'] : supportedExtensions.map((i) => i.replace('.', ''))
|
||||
}
|
||||
]
|
||||
})
|
||||
setSelecting(false)
|
||||
|
||||
if (_files) {
|
||||
if (!useAllFiles) {
|
||||
setFiles([...files, ..._files])
|
||||
return
|
||||
}
|
||||
const supportedFiles = await filterSupportedFiles(_files, supportedExtensions)
|
||||
if (supportedFiles.length > 0) {
|
||||
setFiles([...files, ...supportedFiles])
|
||||
}
|
||||
|
||||
if (supportedFiles.length !== _files.length) {
|
||||
window.message.info({
|
||||
key: 'file_not_supported',
|
||||
content: t('chat.input.file_not_supported_count', {
|
||||
count: _files.length - supportedFiles.length
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [extensions, files, selectProps, selecting, t])
|
||||
},
|
||||
[extensions, files, selecting, t]
|
||||
)
|
||||
|
||||
return {
|
||||
files,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user