Shortener.tsx 7.4 KB

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