SettingsApi.tsx 3.1 KB

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