"use client"; import React from "react"; import type { AspectRatio } from "@pien-studio/types"; import { useTranslations } from "../hooks/use-translations"; interface CanvasSizeModalProps { isOpen: boolean; onClose: () => void; currentWidth: number; currentHeight: number; currentAspect: AspectRatio; onApply: (width: number, height: number, aspect: AspectRatio) => void; isDark: boolean; } const PRESETS = [ { labelKey: "editor.presetSquare", sublabel: "1080 × 1080", aspect: "1:1" as AspectRatio, width: 1080, height: 1080, }, { labelKey: "editor.presetPortrait45", sublabel: "1080 × 1350", aspect: "4:5" as AspectRatio, width: 1080, height: 1350, }, { labelKey: "editor.presetStory916", sublabel: "1080 × 1920", aspect: "9:16" as AspectRatio, width: 1080, height: 1920, }, { labelKey: "editor.presetWidescreen", sublabel: "1920 × 1080", aspect: "16:9" as AspectRatio, width: 1920, height: 1080, }, { labelKey: "editor.presetPhoto43", sublabel: "1440 × 1080", aspect: "4:3" as AspectRatio, width: 1440, height: 1080, }, { labelKey: "editor.presetClassic32", sublabel: "1620 × 1080", aspect: "3:2" as AspectRatio, width: 1620, height: 1080, }, ]; export function CanvasSizeModal({ isOpen, onClose, currentWidth, currentHeight, currentAspect, onApply, isDark, }: CanvasSizeModalProps) { const { t } = useTranslations(); const [mode, setMode] = React.useState<"preset" | "custom">( PRESETS.some((p) => p.aspect === currentAspect) ? "preset" : "custom", ); const [customWidth, setCustomWidth] = React.useState(currentWidth.toString()); const [customHeight, setCustomHeight] = React.useState( currentHeight.toString(), ); const [selectedPreset, setSelectedPreset] = React.useState(currentAspect); if (!isOpen) return null; function handleApply() { if (mode === "preset") { const preset = PRESETS.find((p) => p.aspect === selectedPreset)!; onApply(preset.width, preset.height, preset.aspect); } else { const w = parseInt(customWidth, 10); const h = parseInt(customHeight, 10); if (!isNaN(w) && !isNaN(h) && w > 0 && h > 0) { onApply(w, h, "free"); } } onClose(); } const overlay = "fixed inset-0 z-50 flex items-center justify-center bg-black/40"; const panel = `w-full max-w-sm rounded-2xl border p-5 shadow-2xl ${ isDark ? "border-white/15 bg-[#2b2d31]" : "border-black/15 bg-white" }`; return (
e.stopPropagation()}>

{t("editor.canvasSizeTitle")}

{(["preset", "custom"] as const).map((m) => ( ))}
{mode === "preset" ? (
{PRESETS.map((p) => ( ))}
) : (
setCustomWidth(e.target.value)} min={1} className={`flex-1 rounded border px-2 py-1.5 text-sm ${ isDark ? "border-white/20 bg-[#25272b] text-[#e8eaed]" : "border-black/20 bg-[#f6f8fb] text-[#1f2430]" }`} /> px
setCustomHeight(e.target.value)} min={1} className={`flex-1 rounded border px-2 py-1.5 text-sm ${ isDark ? "border-white/20 bg-[#25272b] text-[#e8eaed]" : "border-black/20 bg-[#f6f8fb] text-[#1f2430]" }`} /> px
{parseInt(customWidth) || 0} × {parseInt(customHeight) || 0} px
)}
); }