Update chatInput component to include file preview functionality
This commit is contained in:
parent
601a9eff47
commit
30aca5e7db
3 changed files with 37 additions and 12 deletions
|
|
@ -6,10 +6,11 @@ import {
|
||||||
CHAT_INPUT_PLACEHOLDER_SEND,
|
CHAT_INPUT_PLACEHOLDER_SEND,
|
||||||
} from "../../../../../constants/constants";
|
} from "../../../../../constants/constants";
|
||||||
import useFlowsManagerStore from "../../../../../stores/flowsManagerStore";
|
import useFlowsManagerStore from "../../../../../stores/flowsManagerStore";
|
||||||
import { chatInputType } from "../../../../../types/components";
|
import { FilePreviewType, chatInputType } from "../../../../../types/components";
|
||||||
import { classNames } from "../../../../../utils/utils";
|
import { classNames } from "../../../../../utils/utils";
|
||||||
import { uploadFile } from "../../../../../controllers/API";
|
import { uploadFile } from "../../../../../controllers/API";
|
||||||
|
import ShortUniqueId from "short-unique-id";
|
||||||
|
import FilePreview from "../filePreviewChat";
|
||||||
export default function ChatInput({
|
export default function ChatInput({
|
||||||
lockChat,
|
lockChat,
|
||||||
chatValue,
|
chatValue,
|
||||||
|
|
@ -21,6 +22,9 @@ export default function ChatInput({
|
||||||
const [repeat, setRepeat] = useState(1);
|
const [repeat, setRepeat] = useState(1);
|
||||||
const saveLoading = useFlowsManagerStore((state) => state.saveLoading);
|
const saveLoading = useFlowsManagerStore((state) => state.saveLoading);
|
||||||
const currentFlowId = useFlowsManagerStore((state) => state.currentFlowId);
|
const currentFlowId = useFlowsManagerStore((state) => state.currentFlowId);
|
||||||
|
const uid = new ShortUniqueId({ length: 3 });
|
||||||
|
const [files, setFiles] = useState<FilePreviewType[]>([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!lockChat && inputRef.current) {
|
if (!lockChat && inputRef.current) {
|
||||||
inputRef.current.focus();
|
inputRef.current.focus();
|
||||||
|
|
@ -43,16 +47,15 @@ export default function ChatInput({
|
||||||
if (items[i].type.indexOf("image") !== -1) {
|
if (items[i].type.indexOf("image") !== -1) {
|
||||||
const blob = items[i].getAsFile();
|
const blob = items[i].getAsFile();
|
||||||
if (blob) {
|
if (blob) {
|
||||||
// const reader = new FileReader();
|
const id = uid()
|
||||||
// reader.onload = (e) => {
|
setFiles([...files, { file: blob, loading: true, error: false,id}]);
|
||||||
// const data = e.target?.result;
|
|
||||||
// if (typeof data === "string") {
|
|
||||||
// // sendMessage(data);
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
// reader.readAsDataURL(blob);
|
|
||||||
uploadFile(blob, currentFlowId).then((res) => {
|
uploadFile(blob, currentFlowId).then((res) => {
|
||||||
console.log(res);
|
setFiles(prev=>{
|
||||||
|
const newFiles = [...prev];
|
||||||
|
const updatedIndex = newFiles.findIndex((file)=>file.id===id);
|
||||||
|
newFiles[updatedIndex].loading = false;
|
||||||
|
return newFiles;
|
||||||
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -66,7 +69,7 @@ export default function ChatInput({
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex w-full gap-2">
|
<div className="flex w-full flex-col-reverse">
|
||||||
<div className="relative w-full">
|
<div className="relative w-full">
|
||||||
<Textarea
|
<Textarea
|
||||||
onKeyDown={(event) => {
|
onKeyDown={(event) => {
|
||||||
|
|
@ -147,6 +150,13 @@ export default function ChatInput({
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div className="flex w-full gap-2">
|
||||||
|
{
|
||||||
|
files.map((file)=>(
|
||||||
|
<FilePreview error={file.error} file={file.file} loading={file.loading} key={file.id}/>
|
||||||
|
))
|
||||||
|
}
|
||||||
|
</div>
|
||||||
{/*
|
{/*
|
||||||
<Popover>
|
<Popover>
|
||||||
<PopoverTrigger asChild>
|
<PopoverTrigger asChild>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
export default function FilePreview(
|
||||||
|
{ error, file, loading }:
|
||||||
|
{ loading: boolean, file: File, error: boolean }
|
||||||
|
) {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
{loading && <div>Loading...</div>}
|
||||||
|
{error && <div>Error...</div>}
|
||||||
|
<div>{file.name}</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -187,6 +187,8 @@ export type FloatComponentType = {
|
||||||
id?: string;
|
id?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type FilePreviewType={loading:boolean,file:File,error:boolean,id:string}
|
||||||
|
|
||||||
export type TooltipComponentType = {
|
export type TooltipComponentType = {
|
||||||
children: ReactElement;
|
children: ReactElement;
|
||||||
title: string | ReactElement;
|
title: string | ReactElement;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue