(chatInput): add focus state handling and dynamic styling for text area

♻️ (filePreviewChat): refactor file preview component for better UX and code clarity
This commit is contained in:
cristhianzl 2024-05-29 12:36:18 -03:00
commit 826f9656d9
3 changed files with 100 additions and 41 deletions

View file

@ -16,9 +16,16 @@ const TextAreaWrapper = ({
CHAT_INPUT_PLACEHOLDER,
CHAT_INPUT_PLACEHOLDER_SEND,
inputRef,
setInputFocus,
files,
}) => {
return (
<Textarea
onFocus={(e) => {
setInputFocus(true);
e.target.style.borderTopWidth = "0";
}}
onBlur={() => setInputFocus(false)}
onDragOver={dragOver}
onDragEnter={dragEnter}
onDragLeave={dragLeave}
@ -53,6 +60,12 @@ const TextAreaWrapper = ({
: " form-modal-lock-false bg-background",
"form-modal-lockchat",
`${
files.length > 0
? "rounded-b-md border-t-0 border-border focus:border-t-0 focus:border-ring"
: "rounded-md border-t-2 border-border focus:border-ring"
}`,
"pl-10",
)}
placeholder={
noInput ? CHAT_INPUT_PLACEHOLDER : CHAT_INPUT_PLACEHOLDER_SEND

View file

@ -29,6 +29,7 @@ export default function ChatInput({
const currentFlowId = useFlowsManagerStore((state) => state.currentFlowId);
const [files, setFiles] = useState<FilePreviewType[]>([]);
const [isDragging, setIsDragging] = useState(false);
const [inputFocus, setInputFocus] = useState<boolean>(false);
useFocusOnUnlock(lockChat, inputRef);
useAutoResizeTextArea(chatValue, inputRef);
@ -76,6 +77,8 @@ export default function ChatInput({
CHAT_INPUT_PLACEHOLDER={CHAT_INPUT_PLACEHOLDER}
CHAT_INPUT_PLACEHOLDER_SEND={CHAT_INPUT_PLACEHOLDER_SEND}
inputRef={inputRef}
setInputFocus={setInputFocus}
files={files}
/>
<div className="form-modal-send-icon-position">
@ -88,19 +91,28 @@ export default function ChatInput({
/>
</div>
</div>
<div className="flex w-full gap-2 pb-2">
{files.map((file) => (
<FilePreview
error={file.error}
file={file.file}
loading={file.loading}
key={file.id}
onDelete={() => {
setFiles((prev) => prev.filter((f) => f.id !== file.id));
}}
/>
))}
</div>
{files.length > 0 && (
<div
className={`flex w-full items-center gap-2 rounded-t-md bg-background px-10 py-5 ${
inputFocus
? "border-2 border-b-0 border-ring"
: "border border-b-0 border-border"
}`}
>
{files.map((file) => (
<FilePreview
error={file.error}
file={file.file}
loading={file.loading}
key={file.id}
onDelete={() => {
setFiles((prev) => prev.filter((f) => f.id !== file.id));
// TODO: delete file on backend
}}
/>
))}
</div>
)}
</div>
);
}

View file

@ -1,33 +1,67 @@
import React, { useState } from "react";
import LoadingComponent from "../../../../../components/loadingComponent";
import { useState } from "react";
import IconComponent from "../../../../../components/genericIconComponent";
export default function FilePreview({ error, file, loading,onDelete }: { loading: boolean, file: File, error: boolean,onDelete:()=>void }) {
const [isHovered, setIsHovered] = useState(false);
export default function FilePreview({
error,
file,
loading,
onDelete,
}: {
loading: boolean;
file: File;
error: boolean;
onDelete: () => void;
}) {
const [isHovered, setIsHovered] = useState(false);
return (
<div className="inline-block relative w-56">
{loading && <LoadingComponent remSize={5} />}
{error && <div>Error...</div>}
<div
className={`rounded-md overflow-hidden transition duration-300 bg-background p-4 relative ${
isHovered ? "shadow-md" : ""
}`}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
<img src={URL.createObjectURL(file)} alt="file" className="w-full h-auto block" />
{isHovered && (
<div className="absolute inset-0 bg-black bg-opacity-30 flex items-center justify-center">
<div
className="bg-white bg-opacity-80 rounded-full cursor-pointer p-2"
onClick={onDelete}
>
<IconComponent name="trash" className="stroke-red-500" />
</div>
</div>
)}
</div>
return (
<div className="relative inline-block">
{loading ? (
<div className="flex h-24 w-24 items-center justify-center rounded-md border border-ring bg-background ">
<svg
aria-hidden="true"
className={`h-10 w-10 animate-spin fill-black text-muted`}
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
</div>
);
) : error ? (
<div>Error...</div>
) : (
<div
className={`relative h-24 w-24 rounded-md border border-ring bg-background transition duration-300 ${
isHovered ? "shadow-md" : ""
}`}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
<img
src={URL.createObjectURL(file)}
alt="file"
className="block h-full w-full rounded-md border border-border"
/>
{isHovered && (
<div className="absolute bottom-20 left-20 flex h-5 w-5 items-center justify-center bg-black bg-opacity-30">
<div
className="flex h-7 w-7 cursor-pointer items-center justify-center rounded-full bg-gray-200 p-2 transition-all"
onClick={onDelete}
>
<IconComponent name="X" className="stroke-slate-950 stroke-2" />
</div>
</div>
)}
</div>
)}
</div>
);
}