Skip to content

Commit

Permalink
fix: handle race condition in resetUiState
Browse files Browse the repository at this point in the history
using dirty hack to fix prod build with no side effects
If files to upload,
then resetUIState will use dark icons instead of home theme
  • Loading branch information
acezard committed Dec 3, 2023
1 parent 0064bcd commit d6be0fd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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

0 comments on commit d6be0fd

Please sign in to comment.