import { ModeToggle } from '@/components/mode-toggle' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogFooter, DialogTitle, DialogTrigger, } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu' import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Toaster } from '@/components/ui/toaster' import { useToast } from '@/components/ui/use-toast' import { Copy, Loader2, MoreHorizontal, Settings, User } from 'lucide-react' import { useEffect, useState } from 'react' import { useNavigate } from 'react-router-dom' const backend_url = import.meta.env.VITE_BACKEND_URL ?? 'http://192.168.100.40:3000' type Shortener = { id: number link: string code: string visitor_count: string } export default function App() { const [shorteners, setShorteners] = useState([]) const [isLoading, setIsLoading] = useState(false) const getShorteners = async () => { setIsLoading(true) const response = await fetch(backend_url + '/link', { method: 'GET', }) const data = (await response.json()).shorteners as Shortener[] setShorteners(data) setIsLoading(false) } useEffect(() => { if (!shorteners.length) { getShorteners() } }, []) return ( <>
) } export const Navbar = () => { const navigate = useNavigate() return (
navigate('/')}> Shortener
) } const CreateShortener = ({ getShorteners, }: { getShorteners: () => Promise }) => { const [isOpen, setIsOpen] = useState(false) const [link, setLink] = useState('') const [error, setError] = useState('') const addShortener = async () => { setError('') await fetch(backend_url + '/link', { headers: { 'Content-Type': 'application/json', }, method: 'POST', body: JSON.stringify({ link: link.startsWith('https://') ? link : 'https://' + link, }), }).then((response) => { if (response.status === 400) { setError('Invalid Url') return } getShorteners() setLink('') setIsOpen(false) }) } return ( Add Shortener Create a new shortener for your link.
setLink(e.target.value)} />
{error}
) } const ShortenerTable = ({ shorteners, isLoading, getShorteners, }: { shorteners: Shortener[] isLoading: boolean getShorteners: () => Promise }) => { const { toast } = useToast() const copyLinkToClipboard = async (code: string) => { await navigator.clipboard.writeText(backend_url + '/' + code) toast({ title: 'Link Copied', description: `Copied ${backend_url + '/' + code} To Clipboard`, }) } const navigate = useNavigate() return (
Shorteners
{isLoading && ( )}
{!shorteners.length && ( No Shorteners )} Link Shortener Visitors {shorteners.length ? ( shorteners.map((shortener) => ( {shortener.link} copyLinkToClipboard(shortener.code) }>
{shortener.code}
{shortener.visitor_count} Actions { navigate( '/dashboard/' + shortener.code ) }}> View Details {' '}
)) ) : ( )}
) }