Skip to content

Commit 3e328c1

Browse files
committed
fix: do not prompt clipboard access permission when the app first starts up
1 parent cb3cd01 commit 3e328c1

File tree

2 files changed

+10
-26
lines changed

2 files changed

+10
-26
lines changed

src/node-detail/primitive/components/TextCopyBox.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Chip } from '@nextui-org/chip';
2-
import { memo, useCallback, useEffect } from 'react';
2+
import { memo, useCallback } from 'react';
33
import { Text } from '../../../ui/components/Text';
44
import { isNull } from '../../../utils/json.util';
5-
import useCopyToClipboard from '../../../utils/react-hooks/useCopyToClipboard';
5+
import { useCopyToClipboard } from '../../../utils/react-hooks/useCopyToClipboard';
66
import { useHover } from '../../../utils/react-hooks/useHover';
77

88
type Props = {
@@ -11,18 +11,12 @@ type Props = {
1111

1212
const _TextCopyBox = ({ text }: Props) => {
1313
const [hostRef, isHostHovered] = useHover<HTMLDivElement>();
14-
const { copiedText, copyToClipboard, clearClipboard } = useCopyToClipboard();
14+
const { copiedText, copyToClipboard } = useCopyToClipboard();
1515

1616
const copyText = useCallback(() => {
1717
copyToClipboard(text);
1818
}, [copyToClipboard, text]);
1919

20-
useEffect(() => {
21-
if (!isHostHovered) {
22-
clearClipboard();
23-
}
24-
}, [clearClipboard, isHostHovered]);
25-
2620
return (
2721
<div
2822
ref={hostRef}

src/utils/react-hooks/useCopyToClipboard.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import { useState } from 'react';
44

55
type CopiedText = string | null;
66
type CopyFn = (text: string) => Promise<boolean>; // Return success
7-
type ClearFn = () => Promise<boolean>; // Return success
87

9-
const useCopyToClipboard = () => {
8+
export const useCopyToClipboard = () => {
109
const [copiedText, setCopiedText] = useState<CopiedText>(null);
1110

1211
const copyToClipboard: CopyFn = async (text) => {
12+
if (!navigator?.clipboard) {
13+
console.warn('Clipboard not supported');
14+
return false;
15+
}
16+
1317
// Try to save to clipboard then save it in the state if worked
1418
try {
1519
await navigator.clipboard.writeText(text);
@@ -22,19 +26,5 @@ const useCopyToClipboard = () => {
2226
}
2327
};
2428

25-
const clearClipboard: ClearFn = async () => {
26-
try {
27-
await navigator.clipboard.writeText('');
28-
setCopiedText(null);
29-
return true;
30-
} catch (error) {
31-
console.warn('Clear failed', error);
32-
setCopiedText(null);
33-
return false;
34-
}
35-
};
36-
37-
return { copiedText, copyToClipboard, clearClipboard };
29+
return { copiedText, copyToClipboard };
3830
};
39-
40-
export default useCopyToClipboard;

0 commit comments

Comments
 (0)