Shortener.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 [qrModal, setQRModal] = useState(false);
  58. const [copied, setCopied] = useState(false);
  59. const [formState, { raw, password, text, label }] = useFormState<Form>(null, {
  60. withIds: true,
  61. onChange(e, stateValues, nextStateValues) {
  62. if (stateValues.showAdvanced && !nextStateValues.showAdvanced) {
  63. formState.clear();
  64. formState.setField("target", stateValues.target);
  65. }
  66. }
  67. });
  68. const onSubmit = async e => {
  69. e.preventDefault();
  70. if (loading) return;
  71. setCopied(false);
  72. setLoading(true);
  73. try {
  74. const link = await submit(formState.values);
  75. setLink(link);
  76. formState.clear();
  77. } catch (err) {
  78. setMessage(
  79. err?.response?.data?.error || "Couldn't create the short link."
  80. );
  81. }
  82. setLoading(false);
  83. };
  84. const title = !link && (
  85. <H1 light>
  86. Kutt your links{" "}
  87. <Span style={{ borderBottom: "2px dotted #999" }} light>
  88. shorter
  89. </Span>
  90. .
  91. </H1>
  92. );
  93. const onCopy = () => {
  94. setCopied(true);
  95. setTimeout(() => {
  96. setCopied(false);
  97. }, 1500);
  98. };
  99. const result = link && (
  100. <Animation
  101. as={RowCenter}
  102. offset="-20px"
  103. duration="0.4s"
  104. style={{ position: "relative" }}
  105. >
  106. {copied ? (
  107. <Animation offset="10px" duration="0.2s" alignItems="center">
  108. <Icon
  109. size={[35]}
  110. py={0}
  111. px={0}
  112. mr={3}
  113. p="5px"
  114. name="check"
  115. strokeWidth="3"
  116. stroke={Colors.CheckIcon}
  117. />
  118. </Animation>
  119. ) : (
  120. <Animation offset="-10px" duration="0.2s">
  121. <CopyToClipboard text={link.shortLink} onCopy={onCopy}>
  122. <Icon
  123. as="button"
  124. py={0}
  125. px={0}
  126. mr={3}
  127. size={[35]}
  128. p={["7px"]}
  129. name="copy"
  130. strokeWidth="2.5"
  131. stroke={Colors.CopyIcon}
  132. backgroundColor={Colors.CopyIconBg}
  133. />
  134. </CopyToClipboard>
  135. </Animation>
  136. )}
  137. <CopyToClipboard text={link.shortLink} onCopy={onCopy}>
  138. <ShortenedLink fontSize={[30]} pb="2px" light>
  139. {removeProtocol(link.shortLink)}
  140. </ShortenedLink>
  141. </CopyToClipboard>
  142. </Animation>
  143. );
  144. return (
  145. <Col width={800} maxWidth="98%" flex="0 0 auto" mt={4}>
  146. <RowCenterH mb={40}>
  147. {title}
  148. {result}
  149. </RowCenterH>
  150. <Flex
  151. as="form"
  152. id="shortenerform"
  153. width={800}
  154. maxWidth="100%"
  155. alignItems="center"
  156. justifyContent="center"
  157. style={{ position: "relative" }}
  158. onSubmit={onSubmit}
  159. >
  160. <TextInput
  161. {...text("target")}
  162. placeholder="Paste your long URL"
  163. placeholderSize={[16, 18]}
  164. fontSize={[20, 22]}
  165. width={1}
  166. height={[72]}
  167. autoFocus
  168. data-lpignore
  169. />
  170. <SubmitIconWrapper onClick={onSubmit}>
  171. <Icon
  172. name={loading ? "spinner" : "send"}
  173. size={28}
  174. fill={loading ? "none" : "#aaa"}
  175. stroke={loading ? Colors.Spinner : "none"}
  176. mb={1}
  177. mr={1}
  178. />
  179. </SubmitIconWrapper>
  180. </Flex>
  181. {message.text && (
  182. <Text color={message.color} mt={24} mb={1} textAlign="center">
  183. {message.text}
  184. </Text>
  185. )}
  186. <Checkbox
  187. {...raw({
  188. name: "showAdvanced",
  189. onChange: e => {
  190. if (!isAuthenticated) {
  191. setMessage(
  192. "You need to log in or sign up to use advanced options."
  193. );
  194. return false;
  195. }
  196. return !formState.values.showAdvanced;
  197. }
  198. })}
  199. checked={formState.values.showAdvanced}
  200. label="Show advanced options"
  201. mt={24}
  202. alignSelf="flex-start"
  203. />
  204. {formState.values.showAdvanced && (
  205. <Flex mt={4}>
  206. <Col>
  207. <Text as="label" {...label("customurl")} fontSize={15} mb={2} bold>
  208. {(domain || {}).customDomain ||
  209. (typeof window !== "undefined" && window.location.hostname)}
  210. /
  211. </Text>
  212. <TextInput
  213. {...text("customurl")}
  214. placeholder="Custom address"
  215. data-lpignore
  216. pl={24}
  217. pr={24}
  218. placeholderSize={[13, 14, 14, 14]}
  219. fontSize={[14, 15]}
  220. height={44}
  221. width={240}
  222. />
  223. </Col>
  224. <Col ml={4}>
  225. <Text as="label" {...label("password")} fontSize={15} mb={2} bold>
  226. Password:
  227. </Text>
  228. <TextInput
  229. {...password("password")}
  230. placeholder="Password"
  231. data-lpignore
  232. pl={24}
  233. pr={24}
  234. placeholderSize={[13, 14, 14, 14]}
  235. fontSize={[14, 15]}
  236. height={44}
  237. width={240}
  238. />
  239. </Col>
  240. </Flex>
  241. )}
  242. </Col>
  243. );
  244. };
  245. export default Shortener;