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

39061 adjust virtual viewport with smart app banner #39209

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/hooks/useViewportOffsetTop/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import {useEffect, useRef, useState} from 'react';
import {useCallback, useEffect, useRef, useState} from 'react';
import addViewportResizeListener from '@libs/VisualViewport';
import CONST from '@src/CONST';

/**
* A hook that returns the offset of the top edge of the visual viewport
*/
export default function useViewportOffsetTop(shouldAdjustScrollView = false): number {
const [viewportOffsetTop, setViewportOffsetTop] = useState(0);
const initialHeight = useRef(window.visualViewport?.height ?? window.innerHeight).current;
const initialHeight = useRef(0);
const cachedDefaultOffsetTop = useRef<number>(0);
useEffect(() => {
const updateOffsetTop = (event?: Event) => {

const updateOffsetTop = useCallback(
(event?: Event) => {
let targetOffsetTop = window.visualViewport?.offsetTop ?? 0;
if (event?.target instanceof VisualViewport) {
targetOffsetTop = event.target.offsetTop;
}

if (shouldAdjustScrollView && window.visualViewport) {
const adjustScrollY = Math.round(initialHeight - window.visualViewport.height);
if (shouldAdjustScrollView && window.visualViewport && initialHeight.current) {
const adjustScrollY = Math.round(initialHeight.current - window.visualViewport.height);
if (cachedDefaultOffsetTop.current === 0) {
cachedDefaultOffsetTop.current = targetOffsetTop;
}
Expand All @@ -29,12 +31,20 @@ export default function useViewportOffsetTop(shouldAdjustScrollView = false): nu
setViewportOffsetTop(targetOffsetTop);
}
} else {
setViewportOffsetTop(targetOffsetTop);
setViewportOffsetTop(0);
}
};
updateOffsetTop();
return addViewportResizeListener(updateOffsetTop);
}, [initialHeight, shouldAdjustScrollView]);
},
[shouldAdjustScrollView],
);

useEffect(() => addViewportResizeListener(updateOffsetTop), [shouldAdjustScrollView, updateOffsetTop]);

useEffect(() => {
setTimeout(() => {
initialHeight.current = window.visualViewport?.height ?? window.innerHeight;
Copy link
Contributor

@sobitneupane sobitneupane Apr 8, 2024

Choose a reason for hiding this comment

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

@suneox use of setTimeout is discouraged.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i have removed

updateOffsetTop();
}, CONST.TIMING.RESIZE_DEBOUNCE_TIME);
}, [updateOffsetTop]);

useEffect(() => {
if (!shouldAdjustScrollView) {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ function ReportScreen({
Performance.markStart(CONST.TIMING.CHAT_RENDER);
}
const [isComposerFocus, setIsComposerFocus] = useState(false);
const viewportOffsetTop = useViewportOffsetTop(Browser.isMobileSafari() && isComposerFocus && !modal?.willAlertModalBecomeVisible);
const shouldAdjustScrollView = useMemo(() => Browser.isMobileSafari() && isComposerFocus && !modal?.willAlertModalBecomeVisible, [isComposerFocus, modal]);
Copy link
Contributor

Choose a reason for hiding this comment

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

index.ts is only a web file (since there is also an index.native.ts), which means that the Browser.isMobileSafari() shouldn't really be here. I think this browser check should be moved inside of index.ts so that the logic is better incapsulated. The browser check should be coupled with an early return.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes we should move Browser.isMobileSafari() into index.ts and I have updated

const viewportOffsetTop = useViewportOffsetTop(shouldAdjustScrollView);

const {reportPendingAction, reportErrors} = ReportUtils.getReportOfflinePendingActionAndErrors(report);
const screenWrapperStyle: ViewStyle[] = [styles.appContent, styles.flex1, {marginTop: viewportOffsetTop}];
Expand Down
Loading