diff --git a/packages/core/src/js/feedback/FeedbackForm.tsx b/packages/core/src/js/feedback/FeedbackForm.tsx index 05b6520f1..3049a53a3 100644 --- a/packages/core/src/js/feedback/FeedbackForm.tsx +++ b/packages/core/src/js/feedback/FeedbackForm.tsx @@ -16,7 +16,6 @@ import { View } from 'react-native'; -import { base64ToUint8Array } from '../utils/base64ToUint8Array'; import { sentryLogo } from './branding'; import { defaultConfiguration } from './defaults'; import defaultStyles from './FeedbackForm.styles'; @@ -68,13 +67,11 @@ export class FeedbackForm extends React.Component void) => void; + onAddScreenshot?: (attachFile: (filename: string, base64Attachment: Uint8Array) => void) => void; } /** diff --git a/packages/core/src/js/feedback/defaults.ts b/packages/core/src/js/feedback/defaults.ts index dd4f9cec9..d30fad02c 100644 --- a/packages/core/src/js/feedback/defaults.ts +++ b/packages/core/src/js/feedback/defaults.ts @@ -29,7 +29,7 @@ export const defaultConfiguration: Partial = { ); } }, - onAddScreenshot: (_: (filename: string, base64Attachment: string | Uint8Array) => void) => { + onAddScreenshot: (_: (filename: string, base64Attachment: Uint8Array) => void) => { if (__DEV__) { Alert.alert('Development note', 'onAddScreenshot callback is not implemented.'); } diff --git a/packages/core/src/js/utils/base64ToUint8Array.ts b/packages/core/src/js/utils/base64ToUint8Array.ts deleted file mode 100644 index a6dd45d1f..000000000 --- a/packages/core/src/js/utils/base64ToUint8Array.ts +++ /dev/null @@ -1,29 +0,0 @@ -/* eslint-disable no-bitwise */ -export const base64ToUint8Array = (base64?: string): Uint8Array | undefined => { - if (!base64) return undefined; - - const cleanedBase64 = base64.replace(/^data:.*;base64,/, ''); // Remove any prefix before the base64 string - const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; - const bytes: number[] = []; - - let buffer = 0; - let bits = 0; - - for (const char of cleanedBase64) { - if (char === '=') break; - - const value = chars.indexOf(char); // Validate each character - if (value === -1) return undefined; - - buffer = (buffer << 6) | value; // Shift 6 bits to the left and add the value - bits += 6; - - if (bits >= 8) { - // Add a byte when we have 8 or more bits - bits -= 8; - bytes.push((buffer >> bits) & 0xff); - } - } - - return new Uint8Array(bytes); -}; diff --git a/samples/react-native/src/App.tsx b/samples/react-native/src/App.tsx index 14b1f93e3..f8c87e56b 100644 --- a/samples/react-native/src/App.tsx +++ b/samples/react-native/src/App.tsx @@ -141,7 +141,37 @@ const Stack = isMobileOs : createStackNavigator(); const Tab = createBottomTabNavigator(); -const handleChooseImage = (attachFile: (filename: string, base64Attachment: string | Uint8Array) => void): void => { +/* eslint-disable no-bitwise */ +const base64ToUint8Array = (base64?: string): Uint8Array | undefined => { + if (!base64) return undefined; + + const cleanedBase64 = base64.replace(/^data:.*;base64,/, ''); // Remove any prefix before the base64 string + const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; + const bytes: number[] = []; + + let buffer = 0; + let bits = 0; + + for (const char of cleanedBase64) { + if (char === '=') break; + + const value = chars.indexOf(char); // Validate each character + if (value === -1) return undefined; + + buffer = (buffer << 6) | value; // Shift 6 bits to the left and add the value + bits += 6; + + if (bits >= 8) { + // Add a byte when we have 8 or more bits + bits -= 8; + bytes.push((buffer >> bits) & 0xff); + } + } + + return new Uint8Array(bytes); +}; + +const handleChooseImage = (attachFile: (filename: string, base64Attachment: Uint8Array) => void): void => { launchImageLibrary({ mediaType: 'photo', includeBase64: true }, (response) => { if (response.didCancel) { console.log('User cancelled image picker'); @@ -150,8 +180,9 @@ const handleChooseImage = (attachFile: (filename: string, base64Attachment: stri } else if (response.assets && response.assets.length > 0) { const filename = response.assets[0].fileName; const base64String = response.assets[0].base64; - if (filename && base64String) { - attachFile(filename, base64String); + const screenShotUint8Array = base64ToUint8Array(base64String); + if (filename && screenShotUint8Array) { + attachFile(filename, screenShotUint8Array); } } });