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

fix: handle race condition in resetUiState #1043

Merged
merged 1 commit into from
Dec 5, 2023
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
12 changes: 12 additions & 0 deletions __tests__/jestSetupFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,15 @@ jest.mock(
'react-native-background-geolocation',
() => mockRNBackgroundGeolocation
)

// This function isn't meant to be tested
// It is breaking the tests just by being imported
// Have to remove that mock when we refactor the actual function
jest.mock('../src/app/view/OsReceive/state/OsReceiveState', () => ({
getDangerousOsReceiveState: jest.fn().mockReturnValue({
filesToUpload: [],
routeToUpload: {},
errored: false,
candidateApps: undefined
})
}))
16 changes: 16 additions & 0 deletions src/app/view/OsReceive/state/OsReceiveState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const osReceiveReducer = (
action: OsReceiveAction
): OsReceiveState => {
let nextState = state
dangerousOsReceiveState = nextState

switch (action.type) {
case OsReceiveActionType.SetFilesToUpload:
Expand Down Expand Up @@ -92,6 +93,21 @@ export const initialState: OsReceiveState = {
candidateApps: undefined
}

/**
* This variable is used to expose the state to the outside world.
*
* It is meant to be a temporary solution to fix an issue where resetUIstate()
* has to know the state of this feature before executing its logic,
* but at the time of writing resetUIState has no connection to the React tree.
*
* It should not be used anywhere else unless absolutely necessary.
* ResetUIState() should be refactored to use a proper React context.
* It can't be done at the time of this commit for time constraints.
*/
let dangerousOsReceiveState = initialState
export const getDangerousOsReceiveState = (): OsReceiveState =>
dangerousOsReceiveState

export const useOsReceiveState = (): OsReceiveState => {
const context = useContext(OsReceiveStateContext)
if (context === undefined) {
Expand Down
14 changes: 13 additions & 1 deletion src/libs/intents/setFlagshipUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '/app/theme/themeManager'
import { navigationRef } from '/libs/RootNavigation'
import { routes } from '/constants/routes'
import { getDangerousOsReceiveState } from '/app/view/OsReceive/state/OsReceiveState'

const isDarkMode = (bottomTheme: StatusBarStyle): boolean =>
bottomTheme === StatusBarStyle.Light
Expand Down Expand Up @@ -207,10 +208,21 @@ export const resetUIState = (
uri: string,
callback?: (theme: StatusBarStyle) => void
): void => {
const theme = urlHasKonnectorOpen(uri)
let theme = urlHasKonnectorOpen(uri)
? ThemeInput.Dark
: getHomeThemeAsThemeInput()

// If the theme is light, and there are files to upload (OsReceiveScreen visible), then set the theme to dark
// This is done because the file upload dialog is light background, so we want to use dark icons
// This is a temporary fix until we have a proper React context
// see src/app/view/OsReceive/state/OsReceiveState.ts#getDangerousOsReceiveState
if (theme === ThemeInput.Light) {
const hasFilesToUpload =
getDangerousOsReceiveState().filesToUpload.length > 0

if (hasFilesToUpload) theme = ThemeInput.Dark
}

themeLog.info('resetUIState', { uri, theme })

void setFlagshipUI({ bottomTheme: theme, topTheme: theme }, 'resetUIState')
Expand Down
Loading