Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions __tests__/components/settings/link-item.test.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof import('react')>('react');
return {
Link: (props: { href: unknown; children: React.ReactElement }) => {
mockLink(props.href);
return React.cloneElement(props.children, {
onPress: () => mockLink(props.href),
} as Record<string, unknown>);
},
};
});

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(<LinkItem text="settings.terms" url={URL} />);

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(<LinkItem text="settings.terms" url={URL} />);

fireEvent.press(screen.getByText('Terms of Service'));

expect(mockLink).toHaveBeenLastCalledWith({
pathname: '/www',
params: { url: URL, title: 'Terms of Service' },
});
expect(screen.queryByRole('link')).toBeNull();
},
);
});
35 changes: 7 additions & 28 deletions src/app/(app)/settings.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable max-lines-per-function */
import { Link } from 'expo-router';
import { useColorScheme } from 'nativewind';
import * as React from 'react';
Expand All @@ -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,
Expand Down Expand Up @@ -71,33 +71,12 @@ export default function Settings() {
</ItemsContainer>

<ItemsContainer title="settings.links">
<Link
asChild
href={{
pathname: '/www',
params: {
url: Env.TERMS_OF_SERVICE_URL,
title: translate('settings.terms'),
},
}}
>
<Item text="settings.terms" />
</Link>
<Link
asChild
href={{
pathname: '/www',
params: {
url: Env.WEBSITE_URL,
title: translate('settings.website'),
},
}}
>
<Item
text="settings.website"
icon={<Website color={iconColor} />}
/>
</Link>
<LinkItem text="settings.terms" url={Env.TERMS_OF_SERVICE_URL} />
<LinkItem
text="settings.website"
url={Env.WEBSITE_URL}
icon={<Website color={iconColor} />}
/>
</ItemsContainer>

<ItemsContainer title="settings.about">
Expand Down
20 changes: 18 additions & 2 deletions src/components/settings/item.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PressableProps } from 'react-native';
import type { TxKeyPath } from '@/lib';
import * as React from 'react';

Expand All @@ -10,15 +11,30 @@ type ItemProps = {
value?: string;
onPress?: () => void;
icon?: React.ReactNode;
role?: PressableProps['role'];
/**
* Web only: renders the row as an anchor (`<a href>`) that opens `externalHref`
* in a new browser tab.
*/
externalHref?: string;
};

export const Item = forwardRef<View, ItemProps>(
({ 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 `<a>` element.
const anchorProps = externalHref === undefined
? {}
: {
href: externalHref,
hrefAttrs: { target: '_blank', rel: 'noopener noreferrer' },
};
return (
<Pressable
ref={ref}
onPress={onPress}
role={role}
{...anchorProps}
pointerEvents={isPressable ? 'auto' : 'none'}
className="flex-1 flex-row items-center justify-between px-4 py-2"
>
Expand Down
44 changes: 44 additions & 0 deletions src/components/settings/link-item.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Item
text={text}
icon={icon}
role="link"
externalHref={url}
/>
);
}

return (
<Link
asChild
href={{
pathname: '/www',
params: { url, title: translate(text) },
}}
>
<Item text={text} icon={icon} />
</Link>
);
}
Loading