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 'ComposerUtils' lib to TypeScript #28008

Merged
Merged
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
2 changes: 1 addition & 1 deletion src/components/Composer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ function Composer({
const paddingTopAndBottom = parseInt(computedStyle.paddingBottom, 10) + parseInt(computedStyle.paddingTop, 10);
setTextInputWidth(computedStyle.width);

const computedNumberOfLines = ComposerUtils.getNumberOfLines(maxLines, lineHeight, paddingTopAndBottom, textInput.current.scrollHeight);
const computedNumberOfLines = ComposerUtils.getNumberOfLines(lineHeight, paddingTopAndBottom, textInput.current.scrollHeight, maxLines);
const generalNumberOfLines = computedNumberOfLines === 0 ? numberOfLinesProp : computedNumberOfLines;

onNumberOfLinesChange(generalNumberOfLines);
Expand Down
13 changes: 0 additions & 13 deletions src/libs/ComposerUtils/debouncedSaveReportComment.js

This file was deleted.

11 changes: 11 additions & 0 deletions src/libs/ComposerUtils/debouncedSaveReportComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import debounce from 'lodash/debounce';
import * as Report from '../actions/Report';

/**
* Save draft report comment. Debounced to happen at most once per second.
*/
const debouncedSaveReportComment = debounce((reportID: string, comment = '') => {
Report.saveReportComment(reportID, comment);
}, 1000);

export default debouncedSaveReportComment;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Onyx from 'react-native-onyx';
import Onyx, {OnyxEntry} from 'react-native-onyx';
import ONYXKEYS from '../../ONYXKEYS';

const draftCommentMap = {};
const draftCommentMap: Record<string, OnyxEntry<string>> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT,
callback: (value, key) => {
Expand All @@ -18,9 +18,7 @@ Onyx.connect({
* Returns a draft comment from the onyx collection.
* Note: You should use the HOCs/hooks to get onyx data, instead of using this directly.
* A valid use case to use this is if the value is only needed once for an initial value.
* @param {String} reportID
* @returns {String|undefined}
*/
export default function getDraftComment(reportID) {
export default function getDraftComment(reportID: string): OnyxEntry<string> {
return draftCommentMap[reportID];
}
14 changes: 0 additions & 14 deletions src/libs/ComposerUtils/getNumberOfLines/index.native.js

This file was deleted.

8 changes: 8 additions & 0 deletions src/libs/ComposerUtils/getNumberOfLines/index.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import GetNumberOfLines from './types';

/**
* Get the current number of lines in the composer
*/
const getNumberOfLines: GetNumberOfLines = (lineHeight, paddingTopAndBottom, scrollHeight) => Math.ceil((scrollHeight - paddingTopAndBottom) / lineHeight);

export default getNumberOfLines;
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import GetNumberOfLines from './types';

/**
* Get the current number of lines in the composer
*
* @param {Number} maxLines
* @param {Number} lineHeight
* @param {Number} paddingTopAndBottom
* @param {Number} scrollHeight
*
* @returns {Number}
*/
function getNumberOfLines(maxLines, lineHeight, paddingTopAndBottom, scrollHeight) {
const getNumberOfLines: GetNumberOfLines = (lineHeight, paddingTopAndBottom, scrollHeight, maxLines = 0) => {
let newNumberOfLines = Math.ceil((scrollHeight - paddingTopAndBottom) / lineHeight);
newNumberOfLines = maxLines <= 0 ? newNumberOfLines : Math.min(newNumberOfLines, maxLines);
return newNumberOfLines;
}
};

export default getNumberOfLines;
3 changes: 3 additions & 0 deletions src/libs/ComposerUtils/getNumberOfLines/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type GetNumberOfLines = (lineHeight: number, paddingTopAndBottom: number, scrollHeight: number, maxLines?: number) => number;

export default GetNumberOfLines;
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@ import getNumberOfLines from './getNumberOfLines';
import updateNumberOfLines from './updateNumberOfLines';
import * as DeviceCapabilities from '../DeviceCapabilities';

type Selection = {
start: number;
end: number;
};

/**
* Replace substring between selection with a text.
* @param {String} text
* @param {Object} selection
* @param {String} textToInsert
* @returns {String}
*/
function insertText(text, selection, textToInsert) {
function insertText(text: string, selection: Selection, textToInsert: string): string {
return text.slice(0, selection.start) + textToInsert + text.slice(selection.end, text.length);
}

/**
* Check whether we can skip trigger hotkeys on some specific devices.
* @param {Boolean} isSmallScreenWidth
* @param {Boolean} isKeyboardShown
* @returns {Boolean}
*/
function canSkipTriggerHotkeys(isSmallScreenWidth, isKeyboardShown) {
function canSkipTriggerHotkeys(isSmallScreenWidth: boolean, isKeyboardShown: boolean): boolean {
// Do not trigger actions for mobileWeb or native clients that have the keyboard open
// because for those devices, we want the return key to insert newlines rather than submit the form
return (isSmallScreenWidth && DeviceCapabilities.canUseTouchScreen()) || isKeyboardShown;
Expand All @@ -30,11 +28,9 @@ function canSkipTriggerHotkeys(isSmallScreenWidth, isKeyboardShown) {
* The common suffix is the number of characters shared by both strings
* at the end (suffix) until a mismatch is encountered.
*
* @param {string} str1
* @param {string} str2
* @returns {number} The length of the common suffix between the strings.
* @returns The length of the common suffix between the strings.
*/
function getCommonSuffixLength(str1, str2) {
function getCommonSuffixLength(str1: string, str2: string): number {
let i = 0;
while (str1[str1.length - 1 - i] === str2[str2.length - 1 - i]) {
i++;
Expand Down
6 changes: 6 additions & 0 deletions src/libs/ComposerUtils/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type ComposerProps = {
isFullComposerAvailable: boolean;
setIsFullComposerAvailable: (isFullComposerAvailable: boolean) => void;
};

export default ComposerProps;
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import CONST from '../../CONST';
import ComposerProps from './types';

/**
* Update isFullComposerAvailable if needed
* @param {Object} props
* @param {Number} numberOfLines The number of lines in the text input
* @param numberOfLines The number of lines in the text input
*/
function updateIsFullComposerAvailable(props, numberOfLines) {
function updateIsFullComposerAvailable(props: ComposerProps, numberOfLines: number) {
const isFullComposerAvailable = numberOfLines >= CONST.COMPOSER.FULL_COMPOSER_MIN_LINES;
if (isFullComposerAvailable !== props.isFullComposerAvailable) {
props.setIsFullComposerAvailable(isFullComposerAvailable);
Expand Down
1 change: 0 additions & 1 deletion src/libs/ComposerUtils/updateNumberOfLines/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import lodashGet from 'lodash/get';
import styles from '../../../styles/styles';
import updateIsFullComposerAvailable from '../updateIsFullComposerAvailable';
import getNumberOfLines from '../getNumberOfLines';
import UpdateNumberOfLines from './types';

/**
* Check the current scrollHeight of the textarea (minus any padding) and
* divide by line height to get the total number of rows for the textarea.
* @param {Object} props
* @param {Event} e
*/
function updateNumberOfLines(props, e) {
const updateNumberOfLines: UpdateNumberOfLines = (props, event) => {
const lineHeight = styles.textInputCompose.lineHeight;
const paddingTopAndBottom = styles.textInputComposeSpacing.paddingVertical * 2;
const inputHeight = lodashGet(e, 'nativeEvent.contentSize.height', null);
const inputHeight = event?.nativeEvent?.contentSize?.height ?? null;
if (!inputHeight) {
return;
}
const numberOfLines = getNumberOfLines(lineHeight, paddingTopAndBottom, inputHeight);
updateIsFullComposerAvailable(props, numberOfLines);
}
};

export default updateNumberOfLines;
5 changes: 5 additions & 0 deletions src/libs/ComposerUtils/updateNumberOfLines/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import UpdateNumberOfLines from './types';

const updateNumberOfLines: UpdateNumberOfLines = () => {};

export default updateNumberOfLines;
6 changes: 6 additions & 0 deletions src/libs/ComposerUtils/updateNumberOfLines/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {NativeSyntheticEvent, TextInputContentSizeChangeEventData} from 'react-native';
import ComposerProps from '../types';

type UpdateNumberOfLines = (props: ComposerProps, event: NativeSyntheticEvent<TextInputContentSizeChangeEventData>) => void;

export default UpdateNumberOfLines;
Loading