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

Hide flagged attachment in attachment carousel #24564

Merged
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f53d3dc
hide flagged attachment in carousel
bernhardoj Aug 15, 2023
6d0190c
Merge branch 'main' into fix/22915-hide-attachment-in-carousel
bernhardoj Aug 16, 2023
104f0f4
remove auto hide after out of focus
bernhardoj Aug 16, 2023
7dfcf17
remove unused import
bernhardoj Aug 16, 2023
93fe8c3
Merge branch 'main' into fix/22915-hide-attachment-in-carousel
bernhardoj Aug 18, 2023
0fd41e9
add comment and SELECTION_SCRAPER_HIDDEN_ELEMENT dataset
bernhardoj Aug 18, 2023
6b6f267
Merge branch 'main' into fix/22915-hide-attachment-in-carousel
bernhardoj Aug 21, 2023
95045a5
Merge branch 'main' into fix/22915-hide-attachment-in-carousel
bernhardoj Aug 23, 2023
1734b16
show hidden message button on a flagged attachment
bernhardoj Aug 23, 2023
83c629e
change default onpress to undefined
bernhardoj Aug 23, 2023
d5b8130
use view if it's not pressable
bernhardoj Aug 23, 2023
4dbf950
add safe area padding bottom
bernhardoj Aug 23, 2023
e7956b3
add bg
bernhardoj Aug 23, 2023
8863e79
rename jsdoc param
bernhardoj Aug 23, 2023
dce6721
created new style
bernhardoj Aug 23, 2023
fc337d6
created new style
bernhardoj Aug 23, 2023
4202190
lint
bernhardoj Aug 23, 2023
4d41062
Merge branch 'main' into fix/22915-hide-attachment-in-carousel
bernhardoj Aug 28, 2023
8d22119
Merge with 'main' branch
bernhardoj Sep 3, 2023
e850d52
share the attachment visibilty state through context
bernhardoj Sep 4, 2023
3f396d6
Merge branch 'main' into fix/22915-hide-attachment-in-carousel
bernhardoj Sep 4, 2023
0b586f4
Merge branch 'main' into fix/22915-hide-attachment-in-carousel
bernhardoj Sep 6, 2023
619af69
memoize context value
bernhardoj Sep 6, 2023
f151ae0
Merge branch 'main' into fix/22915-hide-attachment-in-carousel
bernhardoj Sep 7, 2023
c315df5
use prev state to update
bernhardoj Sep 7, 2023
fe07eba
move the optimization up to the context
bernhardoj Sep 7, 2023
0315c87
rename variable
bernhardoj Sep 7, 2023
7244223
Merge branch 'main' into fix/22915-hide-attachment-in-carousel
bernhardoj Sep 11, 2023
31385c3
Merge branch 'main' into fix/22915-hide-attachment-in-carousel
bernhardoj Sep 11, 2023
b9ef612
use ref instead of state to prevent unnecessary rerender
bernhardoj Sep 11, 2023
ab4d5f3
optimize the code
bernhardoj Sep 11, 2023
9a0755c
update comment
bernhardoj Sep 12, 2023
4df91a2
simplify code
bernhardoj Sep 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {CurrentReportIDContextProvider} from './components/withCurrentReportID';
import {EnvironmentProvider} from './components/withEnvironment';
import * as Session from './libs/actions/Session';
import useDefaultDragAndDrop from './hooks/useDefaultDragAndDrop';
import {ReportAttachmentsProvider} from './pages/home/report/ReportAttachmentsContext';

// For easier debugging and development, when we are in web we expose Onyx to the window, so you can more easily set data into Onyx
if (window && Environment.isDevelopment()) {
Expand Down Expand Up @@ -56,6 +57,7 @@ function App() {
KeyboardStateProvider,
PopoverContextProvider,
CurrentReportIDContextProvider,
ReportAttachmentsProvider,
PickerStateProvider,
EnvironmentProvider,
ThemeProvider,
Expand Down
117 changes: 117 additions & 0 deletions src/components/Attachments/AttachmentCarousel/CarouselItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import React, {useContext, useState} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import CONST from '../../../CONST';
import styles from '../../../styles/styles';
import useLocalize from '../../../hooks/useLocalize';
import PressableWithoutFeedback from '../../Pressable/PressableWithoutFeedback';
import Text from '../../Text';
import Button from '../../Button';
import AttachmentView from '../AttachmentView';
import SafeAreaConsumer from '../../SafeAreaConsumer';
import ReportAttachmentsContext from '../../../pages/home/report/ReportAttachmentsContext';

const propTypes = {
/** Attachment required information such as the source and file name */
item: PropTypes.shape({
/** Report action ID of the attachment */
reportActionID: PropTypes.string,

/** Whether source URL requires authentication */
isAuthTokenRequired: PropTypes.bool,

/** The source (URL) of the attachment */
source: PropTypes.string,

/** File additional information of the attachment */
bernhardoj marked this conversation as resolved.
Show resolved Hide resolved
file: PropTypes.shape({
/** File name of the attachment */
name: PropTypes.string,
}),

/** Whether the attachment has been flagged */
hasBeenFlagged: PropTypes.bool,
}).isRequired,

/** Whether the attachment is currently being viewed in the carousel */
isFocused: PropTypes.bool.isRequired,

/** onPress callback */
onPress: PropTypes.func,
};

const defaultProps = {
onPress: undefined,
};

function CarouselItem({item, isFocused, onPress}) {
const {translate} = useLocalize();
const {isAttachmentHidden} = useContext(ReportAttachmentsContext);
const [isHidden, setIsHidden] = useState(() => {
const isAttachmentHiddenValue = isAttachmentHidden[item.reportActionID];
return isAttachmentHiddenValue === undefined ? item.hasBeenFlagged : isAttachmentHiddenValue;
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we create a simple hook, useHiddenAttachements(reportActionID) that returns a boolean, that will simplify the logic and make it more readable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely can, but I decided to not create it because it doesn't solve any actual problem. It reduces the number of characters we type but both isAttachmentHidden and updateIsAttachmentHidden are already available through ReportAttachmentsContext and it's still readable. (Actually my honest reason is I don't like creating a new file 😂)

Before:

const {isAttachmentHidden} = useContext(ReportAttachmentsContext);
const [isHidden, setIsHidden] = useState(() => {
    const isAttachmentHiddenValue = isAttachmentHidden[item.reportActionID];
    return isAttachmentHiddenValue === undefined ? item.hasBeenFlagged : isAttachmentHiddenValue;
});

After:

const isAttachmentHidden = useHiddenAttachments(item.reportActionID);
const [isHidden, setIsHidden] = useState(() => isAttachmentHidden === undefined ? item.hasBeenFlagged : isAttachmentHidden);

Let me know if it makes sense to not create the hook.

Copy link
Contributor

Choose a reason for hiding this comment

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

The problem is that any update to isAttachmentHidden will cause a re-render on all mounted CarouselItem components, even those which are not flagged.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That will be the same case with using hook. Using hook will still require us to access the context. Btw, it's not possible for the isAttachmentHidden state to get updated when we are inside the carousel.

Also, updates to isAttachmentHidden will re-render the whole app.


const renderButton = (style) => (
<Button
small
style={style}
onPress={() => setIsHidden(!isHidden)}
>
<Text
style={styles.buttonSmallText}
selectable={false}
dataSet={{[CONST.SELECTION_SCRAPER_HIDDEN_ELEMENT]: true}}
>
{isHidden ? translate('moderation.revealMessage') : translate('moderation.hideMessage')}
</Text>
</Button>
);

if (isHidden) {
const children = (
<>
<Text style={[styles.textLabelSupporting, styles.textAlignCenter, styles.lh20]}>{translate('moderation.flaggedContent')}</Text>
{renderButton([styles.mt2])}
</>
);
return onPress ? (
<PressableWithoutFeedback
style={[styles.attachmentRevealButtonContainer]}
onPress={onPress}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.IMAGEBUTTON}
accessibilityLabel={item.file.name || translate('attachmentView.unknownFilename')}
>
{children}
</PressableWithoutFeedback>
) : (
<View style={[styles.attachmentRevealButtonContainer]}>{children}</View>
);
}

return (
<View style={[styles.flex1]}>
<View style={[styles.flex1]}>
<AttachmentView
source={item.source}
file={item.file}
isAuthTokenRequired={item.isAuthTokenRequired}
isFocused={isFocused}
onPress={onPress}
isUsedInCarousel
/>
</View>

{item.hasBeenFlagged && (
<SafeAreaConsumer>
{({safeAreaPaddingBottomStyle}) => <View style={[styles.appBG, safeAreaPaddingBottomStyle]}>{renderButton([styles.m4, styles.alignSelfCenter])}</View>}
</SafeAreaConsumer>
)}
</View>
);
}

CarouselItem.propTypes = propTypes;
CarouselItem.defaultProps = defaultProps;

export default CarouselItem;
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ function extractAttachmentsFromReport(report, reportActions) {
// By iterating actions in chronological order and prepending each attachment
// we ensure correct order of attachments even across actions with multiple attachments.
attachments.unshift({
reportActionID: attribs['data-id'],
source: tryResolveUrlFromApiRoot(expensifySource || attribs.src),
isAuthTokenRequired: Boolean(expensifySource),
file: {name: attribs[CONST.ATTACHMENT_ORIGINAL_FILENAME_ATTRIBUTE]},
isReceipt: false,
hasBeenFlagged: attribs['data-flagged'] === 'true',
});
},
});
Expand Down Expand Up @@ -62,7 +64,10 @@ function extractAttachmentsFromReport(report, reportActions) {
}
}

htmlParser.write(_.get(action, ['message', 0, 'html']));
const decision = _.get(action, ['message', 0, 'moderationDecision', 'decision'], '');
const hasBeenFlagged = decision === CONST.MODERATION.MODERATOR_DECISION_PENDING_HIDE || decision === CONST.MODERATION.MODERATOR_DECISION_HIDDEN;
const html = _.get(action, ['message', 0, 'html'], '').replace('/>', `data-flagged="${hasBeenFlagged}" data-id="${action.reportActionID}"/>`);
htmlParser.write(html);
});
htmlParser.end();

Expand Down
11 changes: 5 additions & 6 deletions src/components/Attachments/AttachmentCarousel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import _ from 'underscore';
import * as DeviceCapabilities from '../../../libs/DeviceCapabilities';
import styles from '../../../styles/styles';
import CarouselActions from './CarouselActions';
import AttachmentView from '../AttachmentView';
import withWindowDimensions from '../../withWindowDimensions';
import CarouselButtons from './CarouselButtons';
import extractAttachmentsFromReport from './extractAttachmentsFromReport';
Expand All @@ -15,6 +14,7 @@ import withLocalize from '../../withLocalize';
import compose from '../../../libs/compose';
import useCarouselArrows from './useCarouselArrows';
import useWindowDimensions from '../../../hooks/useWindowDimensions';
import CarouselItem from './CarouselItem';
import Navigation from '../../../libs/Navigation/Navigation';
import BlockingView from '../../BlockingViews/BlockingView';
import * as Illustrations from '../../Icon/Illustrations';
Expand Down Expand Up @@ -143,21 +143,20 @@ function AttachmentCarousel({report, reportActions, source, onNavigate, setDownl
/**
* Defines how a single attachment should be rendered
* @param {Object} item
* @param {String} item.reportActionID
* @param {Boolean} item.isAuthTokenRequired
* @param {String} item.source
* @param {Object} item.file
* @param {String} item.file.name
* @param {Boolean} item.hasBeenFlagged
* @returns {JSX.Element}
*/
const renderItem = useCallback(
({item}) => (
<AttachmentView
source={item.source}
file={item.file}
isAuthTokenRequired={item.isAuthTokenRequired}
<CarouselItem
item={item}
isFocused={activeSource === item.source}
onPress={canUseTouchScreen ? () => setShouldShowArrows(!shouldShowArrows) : undefined}
isUsedInCarousel
/>
),
[activeSource, setShouldShowArrows, shouldShowArrows],
Expand Down
11 changes: 4 additions & 7 deletions src/components/Attachments/AttachmentCarousel/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import _ from 'underscore';
import AttachmentCarouselPager from './Pager';
import styles from '../../../styles/styles';
import CarouselButtons from './CarouselButtons';
import AttachmentView from '../AttachmentView';
import ONYXKEYS from '../../../ONYXKEYS';
import {propTypes, defaultProps} from './attachmentCarouselPropTypes';
import extractAttachmentsFromReport from './extractAttachmentsFromReport';
import useCarouselArrows from './useCarouselArrows';
import CarouselItem from './CarouselItem';
import Navigation from '../../../libs/Navigation/Navigation';
import BlockingView from '../../BlockingViews/BlockingView';
import * as Illustrations from '../../Icon/Illustrations';
Expand Down Expand Up @@ -85,17 +85,14 @@ function AttachmentCarousel({report, reportActions, source, onNavigate, onClose,

/**
* Defines how a single attachment should be rendered
* @param {{ isAuthTokenRequired: Boolean, source: String, file: { name: String } }} item
* @param {{ reportActionID: String, isAuthTokenRequired: Boolean, source: String, file: { name: String }, hasBeenFlagged: Boolean }} item
* @returns {JSX.Element}
*/
const renderItem = useCallback(
({item}) => (
<AttachmentView
source={item.source}
file={item.file}
isAuthTokenRequired={item.isAuthTokenRequired}
<CarouselItem
item={item}
isFocused={activeSource === item.source}
isUsedInCarousel
onPress={() => setShouldShowArrows(!shouldShowArrows)}
/>
),
Expand Down
11 changes: 11 additions & 0 deletions src/pages/home/report/ReportActionItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import * as BankAccounts from '../../../libs/actions/BankAccounts';
import usePrevious from '../../../hooks/usePrevious';
import ReportScreenContext from '../ReportScreenContext';
import Permissions from '../../../libs/Permissions';
import ReportAttachmentsContext from './ReportAttachmentsContext';

const propTypes = {
...windowDimensionsPropTypes,
Expand Down Expand Up @@ -129,13 +130,23 @@ function ReportActionItem(props) {
const [isHidden, setIsHidden] = useState(false);
const [moderationDecision, setModerationDecision] = useState(CONST.MODERATION.MODERATOR_DECISION_APPROVED);
const {reactionListRef} = useContext(ReportScreenContext);
const {isAttachmentHidden, updateIsAttachmentHidden} = useContext(ReportAttachmentsContext);
const textInputRef = useRef();
const popoverAnchorRef = useRef();
const downloadedPreviews = useRef([]);
const prevDraftMessage = usePrevious(props.draftMessage);
const originalReportID = ReportUtils.getOriginalReportID(props.report.reportID, props.action);
const originalReport = props.report.reportID === originalReportID ? props.report : ReportUtils.getReport(originalReportID);

useEffect(() => {
const isAttachment = ReportUtils.isReportMessageAttachment(_.last(props.action.message));
// To prevent unnecessary re-render, skip updating the state if the value is the same
if (!isAttachment || Boolean(isAttachmentHidden[props.action.reportActionID]) === isHidden) {
return;
}
updateIsAttachmentHidden(props.action.reportActionID, isHidden);
}, [props.action.reportActionID, props.action.message, isHidden, isAttachmentHidden, updateIsAttachmentHidden]);

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 this is not really necessary and it just adds overload and complexity to the code.

The reportActionItem will re-render when isAttachmentHidden is updated, isAttachmentHidden should be used only for the attachments carousel.

Actually, all report action items will re-render and this is not really a performance-wise approach.

I would suggest creating a new function updateHidden instead of using a useEffect hook :

updateHidden(hiddenState){
    setIsHidden(hiddenState)
    updateIsAttachmentHidden(hiddenState)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we remove the optimization, the app will freeze because of an infinite amount of set state calls.

In the useEffect, we depend on updateIsAttachmentHidden, and calling updateIsAttachmentHidden will update the isAttachmentHidden state which means updateIsAttachmentHidden instance is recreated which then triggers the useEffect again, and so on.

I already tried using your suggestion before but it suffers from the same case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I think we can move the optimization up to the context. I will try it and push if it works fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I think we can move the optimization up to the context. I will try it and push if it works fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fedirjh can I resolve this one?

Copy link
Contributor Author

@bernhardoj bernhardoj Sep 11, 2023

Choose a reason for hiding this comment

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

setIsHidden is also being used in this useEffect

useEffect(() => {
if (props.action.actionName !== CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT) {
return;
}
// Hide reveal message button and show the message if latestDecision is changed to empty
if (_.isEmpty(latestDecision)) {
setModerationDecision(CONST.MODERATION.MODERATOR_DECISION_APPROVED);
setIsHidden(false);
return;
}
setModerationDecision(latestDecision);
if (!_.contains([CONST.MODERATION.MODERATOR_DECISION_APPROVED, CONST.MODERATION.MODERATOR_DECISION_PENDING], latestDecision)) {
setIsHidden(true);
return;
}
setIsHidden(false);
}, [latestDecision, props.action.actionName]);

If we create a new function (let's call it updateHiddenState), we must wrap it in a callback.

useCallback((isHiddenValue) => {
    setIsHiddenState(isHiddenValue);
    updateHiddenAttachments(actionID, isHiddenValie);
}, [updateHiddenAttachments])

Then the above useEffect will now depend on the new function

[..., updateHiddenState]

Each time hiddenAttachments context is updated, the useEffect will be triggered which will update the state infinitely.

I'm thinking that if hiddenAttachments is updated, the updateHiddenAttachemnts shouldn't be recreated. I will try that and if it works, I will create the new function.

Copy link
Contributor

Choose a reason for hiding this comment

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

setIsHidden is also being used in this useEffect

Hmmm, I wonder why we need to include updateHiddenState in that hook. If a user intends to view the attachment in fullscreen, they should first reveal it.

It would be better to pass updateHiddenState exclusively to the onPress handler of the hide/reveal button. This approach eliminates unnecessary initialization, which, in turn, avoids triggering superfluous updates.

This results in the following evaluation:

  • If it's hidden and not revealed, take no action.
  • If it's hidden and revealed, initialize and update the context. This ensures that we minimize unnecessary updates, with updates contingent on user interactions.

Currently, the code operates as follows:

All attachments are added to the context, and their values are set to either true or false based on their hidden status.

I'm thinking that if hiddenAttachments is updated, the updateHiddenAttachemnts shouldn't be recreated.

This would ultimately resolve any unnecessary re-renders. Maybe using ref to store the state would help achieve that.

Copy link
Contributor Author

@bernhardoj bernhardoj Sep 11, 2023

Choose a reason for hiding this comment

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

Ah, you're right. I already handle the case where the state is undefined here.
image

Maybe using ref to store the state would help achieve that.

Yep, this is what I recently thought and tested. The hiddenAttachments will be a ref instead of a state. Then, we will expose a new function called isAttachmentHidden(reportActionID) through the context, so CarouselItem can get the ref value. This way, updating hiddenAttachments won't re-render the whole app. We don't need the reactivity from the state, so I think it's fine to use ref. What do you think?

diff --git a/src/components/Attachments/AttachmentCarousel/CarouselItem.js b/src/components/Attachments/AttachmentCarousel/CarouselItem.js
index b5b1e0352c..538ebac8d7 100644
--- a/src/components/Attachments/AttachmentCarousel/CarouselItem.js
+++ b/src/components/Attachments/AttachmentCarousel/CarouselItem.js
@@ -46,10 +46,10 @@ const defaultProps = {
 
 function CarouselItem({item, isFocused, onPress}) {
     const {translate} = useLocalize();
-    const {hiddenAttachments} = useContext(ReportAttachmentsContext);
+    const {isAttachmentHidden} = useContext(ReportAttachmentsContext);
     const [isHidden, setIsHidden] = useState(() => {
-        const isAttachmentHidden = hiddenAttachments[item.reportActionID];
-        return isAttachmentHidden === undefined ? item.hasBeenFlagged : isAttachmentHidden;
+        const isAttachmentHiddenValue = isAttachmentHidden(item.reportActionID);
+        return isAttachmentHiddenValue === undefined ? item.hasBeenFlagged : isAttachmentHiddenValue;
     });
 
     const renderButton = (style) => (
diff --git a/src/pages/home/report/ReportAttachmentsContext.js b/src/pages/home/report/ReportAttachmentsContext.js
index d763f9389c..cca47b54aa 100644
--- a/src/pages/home/report/ReportAttachmentsContext.js
+++ b/src/pages/home/report/ReportAttachmentsContext.js
@@ -1,4 +1,4 @@
-import React, {useEffect, useMemo, useState} from 'react';
+import React, {useEffect, useMemo, useRef} from 'react';
 import PropTypes from 'prop-types';
 import useCurrentReportID from '../../../hooks/useCurrentReportID';
 
@@ -11,26 +11,25 @@ const propTypes = {
 
 function ReportAttachmentsProvider(props) {
     const currentReportID = useCurrentReportID();
-    const [hiddenAttachments, setHiddenAttachments] = useState({});
+    const hiddenAttachments = useRef({});
 
     useEffect(() => {
         // We only want to store the attachment visibility for the current report.
         // If the current report ID changes, clear the state.
-        setHiddenAttachments({});
+        hiddenAttachments.current = {};
     }, [currentReportID]);
 
     const contextValue = useMemo(
         () => ({
-            hiddenAttachments,
+            isAttachmentHidden: (reportActionID) => hiddenAttachments.current[reportActionID],
             updateHiddenAttachments: (reportActionID, value) => {
-                // To prevent unnecessary re-render, skip updating the state if the value is the same
-                if (Boolean(hiddenAttachments[reportActionID]) === value) {
-                    return;
-                }
-                setHiddenAttachments((state) => ({...state, [reportActionID]: value}));
+                hiddenAttachments.current = {
+                    ...hiddenAttachments.current,
+                    [reportActionID]: value,
+                };
             },
         }),
-        [hiddenAttachments],
+        [],
     );
 
     return <ReportAttachmentsContext.Provider value={contextValue}>{props.children}</ReportAttachmentsContext.Provider>;

Copy link
Contributor

Choose a reason for hiding this comment

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

@bernhardoj That makes more sense. Let's proceed with this approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fedirjh Great, pushed the new updated.

bernhardoj marked this conversation as resolved.
Show resolved Hide resolved
useEffect(
() => () => {
// ReportActionContextMenu, EmojiPicker and PopoverReactionList are global components,
Expand Down
37 changes: 37 additions & 0 deletions src/pages/home/report/ReportAttachmentsContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, {useEffect, useMemo, useState} from 'react';
import PropTypes from 'prop-types';
import useCurrentReportID from '../../../hooks/useCurrentReportID';

const ReportAttachmentsContext = React.createContext();

const propTypes = {
/** Rendered child component */
children: PropTypes.node.isRequired,
};

function ReportAttachmentsProvider(props) {
const currentReportID = useCurrentReportID();
const [isAttachmentHidden, setIsAttachmentHidden] = useState({});
bernhardoj marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
// We only want to store the attachment visibility for the current report.
// If the current report ID changes, clear the state.
setIsAttachmentHidden({});
}, [currentReportID]);

const contextValue = useMemo(
() => ({
isAttachmentHidden,
updateIsAttachmentHidden: (reportActionID, value) => setIsAttachmentHidden((state) => ({...state, [reportActionID]: value})),
}),
[isAttachmentHidden],
);

return <ReportAttachmentsContext.Provider value={contextValue}>{props.children}</ReportAttachmentsContext.Provider>;
}

ReportAttachmentsProvider.propTypes = propTypes;
ReportAttachmentsProvider.displayName = 'ReportAttachmentsProvider';

export default ReportAttachmentsContext;
export {ReportAttachmentsProvider};
7 changes: 7 additions & 0 deletions src/styles/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2545,6 +2545,13 @@ const styles = {
position: 'absolute',
},

attachmentRevealButtonContainer: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
...spacing.ph4,
},

arrowIcon: {
height: 40,
width: 40,
Expand Down
Loading