SettingsApi.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. const ApiKey = styled(Text).attrs({
  12. mr: 3,
  13. fontSize: [14, 16],
  14. fontWeight: 700
  15. })`
  16. max-width: 100%;
  17. border-bottom: 2px dotted #999;
  18. `;
  19. const SettingsApi: FC = () => {
  20. const [copied, setCopied] = useState(false);
  21. const [loading, setLoading] = useState(false);
  22. const apikey = useStoreState(s => s.settings.apikey);
  23. const generateApiKey = useStoreActions(s => s.settings.generateApiKey);
  24. const onCopy = () => {
  25. setCopied(true);
  26. setTimeout(() => {
  27. setCopied(false);
  28. }, 1500);
  29. };
  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. <Col style={{ position: "relative" }} my={3}>
  56. {copied && (
  57. <Text
  58. color="green"
  59. fontSize={14}
  60. style={{ position: "absolute", top: -24 }}
  61. >
  62. Copied to clipboard.
  63. </Text>
  64. )}
  65. <Flex
  66. maxWidth="100%"
  67. flexDirection={["column", "column", "row"]}
  68. flexWrap="wrap"
  69. alignItems={["flex-start", "flex-start", "center"]}
  70. mb={16}
  71. >
  72. <ApiKey>{apikey}</ApiKey>
  73. <CopyToClipboard text={apikey} onCopy={onCopy}>
  74. <Button icon="copy" height={36} mt={[3, 3, 0]}>
  75. Copy
  76. </Button>
  77. </CopyToClipboard>
  78. </Flex>
  79. </Col>
  80. )}
  81. <Button color="purple" onClick={onSubmit} disabled={loading}>
  82. <Icon name={loading ? "spinner" : "zap"} mr={2} stroke="white" />
  83. {loading ? "Generating..." : apikey ? "Regenerate" : "Generate"} key
  84. </Button>
  85. </Col>
  86. );
  87. };
  88. export default SettingsApi;