forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipboard.ts
42 lines (36 loc) · 1.05 KB
/
clipboard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { useAsyncStorage } from '@react-native-async-storage/async-storage';
import Clipboard from '@react-native-clipboard/clipboard';
const BlueClipboard = () => {
const STORAGE_KEY = 'ClipboardReadAllowed';
const { getItem, setItem } = useAsyncStorage(STORAGE_KEY);
const isReadClipboardAllowed = async () => {
try {
const clipboardAccessAllowed = await getItem();
if (clipboardAccessAllowed === null) {
await setItem(JSON.stringify(true));
return true;
}
return !!JSON.parse(clipboardAccessAllowed);
} catch {
await setItem(JSON.stringify(true));
return true;
}
};
const setReadClipboardAllowed = (value: boolean) => {
setItem(JSON.stringify(!!value));
};
const getClipboardContent = async () => {
const isAllowed = await isReadClipboardAllowed();
if (isAllowed) {
return Clipboard.getString();
} else {
return '';
}
};
return {
isReadClipboardAllowed,
setReadClipboardAllowed,
getClipboardContent,
};
};
export default BlueClipboard;