Shortener.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. import { CopyToClipboard } from "react-copy-to-clipboard";
  2. import { useFormState } from "react-use-form-state";
  3. import { Flex } from "reflexbox/styled-components";
  4. import React, { FC, useState } from "react";
  5. import styled from "styled-components";
  6. import getConfig from "next/config";
  7. import { useStoreActions, useStoreState } from "../store";
  8. import { Checkbox, Select, TextInput } from "./Input";
  9. import { Col, RowCenterH, RowCenter } from "./Layout";
  10. import { useMessage, useCopy } from "../hooks";
  11. import { removeProtocol } from "../utils";
  12. import Text, { H1, Span } from "./Text";
  13. import { Link } from "../store/links";
  14. import Animation from "./Animation";
  15. import { Colors } from "../consts";
  16. import Icon from "./Icon";
  17. const { publicRuntimeConfig } = getConfig();
  18. const SubmitIconWrapper = styled.div`
  19. content: "";
  20. position: absolute;
  21. top: 0;
  22. right: 12px;
  23. width: 64px;
  24. height: 100%;
  25. display: flex;
  26. justify-content: center;
  27. align-items: center;
  28. cursor: pointer;
  29. :hover svg {
  30. fill: #673ab7;
  31. }
  32. @media only screen and (max-width: 448px) {
  33. right: 8px;
  34. width: 40px;
  35. }
  36. `;
  37. const ShortenedLink = styled(H1)`
  38. cursor: "pointer";
  39. border-bottom: 1px dotted ${Colors.StatsTotalUnderline};
  40. cursor: pointer;
  41. :hover {
  42. opacity: 0.8;
  43. }
  44. `;
  45. interface Form {
  46. target: string;
  47. domain?: string;
  48. customurl?: string;
  49. password?: string;
  50. description?: string;
  51. showAdvanced?: boolean;
  52. }
  53. const defaultDomain = publicRuntimeConfig.DEFAULT_DOMAIN;
  54. const Shortener = () => {
  55. const { isAuthenticated } = useStoreState(s => s.auth);
  56. const domains = useStoreState(s => s.settings.domains);
  57. const submit = useStoreActions(s => s.links.submit);
  58. const [link, setLink] = useState<Link | null>(null);
  59. const [message, setMessage] = useMessage(3000);
  60. const [loading, setLoading] = useState(false);
  61. const [copied, setCopied] = useCopy();
  62. const [formState, { raw, password, text, select, label }] = useFormState<
  63. Form
  64. >(
  65. { showAdvanced: false },
  66. {
  67. withIds: true,
  68. onChange(e, stateValues, nextStateValues) {
  69. if (stateValues.showAdvanced && !nextStateValues.showAdvanced) {
  70. formState.clear();
  71. formState.setField("target", stateValues.target);
  72. }
  73. }
  74. }
  75. );
  76. const submitLink = async (reCaptchaToken?: string) => {
  77. try {
  78. const link = await submit({ ...formState.values, reCaptchaToken });
  79. setLink(link);
  80. formState.clear();
  81. } catch (err) {
  82. setMessage(
  83. err?.response?.data?.error || "Couldn't create the short link."
  84. );
  85. }
  86. setLoading(false);
  87. };
  88. const onSubmit = async e => {
  89. e.preventDefault();
  90. if (loading) return;
  91. setCopied(false);
  92. setLoading(true);
  93. if (
  94. process.env.NODE_ENV === "production" &&
  95. !!publicRuntimeConfig.RECAPTCHA_SITE_KEY &&
  96. !isAuthenticated
  97. ) {
  98. window.grecaptcha.execute(window.captchaId);
  99. const getCaptchaToken = () => {
  100. setTimeout(() => {
  101. if (window.isCaptchaReady) {
  102. const reCaptchaToken = window.grecaptcha.getResponse(
  103. window.captchaId
  104. );
  105. window.isCaptchaReady = false;
  106. window.grecaptcha.reset(window.captchaId);
  107. return submitLink(reCaptchaToken);
  108. }
  109. return getCaptchaToken();
  110. }, 200);
  111. };
  112. return getCaptchaToken();
  113. }
  114. return submitLink();
  115. };
  116. const title = !link && (
  117. <H1 fontSize={[25, 27, 32]} light>
  118. Kutt your links{" "}
  119. <Span style={{ borderBottom: "2px dotted #999" }} light>
  120. shorter
  121. </Span>
  122. .
  123. </H1>
  124. );
  125. const result = link && (
  126. <Animation
  127. as={RowCenter}
  128. offset="-20px"
  129. duration="0.4s"
  130. style={{ position: "relative" }}
  131. >
  132. {copied ? (
  133. <Animation offset="10px" duration="0.2s" alignItems="center">
  134. <Icon
  135. size={[30, 35]}
  136. py={0}
  137. px={0}
  138. mr={3}
  139. p={["4px", "5px"]}
  140. name="check"
  141. strokeWidth="3"
  142. stroke={Colors.CheckIcon}
  143. />
  144. </Animation>
  145. ) : (
  146. <Animation offset="-10px" duration="0.2s">
  147. <CopyToClipboard text={link.link} onCopy={setCopied}>
  148. <Icon
  149. as="button"
  150. py={0}
  151. px={0}
  152. mr={3}
  153. size={[30, 35]}
  154. p={["6px", "7px"]}
  155. name="copy"
  156. strokeWidth="2.5"
  157. stroke={Colors.CopyIcon}
  158. backgroundColor={Colors.CopyIconBg}
  159. />
  160. </CopyToClipboard>
  161. </Animation>
  162. )}
  163. <CopyToClipboard text={link.link} onCopy={setCopied}>
  164. <ShortenedLink fontSize={[24, 26, 30]} pb="2px" light>
  165. {removeProtocol(link.link)}
  166. </ShortenedLink>
  167. </CopyToClipboard>
  168. </Animation>
  169. );
  170. return (
  171. <Col width={800} maxWidth="100%" px={[3]} flex="0 0 auto" mt={4}>
  172. <RowCenterH mb={[4, 48]}>
  173. {title}
  174. {result}
  175. </RowCenterH>
  176. <Flex
  177. as="form"
  178. id="shortenerform"
  179. width={1}
  180. alignItems="center"
  181. justifyContent="center"
  182. style={{ position: "relative" }}
  183. onSubmit={onSubmit}
  184. >
  185. <TextInput
  186. {...text("target")}
  187. placeholder="Paste your long URL"
  188. placeholderSize={[16, 17, 18]}
  189. fontSize={[18, 20, 22]}
  190. width={1}
  191. height={[58, 64, 72]}
  192. px={0}
  193. pr={[48, 84]}
  194. pl={[32, 40]}
  195. autoFocus
  196. data-lpignore
  197. />
  198. <SubmitIconWrapper onClick={onSubmit}>
  199. <Icon
  200. name={loading ? "spinner" : "send"}
  201. size={[22, 26, 28]}
  202. fill={loading ? "none" : "#aaa"}
  203. stroke={loading ? Colors.Spinner : "none"}
  204. mb={1}
  205. mr={1}
  206. />
  207. </SubmitIconWrapper>
  208. </Flex>
  209. {message.text && (
  210. <Text color={message.color} mt={24} mb={1} textAlign="center">
  211. {message.text}
  212. </Text>
  213. )}
  214. <Checkbox
  215. {...raw({
  216. name: "showAdvanced",
  217. onChange: e => {
  218. if (!isAuthenticated) {
  219. setMessage(
  220. "You need to log in or sign up to use advanced options."
  221. );
  222. return false;
  223. }
  224. return !formState.values.showAdvanced;
  225. }
  226. })}
  227. checked={formState.values.showAdvanced}
  228. label="Show advanced options"
  229. mt={[3, 24]}
  230. alignSelf="flex-start"
  231. />
  232. {formState.values.showAdvanced && (
  233. <div>
  234. <Flex mt={4} flexDirection={["column", "row"]}>
  235. <Col mb={[3, 0]}>
  236. <Text
  237. as="label"
  238. {...label("domain")}
  239. fontSize={[14, 15]}
  240. mb={2}
  241. bold
  242. >
  243. Domain
  244. </Text>
  245. <Select
  246. {...select("domain")}
  247. data-lpignore
  248. pl={[3, 24]}
  249. pr={[3, 24]}
  250. fontSize={[14, 15]}
  251. height={[40, 44]}
  252. width={[1, 210, 240]}
  253. options={[
  254. { key: defaultDomain, value: "" },
  255. ...domains.map(d => ({
  256. key: d.address,
  257. value: d.address
  258. }))
  259. ]}
  260. />
  261. </Col>
  262. <Col mb={[3, 0]} ml={[0, 24]}>
  263. <Text
  264. as="label"
  265. {...label("customurl")}
  266. fontSize={[14, 15]}
  267. mb={2}
  268. bold
  269. >
  270. {formState.values.domain || defaultDomain}/
  271. </Text>
  272. <TextInput
  273. {...text("customurl")}
  274. placeholder="Custom address..."
  275. autocomplete="off"
  276. data-lpignore
  277. pl={[3, 24]}
  278. pr={[3, 24]}
  279. placeholderSize={[13, 14]}
  280. fontSize={[14, 15]}
  281. height={[40, 44]}
  282. width={[1, 210, 240]}
  283. />
  284. </Col>
  285. <Col ml={[0, 24]}>
  286. <Text
  287. as="label"
  288. {...label("password")}
  289. fontSize={[14, 15]}
  290. mb={2}
  291. bold
  292. >
  293. Password:
  294. </Text>
  295. <TextInput
  296. {...password("password")}
  297. placeholder="Password..."
  298. autocomplete="off"
  299. data-lpignore
  300. pl={[3, 24]}
  301. pr={[3, 24]}
  302. placeholderSize={[13, 14]}
  303. fontSize={[14, 15]}
  304. height={[40, 44]}
  305. width={[1, 210, 240]}
  306. />
  307. </Col>
  308. </Flex>
  309. <Flex mt={[3]} flexDirection={["column", "row"]}>
  310. <Col width={1}>
  311. <Text
  312. as="description"
  313. {...label("description")}
  314. fontSize={[14, 15]}
  315. mb={2}
  316. bold
  317. >
  318. Description
  319. </Text>
  320. <TextInput
  321. {...text("description")}
  322. placeholder="Description"
  323. data-lpignore
  324. pl={[3, 24]}
  325. pr={[3, 24]}
  326. placeholderSize={[13, 14]}
  327. fontSize={[14, 15]}
  328. height={[40, 44]}
  329. width={1}
  330. maxWidth="100%"
  331. />
  332. </Col>
  333. </Flex>
  334. </div>
  335. )}
  336. </Col>
  337. );
  338. };
  339. export default Shortener;