SettingsApi.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { CopyToClipboard } from "react-copy-to-clipboard";
  2. import { Flex } from "reflexbox/styled-components";
  3. import React, { FC, useState } from "react";
  4. import styled from "styled-components";
  5. import { useStoreState, useStoreActions } from "../../store";
  6. import { useCopy, useMessage } from "../../hooks";
  7. import { errorMessage } from "../../utils";
  8. import { Colors } from "../../consts";
  9. import Animation from "../Animation";
  10. import { Button } from "../Button";
  11. import Text, { H2 } from "../Text";
  12. import { Col } from "../Layout";
  13. import ALink from "../ALink";
  14. import Icon from "../Icon";
  15. const ApiKey = styled(Text).attrs({
  16. mt: [0, "2px"],
  17. fontSize: [15, 16],
  18. bold: true
  19. })`
  20. border-bottom: 1px dotted ${Colors.StatsTotalUnderline};
  21. cursor: pointer;
  22. word-break: break-word;
  23. :hover {
  24. opacity: 0.8;
  25. }
  26. `;
  27. const SettingsApi: FC = () => {
  28. const [copied, setCopied] = useCopy();
  29. const [message, setMessage] = useMessage(1500);
  30. const [loading, setLoading] = useState(false);
  31. const apikey = useStoreState(s => s.settings.apikey);
  32. const generateApiKey = useStoreActions(s => s.settings.generateApiKey);
  33. const onSubmit = async () => {
  34. if (loading) return;
  35. setLoading(true);
  36. await generateApiKey().catch(err => setMessage(errorMessage(err)));
  37. setLoading(false);
  38. };
  39. return (
  40. <Col alignItems="flex-start" maxWidth="100%">
  41. <H2 mb={4} bold>
  42. API
  43. </H2>
  44. <Text mb={4}>
  45. In additional to this website, you can use the API to create, delete and
  46. get shortend URLs. If
  47. {" you're"} not familiar with API, {"don't"} generate the key. DO NOT
  48. share this key on the client side of your website.{" "}
  49. <ALink
  50. href="https://github.com/thedevs-network/kutt#api"
  51. title="API Docs"
  52. target="_blank"
  53. >
  54. Read API docs.
  55. </ALink>
  56. </Text>
  57. {apikey && (
  58. <Flex alignItems={["flex-start", "center"]} my={3}>
  59. {copied ? (
  60. <Animation offset="10px" duration="0.2s">
  61. <Icon
  62. size={[23, 24]}
  63. py={0}
  64. px={0}
  65. mr={2}
  66. p="3px"
  67. name="check"
  68. strokeWidth="3"
  69. stroke={Colors.CheckIcon}
  70. />
  71. </Animation>
  72. ) : (
  73. <Animation offset="-10px" duration="0.2s">
  74. <CopyToClipboard text={apikey} onCopy={setCopied}>
  75. <Icon
  76. as="button"
  77. py={0}
  78. px={0}
  79. mr={2}
  80. size={[23, 24]}
  81. p={["4px", "5px"]}
  82. name="copy"
  83. strokeWidth="2.5"
  84. stroke={Colors.CopyIcon}
  85. backgroundColor={Colors.CopyIconBg}
  86. />
  87. </CopyToClipboard>
  88. </Animation>
  89. )}
  90. <CopyToClipboard text={apikey} onCopy={setCopied}>
  91. <ApiKey>{apikey}</ApiKey>
  92. </CopyToClipboard>
  93. </Flex>
  94. )}
  95. <Button mt={3} color="purple" onClick={onSubmit} disabled={loading}>
  96. <Icon name={loading ? "spinner" : "zap"} mr={2} stroke="white" />
  97. {loading ? "Generating..." : apikey ? "Regenerate" : "Generate"} key
  98. </Button>
  99. <Text fontSize={15} mt={3} color={message.color}>
  100. {message.text}
  101. </Text>
  102. </Col>
  103. );
  104. };
  105. export default SettingsApi;