import { useContext, useState } from "react";
import Image from "next/image";
import { useTour } from "@reactour/tour";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faDownload } from "@fortawesome/free-solid-svg-icons";
import saveAsPng from "save-svg-as-png";
import { EditorType, IconItem } from "types/editor";
import { Icon } from "./icon";
import { EditorTabs } from "components/editor-icons/comps/tabs";
import { ListIcons } from "components/editor-icons/comps/list";
import { Search } from "components/search";
import { ClydeMessage } from "components/clyde/message";
import { Switch } from "components/switch";
import { Shapes } from "@/components/editor-icons/comps/shapes";
import { Icons } from "components/editor-icons/comps/icons";
import { useUser } from "utils/auth";
import { PremiumContext } from "components/premium/premium";
import DefaultAvatar from "assets/images/avatars/default-avatar.svg";
import DefaultAvatar2 from "assets/images/avatars/default-avatar-2.svg";
import BackgroundTransparent from "assets/images/editor/transparent_bg.svg";
import { API } from "@/utils/api";
import { Premium } from "../premium";
import { FormattedMessage, useIntl } from "react-intl";
export default function Editor({
editor,
onChange,
}: {
editor: EditorType;
onChange: (e: EditorType) => void;
}) {
const intl = useIntl();
const { user } = useUser();
const { setOpen } = useContext(PremiumContext);
const { setIsOpen } = useTour();
const [currentTab, setCurrentTab] = useState(0);
const [multiIcons, setMultiIcons] = useState(false);
const [search, setSearch] = useState("");
const renderTabs = (currentTab: number) => {
switch (currentTab) {
case 0:
return (
<>
setOpen(true) : setMultiIcons}
/>
{/* {!user?.id ? : ""} */}
{
// if (!user?.id) return setOpen(true);
onChange({
...editor,
icons: multiIcons
? [
...editor.icons,
{
colour: "#ffffff",
custom_text: {
enabled: true,
text: "10",
size: 160,
},
},
]
: [
{
...editor?.icons[0],
component: undefined,
position: undefined,
colour: "#ffffff",
custom_text: {
enabled: true,
text: "10",
size: 160,
},
},
],
});
if (!multiIcons) setCurrentTab(2);
}}
onCustomUpload={(image) => {
onChange({
...editor,
icons: multiIcons
? [
...editor.icons,
{
colour: "#ffffff",
image,
},
]
: [
{
...editor?.icons[0],
custom_text: undefined,
component: undefined,
position: undefined,
colour: "#ffffff",
image,
},
],
});
if (!multiIcons) setCurrentTab(2);
}}
onSelect={(icon: IconItem) => {
onChange({
...editor,
icons: multiIcons
? [
...editor.icons,
{
component: icon.name,
colour: icon?.defaultColor ?? "#fff",
},
]
: [
{
...editor?.icons[0],
colour: icon?.defaultColor ?? "#fff",
component: icon.name,
position: undefined,
custom_text: undefined,
},
],
});
}}
/>
>
);
case 1:
return (
<>
onChange({ ...editor, shape: { ...editor.shape, ...newShape } })
}
/>
>
);
case 2:
return (
<>
{
setCurrentTab(tabIndex);
setMultiIcons(user?.id ? allowMultiple || false : false);
}}
/>
>
);
default:
return null;
}
};
const handleSaveIcon = () => {
const svg = document.getElementById("discotools-selected-svg");
saveAsPng.saveSvgAsPng(svg, "discotools-xyz-icon.png", {
scale: 1.25,
encoderOptions: 1,
});
// API.download();
};
const previewAvatar = () => {
if (user?.id) {
if (user?.avatar)
return `https://cdn.discordapp.com/avatars/${user?.id}/${user?.avatar}.png`;
else
return `https://cdn.discordapp.com/embed/avatars/${
user.discriminator % 5
}.png`;
}
return DefaultAvatar.src;
};
const renderColor = () => {
if (editor?.shape?.gradient?.enabled) {
const color = editor?.shape?.gradient?.colours?.[0]?.value;
if (color.includes("rgba")) {
const rgba = color?.split(",");
return `rgb(${rgba[0].split("(")[1]}, ${rgba[1]}, ${rgba[2]})`;
}
return color;
}
if (editor?.shape?.colour?.includes("rgba")) {
const rgba = editor?.shape?.colour?.split(",");
return `rgb(${rgba[0].split("(")[1]}, ${rgba[1]}, ${rgba[2]})`;
}
if (editor?.shape?.colour) {
return editor.shape.colour;
}
return "#fff";
};
return (
setIsOpen(true)}
/>
{
// e.preventDefault();
// e.stopPropagation();
}}
>
Captain Astro
Today at 11:23 pm
user?.username ?? "Captain Astro",
}}
/>
);
}