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 "rebass/styled-components";
  4. import React, { 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 }] =
  64. useFormState<Form>(
  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. aria-label="target"
  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} role="button" aria-label="submit">
  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: () => {
  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 mb={[3, 0]}>
  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={[1, 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;