'use client' import { useState } from 'react' export default function ShortUrlForm() { const [longUrl, setLongUrl] = useState('') const [shortUrl, setShortUrl] = useState('') const [expiresIn, setExpiresIn] = useState('3600') // 默认1小时 const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState('') const generateShortUrl = async (e: React.FormEvent) => { e.preventDefault() setIsLoading(true) setError('') try { const response = await fetch('/api/shorten', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ url: longUrl, expiresIn: parseInt(expiresIn) }), }) if (!response.ok) { throw new Error('Failed to create short URL') } const data = await response.json() setShortUrl(data.shortUrl) } catch (err) { setError('An error occurred while creating the short URL') console.error(err) } finally { setIsLoading(false) } } return (