-
Notifications
You must be signed in to change notification settings - Fork 3
🏗️ Add generalized WebView bridge with identity and URL handlers #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
williamchong
wants to merge
3
commits into
likecoin:main
Choose a base branch
from
williamchong:feature/event
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| export type SendToWebView = (data: object) => void; | ||
| export type BridgeHandler = ( | ||
| msg: Record<string, unknown> | ||
| ) => void | Promise<void>; | ||
| export type BridgeHandlerMap = Record<string, BridgeHandler>; | ||
|
|
||
| const handlers = new Map<string, BridgeHandler>(); | ||
|
|
||
| export function registerHandlers( | ||
| map: BridgeHandlerMap | ||
| ): void { | ||
| for (const [type, handler] of Object.entries(map)) { | ||
| handlers.set(type, handler); | ||
| } | ||
| } | ||
|
|
||
| export async function dispatch( | ||
| raw: string | ||
| ): Promise<void> { | ||
| const msg: { type: string; [key: string]: unknown } = JSON.parse(raw); | ||
| const handler = handlers.get(msg.type); | ||
| if (!handler) { | ||
| console.warn(`[bridge] unknown message type: ${msg.type}`); | ||
| return; | ||
| } | ||
| await handler(msg); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import type PostHog from 'posthog-react-native'; | ||
| import type { BridgeHandlerMap } from './bridge-dispatcher'; | ||
|
|
||
| export function getIdentityHandlers(posthog: PostHog): BridgeHandlerMap; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import * as Sentry from '@sentry/react-native'; | ||
| import analytics from '@react-native-firebase/analytics'; | ||
| import type PostHog from 'posthog-react-native'; | ||
| import type { BridgeHandlerMap } from './bridge-dispatcher'; | ||
|
|
||
| export function getIdentityHandlers(posthog: PostHog): BridgeHandlerMap { | ||
| return { | ||
| identifyUser: async (msg) => { | ||
| const userId = typeof msg.userId === 'string' ? msg.userId : undefined; | ||
| if (!userId) return; | ||
|
|
||
| const email = typeof msg.email === 'string' ? msg.email : undefined; | ||
| const displayName = | ||
| typeof msg.displayName === 'string' ? msg.displayName : undefined; | ||
| const isLikerPlus = !!msg.isLikerPlus; | ||
| const loginMethod = | ||
| typeof msg.loginMethod === 'string' ? msg.loginMethod : undefined; | ||
|
|
||
| posthog.identify(userId, { | ||
| email: email ?? null, | ||
| name: displayName ?? null, | ||
| is_liker_plus: isLikerPlus, | ||
| login_method: loginMethod ?? null, | ||
| }); | ||
|
|
||
| await Promise.all([ | ||
| analytics().setUserId(userId), | ||
| analytics().setUserProperties({ | ||
| is_liker_plus: String(isLikerPlus), | ||
| login_method: loginMethod || '', | ||
| }), | ||
| ]); | ||
|
|
||
| Sentry.setUser({ | ||
| id: userId, | ||
| email, | ||
| username: displayName || userId, | ||
| }); | ||
| }, | ||
|
|
||
| resetUser: async () => { | ||
| posthog.reset(); | ||
| await analytics().setUserId(null); | ||
| Sentry.setUser(null); | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import type { BridgeHandlerMap } from './bridge-dispatcher'; | ||
|
|
||
| export function getIdentityHandlers(): BridgeHandlerMap { | ||
| return {}; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import PostHog from 'posthog-react-native'; | ||
|
|
||
| export const posthog = new PostHog( | ||
| 'phc_VOXrU28p44Z0coehNjKThwVPK5dO0A6xwQTQqThWI1c', | ||
| { | ||
| host: 'https://us.i.posthog.com', | ||
| captureAppLifecycleEvents: true, | ||
| } | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import type { BridgeHandlerMap } from './bridge-dispatcher'; | ||
|
|
||
| export function isDeepLink(url: string): boolean; | ||
| export function openDeepLink(url: string): Promise<void>; | ||
| export function getURLHandlers(): BridgeHandlerMap; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import * as WebBrowser from 'expo-web-browser'; | ||
| import * as Linking from 'expo-linking'; | ||
| import type { BridgeHandlerMap } from './bridge-dispatcher'; | ||
|
|
||
| const DEEP_LINK_SCHEME_RE = | ||
| /^(mailto:|tel:|wc:|metamask:|cbwallet:|rainbow:|trust:)/; | ||
|
|
||
| const WALLET_UNIVERSAL_LINK_PREFIXES = [ | ||
| 'https://metamask.app.link/', | ||
| 'https://go.cb-w.com/', | ||
| 'https://link.trustwallet.com/', | ||
| ]; | ||
|
|
||
| /** Returns true for URLs that should be opened by the OS (wallet deep links, | ||
| * mailto, tel) rather than loaded inside the WebView or in-app browser. */ | ||
| export function isDeepLink(url: string): boolean { | ||
| return ( | ||
| DEEP_LINK_SCHEME_RE.test(url) || | ||
| WALLET_UNIVERSAL_LINK_PREFIXES.some((prefix) => url.startsWith(prefix)) | ||
| ); | ||
| } | ||
|
|
||
| export async function openDeepLink(url: string): Promise<void> { | ||
| await Linking.openURL(url); | ||
| } | ||
|
|
||
| export function getURLHandlers(): BridgeHandlerMap { | ||
| return { | ||
| openExternalURL: async (msg) => { | ||
| const url = typeof msg.url === 'string' ? msg.url : undefined; | ||
| if (!url) return; | ||
|
|
||
| try { | ||
| if (isDeepLink(url)) { | ||
| await openDeepLink(url); | ||
| } else { | ||
| await WebBrowser.openBrowserAsync(url, { | ||
| dismissButtonStyle: 'close', | ||
| presentationStyle: | ||
| WebBrowser.WebBrowserPresentationStyle.FULL_SCREEN, | ||
| }); | ||
| } | ||
| } catch (e) { | ||
| console.warn('[openExternalURL]', e); | ||
| } | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import type { BridgeHandlerMap } from './bridge-dispatcher'; | ||
|
|
||
| export function isDeepLink(_url: string): boolean { | ||
| return false; | ||
| } | ||
|
|
||
| export async function openDeepLink(_url: string): Promise<void> {} | ||
|
|
||
| export function getURLHandlers(): BridgeHandlerMap { | ||
| return {}; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dispatch()assumesJSON.parse(raw)returns an object with a stringtype. If the WebView sends malformed JSON ornull, this can throw or lead to confusing logs. Add a small runtime guard (and ideally catchJSON.parseerrors here) sodispatchcan safely ignore/diagnose invalid messages without relying on callers to wrap it.