Skip to content

Commit

Permalink
fix: reinstate undefined; remove checks
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiVandivier committed Dec 12, 2023
1 parent 93351bb commit 6aa5c97
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('useOnlineStatusMessage', () => {
wrapper,
})

expect(result.current.onlineStatusMessage).toBeNull()
expect(result.current.onlineStatusMessage).toBeUndefined()

act(() => {
result.current.setOnlineStatusMessage('8 offline events')
Expand Down
27 changes: 5 additions & 22 deletions services/offline/src/lib/online-status-message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ type SetOnlineStatusMessage = (message: ReactNode) => void

// 'get' and 'set' contexts are separated so 'setter' consumers that don't
// actually need the value don't have to rerender when the value changes:
const OnlineStatusMessageValueContext = React.createContext<ReactNode>(null)
const OnlineStatusMessageValueContext =
React.createContext<ReactNode>(undefined)
const SetOnlineStatusMessageContext =
React.createContext<SetOnlineStatusMessage>(() => undefined)

Expand All @@ -13,8 +14,7 @@ export const OnlineStatusMessageProvider = ({
}: {
children: ReactNode
}): ReactElement => {
const [onlineStatusMessage, setOnlineStatusMessage] =
useState<ReactNode>(null) // note: not undefined
const [onlineStatusMessage, setOnlineStatusMessage] = useState<ReactNode>() // note: not undefined

return (
<OnlineStatusMessageValueContext.Provider value={onlineStatusMessage}>
Expand All @@ -28,28 +28,11 @@ export const OnlineStatusMessageProvider = ({
}

export const useOnlineStatusMessageValue = () => {
const onlineStatusMessage = useContext(OnlineStatusMessageValueContext)

// note: value is initialized to `null` in provider, not undefined
if (onlineStatusMessage === undefined) {
throw new Error(
'useOnlineStatusMessageValue must be used within an OnlineStatusMessageProvider'
)
}

return onlineStatusMessage
return useContext(OnlineStatusMessageValueContext)
}

export const useSetOnlineStatusMessage = () => {
const setOnlineStatusMessage = useContext(SetOnlineStatusMessageContext)

if (setOnlineStatusMessage === undefined) {
throw new Error(
'useSetOnlineStatusMessage must be used within an OnlineStatusMessageProvider'
)
}

return setOnlineStatusMessage
return useContext(SetOnlineStatusMessageContext)
}

// combination of both getter and setter (also provides backward compatability)
Expand Down

0 comments on commit 6aa5c97

Please sign in to comment.