/** * Branding Section * Logo upload and alignment settings */ import { Label } from '@/components/ui/label'; import { Button } from '@/components/ui/button'; import { Slider } from '@/components/ui/slider'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from '@/components/ui/collapsible'; import { ChevronDown, Image, Trash2, AlertCircle } from 'lucide-react'; import { useState } from 'react'; import type { EmailLogo } from '../types'; interface WPMediaAttachment { url: string; id: number; } interface WPMediaSelection { first: () => { toJSON: () => WPMediaAttachment; }; } interface WPMediaState { get: (key: string) => WPMediaSelection; } interface WPMediaFrame { on: (event: string, callback: () => void) => WPMediaFrame; open: () => void; state: () => WPMediaState; } interface WPMedia { (options: { title: string; button: { text: string }; multiple: boolean; library?: { type: string }; }): WPMediaFrame; frames?: Record; } declare global { interface Window { wp?: { media?: WPMedia; }; } } interface BrandingSectionProps { logo: EmailLogo; onUpdate: (updates: Partial) => void; } export function BrandingSection({ logo, onUpdate }: BrandingSectionProps) { const [isOpen, setIsOpen] = useState(true); const [mediaError, setMediaError] = useState(null); const handleSelectLogo = () => { setMediaError(null); // Check if WordPress media library is available if (!window.wp?.media) { setMediaError('WordPress media library is not available. Please refresh the page.'); console.error('WordPress media library not available. window.wp:', window.wp); return; } // Always create a new frame to avoid stale state issues const frame = window.wp.media({ title: 'Select Logo', button: { text: 'Select' }, multiple: false, }); frame.on('select', () => { const attachment = frame.state().get('selection').first().toJSON(); if (attachment?.url) { onUpdate({ url: attachment.url }); } }); frame.open(); }; const handleRemoveLogo = () => { onUpdate({ url: '' }); }; return (
Branding
{/* Media Error Message */} {mediaError && (
{mediaError}
)} {/* Logo Upload */}
{logo.url ? (
Email logo
) : ( )}
{/* Logo Width */}
{logo.width}px
onUpdate({ width: value })} min={50} max={400} step={10} />
{/* Logo Alignment */}
); } export default BrandingSection;