import { type MouseEventHandler, forwardRef, useEffect, useMemo, useState } from 'react' import type { BackendArt } from '../../functions/BackendTypes' import { WEB_BASE } from '../../functions/Backend' import { wtEvent } from '../../functions/Watchtower' import { toast } from '../../functions/Context' import './styles/ModalEmbed.css' import VectorBackgroundEmbed from '../../vectors/background-embed.svg' import HeaderMessage from './HeaderMessage' import InputButtonRow from '../inputs/ButtonRow' import InputButton from '../inputs/Button' import InputDescription from '../inputs/Description' import InputLabel from '../inputs/Label' interface PropsForModalEmbed { item: BackendArt onClose: MouseEventHandler } export default forwardRef(function ModalEmbed( { item, onClose }: PropsForModalEmbed, ref, ) { // Keep User Preferences const KEY_QUALITY = 'preference_embed_quality' const KEY_SCALE = 'preference_embed_scale' const [preferQuality, setQuality] = useState<'standard' | 'transparent'>( (() => { let raw = localStorage.getItem(KEY_QUALITY) ?? 'standard' if (raw !== 'standard' && raw !== 'transparent') { return 'standard' } else { return raw } })(), ) const [preferScale, setScale] = useState( (() => { let raw = localStorage.getItem(KEY_SCALE) ?? String('1') let val = parseFloat(raw) if (isNaN(val) || val < 0 || val > 1) return 1 return val })(), ) useEffect(() => localStorage.setItem(KEY_QUALITY, String(preferQuality)), [preferQuality]) useEffect(() => localStorage.setItem(KEY_SCALE, String(preferScale)), [preferScale]) // Calculate Embed Values const embedScale = useMemo(() => { const maxDim = Math.max(item.width, item.height) const baseScale = maxDim > 640 ? 640 / maxDim : 1 return baseScale * preferScale }, [item.width, item.height, preferScale]) const embedHeight = useMemo(() => (item.height * embedScale) | 0, [embedScale]) const embedWidth = useMemo(() => (item.width * embedScale) | 0, [embedScale]) // const embedQuality = useMemo(() => { // if (preferQuality === 'standard.avif') return 'standard' // return 'transparent' // }, [preferQuality]) const embedHTML = useMemo( () => ``, [preferQuality, embedScale], ) function onCopy() { navigator.clipboard.writeText(embedHTML) toast('action-copy', 'Copied Code to Clipboard!') wtEvent('action_animation_embed_copy', { id: item.id, height: embedHeight, width: embedWidth, scale: (embedScale * 100) | 0, quality: preferQuality, }) } return (
{/* Left-Pane */}