/** * "Upload Custom Font" tab of the Add Font modal. Goes through WordPress's * native Media Library (`wp.media`, enqueued only on this admin page via * wp_enqueue_media()) rather than a bespoke upload endpoint, gated * server-side by the `upload_mimes` filter in * Aureon\Modules\Font_Library\Font_Library. */ import { useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { Button } from '@wordpress/components'; import { Check, UploadCloud } from 'lucide-react'; import TextField from '../../controls/text-field'; import type { FontLibraryEntry } from '../../types'; interface MediaAttachment { id: number; filename: string; title: string; mime: string; } interface MediaFrame { open: () => void; on: ( event: string, callback: () => void ) => void; state: () => { get: ( key: string ) => { first: () => { toJSON: () => MediaAttachment }; }; }; } const newId = (): string => `font-${ Date.now().toString( 36 ) }`; interface UploadFontTabProps { onAdd: ( entry: FontLibraryEntry ) => void; onClose: () => void; } export default function UploadFontTab( { onAdd, onClose, }: UploadFontTabProps ) { const [ attachment, setAttachment ] = useState< MediaAttachment | null >( null ); const [ customFamily, setCustomFamily ] = useState( '' ); const openMediaLibrary = () => { // `window.wp.media`'s return type is declared elsewhere (see // media-image-field.tsx) as the generic `unknown` a plain image // picker needs; cast to this tab's own richer shape rather than // widening that shared global declaration for every other caller. const media = window.wp?.media as | ( ( options: Record< string, unknown > ) => MediaFrame ) | undefined; if ( ! media ) { return; } const frame = media( { title: __( 'Select a font file', 'aureon' ), button: { text: __( 'Use this font', 'aureon' ) }, library: { type: [ 'font/woff', 'font/woff2', 'font/ttf', 'font/otf' ], }, multiple: false, } ); frame.on( 'select', () => { const selected = frame.state().get( 'selection' ).first().toJSON(); setAttachment( selected ); if ( ! customFamily ) { setCustomFamily( selected.title || selected.filename ); } } ); frame.open(); }; const addCustomFont = () => { if ( ! attachment || ! customFamily.trim() ) { return; } onAdd( { id: newId(), source: 'custom', attachment_id: attachment.id, family: customFamily.trim(), styles: __( 'Custom', 'aureon' ), status: 'active', added_on: Math.floor( Date.now() / 1000 ), } ); onClose(); }; return (
{ attachment && ( <>

{ attachment.filename }

) }
); }