Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3379590
D4060.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
D4060.diff
View Options
diff --git a/lib/hooks/search-sidebars.js b/lib/hooks/search-sidebars.js
--- a/lib/hooks/search-sidebars.js
+++ b/lib/hooks/search-sidebars.js
@@ -9,26 +9,19 @@
import type { SidebarInfo, ThreadInfo } from '../types/thread-types';
import { useSelector } from '../utils/redux-utils';
-type SidebarSearchState = {
- +text: string,
- +results: $ReadOnlySet<string>,
-};
-
function useSearchSidebars(
threadInfo: ThreadInfo,
+ text: string,
+ setTextSearchText: SetState<string>,
): {
+listData: $ReadOnlyArray<SidebarInfo>,
- +searchState: SidebarSearchState,
- +setSearchState: SetState<SidebarSearchState>,
+ +setSearchState: SetState<$ReadOnlySet<string>>,
+onChangeSearchText: (
text: SyntheticEvent<HTMLInputElement> | string,
) => mixed,
+clearQuery: (event: SyntheticEvent<HTMLAnchorElement>) => void,
} {
- const [searchState, setSearchState] = React.useState({
- text: '',
- results: new Set<string>(),
- });
+ const [searchState, setSearchState] = React.useState(new Set<string>());
const userInfos = useSelector(state => state.userStore.userInfos);
@@ -37,13 +30,13 @@
);
const listData = React.useMemo(() => {
- if (!searchState.text) {
+ if (!text) {
return sidebarInfos;
}
return sidebarInfos.filter(sidebarInfo =>
- searchState.results.has(sidebarInfo.threadInfo.id),
+ searchState.has(sidebarInfo.threadInfo.id),
);
- }, [sidebarInfos, searchState]);
+ }, [sidebarInfos, searchState, text]);
const viewerID = useSelector(
state => state.currentUserInfo && state.currentUserInfo.id,
@@ -62,45 +55,40 @@
const onChangeSearchText = React.useCallback(
(event: SyntheticEvent<HTMLInputElement> | string) => {
- let text;
+ let onChangeText;
if (typeof event === 'string') {
- text = event;
+ onChangeText = event;
} else {
- text = event.currentTarget.value;
+ onChangeText = event.currentTarget.value;
}
- setSearchState({
- text,
- results: new Set(searchIndex.getSearchResults(text)),
- });
+ setSearchState(new Set(searchIndex.getSearchResults(onChangeText)));
+ setTextSearchText(onChangeText);
},
- [searchIndex, setSearchState],
+ [searchIndex, setSearchState, setTextSearchText],
);
const clearQuery = React.useCallback(
(event: SyntheticEvent<HTMLAnchorElement>) => {
event.preventDefault();
- setSearchState({ text: '', results: new Set() });
+ setSearchState(new Set());
+ setTextSearchText('');
},
- [setSearchState],
+ [setSearchState, setTextSearchText],
);
React.useEffect(() => {
- setSearchState(curState => ({
- ...curState,
- results: new Set(searchIndex.getSearchResults(curState.text)),
- }));
- }, [searchIndex, setSearchState]);
+ setSearchState(new Set(searchIndex.getSearchResults(text)));
+ }, [searchIndex, setSearchState, text]);
return React.useMemo(
() => ({
listData,
- searchState,
setSearchState,
onChangeSearchText,
clearQuery,
}),
- [listData, setSearchState, searchState, onChangeSearchText, clearQuery],
+ [listData, setSearchState, onChangeSearchText, clearQuery],
);
}
diff --git a/native/chat/sidebar-list-modal.react.js b/native/chat/sidebar-list-modal.react.js
--- a/native/chat/sidebar-list-modal.react.js
+++ b/native/chat/sidebar-list-modal.react.js
@@ -31,12 +31,12 @@
+route: NavigationRoute<'SidebarListModal'>,
};
function SidebarListModal(props: Props): React.Node {
- const {
- listData,
- searchState,
- setSearchState,
- onChangeSearchText,
- } = useSearchSidebars(props.route.params.threadInfo);
+ const [searchText, setSearchText] = React.useState('');
+ const { listData, setSearchState, onChangeSearchText } = useSearchSidebars(
+ props.route.params.threadInfo,
+ searchText,
+ setSearchText,
+ );
const searchTextInputRef = React.useRef();
const setSearchTextInputRef = React.useCallback(
@@ -56,10 +56,7 @@
const navigateToThread = useNavigateToThread();
const onPressItem = React.useCallback(
(threadInfo: ThreadInfo) => {
- setSearchState({
- text: '',
- results: new Set(),
- });
+ setSearchState(new Set());
if (searchTextInputRef.current) {
searchTextInputRef.current.blur();
}
@@ -85,7 +82,7 @@
return (
<Modal>
<Search
- searchText={searchState.text}
+ searchText={searchText}
onChangeText={onChangeSearchText}
containerStyle={styles.search}
placeholder="Search sidebars"
diff --git a/web/modals/chat/sidebar-list-modal.react.js b/web/modals/chat/sidebar-list-modal.react.js
--- a/web/modals/chat/sidebar-list-modal.react.js
+++ b/web/modals/chat/sidebar-list-modal.react.js
@@ -22,12 +22,12 @@
function SidebarListModal(props: Props): React.Node {
const { threadInfo } = props;
- const {
- listData,
- searchState,
- onChangeSearchText,
- clearQuery,
- } = useSearchSidebars(threadInfo);
+ const [searchText, setSearchText] = React.useState('');
+ const { listData, onChangeSearchText, clearQuery } = useSearchSidebars(
+ threadInfo,
+ searchText,
+ setSearchText,
+ );
const { popModal } = useModalContext();
const sidebars = React.useMemo(
@@ -48,7 +48,7 @@
);
let clearQueryButton = null;
- if (searchState.text) {
+ if (searchText) {
clearQueryButton = (
<a href="#" onClick={clearQuery}>
<FontAwesomeIcon
@@ -73,7 +73,7 @@
<Input
type="text"
placeholder="Search sidebars"
- value={searchState.text}
+ value={searchText}
onChange={onChangeSearchText}
/>
{clearQueryButton}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Nov 28, 5:23 PM (21 h, 38 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2594514
Default Alt Text
D4060.diff (5 KB)
Attached To
Mode
D4060: [native, lib, web] [refactor] move text out of state
Attached
Detach File
Event Timeline
Log In to Comment