mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2024-11-19 01:40:12 +01:00
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
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();
|
|
const hasString = (await Clipboard.hasString()) || false;
|
|
if (isAllowed && hasString) {
|
|
return Clipboard.getString();
|
|
} else {
|
|
return '';
|
|
}
|
|
};
|
|
|
|
return {
|
|
isReadClipboardAllowed,
|
|
setReadClipboardAllowed,
|
|
getClipboardContent,
|
|
};
|
|
};
|
|
|
|
export default BlueClipboard;
|