Shortener.tsx 10 KB

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