From b5af0b682406f4bb65af7567123f433a59c0ce0a Mon Sep 17 00:00:00 2001 From: devs-rootstrap <112487182+devs-rootstrap@users.noreply.github.com> Date: Wed, 11 Nov 2026 09:06:04 -0300 Subject: [PATCH] feat: open external links in a new tab on web --- .../components/settings/link-item.test.tsx | 66 +++++++++++++++++++ src/app/(app)/settings.tsx | 35 ++-------- src/components/settings/item.tsx | 20 +++++- src/components/settings/link-item.tsx | 44 +++++++++++++ 4 files changed, 135 insertions(+), 30 deletions(-) create mode 100644 __tests__/components/settings/link-item.test.tsx create mode 100644 src/components/settings/link-item.tsx diff --git a/__tests__/components/settings/link-item.test.tsx b/__tests__/components/settings/link-item.test.tsx new file mode 100644 index 000000000..9455c2492 --- /dev/null +++ b/__tests__/components/settings/link-item.test.tsx @@ -0,0 +1,66 @@ +import { Platform } from 'react-native'; + +import { fireEvent, render, screen } from '@/lib/test-utils'; + +import { LinkItem } from '../../../src/components/settings/link-item'; + +const mockLink = jest.fn(); + +jest.mock('expo-router', () => { + const React = jest.requireActual('react'); + return { + Link: (props: { href: unknown; children: React.ReactElement }) => { + mockLink(props.href); + return React.cloneElement(props.children, { + onPress: () => mockLink(props.href), + } as Record); + }, + }; +}); + +const URL = 'https://example.com/terms'; +const originalOS = Platform.OS; + +function setPlatformOS(os: typeof Platform.OS) { + Object.defineProperty(Platform, 'OS', { configurable: true, get: () => os }); +} + +describe('LinkItem component', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + afterEach(() => { + setPlatformOS(originalOS); + }); + + it('should render an anchor that opens the URL in a new tab on web', () => { + setPlatformOS('web'); + render(); + + const link = screen.getByRole('link'); + expect(link).toHaveTextContent('Terms of Service'); + expect(link.props.href).toBe(URL); + expect(link.props.hrefAttrs).toEqual({ + target: '_blank', + rel: 'noopener noreferrer', + }); + expect(mockLink).not.toHaveBeenCalled(); + }); + + it.each(['ios', 'android'] as const)( + 'should navigate to the in-app WebView on %s', + (os) => { + setPlatformOS(os); + render(); + + fireEvent.press(screen.getByText('Terms of Service')); + + expect(mockLink).toHaveBeenLastCalledWith({ + pathname: '/www', + params: { url: URL, title: 'Terms of Service' }, + }); + expect(screen.queryByRole('link')).toBeNull(); + }, + ); +}); diff --git a/src/app/(app)/settings.tsx b/src/app/(app)/settings.tsx index 2ad5ed049..24327cd2e 100644 --- a/src/app/(app)/settings.tsx +++ b/src/app/(app)/settings.tsx @@ -1,4 +1,3 @@ -/* eslint-disable max-lines-per-function */ import { Link } from 'expo-router'; import { useColorScheme } from 'nativewind'; import * as React from 'react'; @@ -10,6 +9,7 @@ import { DeleteAccountItem } from '@/components/settings/delete-account-item'; import { Item } from '@/components/settings/item'; import { ItemsContainer } from '@/components/settings/items-container'; import { LanguageItem } from '@/components/settings/language-item'; +import { LinkItem } from '@/components/settings/link-item'; import { ThemeItem } from '@/components/settings/theme-item'; import { colors, @@ -71,33 +71,12 @@ export default function Settings() { - - - - - } - /> - + + } + /> diff --git a/src/components/settings/item.tsx b/src/components/settings/item.tsx index 7eaff20da..0b4f16f56 100644 --- a/src/components/settings/item.tsx +++ b/src/components/settings/item.tsx @@ -1,3 +1,4 @@ +import type { PressableProps } from 'react-native'; import type { TxKeyPath } from '@/lib'; import * as React from 'react'; @@ -10,15 +11,30 @@ type ItemProps = { value?: string; onPress?: () => void; icon?: React.ReactNode; + role?: PressableProps['role']; + /** + * Web only: renders the row as an anchor (``) that opens `externalHref` + * in a new browser tab. + */ + externalHref?: string; }; export const Item = forwardRef( - ({ text, value, icon, onPress }: ItemProps, ref) => { - const isPressable = onPress !== undefined; + ({ text, value, icon, onPress, role, externalHref }: ItemProps, ref) => { + const isPressable = onPress !== undefined || externalHref !== undefined; + // react-native-web renders a View with `href` as an `` element. + const anchorProps = externalHref === undefined + ? {} + : { + href: externalHref, + hrefAttrs: { target: '_blank', rel: 'noopener noreferrer' }, + }; return ( diff --git a/src/components/settings/link-item.tsx b/src/components/settings/link-item.tsx new file mode 100644 index 000000000..a46997bfb --- /dev/null +++ b/src/components/settings/link-item.tsx @@ -0,0 +1,44 @@ +import type { TxKeyPath } from '@/lib'; +import { Link } from 'expo-router'; +import * as React from 'react'; +import { Platform } from 'react-native'; + +import { translate } from '@/lib'; + +import { Item } from './item'; + +type LinkItemProps = { + text: TxKeyPath; + url: string; + icon?: React.ReactNode; +}; + +/** + * Settings item that opens an external URL. + * On web it renders an anchor that opens the URL in a new browser tab; + * on native it navigates to the in-app WebView screen (`/www`). + */ +export function LinkItem({ text, url, icon }: LinkItemProps) { + if (Platform.OS === 'web') { + return ( + + ); + } + + return ( + + + + ); +}