import formatDistanceToNow from "date-fns/formatDistanceToNow"; import { CopyToClipboard } from "react-copy-to-clipboard"; import React, { FC, useState, useEffect } from "react"; import { useFormState } from "react-use-form-state"; import { Flex } from "reflexbox/styled-components"; import styled, { css } from "styled-components"; import QRCode from "qrcode.react"; import Link from "next/link"; import { useStoreActions, useStoreState } from "../store"; import { removeProtocol, withComma } from "../utils"; import { NavButton, Button } from "./Button"; import { Col, RowCenter } from "./Layout"; import { ifProp } from "styled-tools"; import TextInput from "./TextInput"; import Animation from "./Animation"; import Tooltip from "./Tooltip"; import Table from "./Table"; import ALink from "./ALink"; import Modal from "./Modal"; import Text, { H2, Span } from "./Text"; import Icon from "./Icon"; import { Colors } from "../consts"; const Tr = styled(Flex).attrs({ as: "tr", px: [12, 12, 2] })``; const Th = styled(Flex)``; Th.defaultProps = { as: "th", flexBasis: 0, py: [12, 12, 3], px: [12, 12, 3] }; const Td = styled(Flex)<{ withFade?: boolean }>` position: relative; white-space: nowrap; ${ifProp( "withFade", css` :after { content: ""; position: absolute; right: 0; top: 0; height: 100%; width: 16px; background: linear-gradient(to left, white, white, transparent); } tr:hover &:after { background: linear-gradient( to left, ${Colors.TableRowHover}, ${Colors.TableRowHover}, transparent ); } ` )} `; Td.defaultProps = { as: "td", fontSize: [15, 16], alignItems: "center", flexBasis: 0, py: [12, 12, 3], px: [12, 12, 3] }; const Action = (props: React.ComponentProps) => ( ); const ogLinkFlex = { flexGrow: [1, 3, 7], flexShrink: [1, 3, 7] }; const createdFlex = { flexGrow: [1, 1, 3], flexShrink: [1, 1, 3] }; const shortLinkFlex = { flexGrow: [1, 1, 3], flexShrink: [1, 1, 3] }; const viewsFlex = { flexGrow: [0.5, 0.5, 1], flexShrink: [0.5, 0.5, 1], justifyContent: "flex-end" }; const actionsFlex = { flexGrow: [1, 1, 2.5], flexShrink: [1, 1, 2.5] }; interface Form { count?: string; page?: string; search?: string; } const LinksTable: FC = () => { const links = useStoreState(s => s.links); const { get, deleteOne } = useStoreActions(s => s.links); const [copied, setCopied] = useState([]); const [qrModal, setQRModal] = useState(-1); const [deleteModal, setDeleteModal] = useState(-1); const [deleteLoading, setDeleteLoading] = useState(false); const [formState, { text }] = useFormState
({ page: "1", count: "10" }); const options = formState.values; const linkToDelete = links.items[deleteModal]; useEffect(() => { get(options); }, [options.count, options.page]); const onSubmit = e => { e.preventDefault(); get(options); }; const onCopy = (index: number) => () => { setCopied([index]); setTimeout(() => { setCopied(s => s.filter(i => i !== index)); }, 1500); }; const onDelete = async () => { setDeleteLoading(true); await deleteOne({ id: linkToDelete.address, domain: linkToDelete.domain }); await get(options); setDeleteLoading(false); setDeleteModal(-1); }; const onNavChange = (nextPage: number) => () => { formState.setField("page", (parseInt(options.page) + nextPage).toString()); }; const Nav = ( {["10", "25", "50"].map(c => ( formState.setField("count", c)} > {c} ))} links.total } ml={12} px={2} > ); return (

Recent shortened links.

{Nav} {!links.items.length ? ( ) : ( <> {links.items.map((l, index) => ( ))} )} {Nav}
Original URL Created Short URL Views
{links.loading ? "Loading links..." : "No links to show."}
{l.target} {`${formatDistanceToNow( new Date(l.created_at) )} ago`} {copied.includes(index) ? ( ) : ( )} {removeProtocol(l.shortLink)} {withComma(l.visit_count)} {l.password && ( <> Password protected )} {l.visit_count > 0 && ( )} setQRModal(index)} /> setDeleteModal(index)} />
-1} closeHandler={() => setQRModal(-1)} > {links.items[qrModal] && ( )} -1} closeHandler={() => setDeleteModal(-1)} > {linkToDelete && ( <>

Delete link?

Are you sure do you want to delete the link{" "} "{removeProtocol(linkToDelete.shortLink)}"? {deleteLoading ? ( <> ) : ( <> )} )}
); }; export default LinksTable;