From d64a9971a4c24b46b509015ddcc2401a853dfd7e Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Mon, 4 Mar 2024 16:41:25 +0700 Subject: [PATCH 001/188] Migrate 'AttachmentPicker' component to TypeScript --- .../{index.native.js => index.native.tsx} | 198 ++++++++---------- .../AttachmentPicker/{index.js => index.tsx} | 34 +-- .../launchCamera.android.ts} | 7 +- .../launchCamera.ios.ts} | 0 .../launchCamera.ts} | 2 +- .../AttachmentPicker/launchCamera/types.ts | 47 +++++ ...{attachmentPickerPropTypes.js => types.ts} | 17 +- 7 files changed, 159 insertions(+), 146 deletions(-) rename src/components/AttachmentPicker/{index.native.js => index.native.tsx} (68%) rename src/components/AttachmentPicker/{index.js => index.tsx} (78%) rename src/components/AttachmentPicker/{launchCamera.android.js => launchCamera/launchCamera.android.ts} (88%) rename src/components/AttachmentPicker/{launchCamera.ios.js => launchCamera/launchCamera.ios.ts} (100%) rename src/components/AttachmentPicker/{launchCamera.js => launchCamera/launchCamera.ts} (66%) create mode 100644 src/components/AttachmentPicker/launchCamera/types.ts rename src/components/AttachmentPicker/{attachmentPickerPropTypes.js => types.ts} (58%) diff --git a/src/components/AttachmentPicker/index.native.js b/src/components/AttachmentPicker/index.native.tsx similarity index 68% rename from src/components/AttachmentPicker/index.native.js rename to src/components/AttachmentPicker/index.native.tsx index 0387ee087127..dcc4296b71ac 100644 --- a/src/components/AttachmentPicker/index.native.js +++ b/src/components/AttachmentPicker/index.native.tsx @@ -1,12 +1,9 @@ -import Str from 'expensify-common/lib/str'; import lodashCompact from 'lodash/compact'; -import PropTypes from 'prop-types'; import React, {useCallback, useMemo, useRef, useState} from 'react'; -import {Alert, Image as RNImage, View} from 'react-native'; +import {Alert, View} from 'react-native'; import RNFetchBlob from 'react-native-blob-util'; import RNDocumentPicker from 'react-native-document-picker'; import {launchImageLibrary} from 'react-native-image-picker'; -import _ from 'underscore'; import * as Expensicons from '@components/Icon/Expensicons'; import MenuItem from '@components/MenuItem'; import Popover from '@components/Popover'; @@ -17,19 +14,32 @@ import useThemeStyles from '@hooks/useThemeStyles'; import useWindowDimensions from '@hooks/useWindowDimensions'; import * as FileUtils from '@libs/fileDownload/FileUtils'; import CONST from '@src/CONST'; -import {defaultProps as baseDefaultProps, propTypes as basePropTypes} from './attachmentPickerPropTypes'; -import launchCamera from './launchCamera'; - -const propTypes = { - ...basePropTypes, - +import type {TranslationPaths} from '@src/languages/types'; +import type IconAsset from '@src/types/utils/IconAsset'; +import type {DocumentPickerOptions, DocumentPickerResponse} from 'react-native-document-picker' +import type {SupportedPlatforms} from 'react-native-document-picker/lib/typescript/fileTypes'; +import type {Asset, Callback, CameraOptions, ImagePickerResponse} from 'react-native-image-picker'; +import launchCamera from './launchCamera/launchCamera'; +import type BaseAttachmentPickerProps from './types'; + +type AttachmentPickerProps = BaseAttachmentPickerProps & { /** If this value is true, then we exclude Camera option. */ - shouldHideCameraOption: PropTypes.bool, + shouldHideCameraOption?: boolean; }; -const defaultProps = { - ...baseDefaultProps, - shouldHideCameraOption: false, +type Item = { + icon: IconAsset; + textTranslationKey: string; + pickAttachment: () => Promise; +}; + +type FileResult = { + name: string; + type: string; + width: number | undefined; + height: number | undefined; + uri: string; + size: number | null; }; /** @@ -45,10 +55,8 @@ const imagePickerOptions = { /** * Return imagePickerOptions based on the type - * @param {String} type - * @returns {Object} */ -const getImagePickerOptions = (type) => { +const getImagePickerOptions = (type: string): CameraOptions => { // mediaType property is one of the ImagePicker configuration to restrict types' const mediaType = type === CONST.ATTACHMENT_PICKER_TYPE.IMAGE ? 'photo' : 'mixed'; return { @@ -58,40 +66,26 @@ const getImagePickerOptions = (type) => { }; /** - * Return documentPickerOptions based on the type - * @param {String} type - * @returns {Object} + * See https://github.com/rnmods/react-native-document-picker#options for DocumentPicker configuration options */ - -const getDocumentPickerOptions = (type) => { - if (type === CONST.ATTACHMENT_PICKER_TYPE.IMAGE) { - return { - type: [RNDocumentPicker.types.images], - copyTo: 'cachesDirectory', - }; - } - return { - type: [RNDocumentPicker.types.allFiles], - copyTo: 'cachesDirectory', - }; -}; +const documentPickerOptions = { + type: [RNDocumentPicker.types.allFiles], + copyTo: 'cachesDirectory', +} satisfies DocumentPickerOptions; /** * The data returned from `show` is different on web and mobile, so use this function to ensure the data we * send to the xhr will be handled properly. - * - * @param {Object} fileData - * @return {Promise} */ -const getDataForUpload = (fileData) => { - const fileName = fileData.fileName || fileData.name || 'chat_attachment'; - const fileResult = { +const getDataForUpload = (fileData: Asset & DocumentPickerResponse): Promise => { + const fileName = fileData.fileName ?? fileData.name ?? 'chat_attachment'; + const fileResult: FileResult = { name: FileUtils.cleanFileName(fileName), type: fileData.type, width: fileData.width, height: fileData.height, - uri: fileData.fileCopyUri || fileData.uri, - size: fileData.fileSize || fileData.size, + uri: fileData.fileCopyUri ?? fileData.uri, + size: fileData.fileSize ?? fileData.size, }; if (fileResult.size) { @@ -109,16 +103,15 @@ const getDataForUpload = (fileData) => { * returns a "show attachment picker" method that takes * a callback. This is the ios/android implementation * opening a modal with attachment options - * @param {propTypes} props - * @returns {JSX.Element} */ -function AttachmentPicker({type, children, shouldHideCameraOption}) { +function AttachmentPicker({type = CONST.ATTACHMENT_PICKER_TYPE.FILE, children, shouldHideCameraOption = false}: AttachmentPickerProps) { const styles = useThemeStyles(); const [isVisible, setIsVisible] = useState(false); - const completeAttachmentSelection = useRef(); - const onModalHide = useRef(); - const onCanceled = useRef(); + const completeAttachmentSelection = useRef<(data: FileResult) => void>(() => {}); + const onModalHide = useRef<() => void>(() => {}); + const onCanceled = useRef<() => void>(() => {}); + const popoverRef = useRef(null); const {translate} = useLocalize(); const {isSmallScreenWidth} = useWindowDimensions(); @@ -126,20 +119,19 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { /** * A generic handling when we don't know the exact reason for an error */ - const showGeneralAlert = useCallback(() => { - Alert.alert(translate('attachmentPicker.attachmentError'), translate('attachmentPicker.errorWhileSelectingAttachment')); + const showGeneralAlert = useCallback((message = '') => { + Alert.alert(translate('attachmentPicker.attachmentError'), `${message !== '' ? message : translate('attachmentPicker.errorWhileSelectingAttachment')}`); }, [translate]); /** * Common image picker handling * * @param {function} imagePickerFunc - RNImagePicker.launchCamera or RNImagePicker.launchImageLibrary - * @returns {Promise} */ const showImagePicker = useCallback( - (imagePickerFunc) => + (imagePickerFunc: (options: CameraOptions, callback: Callback) => Promise): Promise => new Promise((resolve, reject) => { - imagePickerFunc(getImagePickerOptions(type), (response) => { + imagePickerFunc(getImagePickerOptions(type), (response: ImagePickerResponse) => { if (response.didCancel) { // When the user cancelled resolve with no attachment return resolve(); @@ -166,11 +158,11 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { /** * Launch the DocumentPicker. Results are in the same format as ImagePicker * - * @returns {Promise} + * @returns {Promise} */ const showDocumentPicker = useCallback( - () => - RNDocumentPicker.pick(getDocumentPickerOptions(type)).catch((error) => { + (): Promise => + RNDocumentPicker.pick(documentPickerOptions).catch((error) => { if (RNDocumentPicker.isCancel(error)) { return; } @@ -178,10 +170,10 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { showGeneralAlert(error.message); throw error; }), - [showGeneralAlert, type], + [showGeneralAlert], ); - const menuItemData = useMemo(() => { + const menuItemData: Item[] = useMemo(() => { const data = lodashCompact([ !shouldHideCameraOption && { icon: Expensicons.Camera, @@ -193,7 +185,7 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { textTranslationKey: 'attachmentPicker.chooseFromGallery', pickAttachment: () => showImagePicker(launchImageLibrary), }, - { + type !== CONST.ATTACHMENT_PICKER_TYPE.IMAGE && { icon: Expensicons.Paperclip, textTranslationKey: 'attachmentPicker.chooseDocument', pickAttachment: showDocumentPicker, @@ -201,7 +193,7 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { ]); return data; - }, [showDocumentPicker, showImagePicker, shouldHideCameraOption]); + }, [showDocumentPicker, showImagePicker, type, shouldHideCameraOption]); const [focusedIndex, setFocusedIndex] = useArrowKeyFocusManager({initialFocusedIndex: -1, maxIndex: menuItemData.length - 1, isActive: isVisible}); @@ -215,10 +207,10 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { /** * Opens the attachment modal * - * @param {function} onPickedHandler A callback that will be called with the selected attachment - * @param {function} onCanceledHandler A callback that will be called without a selected attachment + * @param onPickedHandler A callback that will be called with the selected attachment + * @param onCanceledHandler A callback that will be called without a selected attachment */ - const open = (onPickedHandler, onCanceledHandler = () => {}) => { + const open = (onPickedHandler: () => void, onCanceledHandler: () => void = () => {}) => { completeAttachmentSelection.current = onPickedHandler; onCanceled.current = onCanceledHandler; setIsVisible(true); @@ -232,15 +224,23 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { }; /** - * @param {Object} fileData - * @returns {Promise} + * Handles the image/document picker result and + * sends the selected attachment to the caller (parent component) */ - const validateAndCompleteAttachmentSelection = useCallback( - (fileData) => { + const pickAttachment = useCallback( + (attachments: Array = []): Promise => { + if (attachments.length === 0) { + onCanceled.current(); + return Promise.resolve(); + } + + const fileData = attachments[0]; + if (fileData.width === -1 || fileData.height === -1) { showImageCorruptionAlert(); return Promise.resolve(); } + return getDataForUpload(fileData) .then((result) => { completeAttachmentSelection.current(result); @@ -253,33 +253,6 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { [showGeneralAlert, showImageCorruptionAlert], ); - /** - * Handles the image/document picker result and - * sends the selected attachment to the caller (parent component) - * - * @param {Array} attachments - * @returns {Promise} - */ - const pickAttachment = useCallback( - (attachments = []) => { - if (attachments.length === 0) { - onCanceled.current(); - return Promise.resolve(); - } - const fileData = _.first(attachments); - if (Str.isImage(fileData.fileName || fileData.name)) { - RNImage.getSize(fileData.fileCopyUri || fileData.uri, (width, height) => { - fileData.width = width; - fileData.height = height; - return validateAndCompleteAttachmentSelection(fileData); - }); - } else { - return validateAndCompleteAttachmentSelection(fileData); - } - }, - [validateAndCompleteAttachmentSelection], - ); - /** * Setup native attachment selection to start after this popover closes * @@ -287,24 +260,24 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { * @param {Function} item.pickAttachment */ const selectItem = useCallback( - (item) => { + (item: Item) => { /* setTimeout delays execution to the frame after the modal closes * without this on iOS closing the modal closes the gallery/camera as well */ - onModalHide.current = () => - setTimeout( - () => - item - .pickAttachment() - .then(pickAttachment) - .catch(console.error) - .finally(() => delete onModalHide.current), - 200, - ); - + onModalHide.current = () => { + setTimeout(() => { + item + .pickAttachment() + .then(pickAttachment) + .catch(console.error) + .finally(() => delete onModalHide.current !== undefined); + }, 200); + }; + close(); }, [pickAttachment], ); + useKeyboardShortcut( CONST.KEYBOARD_SHORTCUTS.ENTER, @@ -322,10 +295,8 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { /** * Call the `children` renderProp with the interface defined in propTypes - * - * @returns {React.ReactNode} */ - const renderChildren = () => + const renderChildren = (): React.ReactNode => children({ openPicker: ({onPicked, onCanceled: newOnCanceled}) => open(onPicked, newOnCanceled), }); @@ -338,15 +309,16 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { onCanceled.current(); }} isVisible={isVisible} - anchorPosition={styles.createMenuPosition} + anchorRef={popoverRef} + // anchorPosition={styles.createMenuPosition} onModalHide={onModalHide.current} > - {_.map(menuItemData, (item, menuIndex) => ( + {menuItemData.map((item, menuIndex) => ( selectItem(item)} focused={focusedIndex === menuIndex} /> @@ -358,8 +330,6 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { ); } -AttachmentPicker.propTypes = propTypes; -AttachmentPicker.defaultProps = defaultProps; AttachmentPicker.displayName = 'AttachmentPicker'; -export default AttachmentPicker; +export default AttachmentPicker; \ No newline at end of file diff --git a/src/components/AttachmentPicker/index.js b/src/components/AttachmentPicker/index.tsx similarity index 78% rename from src/components/AttachmentPicker/index.js rename to src/components/AttachmentPicker/index.tsx index 24024eae6515..9a372a9d4a48 100644 --- a/src/components/AttachmentPicker/index.js +++ b/src/components/AttachmentPicker/index.tsx @@ -1,14 +1,12 @@ import React, {useRef} from 'react'; import Visibility from '@libs/Visibility'; import CONST from '@src/CONST'; -import {defaultProps, propTypes} from './attachmentPickerPropTypes'; +import type AttachmentPickerProps from './types'; /** * Returns acceptable FileTypes based on ATTACHMENT_PICKER_TYPE - * @param {String} type - * @returns {String|undefined} Picker will accept all file types when its undefined */ -function getAcceptableFileTypes(type) { +function getAcceptableFileTypes(type: string): string | undefined { if (type !== CONST.ATTACHMENT_PICKER_TYPE.IMAGE) { return; } @@ -22,13 +20,11 @@ function getAcceptableFileTypes(type) { * a callback. This is the web/mWeb/desktop version since * on a Browser we must append a hidden input to the DOM * and listen to onChange event. - * @param {propTypes} props - * @returns {JSX.Element} */ -function AttachmentPicker(props) { - const fileInput = useRef(); - const onPicked = useRef(); - const onCanceled = useRef(() => {}); +function AttachmentPicker({children, type = CONST.ATTACHMENT_PICKER_TYPE.FILE}: AttachmentPickerProps): React.JSX.Element { + const fileInput = useRef(null); + const onPicked = useRef<(file: File) => void>(() => {}); + const onCanceled = useRef<() => void>(() => {}); return ( <> @@ -37,6 +33,10 @@ function AttachmentPicker(props) { type="file" ref={fileInput} onChange={(e) => { + if (!e.target.files) { + return; + } + const file = e.target.files[0]; if (file) { @@ -45,7 +45,9 @@ function AttachmentPicker(props) { } // Cleanup after selecting a file to start from a fresh state - fileInput.current.value = null; + if (fileInput.current) { + fileInput.current.value = ''; + } }} // We are stopping the event propagation because triggering the `click()` on the hidden input // causes the event to unexpectedly bubble up to anything wrapping this component e.g. Pressable @@ -72,12 +74,12 @@ function AttachmentPicker(props) { {once: true}, ); }} - accept={getAcceptableFileTypes(props.type)} + accept={getAcceptableFileTypes(type)} /> - {props.children({ + {children({ openPicker: ({onPicked: newOnPicked, onCanceled: newOnCanceled = () => {}}) => { onPicked.current = newOnPicked; - fileInput.current.click(); + fileInput.current?.click(); onCanceled.current = newOnCanceled; }, })} @@ -85,6 +87,4 @@ function AttachmentPicker(props) { ); } -AttachmentPicker.propTypes = propTypes; -AttachmentPicker.defaultProps = defaultProps; -export default AttachmentPicker; +export default AttachmentPicker; \ No newline at end of file diff --git a/src/components/AttachmentPicker/launchCamera.android.js b/src/components/AttachmentPicker/launchCamera/launchCamera.android.ts similarity index 88% rename from src/components/AttachmentPicker/launchCamera.android.js rename to src/components/AttachmentPicker/launchCamera/launchCamera.android.ts index b431c55e756d..cac42a874495 100644 --- a/src/components/AttachmentPicker/launchCamera.android.js +++ b/src/components/AttachmentPicker/launchCamera/launchCamera.android.ts @@ -1,15 +1,14 @@ import {PermissionsAndroid} from 'react-native'; import {launchCamera} from 'react-native-image-picker'; +import type {Callback, CameraOptions} from './types'; /** * Launching the camera for Android involves checking for permissions * And only then starting the camera * If the user deny permission the callback will be called with an error response * in the same format as the error returned by react-native-image-picker - * @param {CameraOptions} options - * @param {function} callback - callback called with the result */ -export default function launchCameraAndroid(options, callback) { +export default function launchCameraAndroid(options: CameraOptions, callback: Callback) { // Checks current camera permissions and prompts the user in case they aren't granted PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA) .then((permission) => { @@ -29,4 +28,4 @@ export default function launchCameraAndroid(options, callback) { errorCode: error.errorCode || 'others', }); }); -} +} \ No newline at end of file diff --git a/src/components/AttachmentPicker/launchCamera.ios.js b/src/components/AttachmentPicker/launchCamera/launchCamera.ios.ts similarity index 100% rename from src/components/AttachmentPicker/launchCamera.ios.js rename to src/components/AttachmentPicker/launchCamera/launchCamera.ios.ts diff --git a/src/components/AttachmentPicker/launchCamera.js b/src/components/AttachmentPicker/launchCamera/launchCamera.ts similarity index 66% rename from src/components/AttachmentPicker/launchCamera.js rename to src/components/AttachmentPicker/launchCamera/launchCamera.ts index dc1f921086de..d272e629f98f 100644 --- a/src/components/AttachmentPicker/launchCamera.js +++ b/src/components/AttachmentPicker/launchCamera/launchCamera.ts @@ -1,3 +1,3 @@ import {launchCamera} from 'react-native-image-picker'; -export default launchCamera; +export default launchCamera; \ No newline at end of file diff --git a/src/components/AttachmentPicker/launchCamera/types.ts b/src/components/AttachmentPicker/launchCamera/types.ts new file mode 100644 index 000000000000..c7f0481c7773 --- /dev/null +++ b/src/components/AttachmentPicker/launchCamera/types.ts @@ -0,0 +1,47 @@ +type Callback = (response: ImagePickerResponse) => void; +type OptionsCommon = { + mediaType: MediaType; + maxWidth?: number; + maxHeight?: number; + quality?: PhotoQuality; + videoQuality?: AndroidVideoOptions | IOSVideoOptions; + includeBase64?: boolean; + includeExtra?: boolean; + presentationStyle?: 'currentContext' | 'fullScreen' | 'pageSheet' | 'formSheet' | 'popover' | 'overFullScreen' | 'overCurrentContext'; +}; +type ImageLibraryOptions = OptionsCommon & { + selectionLimit?: number; +}; +type CameraOptions = OptionsCommon & { + durationLimit?: number; + saveToPhotos?: boolean; + cameraType?: CameraType; +}; +type Asset = { + base64?: string; + uri?: string; + width?: number; + height?: number; + fileSize?: number; + type?: string; + fileName?: string; + duration?: number; + bitrate?: number; + timestamp?: string; + id?: string; +}; +type ImagePickerResponse = { + didCancel?: boolean; + errorCode?: ErrorCode; + errorMessage?: string; + assets?: Asset[]; +}; +type PhotoQuality = 0 | 0.1 | 0.2 | 0.3 | 0.4 | 0.5 | 0.6 | 0.7 | 0.8 | 0.9 | 1; +type CameraType = 'back' | 'front'; +type MediaType = 'photo' | 'video' | 'mixed'; +type AndroidVideoOptions = 'low' | 'high'; +type IOSVideoOptions = 'low' | 'medium' | 'high'; +type ErrorCode = 'camera_unavailable' | 'permission' | 'others'; +type ErrorLaunchCamera = Error & ErrorCode + +export type {CameraOptions, Callback, ErrorCode, ImagePickerResponse, Asset, ImageLibraryOptions}; \ No newline at end of file diff --git a/src/components/AttachmentPicker/attachmentPickerPropTypes.js b/src/components/AttachmentPicker/types.ts similarity index 58% rename from src/components/AttachmentPicker/attachmentPickerPropTypes.js rename to src/components/AttachmentPicker/types.ts index a3a346f5ea27..19b98d85f691 100644 --- a/src/components/AttachmentPicker/attachmentPickerPropTypes.js +++ b/src/components/AttachmentPicker/types.ts @@ -1,7 +1,8 @@ -import PropTypes from 'prop-types'; -import CONST from '@src/CONST'; +import type {ReactNode} from 'react'; +import type {ValueOf} from 'type-fest'; +import type CONST from '@src/CONST'; -const propTypes = { +type AttachmentPickerProps = { /** * A renderProp with the following interface * @@ -20,14 +21,10 @@ const propTypes = { * )} * * */ - children: PropTypes.func.isRequired, + children: (openPicker: ({onPicked, onCanceled}: {onPicked: (file: File) => void; onCanceled?: () => void}) => void) => ReactNode; /** The types of files that can be selected with this picker. */ - type: PropTypes.oneOf([CONST.ATTACHMENT_PICKER_TYPE.FILE, CONST.ATTACHMENT_PICKER_TYPE.IMAGE]), + type?: ValueOf; }; -const defaultProps = { - type: CONST.ATTACHMENT_PICKER_TYPE.FILE, -}; - -export {propTypes, defaultProps}; +export default AttachmentPickerProps; \ No newline at end of file From 049f95069ee50330c678076d1b4deadcc9aa389c Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Mon, 4 Mar 2024 17:08:58 +0700 Subject: [PATCH 002/188] fix: type in launch camera --- .../AttachmentPicker/index.native.tsx | 23 ++++++++++--------- src/components/AttachmentPicker/index.tsx | 2 +- .../launchCamera/launchCamera.android.ts | 7 +++--- .../launchCamera/launchCamera.ios.ts | 11 ++++----- .../launchCamera/launchCamera.ts | 2 +- .../AttachmentPicker/launchCamera/types.ts | 11 +++++++-- src/components/AttachmentPicker/types.ts | 2 +- 7 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/components/AttachmentPicker/index.native.tsx b/src/components/AttachmentPicker/index.native.tsx index dcc4296b71ac..3aaaac1083a0 100644 --- a/src/components/AttachmentPicker/index.native.tsx +++ b/src/components/AttachmentPicker/index.native.tsx @@ -3,7 +3,10 @@ import React, {useCallback, useMemo, useRef, useState} from 'react'; import {Alert, View} from 'react-native'; import RNFetchBlob from 'react-native-blob-util'; import RNDocumentPicker from 'react-native-document-picker'; +import type {DocumentPickerOptions, DocumentPickerResponse} from 'react-native-document-picker'; +import type {SupportedPlatforms} from 'react-native-document-picker/lib/typescript/fileTypes'; import {launchImageLibrary} from 'react-native-image-picker'; +import type {Asset, Callback, CameraOptions, ImagePickerResponse} from 'react-native-image-picker'; import * as Expensicons from '@components/Icon/Expensicons'; import MenuItem from '@components/MenuItem'; import Popover from '@components/Popover'; @@ -16,9 +19,6 @@ import * as FileUtils from '@libs/fileDownload/FileUtils'; import CONST from '@src/CONST'; import type {TranslationPaths} from '@src/languages/types'; import type IconAsset from '@src/types/utils/IconAsset'; -import type {DocumentPickerOptions, DocumentPickerResponse} from 'react-native-document-picker' -import type {SupportedPlatforms} from 'react-native-document-picker/lib/typescript/fileTypes'; -import type {Asset, Callback, CameraOptions, ImagePickerResponse} from 'react-native-image-picker'; import launchCamera from './launchCamera/launchCamera'; import type BaseAttachmentPickerProps from './types'; @@ -119,9 +119,12 @@ function AttachmentPicker({type = CONST.ATTACHMENT_PICKER_TYPE.FILE, children, s /** * A generic handling when we don't know the exact reason for an error */ - const showGeneralAlert = useCallback((message = '') => { - Alert.alert(translate('attachmentPicker.attachmentError'), `${message !== '' ? message : translate('attachmentPicker.errorWhileSelectingAttachment')}`); - }, [translate]); + const showGeneralAlert = useCallback( + (message = '') => { + Alert.alert(translate('attachmentPicker.attachmentError'), `${message !== '' ? message : translate('attachmentPicker.errorWhileSelectingAttachment')}`); + }, + [translate], + ); /** * Common image picker handling @@ -265,19 +268,17 @@ function AttachmentPicker({type = CONST.ATTACHMENT_PICKER_TYPE.FILE, children, s * without this on iOS closing the modal closes the gallery/camera as well */ onModalHide.current = () => { setTimeout(() => { - item - .pickAttachment() + item.pickAttachment() .then(pickAttachment) .catch(console.error) .finally(() => delete onModalHide.current !== undefined); }, 200); }; - + close(); }, [pickAttachment], ); - useKeyboardShortcut( CONST.KEYBOARD_SHORTCUTS.ENTER, @@ -332,4 +333,4 @@ function AttachmentPicker({type = CONST.ATTACHMENT_PICKER_TYPE.FILE, children, s AttachmentPicker.displayName = 'AttachmentPicker'; -export default AttachmentPicker; \ No newline at end of file +export default AttachmentPicker; diff --git a/src/components/AttachmentPicker/index.tsx b/src/components/AttachmentPicker/index.tsx index 9a372a9d4a48..e8a23e29f114 100644 --- a/src/components/AttachmentPicker/index.tsx +++ b/src/components/AttachmentPicker/index.tsx @@ -87,4 +87,4 @@ function AttachmentPicker({children, type = CONST.ATTACHMENT_PICKER_TYPE.FILE}: ); } -export default AttachmentPicker; \ No newline at end of file +export default AttachmentPicker; diff --git a/src/components/AttachmentPicker/launchCamera/launchCamera.android.ts b/src/components/AttachmentPicker/launchCamera/launchCamera.android.ts index cac42a874495..135b5dfd80e6 100644 --- a/src/components/AttachmentPicker/launchCamera/launchCamera.android.ts +++ b/src/components/AttachmentPicker/launchCamera/launchCamera.android.ts @@ -1,6 +1,7 @@ import {PermissionsAndroid} from 'react-native'; import {launchCamera} from 'react-native-image-picker'; import type {Callback, CameraOptions} from './types'; +import {ErrorLaunchCamera} from './types'; /** * Launching the camera for Android involves checking for permissions @@ -13,9 +14,7 @@ export default function launchCameraAndroid(options: CameraOptions, callback: Ca PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA) .then((permission) => { if (permission !== PermissionsAndroid.RESULTS.GRANTED) { - const error = new Error('User did not grant permissions'); - error.errorCode = 'permission'; - throw error; + throw new ErrorLaunchCamera('User did not grant permissions', 'permission'); } launchCamera(options, callback); @@ -28,4 +27,4 @@ export default function launchCameraAndroid(options: CameraOptions, callback: Ca errorCode: error.errorCode || 'others', }); }); -} \ No newline at end of file +} diff --git a/src/components/AttachmentPicker/launchCamera/launchCamera.ios.ts b/src/components/AttachmentPicker/launchCamera/launchCamera.ios.ts index d6e3518d7188..cffb00f39e4a 100644 --- a/src/components/AttachmentPicker/launchCamera/launchCamera.ios.ts +++ b/src/components/AttachmentPicker/launchCamera/launchCamera.ios.ts @@ -1,24 +1,21 @@ import {launchCamera} from 'react-native-image-picker'; import {PERMISSIONS, request, RESULTS} from 'react-native-permissions'; +import type {Callback, CameraOptions} from './types'; +import {ErrorLaunchCamera} from './types'; /** * Launching the camera for iOS involves checking for permissions * And only then starting the camera * If the user deny permission the callback will be called with an error response * in the same format as the error returned by react-native-image-picker - * @param {CameraOptions} options - * @param {function} callback - callback called with the result */ -export default function launchCameraIOS(options, callback) { +export default function launchCameraIOS(options: CameraOptions, callback: Callback) { // Checks current camera permissions and prompts the user in case they aren't granted request(PERMISSIONS.IOS.CAMERA) .then((permission) => { if (permission !== RESULTS.GRANTED) { - const error = new Error('User did not grant permissions'); - error.errorCode = 'permission'; - throw error; + throw new ErrorLaunchCamera('User did not grant permissions', 'permission'); } - launchCamera(options, callback); }) .catch((error) => { diff --git a/src/components/AttachmentPicker/launchCamera/launchCamera.ts b/src/components/AttachmentPicker/launchCamera/launchCamera.ts index d272e629f98f..dc1f921086de 100644 --- a/src/components/AttachmentPicker/launchCamera/launchCamera.ts +++ b/src/components/AttachmentPicker/launchCamera/launchCamera.ts @@ -1,3 +1,3 @@ import {launchCamera} from 'react-native-image-picker'; -export default launchCamera; \ No newline at end of file +export default launchCamera; diff --git a/src/components/AttachmentPicker/launchCamera/types.ts b/src/components/AttachmentPicker/launchCamera/types.ts index c7f0481c7773..1a3aae3f0ad7 100644 --- a/src/components/AttachmentPicker/launchCamera/types.ts +++ b/src/components/AttachmentPicker/launchCamera/types.ts @@ -42,6 +42,13 @@ type MediaType = 'photo' | 'video' | 'mixed'; type AndroidVideoOptions = 'low' | 'high'; type IOSVideoOptions = 'low' | 'medium' | 'high'; type ErrorCode = 'camera_unavailable' | 'permission' | 'others'; -type ErrorLaunchCamera = Error & ErrorCode +class ErrorLaunchCamera extends Error { + errorCode: ErrorCode; -export type {CameraOptions, Callback, ErrorCode, ImagePickerResponse, Asset, ImageLibraryOptions}; \ No newline at end of file + constructor(message: string, errorCode: ErrorCode) { + super(message); + this.errorCode = errorCode; + } +} +export {ErrorLaunchCamera}; +export type {CameraOptions, Callback, ErrorCode, ImagePickerResponse, Asset, ImageLibraryOptions}; diff --git a/src/components/AttachmentPicker/types.ts b/src/components/AttachmentPicker/types.ts index 19b98d85f691..b9974f4082b2 100644 --- a/src/components/AttachmentPicker/types.ts +++ b/src/components/AttachmentPicker/types.ts @@ -27,4 +27,4 @@ type AttachmentPickerProps = { type?: ValueOf; }; -export default AttachmentPickerProps; \ No newline at end of file +export default AttachmentPickerProps; From d34c62bd1aa9ab68b95f58bf37d8268111d0b72a Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Tue, 5 Mar 2024 10:21:51 +0700 Subject: [PATCH 003/188] fix type select item function --- src/components/AttachmentPicker/index.native.tsx | 9 ++++----- src/components/AttachmentPicker/index.tsx | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/components/AttachmentPicker/index.native.tsx b/src/components/AttachmentPicker/index.native.tsx index 3aaaac1083a0..e144a210fa7c 100644 --- a/src/components/AttachmentPicker/index.native.tsx +++ b/src/components/AttachmentPicker/index.native.tsx @@ -109,7 +109,7 @@ function AttachmentPicker({type = CONST.ATTACHMENT_PICKER_TYPE.FILE, children, s const [isVisible, setIsVisible] = useState(false); const completeAttachmentSelection = useRef<(data: FileResult) => void>(() => {}); - const onModalHide = useRef<() => void>(() => {}); + const onModalHide = useRef<() => void>(); const onCanceled = useRef<() => void>(() => {}); const popoverRef = useRef(null); @@ -266,15 +266,14 @@ function AttachmentPicker({type = CONST.ATTACHMENT_PICKER_TYPE.FILE, children, s (item: Item) => { /* setTimeout delays execution to the frame after the modal closes * without this on iOS closing the modal closes the gallery/camera as well */ - onModalHide.current = () => { + onModalHide.current = () => { setTimeout(() => { item.pickAttachment() - .then(pickAttachment) + .then((result) => pickAttachment(result as Array)) .catch(console.error) - .finally(() => delete onModalHide.current !== undefined); + .finally(() => delete onModalHide.current); }, 200); }; - close(); }, [pickAttachment], diff --git a/src/components/AttachmentPicker/index.tsx b/src/components/AttachmentPicker/index.tsx index e8a23e29f114..9a372a9d4a48 100644 --- a/src/components/AttachmentPicker/index.tsx +++ b/src/components/AttachmentPicker/index.tsx @@ -87,4 +87,4 @@ function AttachmentPicker({children, type = CONST.ATTACHMENT_PICKER_TYPE.FILE}: ); } -export default AttachmentPicker; +export default AttachmentPicker; \ No newline at end of file From e3c79db7b98918f70b23796604aa25afcb02af1d Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Tue, 5 Mar 2024 16:23:56 +0000 Subject: [PATCH 004/188] [TS migration] Migrate Storybook files --- .storybook/{main.js => main.ts} | 21 ++++++- .storybook/{manager.js => manager.ts} | 0 .storybook/{preview.js => preview.tsx} | 28 +++++---- .storybook/{theme.js => theme.ts} | 5 +- .storybook/webpack.config.js | 54 ------------------ .storybook/webpack.config.ts | 79 ++++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 67 deletions(-) rename .storybook/{main.js => main.ts} (59%) rename .storybook/{manager.js => manager.ts} (100%) rename .storybook/{preview.js => preview.tsx} (51%) rename .storybook/{theme.js => theme.ts} (86%) delete mode 100644 .storybook/webpack.config.js create mode 100644 .storybook/webpack.config.ts diff --git a/.storybook/main.js b/.storybook/main.ts similarity index 59% rename from .storybook/main.js rename to .storybook/main.ts index 7d063fd6ffe1..0234f18ff488 100644 --- a/.storybook/main.js +++ b/.storybook/main.ts @@ -1,12 +1,29 @@ -module.exports = { +type Dir = { + from: string; + to: string; +}; + +type Main = { + stories: string[]; + addons: string[]; + staticDirs: Array; + core: { + builder: string; + }; + managerHead: (head: string) => string; +}; + +const main: Main = { stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'], addons: ['@storybook/addon-essentials', '@storybook/addon-a11y', '@storybook/addon-react-native-web'], staticDirs: ['./public', {from: '../assets/css', to: 'css'}, {from: '../assets/fonts/web', to: 'fonts'}], core: { builder: 'webpack5', }, - managerHead: (head) => ` + managerHead: (head: string) => ` ${head} ${process.env.ENV === 'staging' ? '' : ''} `, }; + +export default main; diff --git a/.storybook/manager.js b/.storybook/manager.ts similarity index 100% rename from .storybook/manager.js rename to .storybook/manager.ts diff --git a/.storybook/preview.js b/.storybook/preview.tsx similarity index 51% rename from .storybook/preview.js rename to .storybook/preview.tsx index a89c720976c9..5ddb9d04d8ae 100644 --- a/.storybook/preview.js +++ b/.storybook/preview.tsx @@ -2,16 +2,24 @@ import {PortalProvider} from '@gorhom/portal'; import React from 'react'; import Onyx from 'react-native-onyx'; import {SafeAreaProvider} from 'react-native-safe-area-context'; -import ComposeProviders from '../src/components/ComposeProviders'; -import HTMLEngineProvider from '../src/components/HTMLEngineProvider'; -import {LocaleContextProvider} from '../src/components/LocaleContextProvider'; -import OnyxProvider from '../src/components/OnyxProvider'; -import {EnvironmentProvider} from '../src/components/withEnvironment'; -import {KeyboardStateProvider} from '../src/components/withKeyboardState'; -import {WindowDimensionsProvider} from '../src/components/withWindowDimensions'; -import ONYXKEYS from '../src/ONYXKEYS'; +import ComposeProviders from '@src/components/ComposeProviders'; +import HTMLEngineProvider from '@src/components/HTMLEngineProvider'; +import {LocaleContextProvider} from '@src/components/LocaleContextProvider'; +import OnyxProvider from '@src/components/OnyxProvider'; +import {EnvironmentProvider} from '@src/components/withEnvironment'; +import {KeyboardStateProvider} from '@src/components/withKeyboardState'; +import {WindowDimensionsProvider} from '@src/components/withWindowDimensions'; +import ONYXKEYS from '@src/ONYXKEYS'; import './fonts.css'; +type Parameter = { + controls: { + matchers: { + color: RegExp; + }; + }; +}; + Onyx.init({ keys: ONYXKEYS, initialKeyStates: { @@ -20,7 +28,7 @@ Onyx.init({ }); const decorators = [ - (Story) => ( + (Story: React.ElementType) => ( @@ -29,7 +37,7 @@ const decorators = [ ), ]; -const parameters = { +const parameters: Parameter = { controls: { matchers: { color: /(background|color)$/i, diff --git a/.storybook/theme.js b/.storybook/theme.ts similarity index 86% rename from .storybook/theme.js rename to .storybook/theme.ts index 08d8b584d580..f1ace5dd37c0 100644 --- a/.storybook/theme.js +++ b/.storybook/theme.ts @@ -1,7 +1,8 @@ +import type {ThemeVars} from '@storybook/theming'; import {create} from '@storybook/theming'; import colors from '../src/styles/theme/colors'; -export default create({ +const theme: ThemeVars = create({ brandTitle: 'New Expensify UI Docs', brandImage: 'logomark.svg', fontBase: 'ExpensifyNeue-Regular', @@ -21,3 +22,5 @@ export default create({ appBorderRadius: 8, inputBorderRadius: 8, }); + +export default theme; diff --git a/.storybook/webpack.config.js b/.storybook/webpack.config.js deleted file mode 100644 index 204f70344b18..000000000000 --- a/.storybook/webpack.config.js +++ /dev/null @@ -1,54 +0,0 @@ -/* eslint-disable no-underscore-dangle */ -/* eslint-disable no-param-reassign */ -const path = require('path'); -const dotenv = require('dotenv'); -const _ = require('underscore'); - -let envFile; -switch (process.env.ENV) { - case 'production': - envFile = '.env.production'; - break; - case 'staging': - envFile = '.env.staging'; - break; - default: - envFile = '.env'; -} - -const env = dotenv.config({path: path.resolve(__dirname, `../${envFile}`)}); -const custom = require('../config/webpack/webpack.common')({ - envFile, -}); - -module.exports = ({config}) => { - config.resolve.alias = { - 'react-native-config': 'react-web-config', - 'react-native$': 'react-native-web', - '@react-native-community/netinfo': path.resolve(__dirname, '../__mocks__/@react-native-community/netinfo.ts'), - '@react-navigation/native': path.resolve(__dirname, '../__mocks__/@react-navigation/native'), - - // Module alias support for storybook files, coping from `webpack.common.js` - ...custom.resolve.alias, - }; - - // Necessary to overwrite the values in the existing DefinePlugin hardcoded to the Config staging values - const definePluginIndex = _.findIndex(config.plugins, (plugin) => plugin.constructor.name === 'DefinePlugin'); - config.plugins[definePluginIndex].definitions.__REACT_WEB_CONFIG__ = JSON.stringify(env); - config.resolve.extensions = custom.resolve.extensions; - - const babelRulesIndex = _.findIndex(custom.module.rules, (rule) => rule.loader === 'babel-loader'); - const babelRule = custom.module.rules[babelRulesIndex]; - config.module.rules.push(babelRule); - - // Allows loading SVG - more context here https://github.com/storybookjs/storybook/issues/6188 - const fileLoaderRule = _.find(config.module.rules, (rule) => rule.test && rule.test.test('.svg')); - fileLoaderRule.exclude = /\.svg$/; - config.module.rules.push({ - test: /\.svg$/, - enforce: 'pre', - loader: require.resolve('@svgr/webpack'), - }); - - return config; -}; diff --git a/.storybook/webpack.config.ts b/.storybook/webpack.config.ts new file mode 100644 index 000000000000..0c66e184c18f --- /dev/null +++ b/.storybook/webpack.config.ts @@ -0,0 +1,79 @@ +/* eslint-disable @typescript-eslint/naming-convention */ + +/* eslint-disable no-underscore-dangle */ + +/* eslint-disable no-param-reassign */ +import dotenv from 'dotenv'; +import path from 'path'; +import {DefinePlugin} from 'webpack'; +import type {Configuration, RuleSetRule} from 'webpack'; + +type CustomWebpackConfig = { + resolve: { + alias: Record; + extensions: string[]; + }; + module: { + rules: RuleSetRule[]; + }; +}; + +let envFile; +switch (process.env.ENV) { + case 'production': + envFile = '.env.production'; + break; + case 'staging': + envFile = '.env.staging'; + break; + default: + envFile = '.env'; +} + +const env = dotenv.config({path: path.resolve(__dirname, `../${envFile}`)}); +const custom: CustomWebpackConfig = require('../config/webpack/webpack.common')({ + envFile, +}); + +module.exports = ({config}: {config: Configuration}) => { + if (config.resolve && config.plugins && config.module) { + config.resolve.alias = { + 'react-native-config': 'react-web-config', + 'react-native$': 'react-native-web', + '@react-native-community/netinfo': path.resolve(__dirname, '../__mocks__/@react-native-community/netinfo.ts'), + '@react-navigation/native': path.resolve(__dirname, '../__mocks__/@react-navigation/native'), + ...custom.resolve.alias, + }; + + // Necessary to overwrite the values in the existing DefinePlugin hardcoded to the Config staging values + const definePluginIndex = config.plugins.findIndex((plugin) => plugin instanceof DefinePlugin); + if (definePluginIndex !== -1 && config.plugins[definePluginIndex] instanceof DefinePlugin) { + const definePlugin = config.plugins[definePluginIndex] as DefinePlugin; + if (definePlugin.definitions) { + definePlugin.definitions.__REACT_WEB_CONFIG__ = JSON.stringify(env); + } + } + config.resolve.extensions = custom.resolve.extensions; + + const babelRulesIndex = custom.module.rules.findIndex((rule) => rule.loader === 'babel-loader'); + const babelRule = custom.module.rules[babelRulesIndex]; + if (babelRule) { + config.module.rules?.push(babelRule); + } + + const fileLoaderRule = config.module.rules?.find( + (rule): rule is RuleSetRule => + typeof rule !== 'boolean' && typeof rule !== 'string' && typeof rule !== 'number' && !!rule?.test && rule.test instanceof RegExp && rule.test.test('.svg'), + ); + if (fileLoaderRule?.exclude) { + fileLoaderRule.exclude = /\.svg$/; + } + config.module.rules?.push({ + test: /\.svg$/, + enforce: 'pre', + loader: require.resolve('@svgr/webpack'), + }); + } + + return config; +}; From cd2681e34826070baa28bafde8a7fd0eda10007e Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 6 Mar 2024 10:13:31 +0700 Subject: [PATCH 005/188] fix type --- src/components/AttachmentPicker/index.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/AttachmentPicker/index.tsx b/src/components/AttachmentPicker/index.tsx index 9a372a9d4a48..dc5ce45153a3 100644 --- a/src/components/AttachmentPicker/index.tsx +++ b/src/components/AttachmentPicker/index.tsx @@ -76,13 +76,13 @@ function AttachmentPicker({children, type = CONST.ATTACHMENT_PICKER_TYPE.FILE}: }} accept={getAcceptableFileTypes(type)} /> - {children({ - openPicker: ({onPicked: newOnPicked, onCanceled: newOnCanceled = () => {}}) => { + {children( + ({onPicked: newOnPicked, onCanceled: newOnCanceled = () => {}}) => { onPicked.current = newOnPicked; fileInput.current?.click(); onCanceled.current = newOnCanceled; }, - })} + )} ); } From 2dda80fcfc839d3823f07882bde897b88cf8c3c2 Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 6 Mar 2024 14:32:22 +0700 Subject: [PATCH 006/188] fix: type attachment --- src/components/AttachmentModal.tsx | 10 +++++----- .../AttachmentPicker/index.native.tsx | 20 ++++++------------- src/components/AttachmentPicker/index.tsx | 8 ++++---- src/components/AttachmentPicker/types.ts | 4 ++-- src/components/AvatarWithImagePicker.tsx | 18 +++++++---------- .../AttachmentPickerWithMenuItems.tsx | 1 - 6 files changed, 24 insertions(+), 37 deletions(-) diff --git a/src/components/AttachmentModal.tsx b/src/components/AttachmentModal.tsx index eed40d75387e..2a6ebd12f143 100755 --- a/src/components/AttachmentModal.tsx +++ b/src/components/AttachmentModal.tsx @@ -71,12 +71,12 @@ type Attachment = { }; type ImagePickerResponse = { - height: number; + height?: number; name: string; - size: number; + size?: number | null; type: string; uri: string; - width: number; + width?: number; }; type FileObject = File | ImagePickerResponse; @@ -292,14 +292,14 @@ function AttachmentModal({ }, [transaction, report]); const isValidFile = useCallback((fileObject: FileObject) => { - if (fileObject.size > CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE) { + if (fileObject.size && fileObject.size > CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE) { setIsAttachmentInvalid(true); setAttachmentInvalidReasonTitle('attachmentPicker.attachmentTooLarge'); setAttachmentInvalidReason('attachmentPicker.sizeExceeded'); return false; } - if (fileObject.size < CONST.API_ATTACHMENT_VALIDATIONS.MIN_SIZE) { + if (fileObject.size && fileObject.size < CONST.API_ATTACHMENT_VALIDATIONS.MIN_SIZE) { setIsAttachmentInvalid(true); setAttachmentInvalidReasonTitle('attachmentPicker.attachmentTooSmall'); setAttachmentInvalidReason('attachmentPicker.sizeNotMet'); diff --git a/src/components/AttachmentPicker/index.native.tsx b/src/components/AttachmentPicker/index.native.tsx index e144a210fa7c..7412c7382513 100644 --- a/src/components/AttachmentPicker/index.native.tsx +++ b/src/components/AttachmentPicker/index.native.tsx @@ -7,6 +7,7 @@ import type {DocumentPickerOptions, DocumentPickerResponse} from 'react-native-d import type {SupportedPlatforms} from 'react-native-document-picker/lib/typescript/fileTypes'; import {launchImageLibrary} from 'react-native-image-picker'; import type {Asset, Callback, CameraOptions, ImagePickerResponse} from 'react-native-image-picker'; +import type {FileObject} from '@components/AttachmentModal'; import * as Expensicons from '@components/Icon/Expensicons'; import MenuItem from '@components/MenuItem'; import Popover from '@components/Popover'; @@ -33,15 +34,6 @@ type Item = { pickAttachment: () => Promise; }; -type FileResult = { - name: string; - type: string; - width: number | undefined; - height: number | undefined; - uri: string; - size: number | null; -}; - /** * See https://github.com/react-native-image-picker/react-native-image-picker/#options * for ImagePicker configuration options @@ -77,9 +69,9 @@ const documentPickerOptions = { * The data returned from `show` is different on web and mobile, so use this function to ensure the data we * send to the xhr will be handled properly. */ -const getDataForUpload = (fileData: Asset & DocumentPickerResponse): Promise => { +const getDataForUpload = (fileData: Asset & DocumentPickerResponse): Promise => { const fileName = fileData.fileName ?? fileData.name ?? 'chat_attachment'; - const fileResult: FileResult = { + const fileResult: FileObject = { name: FileUtils.cleanFileName(fileName), type: fileData.type, width: fileData.width, @@ -108,7 +100,7 @@ function AttachmentPicker({type = CONST.ATTACHMENT_PICKER_TYPE.FILE, children, s const styles = useThemeStyles(); const [isVisible, setIsVisible] = useState(false); - const completeAttachmentSelection = useRef<(data: FileResult) => void>(() => {}); + const completeAttachmentSelection = useRef<(data: FileObject) => void>(() => {}); const onModalHide = useRef<() => void>(); const onCanceled = useRef<() => void>(() => {}); const popoverRef = useRef(null); @@ -213,7 +205,7 @@ function AttachmentPicker({type = CONST.ATTACHMENT_PICKER_TYPE.FILE, children, s * @param onPickedHandler A callback that will be called with the selected attachment * @param onCanceledHandler A callback that will be called without a selected attachment */ - const open = (onPickedHandler: () => void, onCanceledHandler: () => void = () => {}) => { + const open = (onPickedHandler: (file: FileObject) => void, onCanceledHandler: () => void = () => {}) => { completeAttachmentSelection.current = onPickedHandler; onCanceled.current = onCanceledHandler; setIsVisible(true); @@ -266,7 +258,7 @@ function AttachmentPicker({type = CONST.ATTACHMENT_PICKER_TYPE.FILE, children, s (item: Item) => { /* setTimeout delays execution to the frame after the modal closes * without this on iOS closing the modal closes the gallery/camera as well */ - onModalHide.current = () => { + onModalHide.current = () => { setTimeout(() => { item.pickAttachment() .then((result) => pickAttachment(result as Array)) diff --git a/src/components/AttachmentPicker/index.tsx b/src/components/AttachmentPicker/index.tsx index dc5ce45153a3..e8a23e29f114 100644 --- a/src/components/AttachmentPicker/index.tsx +++ b/src/components/AttachmentPicker/index.tsx @@ -76,15 +76,15 @@ function AttachmentPicker({children, type = CONST.ATTACHMENT_PICKER_TYPE.FILE}: }} accept={getAcceptableFileTypes(type)} /> - {children( - ({onPicked: newOnPicked, onCanceled: newOnCanceled = () => {}}) => { + {children({ + openPicker: ({onPicked: newOnPicked, onCanceled: newOnCanceled = () => {}}) => { onPicked.current = newOnPicked; fileInput.current?.click(); onCanceled.current = newOnCanceled; }, - )} + })} ); } -export default AttachmentPicker; \ No newline at end of file +export default AttachmentPicker; diff --git a/src/components/AttachmentPicker/types.ts b/src/components/AttachmentPicker/types.ts index b9974f4082b2..66ee91b33c24 100644 --- a/src/components/AttachmentPicker/types.ts +++ b/src/components/AttachmentPicker/types.ts @@ -1,5 +1,6 @@ import type {ReactNode} from 'react'; import type {ValueOf} from 'type-fest'; +import type {FileObject} from '@components/AttachmentModal'; import type CONST from '@src/CONST'; type AttachmentPickerProps = { @@ -21,8 +22,7 @@ type AttachmentPickerProps = { * )} * * */ - children: (openPicker: ({onPicked, onCanceled}: {onPicked: (file: File) => void; onCanceled?: () => void}) => void) => ReactNode; - + children: (props: {openPicker: (options: {onPicked: (file: FileObject) => void; onCanceled?: () => void}) => void}) => ReactNode; /** The types of files that can be selected with this picker. */ type?: ValueOf; }; diff --git a/src/components/AvatarWithImagePicker.tsx b/src/components/AvatarWithImagePicker.tsx index 5755c69641c8..6c8b4e65ae93 100644 --- a/src/components/AvatarWithImagePicker.tsx +++ b/src/components/AvatarWithImagePicker.tsx @@ -15,7 +15,7 @@ import CONST from '@src/CONST'; import type {TranslationPaths} from '@src/languages/types'; import type * as OnyxCommon from '@src/types/onyx/OnyxCommon'; import type IconAsset from '@src/types/utils/IconAsset'; -import AttachmentModal from './AttachmentModal'; +import AttachmentModal, {FileObject} from './AttachmentModal'; import AttachmentPicker from './AttachmentPicker'; import Avatar from './Avatar'; import AvatarCropModal from './AvatarCropModal/AvatarCropModal'; @@ -34,7 +34,7 @@ type ErrorData = { }; type OpenPickerParams = { - onPicked: (image: File) => void; + onPicked: (image: FileObject) => void; }; type OpenPicker = (args: OpenPickerParams) => void; @@ -174,7 +174,7 @@ function AvatarWithImagePicker({ /** * Check if the attachment extension is allowed. */ - const isValidExtension = (image: File): boolean => { + const isValidExtension = (image: FileObject): boolean => { const {fileExtension} = FileUtils.splitExtensionFromFileName(image?.name ?? ''); return CONST.AVATAR_ALLOWED_EXTENSIONS.some((extension) => extension === fileExtension.toLowerCase()); }; @@ -182,12 +182,12 @@ function AvatarWithImagePicker({ /** * Check if the attachment size is less than allowed size. */ - const isValidSize = (image: File): boolean => (image?.size ?? 0) < CONST.AVATAR_MAX_ATTACHMENT_SIZE; + const isValidSize = (image: FileObject): boolean => (image?.size ?? 0) < CONST.AVATAR_MAX_ATTACHMENT_SIZE; /** * Check if the attachment resolution matches constraints. */ - const isValidResolution = (image: File): Promise => + const isValidResolution = (image: FileObject): Promise => getImageResolution(image).then( ({height, width}) => height >= CONST.AVATAR_MIN_HEIGHT_PX && width >= CONST.AVATAR_MIN_WIDTH_PX && height <= CONST.AVATAR_MAX_HEIGHT_PX && width <= CONST.AVATAR_MAX_WIDTH_PX, ); @@ -195,7 +195,7 @@ function AvatarWithImagePicker({ /** * Validates if an image has a valid resolution and opens an avatar crop modal */ - const showAvatarCropModal = (image: File) => { + const showAvatarCropModal = (image: FileObject) => { if (!isValidExtension(image)) { setError('avatarWithImagePicker.notAllowedExtension', {allowedExtensions: CONST.AVATAR_ALLOWED_EXTENSIONS}); return; @@ -343,11 +343,7 @@ function AvatarWithImagePicker({ maybeIcon={isUsingDefaultAvatar} > {({show}) => ( - - {/* @ts-expect-error TODO: Remove this once AttachmentPicker (https://github.com/Expensify/App/issues/25134) is migrated to TypeScript. */} + {({openPicker}) => { const menuItems = createMenuItems(openPicker); diff --git a/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.tsx b/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.tsx index 68c7f0883683..b8b4693ac66e 100644 --- a/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.tsx +++ b/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.tsx @@ -185,7 +185,6 @@ function AttachmentPickerWithMenuItems({ return ( - {/* @ts-expect-error TODO: Remove this once AttachmentPicker (https://github.com/Expensify/App/issues/25134) is migrated to TypeScript. */} {({openPicker}) => { const triggerAttachmentPicker = () => { onTriggerAttachmentPicker(); From 9ac6e89085815d5c3f3064262fc3c37dde8206de Mon Sep 17 00:00:00 2001 From: dukenv0307 Date: Wed, 6 Mar 2024 14:38:28 +0700 Subject: [PATCH 007/188] fix lint --- src/components/AvatarWithImagePicker.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/components/AvatarWithImagePicker.tsx b/src/components/AvatarWithImagePicker.tsx index 6c8b4e65ae93..0e1a8d0e17a6 100644 --- a/src/components/AvatarWithImagePicker.tsx +++ b/src/components/AvatarWithImagePicker.tsx @@ -15,7 +15,8 @@ import CONST from '@src/CONST'; import type {TranslationPaths} from '@src/languages/types'; import type * as OnyxCommon from '@src/types/onyx/OnyxCommon'; import type IconAsset from '@src/types/utils/IconAsset'; -import AttachmentModal, {FileObject} from './AttachmentModal'; +import AttachmentModal from './AttachmentModal'; +import type {FileObject} from './AttachmentModal'; import AttachmentPicker from './AttachmentPicker'; import Avatar from './Avatar'; import AvatarCropModal from './AvatarCropModal/AvatarCropModal'; From d906791422db9dfcc0a3ca4c086dd78095f648b0 Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Wed, 6 Mar 2024 14:55:47 +0000 Subject: [PATCH 008/188] [TS migration][Storybook] Feedback --- .storybook/main.ts | 13 ++----------- .storybook/preview.tsx | 11 ++--------- .storybook/webpack.config.ts | 8 +++++--- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/.storybook/main.ts b/.storybook/main.ts index 0234f18ff488..33f4befb0f40 100644 --- a/.storybook/main.ts +++ b/.storybook/main.ts @@ -1,17 +1,8 @@ -type Dir = { - from: string; - to: string; -}; +import type {StorybookConfig} from '@storybook/core-common'; type Main = { - stories: string[]; - addons: string[]; - staticDirs: Array; - core: { - builder: string; - }; managerHead: (head: string) => string; -}; +} & StorybookConfig; const main: Main = { stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'], diff --git a/.storybook/preview.tsx b/.storybook/preview.tsx index 5ddb9d04d8ae..4767c7d81343 100644 --- a/.storybook/preview.tsx +++ b/.storybook/preview.tsx @@ -1,4 +1,5 @@ import {PortalProvider} from '@gorhom/portal'; +import type {Parameters} from '@storybook/addons'; import React from 'react'; import Onyx from 'react-native-onyx'; import {SafeAreaProvider} from 'react-native-safe-area-context'; @@ -12,14 +13,6 @@ import {WindowDimensionsProvider} from '@src/components/withWindowDimensions'; import ONYXKEYS from '@src/ONYXKEYS'; import './fonts.css'; -type Parameter = { - controls: { - matchers: { - color: RegExp; - }; - }; -}; - Onyx.init({ keys: ONYXKEYS, initialKeyStates: { @@ -37,7 +30,7 @@ const decorators = [ ), ]; -const parameters: Parameter = { +const parameters: Parameters = { controls: { matchers: { color: /(background|color)$/i, diff --git a/.storybook/webpack.config.ts b/.storybook/webpack.config.ts index 0c66e184c18f..6f7ee023643c 100644 --- a/.storybook/webpack.config.ts +++ b/.storybook/webpack.config.ts @@ -1,8 +1,8 @@ -/* eslint-disable @typescript-eslint/naming-convention */ - /* eslint-disable no-underscore-dangle */ /* eslint-disable no-param-reassign */ + +/* eslint-disable @typescript-eslint/naming-convention */ import dotenv from 'dotenv'; import path from 'path'; import {DefinePlugin} from 'webpack'; @@ -35,7 +35,7 @@ const custom: CustomWebpackConfig = require('../config/webpack/webpack.common')( envFile, }); -module.exports = ({config}: {config: Configuration}) => { +const webpackConfig = ({config}: {config: Configuration}) => { if (config.resolve && config.plugins && config.module) { config.resolve.alias = { 'react-native-config': 'react-web-config', @@ -77,3 +77,5 @@ module.exports = ({config}: {config: Configuration}) => { return config; }; + +export default webpackConfig; From 2f5bf904096e487492bd5e4c6f2b66edd70d1d2e Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Wed, 6 Mar 2024 15:09:03 +0000 Subject: [PATCH 009/188] [TS migration][Storybook] Lint --- .storybook/theme.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/.storybook/theme.ts b/.storybook/theme.ts index f1ace5dd37c0..a28a0f031b0c 100644 --- a/.storybook/theme.ts +++ b/.storybook/theme.ts @@ -1,5 +1,6 @@ import type {ThemeVars} from '@storybook/theming'; import {create} from '@storybook/theming'; +// eslint-disable-next-line @dword-design/import-alias/prefer-alias import colors from '../src/styles/theme/colors'; const theme: ThemeVars = create({ From c49ce330283f31e3aebbfdbf750276f446cbb666 Mon Sep 17 00:00:00 2001 From: burczu Date: Wed, 6 Mar 2024 13:12:48 +0100 Subject: [PATCH 010/188] is selectable item property introduced --- src/components/SelectionList/BaseListItem.tsx | 2 +- src/components/SelectionList/BaseSelectionList.tsx | 3 ++- src/components/SelectionList/types.ts | 3 +++ src/pages/workspace/WorkspaceMembersPage.tsx | 7 +++---- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/components/SelectionList/BaseListItem.tsx b/src/components/SelectionList/BaseListItem.tsx index c032fe2d081b..3e8573888404 100644 --- a/src/components/SelectionList/BaseListItem.tsx +++ b/src/components/SelectionList/BaseListItem.tsx @@ -80,7 +80,7 @@ function BaseListItem({ diff --git a/src/components/SelectionList/BaseSelectionList.tsx b/src/components/SelectionList/BaseSelectionList.tsx index bcf45fb2e2f4..5646b993d135 100644 --- a/src/components/SelectionList/BaseSelectionList.tsx +++ b/src/components/SelectionList/BaseSelectionList.tsx @@ -111,7 +111,8 @@ function BaseSelectionList( }); // If disabled, add to the disabled indexes array - if (!!section.isDisabled || item.isDisabled) { + // eslint-disable-next-line + if (!!section.isDisabled || item.isDisabled || !item.isSelectable) { disabledOptionsIndexes.push(disabledIndex); } disabledIndex += 1; diff --git a/src/components/SelectionList/types.ts b/src/components/SelectionList/types.ts index 005a8ab21cc1..a8addd8c68e6 100644 --- a/src/components/SelectionList/types.ts +++ b/src/components/SelectionList/types.ts @@ -58,6 +58,9 @@ type ListItem = { /** Whether this option is selected */ isSelected?: boolean; + /** Whether this option is selectable */ + isSelectable?: boolean; + /** Whether this option is disabled for selection */ isDisabled?: boolean; diff --git a/src/pages/workspace/WorkspaceMembersPage.tsx b/src/pages/workspace/WorkspaceMembersPage.tsx index 3970533870c1..6b886520336f 100644 --- a/src/pages/workspace/WorkspaceMembersPage.tsx +++ b/src/pages/workspace/WorkspaceMembersPage.tsx @@ -200,7 +200,7 @@ function WorkspaceMembersPage({policyMembers, personalDetails, route, policy, se * Add or remove all users passed from the selectedEmployees list */ const toggleAllUsers = (memberList: MemberOption[]) => { - const enabledAccounts = memberList.filter((member) => !member.isDisabled); + const enabledAccounts = memberList.filter((member) => !member.isDisabled && member.isSelectable); const everyoneSelected = enabledAccounts.every((member) => selectedEmployees.includes(member.accountID)); if (everyoneSelected) { @@ -337,11 +337,10 @@ function WorkspaceMembersPage({policyMembers, personalDetails, route, policy, se keyForList: accountIDKey, accountID, isSelected, + isSelectable: isPolicyAdmin && accountID !== session?.accountID && accountID !== policy?.ownerAccountID, isDisabled: isPolicyAdmin && - (accountID === session?.accountID || - accountID === policy?.ownerAccountID || - policyMember.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || + (policyMember.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || !isEmptyObject(policyMember.errors)), text: formatPhoneNumber(PersonalDetailsUtils.getDisplayNameOrDefault(details)), alternateText: formatPhoneNumber(details?.login ?? ''), From 2d361a8798936049716964c176d53541321b6616 Mon Sep 17 00:00:00 2001 From: burczu Date: Thu, 7 Mar 2024 11:43:47 +0100 Subject: [PATCH 011/188] showing disable icon when item is not selectable --- src/components/SelectionList/BaseListItem.tsx | 6 ++++-- src/components/SelectionList/BaseSelectionList.tsx | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/components/SelectionList/BaseListItem.tsx b/src/components/SelectionList/BaseListItem.tsx index 3e8573888404..38998e745196 100644 --- a/src/components/SelectionList/BaseListItem.tsx +++ b/src/components/SelectionList/BaseListItem.tsx @@ -34,6 +34,8 @@ function BaseListItem({ const StyleUtils = useStyleUtils(); const {hovered, bind} = useHover(); + const isItemSelectable = item.isSelectable === undefined || item.isSelectable; + const rightHandSideComponentRender = () => { if (canSelectMultiple || !rightHandSideComponent) { return null; @@ -80,9 +82,9 @@ function BaseListItem({ {item.isSelected && ( diff --git a/src/components/SelectionList/BaseSelectionList.tsx b/src/components/SelectionList/BaseSelectionList.tsx index 5646b993d135..3fc586b5ed13 100644 --- a/src/components/SelectionList/BaseSelectionList.tsx +++ b/src/components/SelectionList/BaseSelectionList.tsx @@ -112,7 +112,7 @@ function BaseSelectionList( // If disabled, add to the disabled indexes array // eslint-disable-next-line - if (!!section.isDisabled || item.isDisabled || !item.isSelectable) { + if (!!section.isDisabled || item.isDisabled || (item.isSelectable !== undefined && !item.isSelectable)) { disabledOptionsIndexes.push(disabledIndex); } disabledIndex += 1; From 8d92c5e61f3849cf94901bdc2fcb258248b06a16 Mon Sep 17 00:00:00 2001 From: burczu Date: Fri, 8 Mar 2024 13:06:57 +0100 Subject: [PATCH 012/188] showing transfer owner button for owners opened by other admins --- src/languages/en.ts | 1 + src/languages/es.ts | 1 + .../members/WorkspaceMemberDetailsPage.tsx | 37 +++++++++++++++---- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/languages/en.ts b/src/languages/en.ts index 3575854ee7e2..956c7d5637de 100755 --- a/src/languages/en.ts +++ b/src/languages/en.ts @@ -1813,6 +1813,7 @@ export default { removeMemberButtonTitle: 'Remove from workspace', removeMemberPrompt: ({memberName}) => `Are you sure you want to remove ${memberName}`, removeMemberTitle: 'Remove member', + transferOwner: 'Transfer owner', makeMember: 'Make member', makeAdmin: 'Make admin', selectAll: 'Select all', diff --git a/src/languages/es.ts b/src/languages/es.ts index 51a83e55fee2..832538e91951 100644 --- a/src/languages/es.ts +++ b/src/languages/es.ts @@ -1837,6 +1837,7 @@ export default { removeMemberButtonTitle: 'Quitar del espacio de trabajo', removeMemberPrompt: ({memberName}) => `¿Estás seguro de que deseas eliminar a ${memberName}`, removeMemberTitle: 'Eliminar miembro', + transferOwner: 'Transferir la propiedad', makeMember: 'Hacer miembro', makeAdmin: 'Hacer administrador', selectAll: 'Seleccionar todo', diff --git a/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx b/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx index d44ff8baa08b..e6befe277d3c 100644 --- a/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx +++ b/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx @@ -30,6 +30,7 @@ import type {Route} from '@src/ROUTES'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import type {PersonalDetails, PersonalDetailsList} from '@src/types/onyx'; +import useCurrentUserPersonalDetails from "@hooks/useCurrentUserPersonalDetails"; type WorkspacePolicyOnyxProps = { /** Personal details of all users */ @@ -42,6 +43,7 @@ function WorkspaceMemberDetailsPage({personalDetails, policyMembers, policy, rou const styles = useThemeStyles(); const {translate} = useLocalize(); const StyleUtils = useStyleUtils(); + const currentUserPersonalDetails = useCurrentUserPersonalDetails(); const [isRemoveMemberConfirmModalVisible, setIsRemoveMemberConfirmModalVisible] = React.useState(false); @@ -54,6 +56,9 @@ function WorkspaceMemberDetailsPage({personalDetails, policyMembers, policy, rou const avatar = details.avatar ?? UserUtils.getDefaultAvatar(); const fallbackIcon = details.fallbackIcon ?? ''; const displayName = details.displayName ?? ''; + const isOwner = policy?.owner === details.login; + const isCurrentUserAdmin = policyMembers?.[currentUserPersonalDetails?.accountID]?.role === CONST.POLICY.ROLE.ADMIN; + const isCurrentUserOwner = policy?.owner === currentUserPersonalDetails?.login; const askForConfirmationToRemove = () => { setIsRemoveMemberConfirmModalVisible(true); @@ -73,6 +78,10 @@ function WorkspaceMemberDetailsPage({personalDetails, policyMembers, policy, rou Navigation.navigate(ROUTES.WORKSPACE_MEMBER_ROLE_SELECTION.getRoute(route.params.policyID, accountID, Navigation.getActiveRoute())); }, [accountID, route.params.policyID]); + const startChangeOwnershipFlow = useCallback(() => { + + }, []); + return ( @@ -101,14 +110,26 @@ function WorkspaceMemberDetailsPage({personalDetails, policyMembers, policy, rou {displayName} )} - + ) : ( + + )} + From ec0d55355e71ea71f81f29a013eb5185a34cf119 Mon Sep 17 00:00:00 2001 From: Julian Kobrynski Date: Mon, 11 Mar 2024 17:32:37 +0100 Subject: [PATCH 027/188] migrate bumpVersion.js to TypeScript --- .../{bumpVersion.js => bumpVersion.ts} | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) rename .github/actions/javascript/bumpVersion/{bumpVersion.js => bumpVersion.ts} (73%) diff --git a/.github/actions/javascript/bumpVersion/bumpVersion.js b/.github/actions/javascript/bumpVersion/bumpVersion.ts similarity index 73% rename from .github/actions/javascript/bumpVersion/bumpVersion.js rename to .github/actions/javascript/bumpVersion/bumpVersion.ts index 647c295fdc52..d08293c856be 100644 --- a/.github/actions/javascript/bumpVersion/bumpVersion.js +++ b/.github/actions/javascript/bumpVersion/bumpVersion.ts @@ -1,17 +1,16 @@ -const {promisify} = require('util'); -const fs = require('fs'); -const exec = promisify(require('child_process').exec); -const _ = require('underscore'); -const core = require('@actions/core'); -const versionUpdater = require('../../../libs/versionUpdater'); -const {updateAndroidVersion, updateiOSVersion, generateAndroidVersionCode} = require('../../../libs/nativeVersionUpdater'); +import * as core from '@actions/core'; +import {exec as originalExec} from 'child_process'; +import fs from 'fs'; +import {promisify} from 'util'; +import {generateAndroidVersionCode, updateAndroidVersion, updateiOSVersion} from '../../../libs/nativeVersionUpdater'; +import * as versionUpdater from '../../../libs/versionUpdater'; + +const exec = promisify(originalExec); /** * Update the native app versions. - * - * @param {String} version */ -function updateNativeVersions(version) { +function updateNativeVersions(version: string) { console.log(`Updating native versions to ${version}`); // Update Android @@ -28,7 +27,7 @@ function updateNativeVersions(version) { // Update iOS try { const cfBundleVersion = updateiOSVersion(version); - if (_.isString(cfBundleVersion) && cfBundleVersion.split('.').length === 4) { + if (typeof cfBundleVersion === 'string' && cfBundleVersion.split('.').length === 4) { core.setOutput('NEW_IOS_VERSION', cfBundleVersion); console.log('Successfully updated iOS!'); } else { @@ -36,17 +35,17 @@ function updateNativeVersions(version) { } } catch (err) { console.error('Error updating iOS'); - core.setFailed(err); + core.setFailed(err as string); } } -let semanticVersionLevel = core.getInput('SEMVER_LEVEL', {require: true}); -if (!semanticVersionLevel || !_.contains(versionUpdater.SEMANTIC_VERSION_LEVELS, semanticVersionLevel)) { +let semanticVersionLevel = core.getInput('SEMVER_LEVEL', {required: true}); +if (!semanticVersionLevel || !Object.keys(versionUpdater.SEMANTIC_VERSION_LEVELS).includes(semanticVersionLevel)) { semanticVersionLevel = versionUpdater.SEMANTIC_VERSION_LEVELS.BUILD; console.log(`Invalid input for 'SEMVER_LEVEL': ${semanticVersionLevel}`, `Defaulting to: ${semanticVersionLevel}`); } -const {version: previousVersion} = JSON.parse(fs.readFileSync('./package.json')); +const {version: previousVersion} = JSON.parse(fs.readFileSync('./package.json').toString()); const newVersion = versionUpdater.incrementVersion(previousVersion, semanticVersionLevel); console.log(`Previous version: ${previousVersion}`, `New version: ${newVersion}`); From 7068222b62669118304ac6a424deda12388b695d Mon Sep 17 00:00:00 2001 From: Julian Kobrynski Date: Mon, 11 Mar 2024 17:33:05 +0100 Subject: [PATCH 028/188] start migrating markPullRequestsAsDeployed to TypeScript --- ...loyed.js => markPullRequestsAsDeployed.ts} | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) rename .github/actions/javascript/markPullRequestsAsDeployed/{markPullRequestsAsDeployed.js => markPullRequestsAsDeployed.ts} (84%) diff --git a/.github/actions/javascript/markPullRequestsAsDeployed/markPullRequestsAsDeployed.js b/.github/actions/javascript/markPullRequestsAsDeployed/markPullRequestsAsDeployed.ts similarity index 84% rename from .github/actions/javascript/markPullRequestsAsDeployed/markPullRequestsAsDeployed.js rename to .github/actions/javascript/markPullRequestsAsDeployed/markPullRequestsAsDeployed.ts index d03a947cdec8..cf132794ce46 100644 --- a/.github/actions/javascript/markPullRequestsAsDeployed/markPullRequestsAsDeployed.js +++ b/.github/actions/javascript/markPullRequestsAsDeployed/markPullRequestsAsDeployed.ts @@ -1,17 +1,15 @@ -const _ = require('underscore'); -const core = require('@actions/core'); -const {context} = require('@actions/github'); -const CONST = require('../../../libs/CONST'); -const ActionUtils = require('../../../libs/ActionUtils'); -const GithubUtils = require('../../../libs/GithubUtils'); +import core from '@actions/core'; +import {context} from '@actions/github'; +import * as ActionUtils from '../../../libs/ActionUtils'; +import CONST from '../../../libs/CONST'; +import * as GithubUtils from '../../../libs/GithubUtils'; + +type PlatformResult = 'success' | 'cancelled' | 'skipped' | 'failure'; /** * Return a nicely formatted message for the table based on the result of the GitHub action job - * - * @param {String} platformResult - * @returns {String} */ -function getDeployTableMessage(platformResult) { +function getDeployTableMessage(platformResult: PlatformResult) { switch (platformResult) { case 'success': return `${platformResult} ✅`; @@ -27,10 +25,6 @@ function getDeployTableMessage(platformResult) { /** * Comment Single PR - * - * @param {Number} PR - * @param {String} message - * @returns {Promise} */ async function commentPR(PR, message) { try { @@ -45,7 +39,7 @@ async function commentPR(PR, message) { const workflowURL = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`; async function run() { - const prList = _.map(ActionUtils.getJSONInput('PR_LIST', {required: true}), (num) => Number.parseInt(num, 10)); + const prList = ActionUtils.getJSONInput('PR_LIST', {required: true}).map((num: string) => Number.parseInt(num, 10)); const isProd = ActionUtils.getJSONInput('IS_PRODUCTION_DEPLOY', {required: true}); const version = core.getInput('DEPLOY_VERSION', {required: true}); @@ -55,10 +49,10 @@ async function run() { const webResult = getDeployTableMessage(core.getInput('WEB', {required: true})); /** - * @param {String} deployer - * @param {String} deployVerb - * @param {String} prTitle - * @returns {String} + * @param deployer + * @param deployVerb + * @param prTitle + * @returns */ function getDeployMessage(deployer, deployVerb, prTitle) { let message = `🚀 [${deployVerb}](${workflowURL}) to ${isProd ? 'production' : 'staging'}`; @@ -83,7 +77,7 @@ async function run() { labels: CONST.LABELS.STAGING_DEPLOY, state: 'closed', }); - const previousChecklistID = _.first(deployChecklists).number; + const previousChecklistID = deployChecklists[0].number; // who closed the last deploy checklist? const deployer = await GithubUtils.getActorWhoClosedIssue(previousChecklistID); @@ -102,7 +96,7 @@ async function run() { repo: CONST.APP_REPO, per_page: 100, }); - const currentTag = _.find(recentTags, (tag) => tag.name === version); + const currentTag = recentTags.find((tag) => tag.name === version); if (!currentTag) { const err = `Could not find tag matching ${version}`; console.error(err); @@ -139,4 +133,4 @@ if (require.main === module) { run(); } -module.exports = run; +export default run; From 2260d2a4a9fcecb00e2dd2c64bcd3379b40a5c70 Mon Sep 17 00:00:00 2001 From: Julian Kobrynski Date: Mon, 11 Mar 2024 17:33:21 +0100 Subject: [PATCH 029/188] start migrating GithubUtils to TypeScript --- .../libs/{GithubUtils.js => GithubUtils.ts} | 235 +++++++++--------- 1 file changed, 120 insertions(+), 115 deletions(-) rename .github/libs/{GithubUtils.js => GithubUtils.ts} (71%) diff --git a/.github/libs/GithubUtils.js b/.github/libs/GithubUtils.ts similarity index 71% rename from .github/libs/GithubUtils.js rename to .github/libs/GithubUtils.ts index e988167850ec..046701a94a9c 100644 --- a/.github/libs/GithubUtils.js +++ b/.github/libs/GithubUtils.ts @@ -1,10 +1,12 @@ -const _ = require('underscore'); -const lodashGet = require('lodash/get'); -const core = require('@actions/core'); -const {GitHub, getOctokitOptions} = require('@actions/github/lib/utils'); -const {throttling} = require('@octokit/plugin-throttling'); -const {paginateRest} = require('@octokit/plugin-paginate-rest'); -const CONST = require('./CONST'); +import * as core from '@actions/core'; +import {getOctokitOptions, GitHub} from '@actions/github/lib/utils'; +import type {Octokit as OctokitCore} from '@octokit/core'; +import type {PaginateInterface} from '@octokit/plugin-paginate-rest'; +import {paginateRest} from '@octokit/plugin-paginate-rest'; +import {throttling} from '@octokit/plugin-throttling'; +import _ from 'underscore'; +import {isEmptyObject} from '@src/types/utils/EmptyObject'; +import CONST from './CONST'; const GITHUB_BASE_URL_REGEX = new RegExp('https?://(?:github\\.com|api\\.github\\.com)'); const PULL_REQUEST_REGEX = new RegExp(`${GITHUB_BASE_URL_REGEX.source}/.*/.*/pull/([0-9]+).*`); @@ -14,11 +16,14 @@ const ISSUE_OR_PULL_REQUEST_REGEX = new RegExp(`${GITHUB_BASE_URL_REGEX.source}/ /** * The standard rate in ms at which we'll poll the GitHub API to check for status changes. * It's 10 seconds :) - * @type {number} */ const POLL_RATE = 10000; +type OctokitOptions = {method: string; url: string; request: {retryCount: number}}; + class GithubUtils { + static internalOctokit: OctokitCore & {paginate: PaginateInterface}; + /** * Initialize internal octokit * @@ -33,7 +38,7 @@ class GithubUtils { getOctokitOptions(token, { throttle: { retryAfterBaseValue: 2000, - onRateLimit: (retryAfter, options) => { + onRateLimit: (retryAfter: number, options: OctokitOptions) => { console.warn(`Request quota exhausted for request ${options.method} ${options.url}`); // Retry five times when hitting a rate limit error, then give up @@ -42,7 +47,7 @@ class GithubUtils { return true; } }, - onAbuseLimit: (retryAfter, options) => { + onAbuseLimit: (retryAfter: number, options: OctokitOptions) => { // does not retry, only logs a warning console.warn(`Abuse detected for request ${options.method} ${options.url}`); }, @@ -98,7 +103,7 @@ class GithubUtils { /** * Finds one open `StagingDeployCash` issue via GitHub octokit library. * - * @returns {Promise} + * @returns */ static getStagingDeployCash() { return this.octokit.issues @@ -128,8 +133,8 @@ class GithubUtils { /** * Takes in a GitHub issue object and returns the data we want. * - * @param {Object} issue - * @returns {Object} + * @param issue + * @returns */ static getStagingDeployCashData(issue) { try { @@ -158,8 +163,8 @@ class GithubUtils { * * @private * - * @param {Object} issue - * @returns {Array} - [{url: String, number: Number, isVerified: Boolean}] + * @param issue + * @returns - [{url: String, number: Number, isVerified: Boolean}] */ static getStagingDeployCashPRList(issue) { let PRListSection = issue.body.match(/pull requests:\*\*\r?\n((?:-.*\r?\n)+)\r?\n\r?\n?/) || []; @@ -169,7 +174,7 @@ class GithubUtils { return []; } PRListSection = PRListSection[1]; - const PRList = _.map([...PRListSection.matchAll(new RegExp(`- \\[([ x])] (${PULL_REQUEST_REGEX.source})`, 'g'))], (match) => ({ + const PRList = [...PRListSection.matchAll(new RegExp(`- \\[([ x])] (${PULL_REQUEST_REGEX.source})`, 'g'))].map((match) => ({ url: match[2], number: Number.parseInt(match[3], 10), isVerified: match[1] === 'x', @@ -182,8 +187,8 @@ class GithubUtils { * * @private * - * @param {Object} issue - * @returns {Array} - [{URL: String, number: Number, isResolved: Boolean}] + * @param issue + * @returns - [{URL: String, number: Number, isResolved: Boolean}] */ static getStagingDeployCashDeployBlockers(issue) { let deployBlockerSection = issue.body.match(/Deploy Blockers:\*\*\r?\n((?:-.*\r?\n)+)/) || []; @@ -191,7 +196,7 @@ class GithubUtils { return []; } deployBlockerSection = deployBlockerSection[1]; - const deployBlockers = _.map([...deployBlockerSection.matchAll(new RegExp(`- \\[([ x])]\\s(${ISSUE_OR_PULL_REQUEST_REGEX.source})`, 'g'))], (match) => ({ + const deployBlockers = [...deployBlockerSection.matchAll(new RegExp(`- \\[([ x])]\\s(${ISSUE_OR_PULL_REQUEST_REGEX.source})`, 'g'))].map((match) => ({ url: match[2], number: Number.parseInt(match[3], 10), isResolved: match[1] === 'x', @@ -204,8 +209,8 @@ class GithubUtils { * * @private * - * @param {Object} issue - * @returns {Array} - [{URL: String, number: Number, isResolved: Boolean}] + * @param issue + * @returns - [{URL: String, number: Number, isResolved: Boolean}] */ static getStagingDeployCashInternalQA(issue) { let internalQASection = issue.body.match(/Internal QA:\*\*\r?\n((?:- \[[ x]].*\r?\n)+)/) || []; @@ -213,7 +218,7 @@ class GithubUtils { return []; } internalQASection = internalQASection[1]; - const internalQAPRs = _.map([...internalQASection.matchAll(new RegExp(`- \\[([ x])]\\s(${PULL_REQUEST_REGEX.source})`, 'g'))], (match) => ({ + const internalQAPRs = [...internalQASection.matchAll(new RegExp(`- \\[([ x])]\\s(${PULL_REQUEST_REGEX.source})`, 'g'))].map((match) => ({ url: match[2].split('-')[0].trim(), number: Number.parseInt(match[3], 10), isResolved: match[1] === 'x', @@ -224,54 +229,52 @@ class GithubUtils { /** * Generate the issue body for a StagingDeployCash. * - * @param {String} tag - * @param {Array} PRList - The list of PR URLs which are included in this StagingDeployCash - * @param {Array} [verifiedPRList] - The list of PR URLs which have passed QA. - * @param {Array} [deployBlockers] - The list of DeployBlocker URLs. - * @param {Array} [resolvedDeployBlockers] - The list of DeployBlockers URLs which have been resolved. - * @param {Array} [resolvedInternalQAPRs] - The list of Internal QA PR URLs which have been resolved. - * @param {Boolean} [isTimingDashboardChecked] - * @param {Boolean} [isFirebaseChecked] - * @param {Boolean} [isGHStatusChecked] - * @returns {Promise} + * @param tag + * @param PRList - The list of PR URLs which are included in this StagingDeployCash + * @param [verifiedPRList] - The list of PR URLs which have passed QA. + * @param [deployBlockers] - The list of DeployBlocker URLs. + * @param [resolvedDeployBlockers] - The list of DeployBlockers URLs which have been resolved. + * @param [resolvedInternalQAPRs] - The list of Internal QA PR URLs which have been resolved. + * @param [isTimingDashboardChecked] + * @param [isFirebaseChecked] + * @param [isGHStatusChecked] + * @returns */ static generateStagingDeployCashBody( - tag, - PRList, - verifiedPRList = [], - deployBlockers = [], - resolvedDeployBlockers = [], - resolvedInternalQAPRs = [], + tag: string, + PRList: string[], + verifiedPRList: string[] = [], + deployBlockers: string[] = [], + resolvedDeployBlockers: string[] = [], + resolvedInternalQAPRs: string[] = [], isTimingDashboardChecked = false, isFirebaseChecked = false, isGHStatusChecked = false, ) { - return this.fetchAllPullRequests(_.map(PRList, this.getPullRequestNumberFromURL)) + return this.fetchAllPullRequests(PRList.map(this.getPullRequestNumberFromURL)) .then((data) => { // The format of this map is following: // { // 'https://github.com/Expensify/App/pull/9641': 'PauloGasparSv', // 'https://github.com/Expensify/App/pull/9642': 'mountiny' // } - const internalQAPRMap = _.reduce( - _.filter(data, (pr) => !_.isEmpty(_.findWhere(pr.labels, {name: CONST.LABELS.INTERNAL_QA}))), - (map, pr) => { + const internalQAPRMap = data + .filter((pr) => !isEmptyObject(_.findWhere(pr.labels, {name: CONST.LABELS.INTERNAL_QA}))) + .reduce((map, pr) => { // eslint-disable-next-line no-param-reassign map[pr.html_url] = pr.merged_by.login; return map; - }, - {}, - ); + }, {}); console.log('Found the following Internal QA PRs:', internalQAPRMap); const noQAPRs = _.pluck( - _.filter(data, (PR) => /\[No\s?QA]/i.test(PR.title)), + data.filter((PR) => /\[No\s?QA]/i.test(PR.title)), 'html_url', ); console.log('Found the following NO QA PRs:', noQAPRs); const verifiedOrNoQAPRs = _.union(verifiedPRList, noQAPRs); - const sortedPRList = _.chain(PRList).difference(_.keys(internalQAPRMap)).unique().sortBy(GithubUtils.getPullRequestNumberFromURL).value(); + const sortedPRList = _.chain(PRList).difference(Object.keys(internalQAPRMap)).unique().sortBy(GithubUtils.getPullRequestNumberFromURL).value(); const sortedDeployBlockers = _.sortBy(_.unique(deployBlockers), GithubUtils.getIssueOrPullRequestNumberFromURL); // Tag version and comparison URL @@ -279,22 +282,22 @@ class GithubUtils { let issueBody = `**Release Version:** \`${tag}\`\r\n**Compare Changes:** https://github.com/Expensify/App/compare/production...staging\r\n`; // PR list - if (!_.isEmpty(sortedPRList)) { + if (!isEmptyObject(sortedPRList)) { issueBody += '\r\n**This release contains changes from the following pull requests:**\r\n'; - _.each(sortedPRList, (URL) => { - issueBody += _.contains(verifiedOrNoQAPRs, URL) ? '- [x]' : '- [ ]'; + sortedPRList.forEach((URL) => { + issueBody += verifiedOrNoQAPRs.includes(URL) ? '- [x]' : '- [ ]'; issueBody += ` ${URL}\r\n`; }); issueBody += '\r\n\r\n'; } // Internal QA PR list - if (!_.isEmpty(internalQAPRMap)) { + if (!isEmptyObject(internalQAPRMap)) { console.log('Found the following verified Internal QA PRs:', resolvedInternalQAPRs); issueBody += '**Internal QA:**\r\n'; - _.each(internalQAPRMap, (merger, URL) => { + internalQAPRMap.each((merger, URL) => { const mergerMention = `@${merger}`; - issueBody += `${_.contains(resolvedInternalQAPRs, URL) ? '- [x]' : '- [ ]'} `; + issueBody += `${resolvedInternalQAPRs.includes(URL) ? '- [x]' : '- [ ]'} `; issueBody += `${URL}`; issueBody += ` - ${mergerMention}`; issueBody += '\r\n'; @@ -303,10 +306,10 @@ class GithubUtils { } // Deploy blockers - if (!_.isEmpty(deployBlockers)) { + if (!isEmptyObject(deployBlockers)) { issueBody += '**Deploy Blockers:**\r\n'; - _.each(sortedDeployBlockers, (URL) => { - issueBody += _.contains(resolvedDeployBlockers, URL) ? '- [x] ' : '- [ ] '; + sortedDeployBlockers.forEach((URL) => { + issueBody += resolvedDeployBlockers.includes(URL) ? '- [x] ' : '- [ ] '; issueBody += URL; issueBody += '\r\n'; }); @@ -326,7 +329,7 @@ class GithubUtils { issueBody += `\r\n- [${isGHStatusChecked ? 'x' : ' '}] I checked [GitHub Status](https://www.githubstatus.com/) and verified there is no reported incident with Actions.`; issueBody += '\r\n\r\ncc @Expensify/applauseleads\r\n'; - const issueAssignees = _.values(internalQAPRMap); + const issueAssignees = Object.values(internalQAPRMap); const issue = {issueBody, issueAssignees}; return issue; }) @@ -335,12 +338,9 @@ class GithubUtils { /** * Fetch all pull requests given a list of PR numbers. - * - * @param {Array} pullRequestNumbers - * @returns {Promise} */ - static fetchAllPullRequests(pullRequestNumbers) { - const oldestPR = _.first(_.sortBy(pullRequestNumbers)); + static fetchAllPullRequests(pullRequestNumbers: number[]) { + const oldestPR = _.sortBy(pullRequestNumbers)[0]; return this.paginate( this.octokit.pulls.list, { @@ -352,19 +352,19 @@ class GithubUtils { per_page: 100, }, ({data}, done) => { - if (_.find(data, (pr) => pr.number === oldestPR)) { + if (data.find((pr) => pr.number === oldestPR)) { done(); } return data; }, ) - .then((prList) => _.filter(prList, (pr) => _.contains(pullRequestNumbers, pr.number))) + .then((prList) => prList.filter((pr) => pullRequestNumbers.includes(pr.number))) .catch((err) => console.error('Failed to get PR list', err)); } /** - * @param {Number} pullRequestNumber - * @returns {Promise} + * @param pullRequestNumber + * @returns */ static getPullRequestBody(pullRequestNumber) { return this.octokit.pulls @@ -377,8 +377,8 @@ class GithubUtils { } /** - * @param {Number} pullRequestNumber - * @returns {Promise} + * @param pullRequestNumber + * @returns */ static getAllReviewComments(pullRequestNumber) { return this.paginate( @@ -389,13 +389,13 @@ class GithubUtils { pull_number: pullRequestNumber, per_page: 100, }, - (response) => _.map(response.data, (review) => review.body), + (response) => response.data.map((review) => review.body), ); } /** - * @param {Number} issueNumber - * @returns {Promise} + * @param issueNumber + * @returns */ static getAllComments(issueNumber) { return this.paginate( @@ -406,17 +406,17 @@ class GithubUtils { issue_number: issueNumber, per_page: 100, }, - (response) => _.map(response.data, (comment) => comment.body), + (response) => response.data.map((comment) => comment.body), ); } /** * Create comment on pull request * - * @param {String} repo - The repo to search for a matching pull request or issue number - * @param {Number} number - The pull request or issue number - * @param {String} messageBody - The comment message - * @returns {Promise} + * @param repo - The repo to search for a matching pull request or issue number + * @param number - The pull request or issue number + * @param messageBody - The comment message + * @returns */ static createComment(repo, number, messageBody) { console.log(`Writing comment on #${number}`); @@ -431,50 +431,53 @@ class GithubUtils { /** * Get the most recent workflow run for the given New Expensify workflow. * - * @param {String} workflow - * @returns {Promise} + * @param workflow + * @returns */ static getLatestWorkflowRunID(workflow) { console.log(`Fetching New Expensify workflow runs for ${workflow}...`); - return this.octokit.actions - .listWorkflowRuns({ - owner: CONST.GITHUB_OWNER, - repo: CONST.APP_REPO, - workflow_id: workflow, - }) - .then((response) => lodashGet(response, 'data.workflow_runs[0].id')); + return ( + this.octokit.actions + .listWorkflowRuns({ + owner: CONST.GITHUB_OWNER, + repo: CONST.APP_REPO, + workflow_id: workflow, + }) + // .then((response) => lodashGet(response, 'data.workflow_runs[0].id')); + .then((response) => response.data.workflow_runs[0].id) + ); } /** * Generate the well-formatted body of a production release. * - * @param {Array} pullRequests - * @returns {String} + * @param pullRequests + * @returns */ static getReleaseBody(pullRequests) { - return _.map(pullRequests, (number) => `- ${this.getPullRequestURLFromNumber(number)}`).join('\r\n'); + return pullRequests.map((number) => `- ${this.getPullRequestURLFromNumber(number)}`).join('\r\n'); } /** * Generate the URL of an New Expensify pull request given the PR number. * - * @param {Number} number - * @returns {String} + * @param number + * @returns */ - static getPullRequestURLFromNumber(number) { - return `${CONST.APP_REPO_URL}/pull/${number}`; + static getPullRequestURLFromNumber(value: number): string { + return `${CONST.APP_REPO_URL}/pull/${value}`; } /** * Parse the pull request number from a URL. * - * @param {String} URL - * @returns {Number} + * @param URL + * @returns * @throws {Error} If the URL is not a valid Github Pull Request. */ - static getPullRequestNumberFromURL(URL) { + static getPullRequestNumberFromURL(URL: string): number { const matches = URL.match(PULL_REQUEST_REGEX); - if (!_.isArray(matches) || matches.length !== 2) { + if (!Array.isArray(matches) || matches.length !== 2) { throw new Error(`Provided URL ${URL} is not a Github Pull Request!`); } return Number.parseInt(matches[1], 10); @@ -483,13 +486,13 @@ class GithubUtils { /** * Parse the issue number from a URL. * - * @param {String} URL - * @returns {Number} + * @param URL + * @returns * @throws {Error} If the URL is not a valid Github Issue. */ static getIssueNumberFromURL(URL) { const matches = URL.match(ISSUE_REGEX); - if (!_.isArray(matches) || matches.length !== 2) { + if (!Array.isArray(matches) || matches.length !== 2) { throw new Error(`Provided URL ${URL} is not a Github Issue!`); } return Number.parseInt(matches[1], 10); @@ -498,13 +501,13 @@ class GithubUtils { /** * Parse the issue or pull request number from a URL. * - * @param {String} URL - * @returns {Number} + * @param URL + * @returns * @throws {Error} If the URL is not a valid Github Issue or Pull Request. */ static getIssueOrPullRequestNumberFromURL(URL) { const matches = URL.match(ISSUE_OR_PULL_REQUEST_REGEX); - if (!_.isArray(matches) || matches.length !== 2) { + if (!Array.isArray(matches) || matches.length !== 2) { throw new Error(`Provided URL ${URL} is not a valid Github Issue or Pull Request!`); } return Number.parseInt(matches[1], 10); @@ -513,18 +516,21 @@ class GithubUtils { /** * Return the login of the actor who closed an issue or PR. If the issue is not closed, return an empty string. * - * @param {Number} issueNumber - * @returns {Promise} + * @param issueNumber + * @returns */ static getActorWhoClosedIssue(issueNumber) { - return this.paginate(this.octokit.issues.listEvents, { - owner: CONST.GITHUB_OWNER, - repo: CONST.APP_REPO, - issue_number: issueNumber, - per_page: 100, - }) - .then((events) => _.filter(events, (event) => event.event === 'closed')) - .then((closedEvents) => lodashGet(_.last(closedEvents), 'actor.login', '')); + return ( + this.paginate(this.octokit.issues.listEvents, { + owner: CONST.GITHUB_OWNER, + repo: CONST.APP_REPO, + issue_number: issueNumber, + per_page: 100, + }) + .then((events) => events.filter((event) => event.event === 'closed')) + // .then((closedEvents) => lodashGet(_.last(closedEvents), 'actor.login', '')); + .then((closedEvents) => _.last(closedEvents).actor.login ?? '') + ); } static getArtifactByName(artefactName) { @@ -536,6 +542,5 @@ class GithubUtils { } } -module.exports = GithubUtils; -module.exports.ISSUE_OR_PULL_REQUEST_REGEX = ISSUE_OR_PULL_REQUEST_REGEX; -module.exports.POLL_RATE = POLL_RATE; +export default GithubUtils; +export {ISSUE_OR_PULL_REQUEST_REGEX, POLL_RATE}; From ca090a701cfd42e80015cfb236fd437ea040362f Mon Sep 17 00:00:00 2001 From: Rushat Gabhane Date: Tue, 12 Mar 2024 05:23:14 +0530 Subject: [PATCH 030/188] setup theme for helpdot --- docs/_sass/_colors.scss | 58 +++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/docs/_sass/_colors.scss b/docs/_sass/_colors.scss index f0c89d31c580..c9473925d791 100644 --- a/docs/_sass/_colors.scss +++ b/docs/_sass/_colors.scss @@ -1,22 +1,46 @@ -// Product Color Spectrum -$color-product-dark-100: #061B09; -$color-product-dark-200: #072419; -$color-product-dark-300: #0A2E25; -$color-product-dark-400: #1A3D32; -$color-product-dark-500: #224F41; -$color-product-dark-600: #2A604F; -$color-product-dark-700: #8B9C8F; -$color-product-dark-800: #AFBBB0; -$color-product-dark-900: #E7ECE9; +@media (prefers-color-scheme: dark) { + // Product Color Spectrum + $color-product-dark-100: #061B09; + $color-product-dark-200: #072419; + $color-product-dark-300: #0A2E25; + $color-product-dark-400: #1A3D32; + $color-product-dark-500: #224F41; + $color-product-dark-600: #2A604F; + $color-product-dark-700: #8B9C8F; + $color-product-dark-800: #AFBBB0; + $color-product-dark-900: #E7ECE9; -// Colors for Links and Success -$color-blue200: #B0D9FF; -$color-blue300: #5AB0FF; -$color-green400: #03D47C; -$color-green500: #00a862; + // Colors for Links and Success + $color-blue200: #B0D9FF; + $color-blue300: #5AB0FF; + $color-green400: #03D47C; + $color-green500: #00a862; -// Overlay BG color -$color-overlay-background: rgba(26, 61, 50, 0.72); + // Overlay BG color + $color-overlay-background: rgba(26, 61, 50, 0.72); +} + +@media (prefers-color-scheme: light) { + // Product Color Spectrum + $color-product-dark-100: #061B09; + $color-product-dark-200: #072419; + $color-product-dark-300: #0A2E25; + $color-product-dark-400: #1A3D32; + $color-product-dark-500: #224F41; + $color-product-dark-600: #2A604F; + $color-product-dark-700: #8B9C8F; + $color-product-dark-800: #AFBBB0; + $color-product-dark-900: #E7ECE9; + + // Colors for Links and Success + $color-blue200: #B0D9FF; + $color-blue300: #5AB0FF; + $color-green400: #03D47C; + $color-green500: #00a862; + + // Overlay BG color + $color-overlay-background: rgba(26, 61, 50, 0.72); +} // UI Colors $color-text: $color-product-dark-900; From 283c4de6983d0fe856e280d068a2e2b320b329e1 Mon Sep 17 00:00:00 2001 From: Rushat Gabhane Date: Tue, 12 Mar 2024 05:23:21 +0530 Subject: [PATCH 031/188] setup theme for helpdot --- docs/_sass/_colors.scss | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/_sass/_colors.scss b/docs/_sass/_colors.scss index c9473925d791..22af28974281 100644 --- a/docs/_sass/_colors.scss +++ b/docs/_sass/_colors.scss @@ -58,3 +58,4 @@ $color-button-background: $color-product-dark-400; $color-button-background-hover: $color-product-dark-500; $color-button-success-background: $color-green400; $color-button-success-background-hover: $color-green500; +$color-overlay: $color-overlay-background; From efaa96a5f02d592032bb7ed48d2850dbd65a5be0 Mon Sep 17 00:00:00 2001 From: Rushat Gabhane Date: Tue, 12 Mar 2024 05:23:25 +0530 Subject: [PATCH 032/188] setup theme for helpdot --- docs/_sass/_search-bar.scss | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_sass/_search-bar.scss b/docs/_sass/_search-bar.scss index f414d25fc266..5c58d70b5851 100644 --- a/docs/_sass/_search-bar.scss +++ b/docs/_sass/_search-bar.scss @@ -67,7 +67,7 @@ left: 0; right: 0; bottom: 0; - background-color: $color-overlay-background; + background-color: $color-overlay; z-index: 1; } From 2e4c65f989ce4f835fe19c876e646ad4bf94d2db Mon Sep 17 00:00:00 2001 From: burczu Date: Tue, 12 Mar 2024 10:17:53 +0100 Subject: [PATCH 033/188] transfer balance confirmation copy handled --- src/languages/en.ts | 6 +++ src/languages/es.ts | 6 +++ .../members/WorkspaceMemberDetailsPage.tsx | 2 +- .../members/WorkspaceOwnerChangeCheckPage.tsx | 48 ++++++++++++++++--- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/languages/en.ts b/src/languages/en.ts index e096aa091c95..51c3623aaaed 100755 --- a/src/languages/en.ts +++ b/src/languages/en.ts @@ -1967,6 +1967,12 @@ export default { updateCurrencyPrompt: 'It looks like your Workspace is currently set to a different currency than USD. Please click the button below to update your currency to USD now.', updateToUSD: 'Update to USD', }, + changeOwner: { + changeOwnerPageTitle: 'Change owner', + outstandingBalance: 'Outstanding balance', + transferBalance: 'Transfer balance', + transferBalanceFirstParagraph: ({email, amount}) => `The account owing this workspace (${email}) has an outstanding balance.\n\nDo you want to transfer this amount ${amount} in order to take over billing for this workspace? Your payment card will be charged immediately.`, + } }, getAssistancePage: { title: 'Get assistance', diff --git a/src/languages/es.ts b/src/languages/es.ts index 99bb64e0af37..6c5b5c73e589 100644 --- a/src/languages/es.ts +++ b/src/languages/es.ts @@ -1993,6 +1993,12 @@ export default { 'Parece que tu espacio de trabajo está configurado actualmente en una moneda diferente a USD. Por favor, haz clic en el botón de abajo para actualizar tu moneda a USD ahora.', updateToUSD: 'Actualizar a USD', }, + changeOwner: { + changeOwnerPageTitle: 'Cambio de propietario', + outstandingBalance: 'Saldo pendiente', + transferBalance: 'Transfer balance', + transferBalanceFirstParagraph: ({email, amount}) => `La cuenta que debe este espacio de trabajo (${email}) tiene un saldo pendiente.\n\n¿Desea transferir este monto ${amount} para hacerse cargo de la facturación de este espacio de trabajo? El cargo en su tarjeta de pago se realizará inmediatamente.`, + } }, getAssistancePage: { title: 'Obtener ayuda', diff --git a/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx b/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx index d12d0235a699..294138c7daa9 100644 --- a/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx +++ b/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx @@ -94,7 +94,7 @@ function WorkspaceMemberDetailsPage({personalDetails, policyMembers, policy, rou }, [policyID]); const temporaryOpenCheckPage = () => { - Navigation.navigate(ROUTES.WORKSPACE_OWNER_CHANGE_CHECK.getRoute(policyID, CONST.POLICY.OWNERSHIP_ERRORS.NO_BILLING_CARD)); + Navigation.navigate(ROUTES.WORKSPACE_OWNER_CHANGE_CHECK.getRoute(policyID, CONST.POLICY.OWNERSHIP_ERRORS.AMOUNT_OWED)); }; return ( diff --git a/src/pages/workspace/members/WorkspaceOwnerChangeCheckPage.tsx b/src/pages/workspace/members/WorkspaceOwnerChangeCheckPage.tsx index 8ae4dc5a232a..f1a01366ad9a 100644 --- a/src/pages/workspace/members/WorkspaceOwnerChangeCheckPage.tsx +++ b/src/pages/workspace/members/WorkspaceOwnerChangeCheckPage.tsx @@ -1,5 +1,5 @@ import type {StackScreenProps} from '@react-navigation/stack'; -import React, {useCallback} from 'react'; +import React, {useCallback, useMemo} from 'react'; import HeaderWithBackButton from '@components/HeaderWithBackButton'; import Text from '@components/Text'; import ScreenWrapper from '@components/ScreenWrapper'; @@ -9,9 +9,10 @@ import AdminPolicyAccessOrNotFoundWrapper from '@pages/workspace/AdminPolicyAcce import PaidPolicyAccessOrNotFoundWrapper from '@pages/workspace/PaidPolicyAccessOrNotFoundWrapper'; import type SCREENS from '@src/SCREENS'; import CONST from "@src/CONST"; -import Button from "@components/Button"; +import Button from '@components/Button'; import {View} from "react-native"; import useThemeStyles from "@hooks/useThemeStyles"; +import useLocalize from "@hooks/useLocalize"; type WorkspaceMemberDetailsPageProps = StackScreenProps; @@ -24,6 +25,7 @@ const CONFIRMABLE_ERRORS: string[] = [ function WorkspaceOwnerChangeCheckPage({route}: WorkspaceMemberDetailsPageProps) { const styles = useThemeStyles(); + const {translate} = useLocalize(); const policyID = route.params.policyID; const error = route.params.error; @@ -35,29 +37,61 @@ function WorkspaceOwnerChangeCheckPage({route}: WorkspaceMemberDetailsPageProps) }, []); const cancel = useCallback(() => { - + }, []); + const confirmationTitle = useMemo(() => { + switch (error) { + case CONST.POLICY.OWNERSHIP_ERRORS.AMOUNT_OWED: + return translate('workspace.changeOwner.outstandingBalance'); + default: + return null; + } + }, [error, translate]); + + const confirmationButtonText = useMemo(() => { + switch (error) { + case CONST.POLICY.OWNERSHIP_ERRORS.AMOUNT_OWED: + return translate('workspace.changeOwner.transferBalance'); + default: + return ''; + } + }, [error, translate]); + + const confirmationText = useMemo(() => { + switch (error) { + case CONST.POLICY.OWNERSHIP_ERRORS.AMOUNT_OWED: + return translate('workspace.changeOwner.transferBalanceFirstParagraph', {email: 'test@test.com', amount: '$50.00'}); + default: + return null; + } + }, [error, translate]); + return ( Navigation.goBack()} /> - Current error: {error} + {confirmationTitle} + {confirmationText} {shouldAskForConfirmation ? ( + text={confirmationButtonText} + /> ) : ( + text={translate('common.buttonConfirm')} + /> )} From 1acd6eaca8c525191a4a21f5b75a90132b66326d Mon Sep 17 00:00:00 2001 From: ruben-rebelo Date: Tue, 12 Mar 2024 10:02:12 +0000 Subject: [PATCH 034/188] [TS migration][storybook] Fixed styling and feedback --- .storybook/webpack.config.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.storybook/webpack.config.ts b/.storybook/webpack.config.ts index 6f7ee023643c..bff8c3fa6747 100644 --- a/.storybook/webpack.config.ts +++ b/.storybook/webpack.config.ts @@ -1,7 +1,5 @@ /* eslint-disable no-underscore-dangle */ - /* eslint-disable no-param-reassign */ - /* eslint-disable @typescript-eslint/naming-convention */ import dotenv from 'dotenv'; import path from 'path'; @@ -18,7 +16,7 @@ type CustomWebpackConfig = { }; }; -let envFile; +let envFile: string | null; switch (process.env.ENV) { case 'production': envFile = '.env.production'; From 1fb509f9469de830ca27b66c644cb97df364c324 Mon Sep 17 00:00:00 2001 From: burczu Date: Tue, 12 Mar 2024 11:28:12 +0100 Subject: [PATCH 035/188] temporary copy added --- src/languages/en.ts | 18 ++++++++-- src/languages/es.ts | 19 +++++++++-- .../members/WorkspaceMemberDetailsPage.tsx | 7 +--- .../members/WorkspaceOwnerChangeCheckPage.tsx | 33 ++++++++++++++++--- 4 files changed, 61 insertions(+), 16 deletions(-) diff --git a/src/languages/en.ts b/src/languages/en.ts index 51c3623aaaed..37a2cfbd03e6 100755 --- a/src/languages/en.ts +++ b/src/languages/en.ts @@ -1969,9 +1969,21 @@ export default { }, changeOwner: { changeOwnerPageTitle: 'Change owner', - outstandingBalance: 'Outstanding balance', - transferBalance: 'Transfer balance', - transferBalanceFirstParagraph: ({email, amount}) => `The account owing this workspace (${email}) has an outstanding balance.\n\nDo you want to transfer this amount ${amount} in order to take over billing for this workspace? Your payment card will be charged immediately.`, + amountOwedTitle: 'Amount owed title', + amountOwedButtonText: 'Amount owed button text', + amountOwedText: 'Amount owed paragraph text.', + ownerOwesAmountTitle: 'Owner owes amount title', + ownerOwesAmountButtonText: 'Owner owes amount button text', + ownerOwesAmountText: 'Owner owes amount paragraph text.', + subscriptionTitle: 'Subscription title', + subscriptionButtonText: 'Subscription button text', + subscriptionText: 'Subscription paragraph text.', + duplicateSubscriptionTitle: 'Duplicate subscription title', + duplicateSubscriptionButtonText: 'Duplicate subscription button text', + duplicateSubscriptionText: 'Duplicate subscription paragraph text.', + hasFailedSettlementsTitle: 'Has failed settlements title', + hasFailedSettlementsButtonText: 'Has failed settlements button text', + hasFailedSettlementsText: 'Has failed settlements paragraph text.', } }, getAssistancePage: { diff --git a/src/languages/es.ts b/src/languages/es.ts index 6c5b5c73e589..51701d38c501 100644 --- a/src/languages/es.ts +++ b/src/languages/es.ts @@ -1995,9 +1995,22 @@ export default { }, changeOwner: { changeOwnerPageTitle: 'Cambio de propietario', - outstandingBalance: 'Saldo pendiente', - transferBalance: 'Transfer balance', - transferBalanceFirstParagraph: ({email, amount}) => `La cuenta que debe este espacio de trabajo (${email}) tiene un saldo pendiente.\n\n¿Desea transferir este monto ${amount} para hacerse cargo de la facturación de este espacio de trabajo? El cargo en su tarjeta de pago se realizará inmediatamente.`, + // TODO: add spanish translations below + amountOwedTitle: 'Amount owed title', + amountOwedButtonText: 'Amount owed button text', + amountOwedText: 'Amount owed paragraph text.', + ownerOwesAmountTitle: 'Owner owes amount title', + ownerOwesAmountButtonText: 'Owner owes amount button text', + ownerOwesAmountText: 'Owner owes amount paragraph text.', + subscriptionTitle: 'Subscription title', + subscriptionButtonText: 'Subscription button text', + subscriptionText: 'Subscription paragraph text.', + duplicateSubscriptionTitle: 'Duplicate subscription title', + duplicateSubscriptionButtonText: 'Duplicate subscription button text', + duplicateSubscriptionText: 'Duplicate subscription paragraph text.', + hasFailedSettlementTitle: 'Has failed settlement title', + hasFailedSettlementButtonText: 'Has failed settlement button text', + hasFailedSettlementText: 'Has failed settlement paragraph text.', } }, getAssistancePage: { diff --git a/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx b/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx index 294138c7daa9..f914d4697a31 100644 --- a/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx +++ b/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx @@ -93,10 +93,6 @@ function WorkspaceMemberDetailsPage({personalDetails, policyMembers, policy, rou Policy.requestWorkspaceOwnerChange(policyID); }, [policyID]); - const temporaryOpenCheckPage = () => { - Navigation.navigate(ROUTES.WORKSPACE_OWNER_CHANGE_CHECK.getRoute(policyID, CONST.POLICY.OWNERSHIP_ERRORS.AMOUNT_OWED)); - }; - return ( @@ -128,8 +124,7 @@ function WorkspaceMemberDetailsPage({personalDetails, policyMembers, policy, rou {isSelectedMemberOwner && isCurrentUserAdmin && !isCurrentUserOwner ? (