-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add value-indepent hook for setting online status message
- Loading branch information
1 parent
3253800
commit 734dff4
Showing
3 changed files
with
52 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,64 @@ | ||
import React, { ReactElement, ReactNode, useContext, useState } from 'react' | ||
import { OnlineStatusMessageContextAPI } from '../types' | ||
|
||
const defaultApi: OnlineStatusMessageContextAPI = { | ||
onlineStatusMessage: undefined, | ||
setOnlineStatusMessage: () => undefined, | ||
} | ||
|
||
const OnlineStatusMessageContext = | ||
React.createContext<OnlineStatusMessageContextAPI>(defaultApi) | ||
type SetOnlineStatusMessage = (message: ReactNode) => void | ||
|
||
export const useOnlineStatusMessage = (): OnlineStatusMessageContextAPI => { | ||
const { onlineStatusMessage, setOnlineStatusMessage } = | ||
useContext<OnlineStatusMessageContextAPI>(OnlineStatusMessageContext) | ||
|
||
return { | ||
onlineStatusMessage, | ||
setOnlineStatusMessage, | ||
} | ||
} | ||
// '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 SetOnlineStatusMessageContext = | ||
React.createContext<SetOnlineStatusMessage>(() => undefined) | ||
|
||
export const OnlineStatusMessageProvider = ({ | ||
children, | ||
}: { | ||
children: ReactNode | ||
}): ReactElement => { | ||
const [onlineStatusMessage, setOnlineStatusMessage] = useState<ReactNode>() | ||
const [onlineStatusMessage, setOnlineStatusMessage] = | ||
useState<ReactNode>(null) // note: not undefined | ||
|
||
return ( | ||
<OnlineStatusMessageContext.Provider | ||
value={{ | ||
onlineStatusMessage, | ||
setOnlineStatusMessage, | ||
}} | ||
> | ||
{children} | ||
</OnlineStatusMessageContext.Provider> | ||
<OnlineStatusMessageValueContext.Provider value={onlineStatusMessage}> | ||
<SetOnlineStatusMessageContext.Provider | ||
value={setOnlineStatusMessage} | ||
> | ||
{children} | ||
</SetOnlineStatusMessageContext.Provider> | ||
</OnlineStatusMessageValueContext.Provider> | ||
) | ||
} | ||
|
||
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 | ||
} | ||
|
||
export const useSetOnlineStatusMessage = () => { | ||
const setOnlineStatusMessage = useContext(SetOnlineStatusMessageContext) | ||
|
||
if (setOnlineStatusMessage === undefined) { | ||
throw new Error( | ||
'useSetOnlineStatusMessage must be used within an OnlineStatusMessageProvider' | ||
) | ||
} | ||
|
||
return setOnlineStatusMessage | ||
} | ||
|
||
// combination of both getter and setter (also provides backward compatability) | ||
export const useOnlineStatusMessage = () => { | ||
const onlineStatusMessage = useOnlineStatusMessageValue() | ||
const setOnlineStatusMessage = useSetOnlineStatusMessage() | ||
|
||
return { | ||
onlineStatusMessage, | ||
setOnlineStatusMessage, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters