Skip to content

Commit

Permalink
feat: copy to clipboard sdk snippet (#8083)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored Sep 5, 2024
1 parent bcf7f56 commit b1ce023
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions frontend/src/component/onboarding/ConnectSdkDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export const ConnectSdkDialog = ({
</Button>
<Button
variant='contained'
disabled={!apiKey}
onClick={() => {
setStage('test-connection');
}}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/component/onboarding/SelectSdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const SelectSdk: FC<ISelectSdkProps> = ({ onSelect }) => {
</SecondarySectionHeader>
<SdkListSection>
{clientSdks.map((sdk) => (
<SdkTile>
<SdkTile key={sdk.name}>
<StyledAvatar src={formatAssetPath(sdk.icon)} />
<SdkTileContent>
<b>{sdk.name}</b>{' '}
Expand Down
48 changes: 43 additions & 5 deletions frontend/src/component/onboarding/TestSdkConnection.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
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 useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
import type { Sdk } from './sharedTypes';
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 }) => ({
padding: theme.spacing(5, 8, 8, 8),
Expand All @@ -20,6 +23,13 @@ const StyledCodeBlock = styled('pre')(({ theme }) => ({
fontSize: theme.typography.body2.fontSize,
wordBreak: 'break-all',
whiteSpace: 'pre-wrap',
position: 'relative',
}));

const CopyToClipboard = styled(Tooltip)(({ theme }) => ({
position: 'absolute',
top: theme.spacing(1),
right: theme.spacing(1),
}));

interface ITestSdkConnectionProps {
Expand All @@ -31,6 +41,7 @@ export const TestSdkConnection: FC<ITestSdkConnectionProps> = ({
apiKey,
}) => {
const { uiConfig } = useUiConfig();
const { setToastData } = useToast();

const clientApiUrl = `${uiConfig.unleashUrl}/api/`;
const frontendApiUrl = `${uiConfig.unleashUrl}/api/frontend/`;
Expand All @@ -40,19 +51,46 @@ export const TestSdkConnection: FC<ITestSdkConnectionProps> = ({
const installCommand =
installCommands[sdk.name] ||
`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 (
<SpacedContainer>
<Typography variant='h2'>Connect an SDK to Unleash</Typography>
<Box sx={{ mt: 4 }}>
<SectionHeader>Setup the SDK</SectionHeader>
<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>
<StyledCodeBlock>
{codeSnippet
.replace('<YOUR_API_TOKEN>', apiKey)
.replace('<YOUR_API_URL>', apiUrl)}
{filledCodeSnippet}
<CopyToClipboard title='Copy snippet' arrow>
<IconButton
onClick={onCopyToClipboard(filledCodeSnippet)}
size='small'
>
<CopyIcon />
</IconButton>
</CopyToClipboard>
</StyledCodeBlock>
</Box>
</SpacedContainer>
Expand Down

0 comments on commit b1ce023

Please sign in to comment.