import { useEffect, useRef, useState } from "react"; import { v4 } from 'uuid'; import { GrFormClose } from 'react-icons/gr'; import { IoMdClose } from "react-icons/io"; import testImage from '../../assets/pfp.jpg' import useFileUpload, { Upload } from "../hooks/useFileUpload"; import useChannel from "../hooks/useChannel"; import { useApi } from "../lib/useApi"; const exampleImages = [ { id: v4(), name: 'image.jpg', data: testImage, } ] export default function ChatInput() { const textBoxRef = useRef(null); const { channel } = useChannel(); const [attachments, setAttachments] = useState([]); const { newFile, getFileInfo } = useFileUpload(); const PADDING = 8; const CHATBOX_SIZE = 64; const addAttachment = (id: string) => { setAttachments(attachments => [...attachments, id]) } const removeAttachment = (id: string) => { setAttachments(attachments => attachments.filter(a => a !== id)); } // useEffect(() => { // addAttachment(newFile('image.jpg', testImage)); // }, []); const pasta: React.ClipboardEventHandler = (event) => { let pastedFiles = false; for (let idx = 0; idx < event.clipboardData.items.length; idx ++) { const item = event.clipboardData.items[idx]; const file = event.clipboardData.files[idx]; const type = event.clipboardData.types[idx] if (item.kind === 'file') { var blob = item.getAsFile(); if(blob === null) continue; addAttachment(newFile(file.name, file.type, blob)); pastedFiles = true; } } if(pastedFiles) { event.preventDefault(); } } const { send } = useApi({}); const keyPress = (event: any) => { if(event.code === 'Enter' && !event.shiftKey) { event.stopPropagation(); event.preventDefault(); event.bubbles = false; for(const attachment of attachments) { const info = getFileInfo(attachment); if(!info?.processed) { return true; } } const [file, ...restFiles] = attachments.map(a => getFileInfo(a)?.externalId) as string[]; const text = textBoxRef.current?.innerHTML ?? ''; if(text === '' && file === undefined) { return true; } if(channel === null) return true; const newMessage: NewMessageRequest = { uid: v4(), text, channel, timestamp: new Date().getTime(), file } console.log(file, restFiles); send('message:message', newMessage); for(const file of restFiles) { const newMessage: NewMessageRequest = { uid: v4(), text: '', channel, timestamp: new Date().getTime(), file } send('message:message', newMessage); } setAttachments([]); if(textBoxRef.current !== null) { textBoxRef.current.innerHTML = ''; } return true; } } return (
{attachments.length > 0 && (
{attachments.map(attachment => { const info = getFileInfo(attachment); if(!info) return Poop; return ( removeAttachment(attachment)} > ); })}
)}
{ textBoxRef.current?.focus(); }} style={{ margin: PADDING + 'px', marginRight: '0px', borderRadius: ((CHATBOX_SIZE - PADDING*2) / 2) + 'px', background: 'var(--neutral-5)', gridArea: 'message', display: 'grid', placeItems: 'center center', padding: '8px 16px', minHeight: '48px', boxSizing: 'border-box', cursor: 'text', overflow: 'auto', }} onPaste={pasta} >
) } function AttachmentBox(props: { attachment: Upload, onClose: React.MouseEventHandler }) { return (
{props.attachment.name}
{/* */}
) }