Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3347431
D8272.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
D8272.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,67 @@
+// @flow
+
+import invariant from 'invariant';
+import * as React from 'react';
+
+type MessageSearchState = {
+ +getQuery: (threadID: string) => string,
+ +setQuery: (query: string, threadID: string) => void,
+ +clearQuery: (threadID: string) => 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 setQuery = React.useCallback(
+ (query: string, threadID: string) =>
+ setQueries(prevQueries => ({ ...prevQueries, [threadID]: query })),
+ [],
+ );
+
+ const clearQuery = React.useCallback(
+ (threadID: string) =>
+ setQueries(prevQueries => {
+ const { [threadID]: deleted, ...newState } = prevQueries;
+ return newState;
+ }),
+ [],
+ );
+
+ const getQuery = React.useCallback(
+ (threadID: string) => queries[threadID] ?? '',
+ [queries],
+ );
+
+ const state = React.useMemo(
+ () => ({
+ getQuery,
+ setQuery,
+ clearQuery,
+ }),
+ [getQuery, setQuery, 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
Sat, Nov 23, 12:15 PM (19 h, 14 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2570544
Default Alt Text
D8272.diff (1 KB)
Attached To
Mode
D8272: [web] Add message search context
Attached
Detach File
Event Timeline
Log In to Comment