mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-06 13:19:33 +08:00
refactor: Extract file drop handling logic to separate utility function
This commit is contained in:
parent
3068cdcf7a
commit
5664d84e2c
@ -27,11 +27,12 @@ import { setGenerating, setSearching } from '@renderer/store/runtime'
|
|||||||
import { Assistant, FileType, KnowledgeBase, Message, Model, Topic } from '@renderer/types'
|
import { Assistant, FileType, KnowledgeBase, Message, Model, Topic } from '@renderer/types'
|
||||||
import { classNames, delay, getFileExtension, uuid } from '@renderer/utils'
|
import { classNames, delay, getFileExtension, uuid } from '@renderer/utils'
|
||||||
import { abortCompletion } from '@renderer/utils/abortController'
|
import { abortCompletion } from '@renderer/utils/abortController'
|
||||||
|
import { getFilesFromDropEvent } from '@renderer/utils/input'
|
||||||
import { documentExts, imageExts, textExts } from '@shared/config/constant'
|
import { documentExts, imageExts, textExts } from '@shared/config/constant'
|
||||||
import { Button, Popconfirm, Tooltip } from 'antd'
|
import { Button, Popconfirm, Tooltip } from 'antd'
|
||||||
import TextArea, { TextAreaRef } from 'antd/es/input/TextArea'
|
import TextArea, { TextAreaRef } from 'antd/es/input/TextArea'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
import Logger from 'electron-log'
|
import Logger from 'electron-log/renderer'
|
||||||
import { debounce, isEmpty } from 'lodash'
|
import { debounce, isEmpty } from 'lodash'
|
||||||
import React, { CSSProperties, FC, useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
import React, { CSSProperties, FC, useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
@ -391,56 +392,11 @@ const Inputbar: FC<Props> = ({ assistant: _assistant, setActiveTopic }) => {
|
|||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
}
|
}
|
||||||
|
|
||||||
const filesFromDropEvent = async (e: React.DragEvent<HTMLDivElement>): Promise<FileType[]> => {
|
|
||||||
if (e.dataTransfer.files.length > 0) {
|
|
||||||
const results = await Promise.allSettled([...e.dataTransfer.files].map((file) => window.api.file.get(file.path)))
|
|
||||||
const list: FileType[] = []
|
|
||||||
for (const result of results) {
|
|
||||||
if (result.status === 'fulfilled') {
|
|
||||||
if (result.value !== null) {
|
|
||||||
list.push(result.value)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Logger.error('[src/renderer/src/pages/home/Inputbar/Inputbar.tsx] filesFromDropEvent:', result.reason)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
} else {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
let existCodefilesFormat = false
|
|
||||||
for (const item of e.dataTransfer.items) {
|
|
||||||
const { type } = item
|
|
||||||
if (type === 'codefiles') {
|
|
||||||
item.getAsString(async (filePathListString) => {
|
|
||||||
const filePathList: string[] = JSON.parse(filePathListString)
|
|
||||||
const filePathListPromises = filePathList.map((filePath) => window.api.file.get(filePath))
|
|
||||||
resolve(
|
|
||||||
await Promise.allSettled(filePathListPromises).then((results) =>
|
|
||||||
results
|
|
||||||
.filter((result) => result.status === 'fulfilled')
|
|
||||||
.filter((result) => result.value !== null)
|
|
||||||
.map((result) => result.value!)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
existCodefilesFormat = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!existCodefilesFormat) {
|
|
||||||
resolve([])
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleDrop = async (e: React.DragEvent<HTMLDivElement>) => {
|
const handleDrop = async (e: React.DragEvent<HTMLDivElement>) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
|
|
||||||
const files = await filesFromDropEvent(e).catch((err) => {
|
const files = await getFilesFromDropEvent(e).catch((err) => {
|
||||||
Logger.error('[src/renderer/src/pages/home/Inputbar/Inputbar.tsx] handleDrop:', err)
|
Logger.error('[src/renderer/src/pages/home/Inputbar/Inputbar.tsx] handleDrop:', err)
|
||||||
return null
|
return null
|
||||||
})
|
})
|
||||||
|
|||||||
47
src/renderer/src/utils/input.ts
Normal file
47
src/renderer/src/utils/input.ts
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import { FileType } from '@renderer/types'
|
||||||
|
import Logger from 'electron-log/renderer'
|
||||||
|
|
||||||
|
export const getFilesFromDropEvent = async (e: React.DragEvent<HTMLDivElement>): Promise<FileType[]> => {
|
||||||
|
if (e.dataTransfer.files.length > 0) {
|
||||||
|
const results = await Promise.allSettled([...e.dataTransfer.files].map((file) => window.api.file.get(file.path)))
|
||||||
|
const list: FileType[] = []
|
||||||
|
for (const result of results) {
|
||||||
|
if (result.status === 'fulfilled') {
|
||||||
|
if (result.value !== null) {
|
||||||
|
list.push(result.value)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Logger.error('[src/renderer/src/utils/input.ts] getFilesFromDropEvent:', result.reason)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
} else {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
let existCodefilesFormat = false
|
||||||
|
for (const item of e.dataTransfer.items) {
|
||||||
|
const { type } = item
|
||||||
|
if (type === 'codefiles') {
|
||||||
|
item.getAsString(async (filePathListString) => {
|
||||||
|
const filePathList: string[] = JSON.parse(filePathListString)
|
||||||
|
const filePathListPromises = filePathList.map((filePath) => window.api.file.get(filePath))
|
||||||
|
resolve(
|
||||||
|
await Promise.allSettled(filePathListPromises).then((results) =>
|
||||||
|
results
|
||||||
|
.filter((result) => result.status === 'fulfilled')
|
||||||
|
.filter((result) => result.value !== null)
|
||||||
|
.map((result) => result.value!)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
existCodefilesFormat = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!existCodefilesFormat) {
|
||||||
|
resolve([])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user