import CopyToClipboard from 'react-copy-to-clipboard'; import type {JSX} from 'react'; import {useEffect, useState} from 'react'; import clsx from 'clsx'; import { useShortenedLinks, ShortenedLinksActionTypes, } from '../contexts/shortened-links-context'; import {MAX_HISTORY_ITEMS} from '../Background/constants'; import Icon from '../components/Icon'; import Modal from './Modal'; import styles from './Table.module.scss'; function Table(): JSX.Element { const [shortenedLinksState, shortenedLinksDispatch] = useShortenedLinks(); const [QRView, setQRView] = useState(false); const [copied, setCopied] = useState(false); // reset copy message useEffect(() => { let timer: ReturnType | null = null; timer = setTimeout(() => { setCopied(false); // reset selected id from context }, 1300); return (): void => { if (timer) { clearTimeout(timer); } }; }, [copied]); function handleCopyToClipboard(selectedItemId: string): void { shortenedLinksDispatch({ type: ShortenedLinksActionTypes.SET_CURRENT_SELECTED, payload: selectedItemId, }); setCopied(true); } function handleQRCodeViewToggle(selectedItemId: string): void { shortenedLinksDispatch({ type: ShortenedLinksActionTypes.SET_CURRENT_SELECTED, payload: selectedItemId, }); setQRView(true); } return ( <>

Recent shortened links. (last {MAX_HISTORY_ITEMS} results)

{!(shortenedLinksState.total === 0) ? ( shortenedLinksState.items.map((item) => ( )) ) : ( )}
Original URL Short URL
{item.target} {copied && shortenedLinksState.selected?.id === item.id && (
Copied to clipboard!
)}
{/* // **** COPY TO CLIPBOARD **** // */} {copied && shortenedLinksState.selected?.id === item.id ? ( ) : ( handleCopyToClipboard(item.id)} > )} handleQRCodeViewToggle(item.id)} className={styles.actionIcon} name="qrcode" />
{/* // **** QR CODE MODAL **** // */} {QRView && shortenedLinksState.selected?.id === item.id && ( )}
No URLs History
); } export default Table;