Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/components/MenuItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {ImageContentFit} from 'expo-image';
import type {ReactElement, ReactNode, Ref} from 'react';
import React, {useContext, useMemo, useRef} from 'react';
import React, {useMemo, useRef} from 'react';
import type {GestureResponderEvent, Role, StyleProp, TextStyle, ViewStyle} from 'react-native';
import {View} from 'react-native';
import type {ValueOf} from 'type-fest';
Expand Down Expand Up @@ -37,7 +37,7 @@ import type {DisplayNameWithTooltip} from './DisplayNames/types';
import FormHelpMessage from './FormHelpMessage';
import Hoverable from './Hoverable';
import Icon from './Icon';
import {MenuItemGroupContext} from './MenuItemGroup';
import {useMenuItemGroupActions, useMenuItemGroupState} from './MenuItemGroup';
import PlaidCardFeedIcon from './PlaidCardFeedIcon';
import type {PressableRef} from './Pressable/GenericPressable/types';
import PressableWithSecondaryInteraction from './PressableWithSecondaryInteraction';
Expand Down Expand Up @@ -582,7 +582,8 @@ function MenuItem({
const StyleUtils = useStyleUtils();
const combinedStyle = [styles.popoverMenuItem, style];
const {shouldUseNarrowLayout} = useResponsiveLayout();
const {isExecuting, singleExecution, waitForNavigate} = useContext(MenuItemGroupContext) ?? {};
const {isExecuting} = useMenuItemGroupState() ?? {};
const {singleExecution, waitForNavigate} = useMenuItemGroupActions() ?? {};
const popoverAnchor = useRef<View>(null);
const deviceHasHoverSupport = hasHoverSupport();
const isCompact = viewMode === CONST.OPTION_MODE.COMPACT;
Expand Down
30 changes: 22 additions & 8 deletions src/components/MenuItemGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import React, {createContext, useMemo} from 'react';
import React, {createContext, useContext} from 'react';
import useSingleExecution from '@hooks/useSingleExecution';
import type {Action} from '@hooks/useSingleExecution';
import useWaitForNavigation from '@hooks/useWaitForNavigation';

type MenuItemGroupContextProps = {
type MenuItemGroupStateContextProps = {
isExecuting: boolean;
};

type MenuItemGroupActionsContextProps = {
singleExecution: <T extends unknown[]>(action: Action<T>) => (...params: T) => void;
waitForNavigate: ReturnType<typeof useWaitForNavigation>;
};

const MenuItemGroupContext = createContext<MenuItemGroupContextProps | undefined>(undefined);
const MenuItemGroupStateContext = createContext<MenuItemGroupStateContextProps | null>(null);
const MenuItemGroupActionsContext = createContext<MenuItemGroupActionsContextProps | null>(null);

type MenuItemGroupProps = {
/* Actual content wrapped by this component */
Expand All @@ -23,13 +27,23 @@ function MenuItemGroup({children, shouldUseSingleExecution = true}: MenuItemGrou
const {isExecuting, singleExecution} = useSingleExecution();
const waitForNavigate = useWaitForNavigation();

const value = useMemo(
() => (shouldUseSingleExecution ? {isExecuting, singleExecution, waitForNavigate} : undefined),
[shouldUseSingleExecution, isExecuting, singleExecution, waitForNavigate],
const stateValue = shouldUseSingleExecution ? {isExecuting} : null;
const actionsValue = shouldUseSingleExecution ? {singleExecution, waitForNavigate} : null;

return (
<MenuItemGroupStateContext.Provider value={stateValue}>
<MenuItemGroupActionsContext.Provider value={actionsValue}>{children}</MenuItemGroupActionsContext.Provider>
</MenuItemGroupStateContext.Provider>
);
}

function useMenuItemGroupState() {
return useContext(MenuItemGroupStateContext);
}

return <MenuItemGroupContext.Provider value={value}>{children}</MenuItemGroupContext.Provider>;
function useMenuItemGroupActions() {
return useContext(MenuItemGroupActionsContext);
}

export {MenuItemGroupContext};
export {useMenuItemGroupState, useMenuItemGroupActions};
export default MenuItemGroup;
4 changes: 2 additions & 2 deletions src/components/Navigation/DebugTabView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import useLocalize from '@hooks/useLocalize';
import type {IndicatorStatus} from '@hooks/useNavigationTabBarIndicatorChecks';
import useOnyx from '@hooks/useOnyx';
import useReportAttributes from '@hooks/useReportAttributes';
import {useSidebarOrderedReports} from '@hooks/useSidebarOrderedReports';
import {useSidebarOrderedReportsState} from '@hooks/useSidebarOrderedReports';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -107,7 +107,7 @@ function DebugTabView({selectedTab, chatTabBrickRoad}: DebugTabViewProps) {
const [reimbursementAccount] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT);
const reportAttributes = useReportAttributes();
const {status, indicatorColor, policyIDWithErrors} = useIndicatorStatus();
const {orderedReportIDs} = useSidebarOrderedReports();
const {orderedReportIDs} = useSidebarOrderedReportsState();
const icons = useMemoizedLazyExpensifyIcons(['DotIndicator'] as const);

const message = useMemo((): TranslationPaths | undefined => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/Navigation/NavigationTabBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import useOnyx from '@hooks/useOnyx';
import useReportAttributes from '@hooks/useReportAttributes';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useRootNavigationState from '@hooks/useRootNavigationState';
import {useSidebarOrderedReports} from '@hooks/useSidebarOrderedReports';
import {useSidebarOrderedReportsState} from '@hooks/useSidebarOrderedReports';
import useStyleUtils from '@hooks/useStyleUtils';
import useSubscriptionPlan from '@hooks/useSubscriptionPlan';
import useTheme from '@hooks/useTheme';
Expand Down Expand Up @@ -55,7 +55,7 @@ function NavigationTabBar({selectedTab, isTopLevelBar = false, shouldShowFloatin
const theme = useTheme();
const styles = useThemeStyles();
const {translate} = useLocalize();
const {orderedReportIDs} = useSidebarOrderedReports();
const {orderedReportIDs} = useSidebarOrderedReportsState();
const [isDebugModeEnabled] = useOnyx(ONYXKEYS.IS_DEBUG_MODE_ENABLED);
const subscriptionPlan = useSubscriptionPlan();
const expensifyIcons = useMemoizedLazyExpensifyIcons(['ExpensifyAppIcon', 'Home', 'Inbox']);
Expand Down
4 changes: 2 additions & 2 deletions src/components/NumberWithSymbolForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {NativeSyntheticEvent} from 'react-native';
import {View} from 'react-native';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import {useMouseContext} from '@hooks/useMouseContext';
import {useMouseActions} from '@hooks/useMouseContext';
import usePrevious from '@hooks/usePrevious';
import useThemeStyles from '@hooks/useThemeStyles';
import {isMobileSafari} from '@libs/Browser';
Expand Down Expand Up @@ -177,7 +177,7 @@ function NumberWithSymbolForm({
// The ref is used to ignore any onSelectionChange event that happens while we are updating the selection manually in setNewNumber
const willSelectionBeUpdatedManually = useRef(false);

const {setMouseDown, setMouseUp} = useMouseContext();
const {setMouseDown, setMouseUp} = useMouseActions();
const handleMouseDown = (e: React.MouseEvent<Element, MouseEvent>) => {
e.stopPropagation();
setMouseDown();
Expand Down
36 changes: 23 additions & 13 deletions src/components/OptionListContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import ONYXKEYS from '@src/ONYXKEYS';
import type {PersonalDetails, Report} from '@src/types/onyx';
import {usePersonalDetails} from './OnyxListItemProvider';

type OptionsListContextProps = {
type OptionsListStateContextProps = {
/** List of options for reports and personal details */
options: OptionList;
/** Function to initialize the options */
initializeOptions: () => void;
/** Flag to check if the options are initialized */
areOptionsInitialized: boolean;
};

type OptionsListActionsContextProps = {
/** Function to initialize the options */
initializeOptions: () => void;
/** Function to reset the options */
resetOptions: () => void;
};
Expand All @@ -27,13 +30,16 @@ type OptionsListProviderProps = {
children: React.ReactNode;
};

const OptionsListContext = createContext<OptionsListContextProps>({
const OptionsListStateContext = createContext<OptionsListStateContextProps>({
options: {
reports: [],
personalDetails: [],
},
initializeOptions: () => {},
areOptionsInitialized: false,
});

const OptionsListActionsContext = createContext<OptionsListActionsContextProps>({
initializeOptions: () => {},
resetOptions: () => {},
});

Expand Down Expand Up @@ -277,21 +283,25 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
});
}, []);

const stateValue = useMemo(() => ({options, areOptionsInitialized: areOptionsInitialized.current}), [options]);
const actionsValue = useMemo(() => ({initializeOptions, resetOptions}), [initializeOptions, resetOptions]);

return (
<OptionsListContext.Provider
value={useMemo(() => ({options, initializeOptions, areOptionsInitialized: areOptionsInitialized.current, resetOptions}), [options, initializeOptions, resetOptions])}
>
{children}
</OptionsListContext.Provider>
<OptionsListStateContext.Provider value={stateValue}>
<OptionsListActionsContext.Provider value={actionsValue}>{children}</OptionsListActionsContext.Provider>
</OptionsListStateContext.Provider>
);
}

const useOptionsListContext = () => useContext(OptionsListContext);
const useOptionsListState = () => useContext(OptionsListStateContext);

const useOptionsListActions = () => useContext(OptionsListActionsContext);

// Hook to use the OptionsListContext with an initializer to load the options
const useOptionsList = (options?: {shouldInitialize: boolean}) => {
const {shouldInitialize = true} = options ?? {};
const {initializeOptions, options: optionsList, areOptionsInitialized, resetOptions} = useOptionsListContext();
const {options: optionsList, areOptionsInitialized} = useOptionsListState();
const {initializeOptions, resetOptions} = useOptionsListActions();
const [internalOptions, setInternalOptions] = useState<OptionList>(optionsList);
const prevOptions = useRef<OptionList>(null);
const [areInternalOptionsInitialized, setAreInternalOptionsInitialized] = useState(false);
Expand Down Expand Up @@ -348,4 +358,4 @@ const useOptionsList = (options?: {shouldInitialize: boolean}) => {

export default OptionsListContextProvider;

export {useOptionsList, OptionsListContext};
export {useOptionsList, useOptionsListState, useOptionsListActions, OptionsListStateContext, OptionsListActionsContext};
5 changes: 3 additions & 2 deletions src/components/SelectionList/ListItem/BaseListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import OfflineWithFeedback from '@components/OfflineWithFeedback';
import PressableWithFeedback from '@components/Pressable/PressableWithFeedback';
import useHover from '@hooks/useHover';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import {useMouseContext} from '@hooks/useMouseContext';
import {useMouseActions, useMouseState} from '@hooks/useMouseContext';
import useStyleUtils from '@hooks/useStyleUtils';
import useSyncFocus from '@hooks/useSyncFocus';
import useTheme from '@hooks/useTheme';
Expand Down Expand Up @@ -53,7 +53,8 @@ function BaseListItem<TItem extends ListItem>({
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {hovered, bind} = useHover();
const {isMouseDownOnInput, setMouseUp} = useMouseContext();
const {isMouseDownOnInput} = useMouseState();
const {setMouseUp} = useMouseActions();
const icons = useMemoizedLazyExpensifyIcons(['ArrowRight', 'Checkmark', 'DotIndicator'] as const);

const pressableRef = useRef<View>(null);
Expand Down
5 changes: 3 additions & 2 deletions src/components/SelectionListWithSections/BaseListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import OfflineWithFeedback from '@components/OfflineWithFeedback';
import PressableWithFeedback from '@components/Pressable/PressableWithFeedback';
import useHover from '@hooks/useHover';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import {useMouseContext} from '@hooks/useMouseContext';
import {useMouseActions, useMouseState} from '@hooks/useMouseContext';
import useStyleUtils from '@hooks/useStyleUtils';
import useSyncFocus from '@hooks/useSyncFocus';
import useTheme from '@hooks/useTheme';
Expand Down Expand Up @@ -53,7 +53,8 @@ function BaseListItem<TItem extends ListItem>({
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {hovered, bind} = useHover();
const {isMouseDownOnInput, setMouseUp} = useMouseContext();
const {isMouseDownOnInput} = useMouseState();
const {setMouseUp} = useMouseActions();

const pressableRef = useRef<View>(null);

Expand Down
4 changes: 2 additions & 2 deletions src/components/TestToolMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {View} from 'react-native';
import useIsAuthenticated from '@hooks/useIsAuthenticated';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import {useSidebarOrderedReports} from '@hooks/useSidebarOrderedReports';
import {useSidebarOrderedReportsActions} from '@hooks/useSidebarOrderedReports';
import useSingleExecution from '@hooks/useSingleExecution';
import useThemeStyles from '@hooks/useThemeStyles';
import useWaitForNavigation from '@hooks/useWaitForNavigation';
Expand Down Expand Up @@ -31,7 +31,7 @@ function TestToolMenu() {
const [isDebugModeEnabled = false] = useOnyx(ONYXKEYS.IS_DEBUG_MODE_ENABLED);
const styles = useThemeStyles();
const {translate} = useLocalize();
const {clearLHNCache} = useSidebarOrderedReports();
const {clearLHNCache} = useSidebarOrderedReportsActions();
const [hasBiometricsRegistered = false] = useOnyx(ONYXKEYS.ACCOUNT, {selector: hasBiometricsRegisteredSelector});
const [isAccountLoading = false] = useOnyx(ONYXKEYS.ACCOUNT, {selector: isAccountLoadingSelector});

Expand Down
9 changes: 5 additions & 4 deletions src/components/VideoPlayer/BaseVideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeed
import {useFullScreenState} from '@components/VideoPlayerContexts/FullScreenContextProvider';
import {usePlaybackActionsContext, usePlaybackStateContext} from '@components/VideoPlayerContexts/PlaybackContext';
import type {PlaybackSpeed} from '@components/VideoPlayerContexts/types';
import {useVideoPopoverMenuContext} from '@components/VideoPlayerContexts/VideoPopoverMenuContext';
import {useVolumeContext} from '@components/VideoPlayerContexts/VolumeContext';
import {useVideoPopoverMenuActions} from '@components/VideoPlayerContexts/VideoPopoverMenuContext';
import {useVolumeActions, useVolumeState} from '@components/VideoPlayerContexts/VolumeContext';
import VideoPopoverMenu from '@components/VideoPopoverMenu';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -136,14 +136,15 @@ function BaseVideoPlayer({
const shouldShowOfflineIndicator = useMemo(() => {
return isOffline && currentTime + bufferedPosition <= 0;
}, [bufferedPosition, currentTime, isOffline]);
const {updateVolume, lastNonZeroVolume} = useVolumeContext();
const {updateVolume} = useVolumeActions();
const {lastNonZeroVolume} = useVolumeState();
useHandleNativeVideoControls({
videoViewRef,
isOffline,
isLocalFile: isUploading,
});

const {updateVideoPopoverMenuPlayerRef, updatePlaybackSpeed, updateSource: updatePopoverMenuSource} = useVideoPopoverMenuContext();
const {updateVideoPopoverMenuPlayerRef, updatePlaybackSpeed, updateSource: updatePopoverMenuSource} = useVideoPopoverMenuActions();

const togglePlayCurrentVideo = useCallback(() => {
if (!isCurrentlyURLSet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Animated, {useAnimatedStyle, useDerivedValue} from 'react-native-reanimat
import {scheduleOnRN} from 'react-native-worklets';
import Hoverable from '@components/Hoverable';
import IconButton from '@components/VideoPlayer/IconButton';
import {useVolumeContext} from '@components/VideoPlayerContexts/VolumeContext';
import {useVolumeActions, useVolumeState} from '@components/VideoPlayerContexts/VolumeContext';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -37,7 +37,8 @@ function VolumeButton({style, small = false}: VolumeButtonProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const expensifyIcons = useMemoizedLazyExpensifyIcons(['Mute', 'VolumeHigh', 'VolumeLow']);
const {updateVolume, volume, toggleMute} = useVolumeContext();
const {volume} = useVolumeState();
const {updateVolume, toggleMute} = useVolumeActions();
const [sliderHeight, setSliderHeight] = useState(1);
const [volumeIcon, setVolumeIcon] = useState({icon: getVolumeIcon(expensifyIcons, volume.get())});
const [isSliderBeingUsed, setIsSliderBeingUsed] = useState(false);
Expand Down
41 changes: 31 additions & 10 deletions src/components/VideoPlayerContexts/VideoPopoverMenuContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import addEncryptedAuthTokenToURL from '@libs/addEncryptedAuthTokenToURL';
import fileDownload from '@libs/fileDownload';
import CONST from '@src/CONST';
import type ChildrenProps from '@src/types/utils/ChildrenProps';
import type {PlaybackSpeed, VideoPopoverMenuContext} from './types';
import type {PlaybackSpeed, VideoPopoverMenuActionsContextType, VideoPopoverMenuStateContextType} from './types';

const Context = React.createContext<VideoPopoverMenuContext | null>(null);
const VideoPopoverMenuStateContext = React.createContext<VideoPopoverMenuStateContextType | null>(null);
const VideoPopoverMenuActionsContext = React.createContext<VideoPopoverMenuActionsContextType | null>(null);

function VideoPopoverMenuContextProvider({children}: ChildrenProps) {
const icons = useMemoizedLazyExpensifyIcons(['Checkmark', 'Download', 'Meter'] as const);
Expand Down Expand Up @@ -78,16 +79,36 @@ function VideoPopoverMenuContextProvider({children}: ChildrenProps) {
return items;
}, [icons.Checkmark, icons.Download, icons.Meter, currentPlaybackSpeed, downloadAttachment, translate, updatePlaybackSpeed, isOffline, isLocalFile]);

const contextValue = useMemo(() => ({menuItems, updateVideoPopoverMenuPlayerRef, updatePlaybackSpeed, updateSource: setSource}), [menuItems, updatePlaybackSpeed, setSource]);
return <Context.Provider value={contextValue}>{children}</Context.Provider>;
const stateValue = {menuItems};
const actionsValue = {updateVideoPopoverMenuPlayerRef, updatePlaybackSpeed, updateSource: setSource};

return (
<VideoPopoverMenuStateContext.Provider value={stateValue}>
<VideoPopoverMenuActionsContext.Provider value={actionsValue}>{children}</VideoPopoverMenuActionsContext.Provider>
</VideoPopoverMenuStateContext.Provider>
);
}

function useVideoPopoverMenuContext() {
const videoPopoverMenuContext = useContext(Context);
if (!videoPopoverMenuContext) {
throw new Error('useVideoPopoverMenuContext must be used within a VideoPopoverMenuContext');
function useVideoPopoverMenuState() {
const context = useContext(VideoPopoverMenuStateContext);
if (!context) {
throw new Error('useVideoPopoverMenuState must be used within a VideoPopoverMenuContextProvider');
}
return videoPopoverMenuContext;
return context;
}

function useVideoPopoverMenuActions() {
const context = useContext(VideoPopoverMenuActionsContext);
if (!context) {
throw new Error('useVideoPopoverMenuActions must be used within a VideoPopoverMenuContextProvider');
}
return context;
}

function useVideoPopoverMenuContext() {
const state = useVideoPopoverMenuState();
const actions = useVideoPopoverMenuActions();
return {...state, ...actions};
}

export {VideoPopoverMenuContextProvider, useVideoPopoverMenuContext};
export {VideoPopoverMenuContextProvider, useVideoPopoverMenuContext, useVideoPopoverMenuState, useVideoPopoverMenuActions};
Loading
Loading