import type { GuildEmbed } from "@/typings"; import { cn } from "@/utils/cn"; import React, { useState } from "react"; import { BiMoon, BiSun } from "react-icons/bi"; import { FaFloppyDisk } from "react-icons/fa6"; import { HiChevronDown, HiChevronUp } from "react-icons/hi"; import { DiscordMarkdown } from "./discord/markdown"; import DiscordMessage from "./discord/message"; import DiscordMessageEmbed from "./discord/message-embed"; import DumbColorInput from "./inputs/dumb-color-input"; import DumbTextInput from "./inputs/dumb-text-input"; import { Button } from "./ui/button"; enum State { Idle = 0, Loading = 1, Success = 2 } interface Props { children?: React.ReactNode; name: string; url: string; dataName: string; defaultMessage?: { content?: string | null; embed?: GuildEmbed; }; isCollapseable?: boolean; messageAttachmentComponent?: React.ReactNode; showMessageAttachmentComponentInEmbed?: boolean; user?: { username: string; avatar: string; bot: boolean; }; disabled?: boolean; onSave?: (state: { content: string; embed: GuildEmbed; }) => void; } export default function MessageCreatorEmbed({ children, name, url, dataName, defaultMessage, isCollapseable, messageAttachmentComponent, showMessageAttachmentComponentInEmbed, user, disabled, onSave }: Props) { const [state, setState] = useState(State.Idle); const [error, setError] = useState(null); const [content, setContent] = useState(defaultMessage?.content || ""); const [embed, setEmbed] = useState(JSON.stringify(defaultMessage?.embed || {})); const [embedfooter, setEmbedfooter] = useState(JSON.stringify(defaultMessage?.embed?.footer || {})); const [open, setOpen] = useState(!isCollapseable); const [mode, setMode] = useState<"DARK" | "LIGHT">("DARK"); const modeToggle = (
); async function save() { setError(null); setState(State.Loading); const body = { content, embed: Object.assign( JSON.parse(embed), embedfooter.length ? { footer: JSON.parse(embedfooter) } : undefined ) }; if (!body.embed.footer.text) body.embed.footer = { text: null }; const res = await fetch(`${process.env.NEXT_PUBLIC_API}${url}`, { method: "PATCH", credentials: "include", headers: { "Content-Type": "application/json" }, body: JSON.stringify(dataName.includes(".") ? { [dataName.split(".")[0]]: { [dataName.split(".")[1]]: body } } : { [dataName]: body } ) }) .then((r) => r.json()) .catch(() => null); if (!res || "status" in res) { setState(State.Idle); setError( "message" in res ? res.message : "Something went wrong while saving.." ); return; } if (onSave) onSave(body); setState(State.Success); setTimeout(() => setState(State.Idle), 1_000 * 8); } return (
{name}
{isCollapseable &&
} {open &&
{children &&
{children}
}
Color Theme
{modeToggle}
{modeToggle}
{JSON.parse(embed).description && } {showMessageAttachmentComponentInEmbed && messageAttachmentComponent} {!showMessageAttachmentComponentInEmbed && messageAttachmentComponent}
The preview might display things wrong*
}
{error &&
{error}
} {state === State.Success &&
Saved
}
); }