Skip to content

Commit 836d887

Browse files
authored
Merge pull request abue-ammar#9 from abue-ammar/ui-migration
feat: reset compressed images and progress on file upload and drag
2 parents 95eaa65 + 23c544d commit 836d887

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

src/components/image-compressor.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const ImageCompressor = () => {
3030
const [filelist, setFilelist] = useState<FileList | File[]>([]);
3131
const [compressProgress, setCompressProgress] = useState<number>(0);
3232
const dropAreaRef = useRef<HTMLLabelElement>(null);
33+
const compressedImagesRef = useRef<HTMLDivElement>(null);
3334
const onImageQualityChange = async (
3435
event: React.ChangeEvent<HTMLInputElement>
3536
) => {
@@ -39,6 +40,8 @@ const ImageCompressor = () => {
3940
const handleImageUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
4041
e.preventDefault();
4142
if (e.target.files) {
43+
setCompressedImages([]);
44+
setCompressProgress(0);
4245
setFilelist(e.target.files);
4346
}
4447
};
@@ -141,6 +144,8 @@ const ImageCompressor = () => {
141144
e.preventDefault();
142145
e.stopPropagation();
143146
setIsDragActive(false);
147+
setCompressedImages([]);
148+
setCompressProgress(0);
144149
setFilelist(e.dataTransfer.files);
145150
};
146151

@@ -170,6 +175,19 @@ const ImageCompressor = () => {
170175
document.body.removeChild(downloadLink);
171176
};
172177

178+
// Add scroll effect when compressed images are available
179+
useEffect(() => {
180+
if (compressedImages.length > 0 && compressedImagesRef.current) {
181+
setTimeout(() => {
182+
compressedImagesRef.current?.scrollIntoView({
183+
behavior: "smooth",
184+
block: "start",
185+
inline: "nearest",
186+
});
187+
}, 300);
188+
}
189+
}, [compressedImages.length]);
190+
173191
return (
174192
<div className="container mx-auto px-4">
175193
<Intro />
@@ -245,7 +263,7 @@ const ImageCompressor = () => {
245263
<LoadingSpinner compressProgress={compressProgress} />
246264
</div>
247265
) : (
248-
<div>
266+
<div ref={compressedImagesRef}>
249267
<h2 className="mb-2 text-xl font-semibold">Compressed Images</h2>
250268
{compressedImages?.length > 0 ? (
251269
<PhotoProvider>

0 commit comments

Comments
 (0)