"use client"; import { redirect } from "next/navigation"; import { useCallback, useEffect, useState } from "react"; import { FileWithPath, useDropzone } from "react-dropzone"; import { Icon } from "@iconify/react"; import qrcode from "qrcode-generator"; import { nameSchema, tagsSchema } from "@/lib/schemas"; import { convertQrCode } from "@/lib/qr-codes"; import Mii from "@/lib/mii.js/mii"; import TomodachiLifeMii from "@/lib/tomodachi-life-mii"; import TagSelector from "./submit/tag-selector"; import ImageList from "./submit/image-list"; import QrUpload from "./submit/qr-upload"; import QrScanner from "./submit/qr-scanner"; export default function SubmitForm() { const [files, setFiles] = useState([]); const handleDrop = useCallback((acceptedFiles: FileWithPath[]) => { setFiles((prev) => [...prev, ...acceptedFiles]); }, []); const { getRootProps, getInputProps } = useDropzone({ onDrop: handleDrop, maxFiles: 3, accept: { "image/*": [".png", ".jpg", ".jpeg", ".bmp", ".webp"], }, }); const [isQrScannerOpen, setIsQrScannerOpen] = useState(false); const [studioUrl, setStudioUrl] = useState(); const [generatedQrCodeUrl, setGeneratedQrCodeUrl] = useState(); const [error, setError] = useState(undefined); const [name, setName] = useState(""); const [tags, setTags] = useState([]); const [qrBytesRaw, setQrBytesRaw] = useState([]); const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); // Validate before sending request const nameValidation = nameSchema.safeParse(name); if (!nameValidation.success) { setError(nameValidation.error.errors[0].message); return; } const tagsValidation = tagsSchema.safeParse(tags); if (!tagsValidation.success) { setError(tagsValidation.error.errors[0].message); return; } // Send request to server const formData = new FormData(); formData.append("name", name); formData.append("tags", JSON.stringify(tags)); formData.append("qrBytesRaw", JSON.stringify(qrBytesRaw)); files.forEach((file, index) => { // image1, image2, etc. formData.append(`image${index + 1}`, file); }); const response = await fetch("/api/submit", { method: "POST", body: formData, }); const { id, error } = await response.json(); if (!response.ok) { setError(error); return; } redirect(`/mii/${id}`); }; useEffect(() => { if (qrBytesRaw.length == 0) return; const qrBytes = new Uint8Array(qrBytesRaw); const preview = async () => { setError(""); // Validate QR code size if (qrBytesRaw.length !== 372) { setError("QR code size is not a valid Tomodachi Life QR code"); return; } // Convert QR code to JS let conversion: { mii: Mii; tomodachiLifeMii: TomodachiLifeMii }; try { conversion = convertQrCode(qrBytes); } catch (error) { setError(error as string); return; } try { setStudioUrl(conversion.mii.studioUrl({ width: 128 })); // Generate a new QR code for aesthetic reasons const byteString = String.fromCharCode(...qrBytes); const generatedCode = qrcode(0, "L"); generatedCode.addData(byteString, "Byte"); generatedCode.make(); setGeneratedQrCodeUrl(generatedCode.createDataURL()); } catch (error) { setError("Failed to get and/or generate Mii images"); } }; preview(); }, [qrBytesRaw]); return (
{!studioUrl && Mii} Nintendo Studio URL
{!generatedQrCodeUrl && QR Code} Generated QR Code

Drag and drop your images here
or click to open

setName(e.target.value)} />
QR Code or
{error && Error: {error}}
); }