-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acadfb6
commit ba3930a
Showing
11 changed files
with
218 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { | ||
createContext, | ||
useState, | ||
ReactNode, | ||
useCallback, | ||
useMemo, | ||
} from "react"; | ||
|
||
export type StatusMessage = { | ||
id: string; | ||
text: string; | ||
color?: string; | ||
}; | ||
|
||
export type StatusMessagesState = { | ||
[key: string]: StatusMessage[]; | ||
}; | ||
|
||
type StatusBarMessagesProviderProps = { | ||
children: ReactNode; | ||
}; | ||
|
||
type StatusBarMessagesContextValue = { | ||
messages: StatusMessagesState; | ||
addMessage: ( | ||
key: string, | ||
message: string, | ||
color?: string, | ||
messageId?: string, | ||
) => string; | ||
removeMessage: (key: string, messageId: string) => void; | ||
clearMessages: (key: string) => void; | ||
}; | ||
|
||
export const StatusBarMessagesContext = | ||
createContext<StatusBarMessagesContextValue | null>(null); | ||
|
||
export function StatusBarMessagesProvider({ | ||
children, | ||
}: StatusBarMessagesProviderProps) { | ||
const [messagesState, setMessagesState] = useState<StatusMessagesState>({}); | ||
|
||
const messages = useMemo(() => messagesState, [messagesState]); | ||
|
||
const addMessage = useCallback( | ||
(key: string, message: string, color?: string, messageId?: string) => { | ||
const id = messageId || Date.now().toString(); | ||
const msgColor = color || "text-danger"; | ||
setMessagesState((prevMessages) => ({ | ||
...prevMessages, | ||
[key]: [ | ||
...(prevMessages[key] || []), | ||
{ id, text: message, color: msgColor }, | ||
], | ||
})); | ||
return id; | ||
}, | ||
[], | ||
); | ||
|
||
const removeMessage = useCallback((key: string, messageId: string) => { | ||
setMessagesState((prevMessages) => ({ | ||
...prevMessages, | ||
[key]: prevMessages[key].filter((msg) => msg.id !== messageId), | ||
})); | ||
}, []); | ||
|
||
const clearMessages = useCallback((key: string) => { | ||
setMessagesState((prevMessages) => { | ||
const updatedMessages = { ...prevMessages }; | ||
delete updatedMessages[key]; | ||
return updatedMessages; | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<StatusBarMessagesContext.Provider | ||
value={{ messages, addMessage, removeMessage, clearMessages }} | ||
> | ||
{children} | ||
</StatusBarMessagesContext.Provider> | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useRef } from "react"; | ||
import { isEqual } from "lodash"; | ||
|
||
export default function useDeepMemo<T>(value: T) { | ||
const ref = useRef<T | undefined>(undefined); | ||
|
||
if (!isEqual(ref.current, value)) { | ||
ref.current = value; | ||
} | ||
|
||
return ref.current; | ||
} |
Oops, something went wrong.