Skip to content

Commit be151b4

Browse files
David-DB88Haaroleanworkshur
authored
[FE] Add a warning when copying to clipboard in non-SSL envs (#3394)
* set a warning message on copyToClipboard if user use http * Update kafka-ui-react-app/src/lib/hooks/useDataSaver.ts * changed custom type whit warning * added warning title * added test case for warning message --------- Co-authored-by: davitbejanyan <[email protected]> Co-authored-by: Roman Zabaluev <[email protected]> Co-authored-by: Oleg Shur <[email protected]>
1 parent 8889463 commit be151b4

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

kafka-ui-react-app/src/components/common/Alert/Alert.styled.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ToastType } from 'react-hot-toast';
21
import styled from 'styled-components';
2+
import { ToastTypes } from 'lib/errorHandling';
33

4-
export const Alert = styled.div<{ $type: ToastType }>`
4+
export const Alert = styled.div<{ $type: ToastTypes }>`
55
background-color: ${({ $type, theme }) => theme.alert.color[$type]};
66
width: 500px;
77
min-height: 64px;

kafka-ui-react-app/src/components/common/Alert/Alert.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React from 'react';
22
import CloseIcon from 'components/common/Icons/CloseIcon';
33
import IconButtonWrapper from 'components/common/Icons/IconButtonWrapper';
4-
import { ToastType } from 'react-hot-toast';
4+
import { ToastTypes } from 'lib/errorHandling';
55

66
import * as S from './Alert.styled';
77

88
export interface AlertProps {
99
title: string;
10-
type: ToastType;
10+
type: ToastTypes;
1111
message: React.ReactNode;
1212
onDissmiss(): void;
1313
}

kafka-ui-react-app/src/lib/errorHandling.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface ServerResponse {
99
url?: string;
1010
message?: ErrorResponse['message'];
1111
}
12+
export type ToastTypes = ToastType | 'warning';
1213

1314
export const getResponse = async (
1415
response: Response
@@ -34,7 +35,7 @@ interface AlertOptions {
3435
}
3536

3637
export const showAlert = (
37-
type: ToastType,
38+
type: ToastTypes,
3839
{ title, message, id }: AlertOptions
3940
) => {
4041
toast.custom(

kafka-ui-react-app/src/lib/hooks/__tests__/useDataSaver.spec.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import React, { useEffect } from 'react';
22
import useDataSaver from 'lib/hooks/useDataSaver';
33
import { render } from '@testing-library/react';
4+
import { showAlert } from 'lib/errorHandling';
45

6+
jest.mock('lib/errorHandling', () => ({
7+
...jest.requireActual('lib/errorHandling'),
8+
showAlert: jest.fn(),
9+
}));
510
describe('useDataSaver hook', () => {
611
const content = {
712
title: 'title',
@@ -38,7 +43,6 @@ describe('useDataSaver hook', () => {
3843
mockCreate.mockRestore();
3944
});
4045
});
41-
4246
describe('copies the data to the clipboard', () => {
4347
Object.assign(navigator, {
4448
clipboard: {
@@ -74,4 +78,29 @@ describe('useDataSaver hook', () => {
7478
);
7579
});
7680
});
81+
describe('navigator clipboard is undefined', () => {
82+
it('calls showAlert with the correct parameters when clipboard API is unavailable', () => {
83+
Object.assign(navigator, {
84+
clipboard: undefined,
85+
});
86+
87+
const HookWrapper: React.FC = () => {
88+
const { copyToClipboard } = useDataSaver('topic', content);
89+
useEffect(() => {
90+
copyToClipboard();
91+
}, [copyToClipboard]);
92+
return null;
93+
};
94+
95+
render(<HookWrapper />);
96+
97+
expect(showAlert).toHaveBeenCalledTimes(1);
98+
expect(showAlert).toHaveBeenCalledWith('warning', {
99+
id: 'topic',
100+
title: 'Warning',
101+
message:
102+
'Copying to clipboard is unavailable due to unsecured (non-HTTPS) connection',
103+
});
104+
});
105+
});
77106
});

kafka-ui-react-app/src/lib/hooks/useDataSaver.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { showSuccessAlert } from 'lib/errorHandling';
1+
import { showAlert, showSuccessAlert } from 'lib/errorHandling';
22

33
const useDataSaver = (
44
subject: string,
@@ -14,6 +14,13 @@ const useDataSaver = (
1414
title: '',
1515
message: 'Copied successfully!',
1616
});
17+
} else {
18+
showAlert('warning', {
19+
id: subject,
20+
title: 'Warning',
21+
message:
22+
'Copying to clipboard is unavailable due to unsecured (non-HTTPS) connection',
23+
});
1724
}
1825
};
1926
const saveFile = () => {

0 commit comments

Comments
 (0)