BlueWallet/blue_modules/clipboard.ts

41 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-11-10 10:25:39 -04:00
import AsyncStorage from '@react-native-async-storage/async-storage';
2023-03-28 07:51:53 -04:00
import Clipboard from '@react-native-clipboard/clipboard';
2024-11-10 10:25:39 -04:00
const STORAGE_KEY: string = 'ClipboardReadAllowed';
2023-03-28 07:51:53 -04:00
2024-11-10 10:25:39 -04:00
export const isReadClipboardAllowed = async (): Promise<boolean> => {
try {
const clipboardAccessAllowed = await AsyncStorage.getItem(STORAGE_KEY);
if (clipboardAccessAllowed === null) {
await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(true));
2023-03-28 07:51:53 -04:00
return true;
}
2024-11-10 10:25:39 -04:00
return !!JSON.parse(clipboardAccessAllowed);
} catch {
await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(true));
return true;
}
};
2023-03-28 07:51:53 -04:00
export const setReadClipboardAllowed = async (value: boolean): Promise<void> => {
try {
await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(Boolean(value)));
} catch (error) {
console.error('Failed to set clipboard permission:', error);
throw error;
}
2023-03-29 20:46:11 -04:00
};
2024-11-10 10:25:39 -04:00
export const getClipboardContent = async (): Promise<string | undefined> => {
try {
const isAllowed = await isReadClipboardAllowed();
if (!isAllowed) return undefined;
2024-11-10 11:53:24 -04:00
const hasString = await Clipboard.hasString();
return hasString ? await Clipboard.getString() : undefined;
} catch (error) {
console.error('Error accessing clipboard:', error);
2024-11-10 10:25:39 -04:00
return undefined;
}
};