'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 (
setLongUrl(e.target.value)} required className="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" placeholder="https://www.rainyun.com/cat_" />
{error && (
{error}
)} {shortUrl && (

生成的短链接,快拿去玩吧!

{shortUrl}
)}
) }