1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-15 01:16:22 +02:00

feat: copy to clipboard sdk snippet (#8083)

This commit is contained in:
Mateusz Kwasniewski 2024-09-05 08:53:52 +02:00 committed by GitHub
parent bcf7f5682e
commit b1ce02369a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 6 deletions

View File

@ -132,6 +132,7 @@ export const ConnectSdkDialog = ({
</Button> </Button>
<Button <Button
variant='contained' variant='contained'
disabled={!apiKey}
onClick={() => { onClick={() => {
setStage('test-connection'); setStage('test-connection');
}} }}

View File

@ -126,7 +126,7 @@ export const SelectSdk: FC<ISelectSdkProps> = ({ onSelect }) => {
</SecondarySectionHeader> </SecondarySectionHeader>
<SdkListSection> <SdkListSection>
{clientSdks.map((sdk) => ( {clientSdks.map((sdk) => (
<SdkTile> <SdkTile key={sdk.name}>
<StyledAvatar src={formatAssetPath(sdk.icon)} /> <StyledAvatar src={formatAssetPath(sdk.icon)} />
<SdkTileContent> <SdkTileContent>
<b>{sdk.name}</b>{' '} <b>{sdk.name}</b>{' '}

View File

@ -1,9 +1,12 @@
import type { FC } from 'react'; import type { FC } from 'react';
import { Box, styled, Typography } from '@mui/material'; import { Box, IconButton, styled, Tooltip, Typography } from '@mui/material';
import { SectionHeader } from './SharedComponents'; import { SectionHeader } from './SharedComponents';
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig'; import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
import type { Sdk } from './sharedTypes'; import type { Sdk } from './sharedTypes';
import { codeSnippets, installCommands } from './sdkSnippets'; import { codeSnippets, installCommands } from './sdkSnippets';
import copy from 'copy-to-clipboard';
import useToast from 'hooks/useToast';
import CopyIcon from '@mui/icons-material/FileCopy';
const SpacedContainer = styled('div')(({ theme }) => ({ const SpacedContainer = styled('div')(({ theme }) => ({
padding: theme.spacing(5, 8, 8, 8), padding: theme.spacing(5, 8, 8, 8),
@ -20,6 +23,13 @@ const StyledCodeBlock = styled('pre')(({ theme }) => ({
fontSize: theme.typography.body2.fontSize, fontSize: theme.typography.body2.fontSize,
wordBreak: 'break-all', wordBreak: 'break-all',
whiteSpace: 'pre-wrap', whiteSpace: 'pre-wrap',
position: 'relative',
}));
const CopyToClipboard = styled(Tooltip)(({ theme }) => ({
position: 'absolute',
top: theme.spacing(1),
right: theme.spacing(1),
})); }));
interface ITestSdkConnectionProps { interface ITestSdkConnectionProps {
@ -31,6 +41,7 @@ export const TestSdkConnection: FC<ITestSdkConnectionProps> = ({
apiKey, apiKey,
}) => { }) => {
const { uiConfig } = useUiConfig(); const { uiConfig } = useUiConfig();
const { setToastData } = useToast();
const clientApiUrl = `${uiConfig.unleashUrl}/api/`; const clientApiUrl = `${uiConfig.unleashUrl}/api/`;
const frontendApiUrl = `${uiConfig.unleashUrl}/api/frontend/`; const frontendApiUrl = `${uiConfig.unleashUrl}/api/frontend/`;
@ -40,6 +51,17 @@ export const TestSdkConnection: FC<ITestSdkConnectionProps> = ({
const installCommand = const installCommand =
installCommands[sdk.name] || installCommands[sdk.name] ||
`No install command found for the ${sdk.name} SDK`; `No install command found for the ${sdk.name} SDK`;
const filledCodeSnippet = codeSnippet
.replace('<YOUR_API_TOKEN>', apiKey)
.replace('<YOUR_API_URL>', apiUrl);
const onCopyToClipboard = (data: string) => () => {
copy(data);
setToastData({
type: 'success',
title: 'Copied to clipboard',
});
};
return ( return (
<SpacedContainer> <SpacedContainer>
@ -47,12 +69,28 @@ export const TestSdkConnection: FC<ITestSdkConnectionProps> = ({
<Box sx={{ mt: 4 }}> <Box sx={{ mt: 4 }}>
<SectionHeader>Setup the SDK</SectionHeader> <SectionHeader>Setup the SDK</SectionHeader>
<p>1. Install the SDK</p> <p>1. Install the SDK</p>
<StyledCodeBlock>{installCommand}</StyledCodeBlock> <StyledCodeBlock>
{installCommand}
<CopyToClipboard title='Copy command' arrow>
<IconButton
onClick={onCopyToClipboard(installCommand)}
size='small'
>
<CopyIcon />
</IconButton>
</CopyToClipboard>
</StyledCodeBlock>
<p>2. Initialize the SDK</p> <p>2. Initialize the SDK</p>
<StyledCodeBlock> <StyledCodeBlock>
{codeSnippet {filledCodeSnippet}
.replace('<YOUR_API_TOKEN>', apiKey) <CopyToClipboard title='Copy snippet' arrow>
.replace('<YOUR_API_URL>', apiUrl)} <IconButton
onClick={onCopyToClipboard(filledCodeSnippet)}
size='small'
>
<CopyIcon />
</IconButton>
</CopyToClipboard>
</StyledCodeBlock> </StyledCodeBlock>
</Box> </Box>
</SpacedContainer> </SpacedContainer>