'use client'; import React, { useState, useEffect, useRef } from 'react'; import { Type, Upload, Check, Star } from 'lucide-react'; import { toast } from 'sonner'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { getSuggestedFont } from '@/lib/language'; interface Font { name: string; family: string; css: string; url?: string; } interface FontSelectorProps { currentFont: string; onFontChange: (font: string) => void; detectedLang?: string; } export default function FontSelector({ currentFont, onFontChange, detectedLang }: FontSelectorProps) { const [fonts, setFonts] = useState([]); const [loading, setLoading] = useState(true); const [suggestedFont, setSuggestedFont] = useState(null); const fileInputRef = useRef(null); const fetchFonts = async () => { try { const res = await fetch('/api/fonts'); const data = await res.json(); if (data.fonts) { setFonts(data.fonts); // Inject custom fonts styles data.fonts.forEach((font: Font) => { if (font.url) { const style = document.createElement('style'); style.textContent = ` @font-face { font-family: '${font.family}'; src: url('${font.url}') format('truetype'); } `; document.head.appendChild(style); } }); } } catch (error) { console.error('Failed to fetch fonts', error); toast.error('Failed to load fonts'); } finally { setLoading(false); } }; useEffect(() => { fetchFonts(); }, []); useEffect(() => { if (detectedLang) { const suggested = getSuggestedFont(detectedLang); setSuggestedFont(suggested); } }, [detectedLang]); const handleFileUpload = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; const formData = new FormData(); formData.append('font', file); const promise = fetch('/api/upload-font', { method: 'POST', body: formData, }).then(async (res) => { if (!res.ok) throw new Error('Upload failed'); await fetchFonts(); return 'Font uploaded successfully'; }); toast.promise(promise, { loading: 'Uploading font...', success: (data) => data, error: 'Failed to upload font', }); }; return ( Typography
{fonts.map((font) => (
onFontChange(font.family)} >
{font.name} {suggestedFont === font.name && ( Recommended )}
{currentFont === font.family && ( )}
))}

Select a font optimized for the target language.

Custom Font
); }