diff --git a/native/navigation/invite-link-handler.react.js b/native/invite-links/invite-links-context-provider.react.js similarity index 78% rename from native/navigation/invite-link-handler.react.js rename to native/invite-links/invite-links-context-provider.react.js index 0dac9332b1..6b2cf76584 100644 --- a/native/navigation/invite-link-handler.react.js +++ b/native/invite-links/invite-links-context-provider.react.js @@ -10,16 +10,32 @@ import { verifyInviteLinkActionTypes, } from 'lib/actions/link-actions.js'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; +import type { SetState } from 'lib/types/hook-types.js'; import { useDispatchActionPromise, useServerCall, } from 'lib/utils/action-utils.js'; -import { InviteLinkModalRouteName } from './route-names.js'; +import { InviteLinkModalRouteName } from '../navigation/route-names.js'; import { useSelector } from '../redux/redux-utils.js'; import { useOnFirstLaunchEffect } from '../utils/hooks.js'; -function InviteLinkHandler(): null { +type InviteLinksContextType = { + +setCurrentLinkUrl: SetState, +}; + +const defaultContext = { + setCurrentLinkUrl: () => {}, +}; + +const InviteLinksContext: React.Context = + React.createContext(defaultContext); + +type Props = { + +children: React.Node, +}; +function InviteLinksContextProvider(props: Props): React.Node { + const { children } = props; const [currentLink, setCurrentLink] = React.useState(null); React.useEffect(() => { @@ -93,7 +109,18 @@ function InviteLinkHandler(): null { })(); }, [currentLink, dispatchActionPromise, loggedIn, navigation, validateLink]); - return null; + const contextValue = React.useMemo( + () => ({ + setCurrentLinkUrl: setCurrentLink, + }), + [], + ); + + return ( + + {children} + + ); } const urlRegex = /invite\/(\S+)$/; @@ -108,4 +135,4 @@ function parseInstallReferrer(referrer: string) { return match?.[1]; } -export default InviteLinkHandler; +export { InviteLinksContext, InviteLinksContextProvider }; diff --git a/native/markdown/markdown-link.react.js b/native/markdown/markdown-link.react.js index af97489dcb..ecee51dcde 100644 --- a/native/markdown/markdown-link.react.js +++ b/native/markdown/markdown-link.react.js @@ -4,6 +4,8 @@ import invariant from 'invariant'; import * as React from 'react'; import { Text, Linking, Alert } from 'react-native'; +import { inviteLinkUrl } from 'lib/facts/links.js'; + import { MarkdownContext, type MarkdownContextType, @@ -11,9 +13,10 @@ import { import { MarkdownSpoilerContext } from './markdown-spoiler-context.js'; import { MessagePressResponderContext } from '../chat/message-press-responder-context.js'; import { TextMessageMarkdownContext } from '../chat/text-message-markdown-context.js'; +import { InviteLinksContext } from '../invite-links/invite-links-context-provider.react.js'; import { normalizeURL } from '../utils/url-utils.js'; -function useDisplayLinkPrompt( +function useHandleLinkClick( inputURL: string, markdownContext: MarkdownContextType, messageKey: ?string, @@ -33,7 +36,13 @@ function useDisplayLinkPrompt( if (url.length > displayURL.length) { displayURL += '…'; } + + const inviteLinksContext = React.useContext(InviteLinksContext); return React.useCallback(() => { + if (url.startsWith(inviteLinkUrl(''))) { + inviteLinksContext?.setCurrentLinkUrl(url); + return; + } messageKey && setLinkModalActive({ [messageKey]: true }); Alert.alert( 'External link', @@ -44,7 +53,15 @@ function useDisplayLinkPrompt( ], { cancelable: true, onDismiss }, ); - }, [setLinkModalActive, messageKey, displayURL, onConfirm, onDismiss]); + }, [ + url, + messageKey, + setLinkModalActive, + displayURL, + onDismiss, + onConfirm, + inviteLinksContext, + ]); } type TextProps = React.ElementConfig; @@ -76,7 +93,7 @@ function MarkdownLink(props: Props): React.Node { const onPressMessage = messagePressResponderContext?.onPressMessage; - const onPressLink = useDisplayLinkPrompt(target, markdownContext, messageKey); + const onPressLink = useHandleLinkClick(target, markdownContext, messageKey); return ( = React.memo<{}>( - {devTools} ); diff --git a/native/root.react.js b/native/root.react.js index 528817c21a..45507bbef9 100644 --- a/native/root.react.js +++ b/native/root.react.js @@ -33,6 +33,7 @@ import ConnectedStatusBar from './connected-status-bar.react.js'; import { SQLiteDataHandler } from './data/sqlite-data-handler.js'; import ErrorBoundary from './error-boundary.react.js'; import InputStateContainer from './input/input-state-container.react.js'; +import { InviteLinksContextProvider } from './invite-links/invite-links-context-provider.react.js'; import LifecycleHandler from './lifecycle/lifecycle-handler.react.js'; import MarkdownContextProvider from './markdown/markdown-context-provider.react.js'; import { filesystemMediaCache } from './media/media-cache.js'; @@ -249,7 +250,9 @@ function Root() { theme={theme} ref={containerRef} > - + + + );