Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate DragAndDrop/index.js to function component #23252

Closed
wants to merge 6 commits into from

Conversation

jczekalski
Copy link
Contributor

Details

Fixed Issues

$ #16142

Tests

  • Verify that no errors appear in the JS console

Offline tests

QA Steps

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
web.mov
Mobile Web - Chrome Not sure if drag and drop to chat feature can be tested on this platform.
Mobile Web - Safari Not sure if drag and drop to chat feature can be tested on this platform.
Desktop
electron.mov
iOS Not sure if drag and drop to chat feature can be tested on this platform.
Android Not sure if drag and drop to chat feature can be tested on this platform.

@jczekalski jczekalski marked this pull request as ready for review July 20, 2023 12:30
@jczekalski jczekalski requested a review from a team as a code owner July 20, 2023 12:30
@melvin-bot melvin-bot bot requested review from thesahindia and removed request for a team July 20, 2023 12:30
@melvin-bot
Copy link

melvin-bot bot commented Jul 20, 2023

@thesahindia Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@AndrewGable AndrewGable requested review from allroundexperts and removed request for thesahindia July 21, 2023 15:08
@allroundexperts
Copy link
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
Mobile Web - Chrome
Mobile Web - Safari
Desktop
iOS
Android

Copy link
Contributor

@allroundexperts allroundexperts left a comment

Choose a reason for hiding this comment

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

Changes needed.

Copy link
Contributor

@roryabraham roryabraham left a comment

Choose a reason for hiding this comment

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

I left a bunch of comments related to this code in #23046 before I knew that this PR existed. Can we try to address those?

Copy link
Contributor

@roryabraham roryabraham left a comment

Choose a reason for hiding this comment

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

Ended up having a lot of comments, so I just created a diff with all the changes I'd like to see here. I think the end result ends up being much simpler and cleaner.

diff --git a/src/components/DragAndDrop/DropZone/index.js b/src/components/DragAndDrop/DropZone/index.js
index 525e5a8c14..a073009e22 100644
--- a/src/components/DragAndDrop/DropZone/index.js
+++ b/src/components/DragAndDrop/DropZone/index.js
@@ -12,7 +12,7 @@ const propTypes = {
     children: PropTypes.node.isRequired,
 
     /** Required for drag and drop to properly detect dropzone */
-    dropZoneId: PropTypes.string.isRequired,
+    dropZoneID: PropTypes.string.isRequired,
 };
 
 function DropZone(props) {
@@ -21,7 +21,7 @@ function DropZone(props) {
             <View style={[styles.fullScreenTransparentOverlay, styles.alignItemsCenter, styles.justifyContentCenter]}>{props.children}</View>
             {/* Necessary for blocking events on content which can publish unwanted dragleave even if we are inside dropzone  */}
             <View
-                nativeID={props.dropZoneId}
+                nativeID={props.dropZoneID}
                 style={styles.dropZoneTopInvisibleOverlay}
             />
         </Portal>
diff --git a/src/components/DragAndDrop/dragAndDropPropTypes.js b/src/components/DragAndDrop/dragAndDropPropTypes.js
index f0b56caeb3..b0720ec99f 100644
--- a/src/components/DragAndDrop/dragAndDropPropTypes.js
+++ b/src/components/DragAndDrop/dragAndDropPropTypes.js
@@ -1,9 +1,6 @@
 import PropTypes from 'prop-types';
 
 export default {
-    /** Callback to fire when a file has being dragged over the text input & report body */
-    onDragOver: PropTypes.func,
-
     /** Callback to fire when a file has been dragged into the text input & report body */
     onDragEnter: PropTypes.func.isRequired,
 
@@ -13,12 +10,15 @@ export default {
     /** Callback to fire when a file is dropped on the text input & report body */
     onDrop: PropTypes.func.isRequired,
 
-    /** Guard for accepting drops in drop zone. Drag event is passed to this function as first parameter */
-    shouldAcceptDrop: PropTypes.func,
-
     /** Id of the element on which we want to detect drag */
-    dropZoneId: PropTypes.string.isRequired,
+    dropZoneID: PropTypes.string.isRequired,
 
     /** Id of the element which is shown while drag is active */
-    activeDropZoneId: PropTypes.string.isRequired,
+    activeDropZoneID: PropTypes.string.isRequired,
+
+    /** Whether drag & drop should be disabled */
+    isDisabled: PropTypes.bool,
+
+    /** Rendered child component */
+    children: PropTypes.node.isRequired,
 };
diff --git a/src/components/DragAndDrop/index.js b/src/components/DragAndDrop/index.js
index 150137a92f..38356efabd 100644
--- a/src/components/DragAndDrop/index.js
+++ b/src/components/DragAndDrop/index.js
@@ -1,202 +1,135 @@
 import {useCallback, useEffect, useRef} from 'react';
-import PropTypes from 'prop-types';
 import _ from 'underscore';
+import {useIsFocused} from '@react-navigation/native';
 
-import variables from '../../styles/variables';
 import DragAndDropPropTypes from './dragAndDropPropTypes';
-import withNavigationFocus from '../withNavigationFocus';
-import usePrevious from '../../hooks/usePrevious';
+import useWindowDimensions from '../../hooks/useWindowDimensions';
+import useEffectOnPageLoad from '../../hooks/useEffectOnPageLoad';
 
 const COPY_DROP_EFFECT = 'copy';
 const NONE_DROP_EFFECT = 'none';
 
-const DRAG_OVER_EVENT = 'dragover';
 const DRAG_ENTER_EVENT = 'dragenter';
 const DRAG_LEAVE_EVENT = 'dragleave';
 const DROP_EVENT = 'drop';
-const RESIZE_EVENT = 'resize';
 
-const propTypes = {
-    ...DragAndDropPropTypes,
-
-    /** Callback to fire when a file has being dragged over the text input & report body. This prop is necessary to be inlined to satisfy the linter */
-    onDragOver: DragAndDropPropTypes.onDragOver,
-
-    /** Guard for accepting drops in drop zone. Drag event is passed to this function as first parameter. This prop is necessary to be inlined to satisfy the linter */
-    shouldAcceptDrop: PropTypes.func,
-
-    /** Whether drag & drop should be disabled */
-    disabled: PropTypes.bool,
+/**
+ * @param {Event} event – drag event
+ * @returns {Boolean}
+ */
+function shouldAcceptDrop(event) {
+    return _.some(event.dataTransfer.types, (type) => type === 'Files');
+}
 
-    /** Rendered child component */
-    children: PropTypes.node.isRequired,
-};
+function DragAndDrop({onDragEnter, onDragLeave, onDrop, dropZoneID, activeDropZoneID, children, isDisabled = false}) {
+    const isFocused = useIsFocused();
+    const {windowWidth, isSmallScreenWidth} = useWindowDimensions();
 
-const defaultProps = {
-    onDragOver: () => {},
-    shouldAcceptDrop: (e) => {
-        if (e.dataTransfer.types) {
-            for (let i = 0; i < e.dataTransfer.types.length; i++) {
-                if (e.dataTransfer.types[i] === 'Files') {
-                    return true;
-                }
-            }
-        }
-        return false;
-    },
-    disabled: false,
-};
-
-function DragAndDrop(props) {
-    const prevIsFocused = usePrevious(props.isFocused);
-    const prevIsDisabled = usePrevious(props.disabled);
     const dropZone = useRef(null);
     const dropZoneRect = useRef(null);
-    /*
-        Last detected drag state on the dropzone -> we start with dragleave since user is not dragging initially.
-        This state is updated when drop zone is left/entered entirely(not taking the children in the account) or entire window is left
-    */
-    const dropZoneDragState = useRef(DRAG_LEAVE_EVENT);
 
-    /**
-     * @param {Object} event native Event
-     */
-    const dragOverHandler = (event) => {
-        props.onDragOver(event);
-    };
-
-    const throttledDragOverHandler = _.throttle(dragOverHandler, 100);
-
-    const calculateDropZoneClientReact = useCallback(() => {
-        const boundingClientRect = dropZone.current.getBoundingClientRect();
-
-        // Handle edge case when we are under responsive breakpoint the browser doesn't normalize rect.left to 0 and rect.right to window.innerWidth
-        return {
-            width: boundingClientRect.width,
-            left: window.innerWidth <= variables.mobileResponsiveWidthBreakpoint ? 0 : boundingClientRect.left,
-            right: window.innerWidth <= variables.mobileResponsiveWidthBreakpoint ? window.innerWidth : boundingClientRect.right,
-            top: boundingClientRect.top,
-            bottom: boundingClientRect.bottom,
-        };
-    }, []);
+    useEffect(() => {
+        dropZone.current = document.getElementById(dropZoneID);
+    }, [dropZoneID]);
+
+    useEffectOnPageLoad(
+        () =>
+            _.throttle(() => {
+                const boundingClientRect = dropZone.current.getBoundingClientRect();
+                dropZoneRect.current = {
+                    width: boundingClientRect.width,
+                    left: isSmallScreenWidth ? 0 : boundingClientRect.left,
+                    right: isSmallScreenWidth ? windowWidth : boundingClientRect.right,
+                    top: boundingClientRect.top,
+                    bottom: boundingClientRect.bottom,
+                };
+            }, 100),
+        [windowWidth, isSmallScreenWidth],
+    );
 
-    const dragNDropWindowResizeListener = () => {
-        // Update bounding client rect on window resize
-        dropZoneRect.current = calculateDropZoneClientReact();
-    };
+    /*
+     * Last detected drag state on the dropzone -> we start with dragleave since user is not dragging initially.
+     * This state is updated when drop zone is left/entered entirely(not taking the children in the account) or entire window is left
+     */
+    const dropZoneDragState = useRef(DRAG_LEAVE_EVENT);
 
-    const throttledDragNDropWindowResizeListener = _.throttle(dragNDropWindowResizeListener, 100);
+    // If this component is out of focus or disabled, reset the drag state back to the default
+    useEffect(() => {
+        if (isFocused && !isDisabled) {
+            return;
+        }
+        dropZoneDragState.current = DRAG_LEAVE_EVENT;
+    }, [isFocused, isDisabled]);
 
     /**
+     * Handles all types of drag-N-drop events on the drop zone associated with composer
+     *
      * @param {Object} event native Event
      */
     const dropZoneDragHandler = useCallback(
         (event) => {
-            // Setting dropEffect for dragover is required for '+' icon on certain platforms/browsers (eg. Safari)
-            switch (event.type) {
-                case DRAG_OVER_EVENT:
-                    // Continuous event -> can hurt performance, be careful when subscribing
-                    // eslint-disable-next-line no-param-reassign
-                    event.dataTransfer.dropEffect = COPY_DROP_EFFECT;
-                    throttledDragOverHandler(event);
-                    break;
-                case DRAG_ENTER_EVENT:
-                    // Avoid reporting onDragEnter for children views -> not performant
-                    if (dropZoneDragState.current === DRAG_LEAVE_EVENT) {
-                        dropZoneDragState.current = DRAG_ENTER_EVENT;
-                        // eslint-disable-next-line no-param-reassign
-                        event.dataTransfer.dropEffect = COPY_DROP_EFFECT;
-                        props.onDragEnter(event);
-                    }
-                    break;
-                case DRAG_LEAVE_EVENT:
-                    if (dropZoneDragState.current === DRAG_ENTER_EVENT) {
-                        if (
-                            event.clientY <= dropZoneRect.current.top ||
-                            event.clientY >= dropZoneRect.current.bottom ||
-                            event.clientX <= dropZoneRect.current.left ||
-                            event.clientX >= dropZoneRect.current.right ||
-                            // Cancel drag when file manager is on top of the drop zone area - works only on chromium
-                            (event.target.getAttribute('id') === props.activeDropZoneId && !event.relatedTarget)
-                        ) {
-                            dropZoneDragState.current = DRAG_LEAVE_EVENT;
-                            props.onDragLeave(event);
-                        }
-                    }
-                    break;
-                case DROP_EVENT:
-                    dropZoneDragState.current = DRAG_LEAVE_EVENT;
-                    props.onDrop(event);
-                    break;
-                default:
-                    break;
+            if (!isFocused || isDisabled) {
+                return;
             }
-        },
-        [props, throttledDragOverHandler],
-    );
 
-    /**
-     * Handles all types of drag-N-drop events on the drop zone associated with composer
-     *
-     * @param {Object} event native Event
-     */
-    const dropZoneDragListener = useCallback(
-        (event) => {
             event.preventDefault();
 
-            if (dropZone.current.contains(event.target) && props.shouldAcceptDrop(event)) {
-                dropZoneDragHandler(event);
+            if (dropZone.current.contains(event.target) && shouldAcceptDrop(event)) {
+                switch (event.type) {
+                    case DRAG_ENTER_EVENT:
+                        // Avoid reporting onDragEnter for children views -> not performant
+                        if (dropZoneDragState.current === DRAG_LEAVE_EVENT) {
+                            dropZoneDragState.current = DRAG_ENTER_EVENT;
+                            // eslint-disable-next-line no-param-reassign
+                            event.dataTransfer.dropEffect = COPY_DROP_EFFECT;
+                            onDragEnter(event);
+                        }
+                        break;
+                    case DRAG_LEAVE_EVENT:
+                        if (dropZoneDragState.current === DRAG_ENTER_EVENT) {
+                            if (
+                                event.clientY <= dropZoneRect.current.top ||
+                                event.clientY >= dropZoneRect.current.bottom ||
+                                event.clientX <= dropZoneRect.current.left ||
+                                event.clientX >= dropZoneRect.current.right ||
+                                // Cancel drag when file manager is on top of the drop zone area - works only on chromium
+                                (event.target.getAttribute('id') === activeDropZoneID && !event.relatedTarget)
+                            ) {
+                                dropZoneDragState.current = DRAG_LEAVE_EVENT;
+                                onDragLeave(event);
+                            }
+                        }
+                        break;
+                    case DROP_EVENT:
+                        dropZoneDragState.current = DRAG_LEAVE_EVENT;
+                        onDrop(event);
+                        break;
+                    default:
+                        break;
+                }
             } else {
                 // eslint-disable-next-line no-param-reassign
                 event.dataTransfer.dropEffect = NONE_DROP_EFFECT;
             }
         },
-        [props, dropZoneDragHandler],
+        [isFocused, isDisabled, onDragEnter, onDragLeave, activeDropZoneID, onDrop],
     );
 
-    const addEventListeners = useCallback(() => {
-        dropZone.current = document.getElementById(props.dropZoneId);
-        dropZoneRect.current = calculateDropZoneClientReact();
-        document.addEventListener(DRAG_OVER_EVENT, dropZoneDragListener);
-        document.addEventListener(DRAG_ENTER_EVENT, dropZoneDragListener);
-        document.addEventListener(DRAG_LEAVE_EVENT, dropZoneDragListener);
-        document.addEventListener(DROP_EVENT, dropZoneDragListener);
-        window.addEventListener(RESIZE_EVENT, throttledDragNDropWindowResizeListener);
-    }, [props.dropZoneId, calculateDropZoneClientReact, dropZoneDragListener, throttledDragNDropWindowResizeListener]);
-
-    const removeEventListeners = useCallback(() => {
-        document.removeEventListener(DRAG_OVER_EVENT, dropZoneDragListener);
-        document.removeEventListener(DRAG_ENTER_EVENT, dropZoneDragListener);
-        document.removeEventListener(DRAG_LEAVE_EVENT, dropZoneDragListener);
-        document.removeEventListener(DROP_EVENT, dropZoneDragListener);
-        window.removeEventListener(RESIZE_EVENT, throttledDragNDropWindowResizeListener);
-    }, [dropZoneDragListener, throttledDragNDropWindowResizeListener]);
-
     useEffect(() => {
-        if (props.disabled) {
-            return;
-        }
-        addEventListeners();
-
-        return removeEventListeners;
-        // eslint-disable-next-line react-hooks/exhaustive-deps
-    }, []);
-
-    useEffect(() => {
-        if (props.isFocused === prevIsFocused && props.disabled === prevIsDisabled) {
-            return;
-        }
-        if (!props.isFocused || props.disabled) {
-            removeEventListeners();
-        } else {
-            addEventListeners();
-        }
-    }, [props.disabled, props.isFocused, prevIsDisabled, prevIsFocused, addEventListeners, removeEventListeners]);
+        document.addEventListener(DRAG_ENTER_EVENT, dropZoneDragHandler);
+        document.addEventListener(DRAG_LEAVE_EVENT, dropZoneDragHandler);
+        document.addEventListener(DROP_EVENT, dropZoneDragHandler);
+        return () => {
+            document.removeEventListener(DRAG_ENTER_EVENT, dropZoneDragHandler);
+            document.removeEventListener(DRAG_ENTER_EVENT, dropZoneDragHandler);
+            document.removeEventListener(DRAG_LEAVE_EVENT, dropZoneDragHandler);
+            document.removeEventListener(DROP_EVENT, dropZoneDragHandler);
+        };
+    }, [dropZoneDragHandler]);
 
-    return props.children;
+    return children;
 }
 
-DragAndDrop.propTypes = propTypes;
-DragAndDrop.defaultProps = defaultProps;
+DragAndDrop.propTypes = DragAndDropPropTypes;
 
-export default withNavigationFocus(DragAndDrop);
+export default DragAndDrop;
diff --git a/src/components/DragAndDrop/index.native.js b/src/components/DragAndDrop/index.native.js
index 4c00e56130..8d678752e7 100644
--- a/src/components/DragAndDrop/index.native.js
+++ b/src/components/DragAndDrop/index.native.js
@@ -1,5 +1,8 @@
-const DragAndDrop = (props) => props.children;
+import DragAndDropPropTypes from './dragAndDropPropTypes';
 
+const DragAndDrop = ({children}) => children;
+
+DragAndDrop.propTypes = DragAndDropPropTypes;
 DragAndDrop.displayName = 'DragAndDrop';
 
 export default DragAndDrop;
diff --git a/src/hooks/useEffectOnPageLoad.js b/src/hooks/useEffectOnPageLoad.js
new file mode 100644
index 0000000000..2f5a40217c
--- /dev/null
+++ b/src/hooks/useEffectOnPageLoad.js
@@ -0,0 +1,24 @@
+import {useEffect, useRef} from 'react';
+
+/**
+ * @param {Function} onPageLoad
+ * @param {Array} dependencies
+ */
+export default function useEffectOnPageLoad(onPageLoad, dependencies = []) {
+    const onPageLoadRef = useRef(onPageLoad);
+    onPageLoadRef.current = onPageLoad;
+
+    useEffect(() => {
+        function onPageLoadCallback() {
+            onPageLoadRef.current();
+        }
+
+        if (document.readyState === 'complete') {
+            onPageLoadCallback();
+        } else {
+            window.addEventListener('load', onPageLoadCallback);
+            return () => window.removeEventListener('load', onPageLoadCallback);
+        }
+        // eslint-disable-next-line react-hooks/exhaustive-deps
+    }, dependencies);
+}
diff --git a/src/pages/home/report/ReportActionCompose.js b/src/pages/home/report/ReportActionCompose.js
index 8155f09b2a..b1b8ad18dd 100644
--- a/src/pages/home/report/ReportActionCompose.js
+++ b/src/pages/home/report/ReportActionCompose.js
@@ -1112,8 +1112,8 @@ class ReportActionCompose extends React.Component {
                                     </AttachmentPicker>
                                     <View style={[styles.textInputComposeSpacing, styles.textInputComposeBorder]}>
                                         <DragAndDrop
-                                            dropZoneId={this.props.dragAndDropId}
-                                            activeDropZoneId={CONST.REPORT.ACTIVE_DROP_NATIVE_ID + this.props.reportID}
+                                            dropZoneID={this.props.dragAndDropId}
+                                            activeDropZoneID={CONST.REPORT.ACTIVE_DROP_NATIVE_ID + this.props.reportID}
                                             onDragEnter={() => {
                                                 this.setState({isDraggingOver: true});
                                             }}
@@ -1133,7 +1133,7 @@ class ReportActionCompose extends React.Component {
 
                                                 this.setState({isDraggingOver: false});
                                             }}
-                                            disabled={this.props.disabled}
+                                            isDisabled={this.props.disabled}
                                         >
                                             <Composer
                                                 checkComposerVisibility={() => this.checkComposerVisibility()}
diff --git a/src/pages/home/report/ReportDropUI.js b/src/pages/home/report/ReportDropUI.js
index f59d0a3113..6e0e6ef68c 100644
--- a/src/pages/home/report/ReportDropUI.js
+++ b/src/pages/home/report/ReportDropUI.js
@@ -16,7 +16,7 @@ function ReportDropUI(props) {
     return (
         <DropZone
             dropZoneViewHolderName={CONST.REPORT.DROP_HOST_NAME}
-            dropZoneId={CONST.REPORT.ACTIVE_DROP_NATIVE_ID}
+            dropZoneID={CONST.REPORT.ACTIVE_DROP_NATIVE_ID}
         >
             <View style={styles.mb3}>
                 <Icon
diff --git a/src/stories/DragAndDrop.stories.js b/src/stories/DragAndDrop.stories.js
index 0398e5af3a..ece5394f49 100644
--- a/src/stories/DragAndDrop.stories.js
+++ b/src/stories/DragAndDrop.stories.js
@@ -27,8 +27,8 @@ function Default() {
             {/* DragAndDrop does not need to render drop area as children since it is connected to it via id, which gives us flexibility to bring DragAndDrop where your
             draggingOver state is located */}
             <DragAndDrop
-                dropZoneId="dropId"
-                activeDropZoneId="activeDropZoneId"
+                dropZoneID="dropId"
+                activeDropZoneID="activeDropZoneID"
                 onDragEnter={() => {
                     setDraggingOver(true);
                 }}
@@ -75,7 +75,7 @@ function Default() {
             {draggingOver && (
                 <DropZone
                     dropZoneViewHolderName="portalHost"
-                    dropZoneId="activeDropZoneId"
+                    dropZoneID="activeDropZoneID"
                 />
             )}
         </PortalProvider>

and looking at the new state of the component, it looks like this:

import {useCallback, useEffect, useRef} from 'react';
import _ from 'underscore';
import {useIsFocused} from '@react-navigation/native';

import DragAndDropPropTypes from './dragAndDropPropTypes';
import useWindowDimensions from '../../hooks/useWindowDimensions';
import useEffectOnPageLoad from '../../hooks/useEffectOnPageLoad';

const COPY_DROP_EFFECT = 'copy';
const NONE_DROP_EFFECT = 'none';

const DRAG_ENTER_EVENT = 'dragenter';
const DRAG_LEAVE_EVENT = 'dragleave';
const DROP_EVENT = 'drop';

/**
 * @param {Event} event – drag event
 * @returns {Boolean}
 */
function shouldAcceptDrop(event) {
    return _.some(event.dataTransfer.types, (type) => type === 'Files');
}

function DragAndDrop({onDragEnter, onDragLeave, onDrop, dropZoneID, activeDropZoneID, children, isDisabled = false}) {
    const isFocused = useIsFocused();
    const {windowWidth, isSmallScreenWidth} = useWindowDimensions();

    const dropZone = useRef(null);
    const dropZoneRect = useRef(null);

    useEffect(() => {
        dropZone.current = document.getElementById(dropZoneID);
    }, [dropZoneID]);

    useEffectOnPageLoad(
        () =>
            _.throttle(() => {
                const boundingClientRect = dropZone.current.getBoundingClientRect();
                dropZoneRect.current = {
                    width: boundingClientRect.width,
                    left: isSmallScreenWidth ? 0 : boundingClientRect.left,
                    right: isSmallScreenWidth ? windowWidth : boundingClientRect.right,
                    top: boundingClientRect.top,
                    bottom: boundingClientRect.bottom,
                };
            }, 100),
        [windowWidth, isSmallScreenWidth],
    );

    /*
     * Last detected drag state on the dropzone -> we start with dragleave since user is not dragging initially.
     * This state is updated when drop zone is left/entered entirely(not taking the children in the account) or entire window is left
     */
    const dropZoneDragState = useRef(DRAG_LEAVE_EVENT);

    // If this component is out of focus or disabled, reset the drag state back to the default
    useEffect(() => {
        if (isFocused && !isDisabled) {
            return;
        }
        dropZoneDragState.current = DRAG_LEAVE_EVENT;
    }, [isFocused, isDisabled]);

    /**
     * Handles all types of drag-N-drop events on the drop zone associated with composer
     *
     * @param {Object} event native Event
     */
    const dropZoneDragHandler = useCallback(
        (event) => {
            if (!isFocused || isDisabled) {
                return;
            }

            event.preventDefault();

            if (dropZone.current.contains(event.target) && shouldAcceptDrop(event)) {
                switch (event.type) {
                    case DRAG_ENTER_EVENT:
                        // Avoid reporting onDragEnter for children views -> not performant
                        if (dropZoneDragState.current === DRAG_LEAVE_EVENT) {
                            dropZoneDragState.current = DRAG_ENTER_EVENT;
                            // eslint-disable-next-line no-param-reassign
                            event.dataTransfer.dropEffect = COPY_DROP_EFFECT;
                            onDragEnter(event);
                        }
                        break;
                    case DRAG_LEAVE_EVENT:
                        if (dropZoneDragState.current === DRAG_ENTER_EVENT) {
                            if (
                                event.clientY <= dropZoneRect.current.top ||
                                event.clientY >= dropZoneRect.current.bottom ||
                                event.clientX <= dropZoneRect.current.left ||
                                event.clientX >= dropZoneRect.current.right ||
                                // Cancel drag when file manager is on top of the drop zone area - works only on chromium
                                (event.target.getAttribute('id') === activeDropZoneID && !event.relatedTarget)
                            ) {
                                dropZoneDragState.current = DRAG_LEAVE_EVENT;
                                onDragLeave(event);
                            }
                        }
                        break;
                    case DROP_EVENT:
                        dropZoneDragState.current = DRAG_LEAVE_EVENT;
                        onDrop(event);
                        break;
                    default:
                        break;
                }
            } else {
                // eslint-disable-next-line no-param-reassign
                event.dataTransfer.dropEffect = NONE_DROP_EFFECT;
            }
        },
        [isFocused, isDisabled, onDragEnter, onDragLeave, activeDropZoneID, onDrop],
    );

    useEffect(() => {
        document.addEventListener(DRAG_ENTER_EVENT, dropZoneDragHandler);
        document.addEventListener(DRAG_LEAVE_EVENT, dropZoneDragHandler);
        document.addEventListener(DROP_EVENT, dropZoneDragHandler);
        return () => {
            document.removeEventListener(DRAG_ENTER_EVENT, dropZoneDragHandler);
            document.removeEventListener(DRAG_ENTER_EVENT, dropZoneDragHandler);
            document.removeEventListener(DRAG_LEAVE_EVENT, dropZoneDragHandler);
            document.removeEventListener(DROP_EVENT, dropZoneDragHandler);
        };
    }, [dropZoneDragHandler]);

    return children;
}

DragAndDrop.propTypes = DragAndDropPropTypes;

export default DragAndDrop;

@AndrewGable discovered in #23046 that it was necessary to measure the dropZoneRect after the page loaded, so that's why I created that useEffectOnPageLoad hook.

this.dropZoneDragListener = this.dropZoneDragListener.bind(this);

/*
function DragAndDrop(props) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please destructure props here

dragOverHandler(event) {
this.props.onDragOver(event);
}
const dragOverHandler = (event) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function seems kind of useless. Let's just fold it into the throttled one? Also, more importantly, with this implementation _.throttle will get called again with every re-render, and I think that will cause the timer to restart. I think something like this is more what we want:

function DragAndDrop({onDragOver, ...allTheOtherProps}) {
   ...
   ...

   const dragOverHandler = useCallback(_.throttle(onDragOver, 100), [onDragOver]);

   ...
   ...
}

const DRAG_LEAVE_EVENT = 'dragleave';
const DROP_EVENT = 'drop';
const RESIZE_EVENT = 'resize';

const propTypes = {
...DragAndDropPropTypes,
Copy link
Contributor

Choose a reason for hiding this comment

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

The onDragOver prop is included in DragAndDropProps so is duplicated on line 16. Let's remove it please.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, onDragOver is unused by any code consuming this component, so we should just remove it.

const DRAG_LEAVE_EVENT = 'dragleave';
const DROP_EVENT = 'drop';
const RESIZE_EVENT = 'resize';

const propTypes = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Also:

  • the disabled prop should be renamed to isDisabled
  • dropZoneId should be renamed to dropZoneID
  • activeDropZoneId should be renamed to activeDropZoneID


if (this.dropZone.contains(event.target) && this.props.shouldAcceptDrop(event)) {
this.dropZoneDragHandler(event);
if (dropZone.current.contains(event.target) && props.shouldAcceptDrop(event)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldAcceptDrop is never passed in as a prop to DragAndDrop AFAICT, so it doesn't really need to be a prop. It can just declare as a pure function outside of the component.

window.removeEventListener(RESIZE_EVENT, throttledDragNDropWindowResizeListener);
}, [dropZoneDragListener, throttledDragNDropWindowResizeListener]);

useEffect(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's pretty weird how we add and remove event listeners in these effect hooks depending on the isDisabled and isFocused props. I think it would be much simpler to just adjust the event handler so that it accounts for those props, and then adding and removing the event listeners becomes much simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! I also thought that this was a bit weird, but decided to keep the current implementation since the purpose of the task is just to rewrite the component from class to functional syntax. I'll implement the changes you suggested.

const DRAG_LEAVE_EVENT = 'dragleave';
const DROP_EVENT = 'drop';
const RESIZE_EVENT = 'resize';

const propTypes = {
...DragAndDropPropTypes,
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, I noticed that DragAndDropPropTypes are not used outside this file...

@roryabraham
Copy link
Contributor

Closing this as I was informed that @jczekalski is going to be working on other projects. Thanks for your work on this so far @jczekalski. I'm going to create a new PR based on this one incorporating my above feedback since this is HOLDing another PR that's a roadmap priority. @allroundexperts I'll request your review on my PR when it's ready

@roryabraham roryabraham mentioned this pull request Jul 25, 2023
58 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants