import React, { ChangeEvent, useState } from "react"; import copyToClipboard, { copySupported } from "../../lib/copyToClipboard"; import useModalStore from "../../stores/useModalStore"; import useToastStore from "../../stores/useToastStore"; import { AttachmentResource, Locale } from "../../types"; import { putJson } from "../../lib/request"; interface AttachmentEditorProps { attachment: AttachmentResource, locale: string, locales: { [index: string]: Locale }, onUpdate: (localizations: Record>) => void } export default function AttachmentEditor(props: AttachmentEditorProps) { const { attachment, locales } = props; const [locale, setLocale] = useState(props.locale); const [localizations, setLocalizations] = useState({ name: attachment.name || {}, description: attachment.description || {}, }); const notice = useToastStore((state) => state.notice); const closeModal = useModalStore((state) => state.close); const updateLocalization = (name: "name" | "description") => (evt: ChangeEvent) => { setLocalizations({ ...localizations, [name]: { ...localizations[name], [locale]: evt.target.value } }); }; const copyEmbedCode = (evt: Event) => { evt.preventDefault(); copyToClipboard(`[attachment:${attachment.id}]`); notice("Embed code copied to clipboard"); }; const save = (evt: Event) => { evt.preventDefault(); evt.stopPropagation(); const data = { ...localizations }; void putJson(`/admin/attachments/${attachment.id}`, { attachment: data }); if (props.onUpdate) { props.onUpdate(data); } closeModal(); }; const inputDir = (locales && locales[locale] && locales[locale].dir) || "ltr"; return (
{props.locales && Object.keys(locales).length > 1 && (
)}