import { CopyToClipboard } from "react-copy-to-clipboard"; import { Flex } from "reflexbox/styled-components"; import React, { useState } from "react"; import styled from "styled-components"; import QRCode from "qrcode.react"; import { useStoreActions, useStoreState } from "../store"; import { Col, RowCenterH, RowCenter } from "./Layout"; import { useFormState } from "react-use-form-state"; import { removeProtocol } from "../utils"; import { Link } from "../store/links"; import { useMessage } from "../hooks"; import TextInput from "./TextInput"; import Animation from "./Animation"; import Checkbox from "./Checkbox"; import { Button } from "./Button"; import Tooltip from "./Tooltip"; import Modal from "./Modal"; import Text from "./Text"; import Icon from "./Icon"; const SubmitIconWrapper = styled.div` content: ""; position: absolute; top: 0; right: 12px; width: 64px; height: 100%; display: flex; justify-content: center; align-items: center; cursor: pointer; :hover svg { fill: #673ab7; } @media only screen and (max-width: 448px) { right: 8px; width: 40px; } `; const ShortenedLink = styled(Text)` border-bottom: 2px dotted #aaa; cursor: pointer; transition: all 0.2s ease; :hover { opacity: 0.5; } `; interface Form { target: string; customurl?: string; password?: string; showAdvanced?: boolean; } const Shortener = () => { const { isAuthenticated } = useStoreState(s => s.auth); const [domain] = useStoreState(s => s.settings.domains); const submit = useStoreActions(s => s.links.submit); const [link, setLink] = useState(null); const [message, setMessage] = useMessage(3000); const [loading, setLoading] = useState(false); const [qrModal, setQRModal] = useState(false); const [copied, setCopied] = useMessage(3000); const [formState, { raw, password, text, label }] = useFormState
(null, { withIds: true, onChange(e, stateValues, nextStateValues) { if (stateValues.showAdvanced && !nextStateValues.showAdvanced) { formState.clear(); formState.setField("target", stateValues.target); } } }); const onSubmit = async e => { e.preventDefault(); if (loading) return; setCopied(""); setLoading(true); try { const link = await submit(formState.values); setLink(link); formState.clear(); } catch (err) { setMessage( err?.response?.data?.error || "Couldn't create the short link." ); } setLoading(false); }; const title = !link && ( Kutt your links{" "} shorter . ); const onCopy = () => setCopied("Copied to clipboard.", "green"); const result = link && ( {removeProtocol(link.shortLink)} {copied && ( {copied.text} )} ); return ( {title} {result} {message.text && ( {message.text} )} { if (!isAuthenticated) { setMessage( "You need to log in or sign up to use advanced options." ); return false; } return !formState.values.showAdvanced; } })} checked={formState.values.showAdvanced} label="Show advanced options" mt={24} /> {formState.values.showAdvanced && ( {(domain || {}).customDomain || (typeof window !== "undefined" && window.location.hostname)} / Password: )} ); }; export default Shortener;