image-converter-client/app/routes/_index.tsx

77 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-11-16 21:52:33 +00:00
import { Input } from "~/components/ui/input";
2024-11-02 22:30:55 +00:00
import type { MetaFunction } from "@remix-run/cloudflare";
2024-11-02 22:38:33 +00:00
import CropSelector from "./CropSelector";
2024-11-16 21:52:33 +00:00
import { Button } from "~/components/ui/button";
import { z, ZodError } from "zod";
import { ChangeEvent, Suspense, useEffect, useRef, useState } from "react";
import { useSubmit } from "@remix-run/react";
import { ActionFunctionArgs, Form, json, useFetcher } from "react-router-dom";
2024-10-30 22:11:22 +00:00
export const meta: MetaFunction = () => {
return [
2024-11-02 15:47:17 +00:00
{ title: "Convert images for the web!" },
{ name: "image converter", content: "Image conversion service" },
2024-10-30 22:11:22 +00:00
];
};
2024-11-16 21:52:33 +00:00
const MAX_FILE_SIZE = 5 * 1024 * 1024;
const ACCEPTED_IMAGE_TYPES = [
"image/jpeg",
"image/jpg",
"image/png",
"image/webp",
"image/gif",
] as const;
export const imageFileSchema = z.object({
name: z.string(),
size: z.number().max(MAX_FILE_SIZE, "File size must be less than 5MB"),
type: z.enum(ACCEPTED_IMAGE_TYPES, {
errorMap: () => ({ message: "Only .jpg, .jpeg, .png, .webp and .gif files are accepted." }),
}),
});
2024-10-30 22:11:22 +00:00
export default function Index() {
2024-11-16 21:52:33 +00:00
const [image, setImage] = useState<string>();
const imageRef = useRef<HTMLImageElement | null>(null);
const handleFileChange = (e: ChangeEvent<HTMLInputElement>) => {
console.log(e.target.files);
if (!e.target.files) {
return;
}
setImage(URL.createObjectURL(e.target.files[0]));
};
useEffect(() => {
if (imageRef.current?.complete && image) {
URL.revokeObjectURL(image);
}
});
2024-10-30 22:11:22 +00:00
return (
2024-11-16 21:52:33 +00:00
<div className="w-screen h-screen flex flex-col justify-center items-center">
{image && <img ref={imageRef} src={image} className="h-40 w-40" />}
<Input name="image-upload" type="file" onChange={handleFileChange} />
<CropSelector image={image} />
<Button>Submit</Button>
</div>
);
2024-10-30 22:11:22 +00:00
}
2024-11-16 21:52:33 +00:00
// export async function action({ request }: ActionFunctionArgs) {
// const formData = await request.formData();
// const image = formData.get("image-upload") as File;
// console.log("skibidi action!");
// try {
// const valid_image = imageFileSchema.parse(image);
// return valid_image;
// } catch (e: any) {
// if (e instanceof ZodError) {
// const errors: Record<string, string> = {};
// errors.image = e.message;
// return json({ errors });
// }
// console.log(e);
// }
// }