Spaces:
Sleeping
Sleeping
File size: 5,930 Bytes
bfbf1a7 cfb938a bfbf1a7 cfb938a bfbf1a7 1f931d5 92f037b bfbf1a7 92f037b db06845 bfbf1a7 cfb938a bfbf1a7 cfb938a db06845 cfb938a 92f037b db06845 92f037b cfb938a bfbf1a7 cfb938a 92f037b cfb938a 92f037b 4af6326 cfb938a 92f037b cfb938a 92f037b cfb938a 92f037b cfb938a ae074fc cfb938a 3c8b24f cfb938a db06845 cfb938a db06845 cfb938a db06845 cfb938a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
'use client';
import {
useState,
useEffect,
useRef,
forwardRef,
useImperativeHandle,
} from 'react';
import { Button } from '@/components/ui/Button';
import { useEnterSubmit } from '@/lib/hooks/useEnterSubmit';
import Img from '../ui/Img';
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from '@/components/ui/Tooltip';
import { IconImage, IconArrowUp, IconClose } from '@/components/ui/Icons';
import { cn } from '@/lib/utils';
import Chip from '../ui/Chip';
import Textarea from 'react-textarea-autosize';
import useMediaUpload from '@/lib/hooks/useMediaUpload';
export interface ComposerProps {
onSubmit: (params: { input: string; mediaUrl: string }) => Promise<void>;
disabled?: boolean;
title?: string;
initMediaUrl?: string;
initInput?: string;
}
export interface ComposerRef {
setMediaUrl: (url: string) => void;
setInput: (input: string) => void;
}
const Composer = forwardRef<ComposerRef, ComposerProps>(
({ disabled, onSubmit, initMediaUrl, initInput }, ref) => {
const { formRef, onKeyDown } = useEnterSubmit();
const inputRef = useRef<HTMLTextAreaElement>(null);
const [localMediaUrl, setLocalMediaUrl] = useState<string | undefined>(
initMediaUrl,
);
const [isSubmitting, setIsSubmitting] = useState<boolean>(false);
const [input, setLocalInput] = useState(initInput ?? '');
const noMediaValidation = !localMediaUrl && !!input;
const {
getRootProps,
getInputProps,
isDragActive,
isUploading,
openUpload,
} = useMediaUpload(uploadUrl => setLocalMediaUrl(uploadUrl));
const finalLoading = isUploading || isSubmitting;
const finalDisabled = finalLoading || disabled;
useEffect(() => {
if (inputRef.current) {
inputRef.current.focus();
}
}, []);
useImperativeHandle(ref, () => ({
setMediaUrl(mediaUrl) {
setLocalMediaUrl(mediaUrl);
},
setInput(input) {
setLocalInput(input);
},
}));
const mediaName = localMediaUrl?.split('/').pop();
return (
<div
{...getRootProps()}
className={cn(
'mx-auto w-[42rem] max-w-full px-6 py-4 bg-zinc-600 rounded-xl relative shadow-lg shadow-zinc-600/40 z-50',
isDragActive && 'bg-indigo-700/50',
)}
>
<input {...getInputProps()} />
<div
className={cn(
'w-1/3 h-1 rounded-full overflow-hidden bg-zinc-700 absolute left-1/2 -translate-x-1/2 top-2',
finalLoading ? 'opacity-100' : 'opacity-0',
)}
>
<div className="h-full bg-primary animate-progress origin-left-right" />
</div>
{localMediaUrl ? (
<Chip className="mb-0.5">
<div className="flex flex-row items-center space-x-2">
<Tooltip>
<TooltipTrigger>
<div className="flex flex-row items-center space-x-2">
<IconImage className="size-3" />
<p>{mediaName ?? 'unnamed_media'}</p>
</div>
</TooltipTrigger>
<TooltipContent sideOffset={12} className="max-w-2xl">
<Img
src={localMediaUrl}
className="m-1"
quality={100}
alt="zoomed-in-image"
/>
</TooltipContent>
</Tooltip>
<Button
size="icon"
variant="ghost"
disabled={finalDisabled}
className="size-4"
onClick={() => setLocalMediaUrl(undefined)}
>
<IconClose className="size-3" />
</Button>
</div>
</Chip>
) : (
<Button
variant="ghost"
size="sm"
className={cn(
'ml-[-10px] border border-transparent',
noMediaValidation && 'border-red-500/50 text-red-500',
)}
onClick={openUpload}
>
<IconImage className="mr-2 size-4" />
{noMediaValidation ? 'Select media (required)' : 'Select media'}
</Button>
)}
<form
onSubmit={async e => {
e.preventDefault();
if (!input?.trim() || !localMediaUrl) {
return;
}
setIsSubmitting(true);
try {
await onSubmit({ input, mediaUrl: localMediaUrl });
} finally {
setIsSubmitting(false);
setLocalInput('');
}
}}
ref={formRef}
className="h-full mt-4"
>
{/* <div className="border-gray-500 flex overflow-hidden size-full flex flex-row items-center"> */}
<Textarea
ref={inputRef}
tabIndex={0}
onKeyDown={onKeyDown}
rows={1}
value={input}
disabled={finalDisabled}
onChange={e => setLocalInput(e.target.value)}
placeholder={
finalDisabled ? '🤖 Agent working ✨' : 'Message Vision Agent'
}
spellCheck={false}
className="w-full grow resize-none bg-transparent focus-within:outline-none"
/>
{/* Submit Icon */}
<Tooltip>
<TooltipTrigger asChild>
<Button
type="submit"
size="icon"
className={cn('size-6 absolute bottom-3 right-3')}
disabled={finalDisabled || input === '' || noMediaValidation}
>
<IconArrowUp className="size-3" />
</Button>
</TooltipTrigger>
<TooltipContent>Message Vision Agent</TooltipContent>
</Tooltip>
</form>
</div>
);
},
);
Composer.displayName = 'Composer';
export default Composer;
|