'use client' import { useState } from 'react' import { PinnedWebsite } from '../types/types' export default function AddWebsite() { const [url, setUrl] = useState('') const [title, setTitle] = useState('') const [isAdding, setIsAdding] = useState(false) const formatUrl = (inputUrl: string) => { // Remove leading/trailing whitespace let formattedUrl = inputUrl.trim() // If URL doesn't start with a protocol, add https:// if (!formattedUrl.match(/^https?:\/\//i)) { formattedUrl = 'https://' + formattedUrl } return formattedUrl } const handleSubmit = (e: React.FormEvent) => { e.preventDefault() const formattedUrl = formatUrl(url) // Validate URL try { new URL(formattedUrl) } catch { alert('Please enter a valid URL') return } const newWebsite: PinnedWebsite = { id: Date.now().toString(), title: title || formattedUrl, url: formattedUrl, addedAt: Date.now() } // Load existing websites const existingWebsites = JSON.parse(localStorage.getItem('pinnedWebsites') || '[]') // Add new website localStorage.setItem('pinnedWebsites', JSON.stringify([...existingWebsites, newWebsite])) // Reset form setUrl('') setTitle('') setIsAdding(false) // Reload page to show new website window.location.reload() } return (