BlueWallet/blue_modules/clipboard.ts

44 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-03-28 07:51:53 -04:00
import { useAsyncStorage } from '@react-native-async-storage/async-storage';
import Clipboard from '@react-native-clipboard/clipboard';
2023-03-29 20:46:11 -04:00
const BlueClipboard = () => {
const STORAGE_KEY = 'ClipboardReadAllowed';
const { getItem, setItem } = useAsyncStorage(STORAGE_KEY);
2023-03-28 07:51:53 -04:00
2023-03-29 20:46:11 -04:00
const isReadClipboardAllowed = async () => {
2023-03-28 07:51:53 -04:00
try {
2023-03-29 20:46:11 -04:00
const clipboardAccessAllowed = await getItem();
if (clipboardAccessAllowed === null) {
await setItem(JSON.stringify(true));
2023-03-28 07:51:53 -04:00
return true;
}
2023-03-29 20:46:11 -04:00
return !!JSON.parse(clipboardAccessAllowed);
2023-03-28 07:51:53 -04:00
} catch {
2023-03-29 20:46:11 -04:00
await setItem(JSON.stringify(true));
2023-03-28 07:51:53 -04:00
return true;
}
};
2023-03-29 20:46:11 -04:00
const setReadClipboardAllowed = (value: boolean) => {
setItem(JSON.stringify(!!value));
2023-03-28 07:51:53 -04:00
};
2023-03-29 20:46:11 -04:00
const getClipboardContent = async () => {
const isAllowed = await isReadClipboardAllowed();
const hasString = (await Clipboard.hasString()) || false;
if (isAllowed && hasString) {
2023-03-28 07:51:53 -04:00
return Clipboard.getString();
} else {
return '';
}
};
2023-03-29 20:46:11 -04:00
return {
isReadClipboardAllowed,
setReadClipboardAllowed,
getClipboardContent,
};
};
export default BlueClipboard;