mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-08 14:29:15 +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 { FileMetadata } from '@renderer/types'
|
||||||
import { filterSupportedFiles } from '@renderer/utils'
|
import { filterSupportedFiles } from '@renderer/utils'
|
||||||
import { useCallback, useMemo, useState } from 'react'
|
import { useCallback, useState } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
/** 支持选择的扩展名 */
|
/** 支持选择的扩展名 */
|
||||||
extensions?: string[]
|
extensions?: string[]
|
||||||
multipleSelections?: boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useFiles = ({ extensions, multipleSelections = true }: Props) => {
|
export const useFiles = ({ extensions }: Props) => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
|
|
||||||
const [files, setFiles] = useState<FileMetadata[]>([])
|
const [files, setFiles] = useState<FileMetadata[]>([])
|
||||||
const [selecting, setSelecting] = useState<boolean>(false)
|
const [selecting, setSelecting] = useState<boolean>(false)
|
||||||
|
|
||||||
const selectProps: Electron.OpenDialogOptions['properties'] = useMemo(
|
const onSelectFile = useCallback(
|
||||||
() => (multipleSelections ? ['openFile', 'multiSelections'] : ['openFile']),
|
async (multipleSelections: boolean = true) => {
|
||||||
[multipleSelections]
|
if (selecting) {
|
||||||
)
|
|
||||||
|
|
||||||
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])
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const supportedFiles = await filterSupportedFiles(_files, supportedExtensions)
|
const supportedExtensions = extensions ?? ['*']
|
||||||
if (supportedFiles.length > 0) {
|
const selectProps: Electron.OpenDialogOptions['properties'] = multipleSelections
|
||||||
setFiles([...files, ...supportedFiles])
|
? ['openFile', 'multiSelections']
|
||||||
}
|
: ['openFile']
|
||||||
|
|
||||||
if (supportedFiles.length !== _files.length) {
|
// when the number of extensions is greater than 20, use *.* to avoid selecting window lag
|
||||||
window.message.info({
|
const useAllFiles = supportedExtensions.length > 20
|
||||||
key: 'file_not_supported',
|
|
||||||
content: t('chat.input.file_not_supported_count', {
|
setSelecting(true)
|
||||||
count: _files.length - supportedFiles.length
|
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 {
|
return {
|
||||||
files,
|
files,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user