SettingsApi.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React, { FC, useState } from "react";
  2. import styled from "styled-components";
  3. import { CopyToClipboard } from "react-copy-to-clipboard";
  4. import { Flex } from "reflexbox/styled-components";
  5. import Button from "../Button";
  6. import Text from "../Text";
  7. import ALink from "../ALink";
  8. import { useStoreState, useStoreActions } from "../../store";
  9. const ApiKey = styled(Text).attrs({
  10. mr: 3,
  11. fontSize: [14, 16],
  12. fontWeight: 700
  13. })`
  14. max-width: 100%;
  15. border-bottom: 2px dotted #999;
  16. `;
  17. const SettingsApi: FC = () => {
  18. const [copied, setCopied] = useState(false);
  19. const [loading, setLoading] = useState(false);
  20. const apikey = useStoreState(s => s.settings.apikey);
  21. const generateApiKey = useStoreActions(s => s.settings.generateApiKey);
  22. const onCopy = () => {
  23. setCopied(true);
  24. setTimeout(() => {
  25. setCopied(false);
  26. }, 1500);
  27. };
  28. const onSubmit = async () => {
  29. setLoading(true);
  30. await generateApiKey();
  31. setLoading(false);
  32. };
  33. return (
  34. <Flex flexDirection="column" alignItems="flex-start">
  35. <Text as="h2" fontWeight={700} mb={4}>
  36. API
  37. </Text>
  38. <Text as="p" mb={4}>
  39. In additional to this website, you can use the API to create, delete and
  40. get shortend URLs. If
  41. {" you're"} not familiar with API, {"don't"} generate the key. DO NOT
  42. share this key on the client side of your website.{" "}
  43. <ALink
  44. href="https://github.com/thedevs-network/kutt#api"
  45. title="API Docs"
  46. target="_blank"
  47. >
  48. Read API docs.
  49. </ALink>
  50. </Text>
  51. {apikey && (
  52. <Flex flexDirection="column" style={{ position: "relative" }} my={3}>
  53. {copied && (
  54. <Text
  55. as="p"
  56. color="green"
  57. fontSize={14}
  58. style={{ position: "absolute", top: -24 }}
  59. >
  60. Copied to clipboard.
  61. </Text>
  62. )}
  63. <Flex
  64. maxWidth="100%"
  65. flexDirection={["column", "column", "row"]}
  66. flexWrap="wrap"
  67. alignItems={["flex-start", "flex-start", "center"]}
  68. mb={16}
  69. >
  70. <ApiKey>{apikey}</ApiKey>
  71. <CopyToClipboard text={apikey} onCopy={onCopy}>
  72. <Button icon="copy" height={36} mt={[3, 3, 0]}>
  73. Copy
  74. </Button>
  75. </CopyToClipboard>
  76. </Flex>
  77. </Flex>
  78. )}
  79. <Button
  80. color="purple"
  81. icon={loading ? "loader" : "zap"}
  82. onClick={onSubmit}
  83. >
  84. {loading ? "Generating..." : apikey ? "Regenerate" : "Generate"} key
  85. </Button>
  86. </Flex>
  87. );
  88. };
  89. export default SettingsApi;