import React, { useState } from 'react'; import { useAppDispatch, useAppSelector } from '../app/hooks'; import { copyToClipboard } from '../utils/clipboard'; import { shortenUrl, selectShortUrl, selectUrlStatus, selectUrlError, clearShortUrl, } from '../features/url/urlSlice'; const Home: React.FC = () => { /* component level state */ const [url, setUrl] = useState(''); /* global state */ const dispatch = useAppDispatch(); const shortUrl = useAppSelector(selectShortUrl); const status = useAppSelector(selectUrlStatus); const error = useAppSelector(selectUrlError); /* methods */ const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && url.trim() !== '') { dispatch(shortenUrl(url)); } }; const handleCopy = () => { if (shortUrl) { copyToClipboard(shortUrl) .then(() => console.log('Copied!')) .catch((err) => console.error('Copy failed', err)); } }; return (

minxa.lol

setUrl(e.target.value)} onKeyDown={handleKeyDown} className="w-80 p-3 rounded-md text-lg border border-gray-300 shadow-sm focus:outline-none focus:ring-2 focus:ring-orange-400" /> {/* Loading State */} {status === 'loading' && (

Shortening your URL...

)} {/* Error Message */} {status === 'failed' && (

{error}

)} {/* Short URL Display */} {status === 'succeeded' && shortUrl && (

Your short URL: {shortUrl}

)}
); }; export default Home;