'use client'; import React, { useState } from 'react'; import { parseHuggingFaceUrl } from '../lib/huggingface'; import QRCodeWithLogo from './QRCodeWithLogo'; import { saveAs } from 'file-saver'; import * as htmlToImage from 'html-to-image'; const HuggingFaceQRGenerator = () => { const [inputUrl, setInputUrl] = useState(''); const [profileData, setProfileData] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [qrCodeInstance, setQrCodeInstance] = useState(null); const handleGenerate = async () => { setError(''); setLoading(true); try { // Parse the URL to extract username and resource info const parsed = parseHuggingFaceUrl(inputUrl); // Fetch profile data from our API const response = await fetch('/api/huggingface', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ username: parsed.username, resourceType: parsed.resourceType, resourceName: parsed.resourceName }), }); if (!response.ok) { throw new Error('Failed to fetch profile data'); } const data = await response.json(); // Use proxy for the avatar to avoid CORS issues const proxiedAvatarUrl = `/api/proxy-image?url=${encodeURIComponent(data.avatarUrl)}`; setProfileData({ ...data, avatarUrl: proxiedAvatarUrl, originalAvatarUrl: data.avatarUrl, qrValue: parsed.profileUrl }); } catch (err) { setError(err.message || 'Invalid URL or username'); setProfileData(null); } finally { setLoading(false); } }; const handleDownload = async (format = 'png') => { if (!profileData) return; try { const cardElement = document.getElementById('qr-card'); if (format === 'png' || format === 'card') { // Download the entire card with user info const dataUrl = await htmlToImage.toPng(cardElement, { quality: 1.0, backgroundColor: '#ffffff', pixelRatio: 2, // Higher quality style: { margin: '0', padding: '20px', borderRadius: '16px' } }); // Convert dataURL to blob const response = await fetch(dataUrl); const blob = await response.blob(); saveAs(blob, `huggingface-${profileData.username}-card.png`); } else if (format === 'qr-only' && qrCodeInstance) { // Download just the QR code const blob = await qrCodeInstance.download({ name: `huggingface-${profileData.username}`, extension: 'png' }); saveAs(blob, `huggingface-${profileData.username}-qr.png`); } else if (format === 'svg' && qrCodeInstance) { // Download QR as SVG const blob = await qrCodeInstance.download({ name: `huggingface-${profileData.username}`, extension: 'svg' }); saveAs(blob, `huggingface-${profileData.username}.svg`); } } catch (err) { console.error('Download error:', err); } }; const handleShare = (platform) => { const shareText = `Check out my Hugging Face profile!`; const shareUrl = profileData?.qrValue || ''; const shareLinks = { twitter: `https://twitter.com/intent/tweet?text=${encodeURIComponent(shareText)}&url=${encodeURIComponent(shareUrl)}`, facebook: `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(shareUrl)}`, linkedin: `https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(shareUrl)}` }; if (shareLinks[platform]) { window.open(shareLinks[platform], '_blank', 'width=600,height=400'); } }; const isValid = inputUrl.trim().length > 0; return (
🤗

Hugging Face

Roast your favorite Hugging Face user!

setInputUrl(e.target.value)} placeholder="https://huggingface.co/" className="hf-input" onKeyPress={(e) => e.key === 'Enter' && handleGenerate()} />
{error && (
{error}
)}
{profileData && (
{profileData.fullName}

{profileData.fullName}

@{profileData.username}

View Profile →
{profileData.fullName}
{profileData.fullName}

Share your QR code so others can follow you

Hugging Face Hugging Face
Tap background to change color

Download Options

Share Profile

)}
); }; export default HuggingFaceQRGenerator;