'use client' import { Box, Checkbox, Flex, Heading } from "@radix-ui/themes"; import { useTranslations } from "next-intl"; import { useState } from "react"; export type BackgroundType = "color" | "image"; export interface BackgroundProp { color: string | null; image: string | null; } export default function BackgroundSelector({ background, setBackground, }: { background: BackgroundProp; setBackground: (bg: BackgroundProp) => void; }) { const t = useTranslations("BackgoundSetting"); const types: BackgroundType[] = []; if (background.color) { types.push("color"); } if (background.image) { types.push("image"); } const [backgroundType, setBackgroundType] = useState(types); const [color, setColor] = useState(background.color); const [image, setImage] = useState(background.image); const handleBackgroundTypeChange = (value: BackgroundType) => { let newTypes: BackgroundType[]; if (backgroundType.includes(value)) { backgroundType.splice(backgroundType.indexOf(value), 1); newTypes = backgroundType; } else { newTypes = [...backgroundType, value]; } setBackgroundType(newTypes); if (newTypes.includes("color")) { background.color = color; } else { background.color = null; } if (newTypes.includes("image")) { background.image = image; } else { background.image = null; } setBackground({ ...background }); } const handleColorChange = (e: React.ChangeEvent) => { setColor(e.target.value); if (backgroundType.includes("color")) { setBackground({ ...background, color: e.target.value, }); } }; const handleImageUpload = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { const reader = new FileReader(); reader.onload = (event) => { const result = event.target?.result as string; setImage(result); if (backgroundType.includes("image")) { setBackground({ ...background, image: result, }); } }; reader.readAsDataURL(file); } }; return ( {t("title")} handleBackgroundTypeChange("color")} className="cursor-pointer" /> {t("colorOption")} {color && ()} handleBackgroundTypeChange("image")} className="cursor-pointer" /> {t("imageOption")} ); }