Skip to content
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

[TS migration] Migrate HTMLEngineProvider component #33643

Merged
12 changes: 6 additions & 6 deletions src/components/HTMLEngineProvider/htmlEngineUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {TNode} from 'react-native-render-html';
import {Predicate} from './types';
import type {TNode} from 'react-native-render-html';
import type {Predicate} from './types';

const MAX_IMG_DIMENSIONS = 512;

Expand All @@ -8,9 +8,9 @@ const MAX_IMG_DIMENSIONS = 512;
* is used by the HTML component in the default renderer for img tags to scale
* down images that would otherwise overflow horizontally.
*
* @param tagName - The name of the tag for which max width should be constrained.
* @param contentWidth - The content width provided to the HTML
* component.
* @param tagName - The name of the tag for which max width should be constrained.
* @returns The minimum between contentWidth and MAX_IMG_DIMENSIONS
*/
function computeEmbeddedMaxWidth(contentWidth: number, tagName: string): number {
Expand All @@ -24,7 +24,7 @@ function computeEmbeddedMaxWidth(contentWidth: number, tagName: string): number
* Check if tagName is equal to any of our custom tags wrapping chat comments.
*
*/
function isCommentTag(tagName?: string): boolean {
function isCommentTag(tagName: string): boolean {
return tagName === 'email-comment' || tagName === 'comment';
}

Expand All @@ -47,15 +47,15 @@ function isChildOfNode(tnode: TNode, predicate: Predicate): boolean {
* Finding node with name 'comment' flags that we are rendering a comment.
*/
function isChildOfComment(tnode: TNode): boolean {
return isChildOfNode(tnode, (node) => isCommentTag(node.domNode?.name));
return isChildOfNode(tnode, (node) => node.domNode?.name !== undefined && isCommentTag(node.domNode?.name));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return isChildOfNode(tnode, (node) => node.domNode?.name !== undefined && isCommentTag(node.domNode?.name));
return isChildOfNode(tnode, (node) => node.domNode?.name !== undefined && isCommentTag(node.domNode.name));


/**
* Check if there is an ancestor node with the name 'h1'.
* Finding a node with the name 'h1' flags that we are rendering inside an h1 element.
*/
function isChildOfH1(tnode: TNode): boolean {
return isChildOfNode(tnode, (node) => node.domNode?.name.toLowerCase() === 'h1');
return isChildOfNode(tnode, (node) => node.domNode?.name !== null && node.domNode?.name.toLowerCase() === 'h1');
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return isChildOfNode(tnode, (node) => node.domNode?.name !== null && node.domNode?.name.toLowerCase() === 'h1');
return isChildOfNode(tnode, (node) => node.domNode?.name !== undefined && node.domNode.name.toLowerCase() === 'h1');

The name is never null, it's either a string or undefined so the condition will always be true and it will cause the app to crash


export {computeEmbeddedMaxWidth, isChildOfComment, isCommentTag, isChildOfH1};
13 changes: 3 additions & 10 deletions src/components/HTMLEngineProvider/index.native.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import React from 'react';
import BaseHTMLEngineProvider from './BaseHTMLEngineProvider';
import {HTMLEngineProviderProps} from './types';
import type {HTMLEngineProviderProps} from './types';

function HTMLEngineProvider({debug = false, children}: HTMLEngineProviderProps) {
return (
<BaseHTMLEngineProvider
debug={debug}
enableExperimentalBRCollapsing
>
{children}
</BaseHTMLEngineProvider>
);
function HTMLEngineProvider({children}: HTMLEngineProviderProps) {
return <BaseHTMLEngineProvider enableExperimentalBRCollapsing>{children}</BaseHTMLEngineProvider>;
}

HTMLEngineProvider.displayName = 'HTMLEngineProvider';
Expand Down
13 changes: 3 additions & 10 deletions src/components/HTMLEngineProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@ import React from 'react';
import useWindowDimensions from '@hooks/useWindowDimensions';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import BaseHTMLEngineProvider from './BaseHTMLEngineProvider';
import {HTMLEngineProviderProps} from './types';
import type {HTMLEngineProviderProps} from './types';

function HTMLEngineProvider({debug = false, children = null}: HTMLEngineProviderProps) {
function HTMLEngineProvider({children = null}: HTMLEngineProviderProps) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function HTMLEngineProvider({children = null}: HTMLEngineProviderProps) {
function HTMLEngineProvider({children}: HTMLEngineProviderProps) {

No default value is needed since the prop is required

const {isSmallScreenWidth} = useWindowDimensions();

return (
<BaseHTMLEngineProvider
debug={debug}
textSelectable={!DeviceCapabilities.canUseTouchScreen() || isSmallScreenWidth}
>
{children}
</BaseHTMLEngineProvider>
);
return <BaseHTMLEngineProvider textSelectable={!DeviceCapabilities.canUseTouchScreen() || !isSmallScreenWidth}>{children}</BaseHTMLEngineProvider>;
}

HTMLEngineProvider.displayName = 'HTMLEngineProvider';
Expand Down
12 changes: 3 additions & 9 deletions src/components/HTMLEngineProvider/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import {ReactNode} from 'react';
import {TNode} from 'react-native-render-html';
import type {TNode} from 'react-native-render-html';
import type ChildrenProps from '@src/types/utils/ChildrenProps';

type HTMLEngineProviderProps = {
/** Children to wrap in HTMLEngineProvider. */
children?: ReactNode;

/** Optional debug flag. Prints the TRT in the console when true. */
debug?: boolean;
};
type HTMLEngineProviderProps = ChildrenProps;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to define a type that is just another type. We can just use ChildrenProps and remove HTMLEngineProviderProps

type Predicate = (node: TNode) => boolean;

Expand Down
Loading