file uploader

This commit is contained in:
StyleZhang 2024-09-18 16:50:53 +08:00
parent fd9b71c4d7
commit f5d1f5a20a
3 changed files with 17 additions and 4 deletions

View File

@ -16,11 +16,12 @@ type Shape = {
setFiles: (files: FileEntity[]) => void setFiles: (files: FileEntity[]) => void
} }
export const createFileStore = () => { export const createFileStore = (onChange?: (files: FileEntity[]) => void) => {
return create<Shape>(set => ({ return create<Shape>(set => ({
files: [], files: [],
setFiles: (files) => { setFiles: (files) => {
set({ files }) set({ files })
onChange?.(files)
}, },
})) }))
} }
@ -44,14 +45,16 @@ type FileProviderProps = {
children: React.ReactNode children: React.ReactNode
isPublicAPI?: boolean isPublicAPI?: boolean
url?: string url?: string
onChange?: (files: FileEntity[]) => void
} }
export const FileContextProvider = ({ export const FileContextProvider = ({
children, children,
onChange,
}: FileProviderProps) => { }: FileProviderProps) => {
const storeRef = useRef<FileStore>() const storeRef = useRef<FileStore>()
if (!storeRef.current) if (!storeRef.current)
storeRef.current = createFileStore() storeRef.current = createFileStore(onChange)
return ( return (
<FileContext.Provider value={storeRef.current}> <FileContext.Provider value={storeRef.current}>

View File

@ -113,7 +113,12 @@ const RunOnce: FC<IRunOnceProps> = ({
</div> </div>
) )
} }
<FileUploaderInAttachmentWrapper onChange={() => {}} /> <FileUploaderInAttachmentWrapper onChange={files => onVisionFilesChange(files.filter(file => file.progress !== -1).map(fileItem => ({
type: fileItem.fileType,
transfer_method: fileItem.type,
url: fileItem.url || '',
upload_file_id: fileItem.fileId,
})))} />
{promptConfig.prompt_variables.length > 0 && ( {promptConfig.prompt_variables.length > 0 && (
<div className='mt-4 h-[1px] bg-gray-100'></div> <div className='mt-4 h-[1px] bg-gray-100'></div>
)} )}

View File

@ -159,7 +159,12 @@ const FormItem: FC<Props> = ({
{/* #TODO# file upload */} {/* #TODO# file upload */}
{(type === InputVarType.singleFile || type === InputVarType.multiFiles) && ( {(type === InputVarType.singleFile || type === InputVarType.multiFiles) && (
<FileUploaderInAttachmentWrapper onChange={() => {}} /> <FileUploaderInAttachmentWrapper onChange={files => onChange(files.filter(file => file.progress !== -1).map(fileItem => ({
type: fileItem.fileType,
transfer_method: fileItem.type,
url: fileItem.url,
upload_file_id: fileItem.fileId,
})))} />
)} )}
{ {
type === InputVarType.files && ( type === InputVarType.files && (