-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1215 from jay-hodgson/PORTALS-3241
- Loading branch information
Showing
15 changed files
with
198 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
apps/synapse-portal-framework/src/shared-config/SharePageLinkButtonConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { SynapseConfig } from '../types/portal-config' | ||
|
||
export const SharePageLinkButtonConfig: SynapseConfig = { | ||
name: 'SharePageLinkButton', | ||
props: { | ||
shortIoPublicApiKey: 'pk_y4sPMLrxonM7kNQV', | ||
buttonProps: { | ||
variant: 'text', | ||
color: 'light', | ||
sx: { | ||
position: 'absolute', | ||
top: '50px', | ||
right: '20px', | ||
zIndex: 100, | ||
}, | ||
}, | ||
}, | ||
containerClassName: 'container-full-width', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...s/synapse-react-client/src/components/SharePageLinkButton/SharePageLinkButton.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Meta, StoryObj } from '@storybook/react' | ||
import SharePageLinkButton from './SharePageLinkButton' | ||
|
||
const meta = { | ||
title: 'UI/SharePageLinkButton', | ||
component: SharePageLinkButton, | ||
} satisfies Meta | ||
export default meta | ||
type Story = StoryObj<typeof meta> | ||
|
||
export const SharePageLinkButtonStory: Story = { | ||
args: { | ||
buttonProps: { | ||
sx: { position: 'fixed', right: '20px' }, | ||
}, | ||
}, | ||
parameters: { | ||
stack: 'mock', | ||
msw: { | ||
handlers: [], | ||
}, | ||
}, | ||
} |
41 changes: 41 additions & 0 deletions
41
...ages/synapse-react-client/src/components/SharePageLinkButton/SharePageLinkButton.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
import React from 'react' | ||
import { createWrapper } from '../../testutils/TestingLibraryUtils' | ||
import SharePageLinkButton, { | ||
SharePageLinkButtonProps, | ||
} from './SharePageLinkButton' | ||
import { MOCK_SHORT_IO_URL } from '../../mocks/mockShortIo' | ||
import { server } from '../../mocks/msw/server' | ||
|
||
function renderComponent(props: SharePageLinkButtonProps) { | ||
return render(<SharePageLinkButton {...props} />, { | ||
wrapper: createWrapper(), | ||
}) | ||
} | ||
describe('SharePageLinkButton', () => { | ||
beforeAll(() => server.listen()) | ||
afterEach(() => server.restoreHandlers()) | ||
afterAll(() => server.close()) | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
// Replace clipboard.writeText with a mock | ||
Object.assign(navigator, { | ||
clipboard: { | ||
writeText: jest.fn().mockImplementation(() => Promise.resolve()), | ||
}, | ||
}) | ||
}) | ||
|
||
it('Copies short.io response to clipboard', async () => { | ||
renderComponent({ shortIoPublicApiKey: 'abc' }) | ||
expect(screen.queryByRole('alert')).not.toBeInTheDocument() | ||
|
||
await userEvent.click( | ||
screen.getByRole('button', { name: 'Share Page Link' }), | ||
) | ||
expect(navigator.clipboard.writeText).toHaveBeenCalledWith( | ||
MOCK_SHORT_IO_URL, | ||
) | ||
}) | ||
}) |
70 changes: 70 additions & 0 deletions
70
packages/synapse-react-client/src/components/SharePageLinkButton/SharePageLinkButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React, { useCallback } from 'react' | ||
import { useMutation } from '@tanstack/react-query' | ||
import { displayToast } from '../ToastMessage' | ||
import IconSvg from '../IconSvg' | ||
import { Button, ButtonProps } from '@mui/material' | ||
|
||
export type SharePageLinkButtonProps = { | ||
shortIoPublicApiKey?: string | ||
domain?: string | ||
buttonProps?: ButtonProps | ||
} | ||
|
||
export const SharePageLinkButton: React.FunctionComponent< | ||
SharePageLinkButtonProps | ||
> = ({ shortIoPublicApiKey, domain = 'sageb.io', buttonProps }) => { | ||
const copyToClipboard = useCallback((value: string) => { | ||
navigator.clipboard.writeText(value).then(() => { | ||
displayToast('Page URL copied to the clipboard', 'success') | ||
}) | ||
}, []) | ||
// create short io link (if not already created) | ||
const { mutate: createShortUrl } = useMutation({ | ||
mutationFn: async () => { | ||
if (!shortIoPublicApiKey) { | ||
return window.location.href | ||
} else { | ||
const response = await fetch('https://api.short.io/links/public', { | ||
method: 'POST', | ||
headers: { | ||
Authorization: shortIoPublicApiKey, | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
originalURL: window.location.href, | ||
domain: domain, | ||
}), | ||
}) | ||
if (!response.ok) { | ||
const responseText = await response.text() | ||
throw new Error(responseText) | ||
} | ||
const jsonResponse = await response.json() | ||
return jsonResponse.shortURL | ||
} | ||
}, | ||
onSuccess: data => { | ||
copyToClipboard(data) | ||
}, | ||
onError: error => { | ||
console.error(error) | ||
copyToClipboard(window.location.href) | ||
}, | ||
}) | ||
|
||
return ( | ||
<Button | ||
variant="contained" | ||
onClick={() => { | ||
createShortUrl() | ||
}} | ||
{...buttonProps} | ||
startIcon={<IconSvg icon="contentCopy" wrap={false} />} | ||
> | ||
Share Page Link | ||
</Button> | ||
) | ||
} | ||
|
||
export default SharePageLinkButton |
4 changes: 4 additions & 0 deletions
4
packages/synapse-react-client/src/components/SharePageLinkButton/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import SharePageLinkButton from './SharePageLinkButton' | ||
import type { SharePageLinkButtonProps } from './SharePageLinkButton' | ||
export { SharePageLinkButton, SharePageLinkButtonProps } | ||
export default SharePageLinkButton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const MOCK_SHORT_IO_URL = 'https://short.io/abc123' | ||
export const mockShortIoResponse: any = ( | ||
originalURL: string, | ||
domain: string, | ||
) => { | ||
return { | ||
id: '123456', | ||
originalURL, | ||
shortURL: MOCK_SHORT_IO_URL, | ||
domain, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/synapse-react-client/src/mocks/msw/handlers/shortIoHandlers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { rest } from 'msw' | ||
import { mockShortIoResponse } from '../../../mocks/mockShortIo' | ||
|
||
export const getShortIoHandlers = () => [ | ||
rest.post('https://api.short.io/links/public', async (req, res, ctx) => { | ||
const body = await req.json() | ||
return res( | ||
ctx.status(200), | ||
ctx.json(mockShortIoResponse(body.originalURL, body.domain)), | ||
) | ||
}), | ||
] |