import { useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { Button } from "@/components/buttons/Button"; import { Icon, Icons } from "@/components/Icon"; import { DisplayError } from "@/components/player/display/displayInterface"; export function ErrorCard(props: { error: DisplayError | string }) { const [showErrorCard, setShowErrorCard] = useState(true); const [hasCopied, setHasCopied] = useState(false); const hasCopiedUnsetDebounce = useRef | null>( null ); const { t } = useTranslation(); let errorMessage: string | null = null; if (typeof props.error === "string") errorMessage = props.error; else if (props.error.key) errorMessage = `${props.error.type}: ${t(props.error.key)}`; else if (props.error.message) errorMessage = `${props.error.type}: ${t(props.error.message)}`; function copyError() { if (!props.error || !navigator.clipboard) return; navigator.clipboard.writeText(`\`\`\`${errorMessage}\`\`\``); setHasCopied(true); // Debounce unsetting the "has copied" label if (hasCopiedUnsetDebounce.current) clearTimeout(hasCopiedUnsetDebounce.current); hasCopiedUnsetDebounce.current = setTimeout(() => setHasCopied(false), 2e3); } if (!showErrorCard) return null; return ( // I didn't put a here because it'd fade out, then jump height weirdly
{t("errors.details")}
{errorMessage}
); }