Skip to content

Commit

Permalink
chore: React Native for macOS support
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Jan 23, 2024
1 parent 7000825 commit 06fb643
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions posthog-react-native/src/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const getLegacyValues = async (): Promise<{ distinctId?: string; anonymou
return
}

// legacy didn't support macos, no need to check it
if (Platform.OS === 'ios') {
const posthogFileDirectory = `${OptionalExpoFileSystem.documentDirectory}../Library/Application%20Support/${OptionalExpoApplication.applicationId}/`
const posthogDistinctIdFile = posthogFileDirectory + 'posthog.distinctId'
Expand Down
6 changes: 5 additions & 1 deletion posthog-react-native/src/optional/OptionalExpoApplication.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type ExpoApplication from 'expo-application'
import { Platform } from 'react-native'

export let OptionalExpoApplication: typeof ExpoApplication | undefined = undefined

try {
OptionalExpoApplication = require('expo-application')
// macos not supported
if (Platform.OS !== 'macos') {
OptionalExpoApplication = require('expo-application')
}
} catch (e) {}
6 changes: 5 additions & 1 deletion posthog-react-native/src/optional/OptionalExpoDevice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type ExpoDevice from 'expo-device'
import { Platform } from 'react-native'

export let OptionalExpoDevice: typeof ExpoDevice | undefined = undefined

try {
OptionalExpoDevice = require('expo-device')
// macos not supported
if (Platform.OS !== 'macos') {
OptionalExpoDevice = require('expo-device')
}
} catch (e) {}
6 changes: 3 additions & 3 deletions posthog-react-native/src/optional/OptionalExpoFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { Platform } from 'react-native'
export let OptionalExpoFileSystem: typeof ExpoFileSystem | undefined = undefined

try {
// do not try to load expo-file-system on web, otherwise it will throw an error
// do not try to load expo-file-system on web and macos, otherwise it will throw an error
// Error: The method or property expo-file-system.writeAsStringAsync is not available on web
// See https://github.com/PostHog/posthog-js-lite/issues/140
// Once expo-file-system is supported on web, we can remove this try/catch block
// Once expo-file-system is supported on web/macos, we can remove this try/catch block
// For now, use the react-native-async-storage/async-storage package instead
if (Platform.OS !== 'web') {
if (Platform.OS !== 'web' && Platform.OS !== 'macos') {
OptionalExpoFileSystem = require('expo-file-system') // No Web support
}
} catch (e) {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type ExpoLocalization from 'expo-localization'
import { Platform } from 'react-native'

export let OptionalExpoLocalization: typeof ExpoLocalization | undefined = undefined

try {
OptionalExpoLocalization = require('expo-localization')
// macos not supported
if (Platform.OS !== 'macos') {
OptionalExpoLocalization = require('expo-localization')
}
} catch (e) {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Platform } from 'react-native'
import type ReactNativeDeviceInfo from 'react-native-device-info'

export let OptionalReactNativeDeviceInfo: typeof ReactNativeDeviceInfo | undefined = undefined

try {
OptionalReactNativeDeviceInfo = require('react-native-device-info') // No Web support
// macos not supported
if (Platform.OS !== 'macos') {
OptionalReactNativeDeviceInfo = require('react-native-device-info') // No Web support, returns unknown
}
} catch (e) {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type ReactNativeNavigation from '@react-navigation/native'
import { Platform } from 'react-native'

export let OptionalReactNativeNavigation: typeof ReactNativeNavigation | undefined = undefined

try {
OptionalReactNativeNavigation = require('@react-navigation/native')
// macos not supported
if (Platform.OS !== 'macos') {
// experimental support for web https://reactnavigation.org/docs/web-support/
OptionalReactNativeNavigation = require('@react-navigation/native')
}
} catch (e) {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Platform } from 'react-native'
import type ReactNativeNavigationWix from 'react-native-navigation'

export let OptionalReactNativeNavigationWix: typeof ReactNativeNavigationWix | undefined = undefined

try {
OptionalReactNativeNavigationWix = require('react-native-navigation')
// macos/web not supported
if (Platform.OS !== 'web' && Platform.OS !== 'macos') {
OptionalReactNativeNavigationWix = require('react-native-navigation')
}
} catch (e) {}

0 comments on commit 06fb643

Please sign in to comment.