/** * "Google Fonts" tab of the Add Font modal: search + category ("Show") + * sort on the left above a checkbox list of the full bundled catalog, a "My * fonts" staging column on the right, Cancel/Done. Clicking Done downloads * and self-hosts each newly staged font via * Aureon\Modules\Font_Library\Font_Download_Controller — nothing is added * to the library until its files have actually been fetched. */ import { useState } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; import apiFetch from '@wordpress/api-fetch'; import { Button, SearchControl } from '@wordpress/components'; import SelectField from '../../controls/select-field'; import FontPickerList from './font-picker-list'; import FontPickerSelected from './font-picker-selected'; import { useFontCatalogFilters, type FontCategoryFilter, type FontSortOrder, } from './use-font-catalog-filters'; import type { FontLibraryCatalogEntry, FontLibraryEntry } from '../../types'; const FONT_DOWNLOAD_PATH = window.aureonAdmin?.fontDownloadPath ?? '/aureon/v1/font-library/download'; const CATEGORY_OPTIONS: { value: FontCategoryFilter; label: string }[] = [ { value: 'all', label: __( 'All fonts', 'aureon' ) }, { value: 'sans-serif', label: __( 'Sans Serif', 'aureon' ) }, { value: 'serif', label: __( 'Serif', 'aureon' ) }, { value: 'display', label: __( 'Display', 'aureon' ) }, { value: 'handwriting', label: __( 'Handwriting', 'aureon' ) }, { value: 'monospace', label: __( 'Monospace', 'aureon' ) }, ]; const SORT_OPTIONS: { value: FontSortOrder; label: string }[] = [ { value: 'popularity', label: __( 'Popularity', 'aureon' ) }, { value: 'alpha', label: __( 'Name (A–Z)', 'aureon' ) }, ]; const newId = (): string => `font-${ Date.now().toString( 36 ) }`; interface DownloadResponse { files: Record< string, string >; } interface GoogleFontsTabProps { catalog: FontLibraryCatalogEntry[]; existingSlugs: string[]; onAdd: ( entry: FontLibraryEntry ) => void; onClose: () => void; } export default function GoogleFontsTab( { catalog, existingSlugs, onAdd, onClose, }: GoogleFontsTabProps ) { const [ search, setSearch ] = useState( '' ); const [ category, setCategory ] = useState< FontCategoryFilter >( 'all' ); const [ sort, setSort ] = useState< FontSortOrder >( 'popularity' ); const [ pending, setPending ] = useState< FontLibraryCatalogEntry[] >( [] ); const [ isSaving, setIsSaving ] = useState( false ); const [ saveError, setSaveError ] = useState< string | null >( null ); const filtered = useFontCatalogFilters( { catalog, search, category, sort, } ); const togglePending = ( font: FontLibraryCatalogEntry ) => { setPending( ( current ) => current.some( ( item ) => item.slug === font.slug ) ? current.filter( ( item ) => item.slug !== font.slug ) : [ ...current, font ] ); }; const removePending = ( slug: string ) => setPending( ( current ) => current.filter( ( item ) => item.slug !== slug ) ); const confirmSelection = async () => { if ( 0 === pending.length ) { onClose(); return; } setIsSaving( true ); setSaveError( null ); for ( const font of pending ) { try { const response = await apiFetch< DownloadResponse >( { path: FONT_DOWNLOAD_PATH, method: 'POST', data: { slug: font.slug, variants: font.variants }, } ); onAdd( { id: newId(), source: 'google', slug: font.slug, family: font.label, styles: font.styles, status: 'active', added_on: Math.floor( Date.now() / 1000 ), files: response.files, } ); } catch ( error ) { setIsSaving( false ); setSaveError( sprintf( /* translators: %s: font family name. */ __( 'Couldn’t download %s. Please try again.', 'aureon' ), font.label ) ); return; } } setIsSaving( false ); onClose(); }; return (
setCategory( value as FontCategoryFilter ) } /> setSort( value as FontSortOrder ) } />
font.slug ) } existingSlugs={ existingSlugs } onToggle={ togglePending } />
{ saveError && (

{ saveError }

) }
); }