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 from "../Text";
  10. const ApiKey = styled(Text).attrs({
  11. mr: 3,
  12. fontSize: [14, 16],
  13. fontWeight: 700
  14. })`
  15. max-width: 100%;
  16. border-bottom: 2px dotted #999;
  17. `;
  18. const SettingsApi: FC = () => {
  19. const [copied, setCopied] = useState(false);
  20. const [loading, setLoading] = useState(false);
  21. const apikey = useStoreState(s => s.settings.apikey);
  22. const generateApiKey = useStoreActions(s => s.settings.generateApiKey);
  23. const onCopy = () => {
  24. setCopied(true);
  25. setTimeout(() => {
  26. setCopied(false);
  27. }, 1500);
  28. };
  29. const onSubmit = async () => {
  30. if (loading) return;
  31. setLoading(true);
  32. await generateApiKey();
  33. setLoading(false);
  34. };
  35. return (
  36. <Flex flexDirection="column" alignItems="flex-start">
  37. <Text as="h2" fontWeight={700} mb={4}>
  38. API
  39. </Text>
  40. <Text as="p" mb={4}>
  41. In additional to this website, you can use the API to create, delete and
  42. get shortend URLs. If
  43. {" you're"} not familiar with API, {"don't"} generate the key. DO NOT
  44. share this key on the client side of your website.{" "}
  45. <ALink
  46. href="https://github.com/thedevs-network/kutt#api"
  47. title="API Docs"
  48. target="_blank"
  49. >
  50. Read API docs.
  51. </ALink>
  52. </Text>
  53. {apikey && (
  54. <Flex flexDirection="column" style={{ position: "relative" }} my={3}>
  55. {copied && (
  56. <Text
  57. as="p"
  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. </Flex>
  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. </Flex>
  86. );
  87. };
  88. export default SettingsApi;