import { ModeToggle } from '@/components/mode-toggle' import { ThemeProvider } from '@/components/theme-provider' 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 { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Toaster } from '@/components/ui/toaster' import { useToast } from '@/components/ui/use-toast' import { Copy } from 'lucide-react' import { useEffect, useState } from 'react' const backend_url = import.meta.env.VITE_BACKEND_URL ?? 'http://192.168.100.40:3000' type Shortener = { id: number link: string code: string } export default function App() { const [shorteners, setShorteners] = useState([]) const getShorteners = async () => { const response = await fetch(backend_url + '/link', { method: 'GET', }) const data = (await response.json()).shorteners as Shortener[] setShorteners(data) } useEffect(() => { if (!shorteners.length) { getShorteners() } }, []) return (
) } const Navbar = ({ getShorteners }: { getShorteners: () => Promise }) => { return (
Shortener
) } const CreateShortener = ({ getShorteners, }: { getShorteners: () => Promise }) => { const [isOpen, setIsOpen] = useState(false) const [link, setLink] = useState('') const addShortener = async () => { await fetch(backend_url + '/link', { headers: { 'Content-Type': 'application/json', }, method: 'POST', body: JSON.stringify({ link: link, }), }).then(() => { getShorteners() setLink('') setIsOpen(false) }) } return ( Add Shortener Create a new shortener for your link.
setLink(e.target.value)} />
) } const ShortenerTable = ({ shorteners }: { shorteners: Shortener[] }) => { 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`, }) } return ( Shorteners {!shorteners.length && ( No Shorteners )} Link Shortener {shorteners.length ? ( shorteners.map((shortener) => ( {shortener.link} copyLinkToClipboard(shortener.code) }>
{shortener.code}
)) ) : ( )}
) }