SettingsApi.tsx 3.1 KB

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