SettingsApi.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 href="https://docs.kutt.it" title="API Docs" target="_blank">
  50. Read API docs.
  51. </ALink>
  52. </Text>
  53. {apikey && (
  54. <Flex alignItems={["flex-start", "center"]} my={3}>
  55. {copied ? (
  56. <Animation offset="10px" duration="0.2s">
  57. <Icon
  58. size={[23, 24]}
  59. py={0}
  60. px={0}
  61. mr={2}
  62. p="3px"
  63. name="check"
  64. strokeWidth="3"
  65. stroke={Colors.CheckIcon}
  66. />
  67. </Animation>
  68. ) : (
  69. <Animation offset="-10px" duration="0.2s">
  70. <CopyToClipboard text={apikey} onCopy={setCopied}>
  71. <Icon
  72. as="button"
  73. py={0}
  74. px={0}
  75. mr={2}
  76. size={[23, 24]}
  77. p={["4px", "5px"]}
  78. name="copy"
  79. strokeWidth="2.5"
  80. stroke={Colors.CopyIcon}
  81. backgroundColor={Colors.CopyIconBg}
  82. />
  83. </CopyToClipboard>
  84. </Animation>
  85. )}
  86. <CopyToClipboard text={apikey} onCopy={setCopied}>
  87. <ApiKey>{apikey}</ApiKey>
  88. </CopyToClipboard>
  89. </Flex>
  90. )}
  91. <Button mt={3} color="purple" onClick={onSubmit} disabled={loading}>
  92. <Icon name={loading ? "spinner" : "zap"} mr={2} stroke="white" />
  93. {loading ? "Generating..." : apikey ? "Regenerate" : "Generate"} key
  94. </Button>
  95. <Text fontSize={15} mt={3} color={message.color}>
  96. {message.text}
  97. </Text>
  98. </Col>
  99. );
  100. };
  101. export default SettingsApi;