Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F32219281
D8272.1765185401.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
D8272.1765185401.diff
View Options
diff --git a/web/search/message-search-state-provider.react.js b/web/search/message-search-state-provider.react.js
new file mode 100644
--- /dev/null
+++ b/web/search/message-search-state-provider.react.js
@@ -0,0 +1,68 @@
+// @flow
+
+import invariant from 'invariant';
+import * as React from 'react';
+
+import { useSelector } from '../redux/redux-utils.js';
+
+type MessageSearchState = {
+ +query: string,
+ +setQuery: string => void,
+ +clearQuery: () => void,
+};
+
+const MessageSearchContext: React.Context<?MessageSearchState> =
+ React.createContext<?MessageSearchState>(null);
+
+type Props = {
+ +children: React.Node,
+};
+
+function MessageSearchStateProvider(props: Props): React.Node {
+ const [queries, setQueries] = React.useState<{
+ [threadID: string]: string,
+ }>({});
+
+ const threadID = useSelector(state => state.navInfo.activeChatThreadID);
+
+ invariant(threadID, 'threadID should be set when search messages');
+
+ const setQuery = React.useCallback(
+ (query: string) =>
+ setQueries(prevQueries => ({ ...prevQueries, [threadID]: query })),
+ [threadID],
+ );
+
+ const clearQuery = React.useCallback(
+ () =>
+ setQueries(prevQueries => {
+ delete prevQueries[threadID];
+ return { ...prevQueries };
+ }),
+ [threadID],
+ );
+
+ const state = React.useMemo(
+ () => ({
+ query: queries[threadID] ?? '',
+ setQuery,
+ clearQuery,
+ }),
+ [queries, setQuery, threadID, clearQuery],
+ );
+
+ return (
+ <MessageSearchContext.Provider value={state}>
+ {props.children}
+ </MessageSearchContext.Provider>
+ );
+}
+
+function useMessageSearchContext(): MessageSearchState {
+ const context = React.useContext(MessageSearchContext);
+ invariant(context, 'MessageSearchContext not found');
+
+ return context;
+}
+
+export { MessageSearchStateProvider, useMessageSearchContext };
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Dec 8, 9:16 AM (14 h, 53 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5846973
Default Alt Text
D8272.1765185401.diff (1 KB)
Attached To
Mode
D8272: [web] Add message search context
Attached
Detach File
Event Timeline
Log In to Comment