"use client"; import { useState, useEffect } from 'react'; import { useSession } from 'next-auth/react'; import { useRouter } from 'next/navigation'; import Link from 'next/link'; import LinkTable from '@/components/ui/dashboard/LinkTable'; import styles from './Dashboard.module.css'; import { useToast } from '@/contexts/ToastContext'; export default function Dashboard() { const { data: session, status } = useSession(); const router = useRouter(); const { showToast } = useToast(); const [links, setLinks] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { if (status === "unauthenticated") { router.push('/'); } }, [status, router]); useEffect(() => { if (session?.user?.accountId) { fetchLinks(); } }, [session]); const fetchLinks = async () => { try { setLoading(true); const response = await fetch('/api/links'); if (!response.ok) { throw new Error('Failed to fetch links'); } const data = await response.json(); if (data.success) { setLinks(data.links); } else { showToast(data.message || 'Failed to load links', 'error'); } } catch (error) { console.error('Error fetching links:', error); showToast('Failed to load links', 'error'); } finally { setLoading(false); } }; if (status === "loading") { return
Loading...
; } if (status === "unauthenticated") { return null; } return (

Dashboard

{session?.user?.isAdmin && ( )}

Create New Short Link

Your Shortened Links

{loading ? (
Loading your links...
) : links.length === 0 ? (
You haven't created any links yet. Create your first short link above!
) : ( )}
); } interface CreateLinkFormProps { onLinkCreated: () => void; } function CreateLinkForm({ onLinkCreated }: CreateLinkFormProps) { const [url, setUrl] = useState(''); const [creating, setCreating] = useState(false); const { showToast } = useToast(); const handleSubmit = async (e: { preventDefault: () => void; }) => { e.preventDefault(); if (!url.trim()) { showToast('Please enter a URL', 'error'); return; } try { setCreating(true); const response = await fetch('/api/link', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ target_url: url }), }); const data = await response.json(); if (response.ok && data.success) { showToast('Link created successfully!', 'success'); setUrl(''); if (onLinkCreated) onLinkCreated(); } else { showToast(data.message || 'Failed to create link', 'error'); } } catch (error) { console.error('Error creating link:', error); showToast('Failed to create link', 'error'); } finally { setCreating(false); } }; return (
setUrl(e.target.value)} placeholder="Enter URL to shorten" className={styles.urlInput} required />
); }