Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

refactor search input

ammarahm-ed5bd6b575f12c
diff --git a/apps/mobile/src/components/SearchInput/index.js b/apps/mobile/src/components/SearchInput/index.js
index b6b018225..98d55d584 100644
--- a/apps/mobile/src/components/SearchInput/index.js
+++ b/apps/mobile/src/components/SearchInput/index.js
@@ -1,291 +1,71 @@
import React, {useEffect, useState} from 'react';
+import {TextInput, View} from 'react-native';
import {useTracked} from '../../provider';
-import {Actions} from '../../provider/Actions';
-import {
- eSubscribeEvent,
- eUnSubscribeEvent,
- ToastEvent,
-} from '../../services/EventManager';
-import {
- eClearSearch,
- eScrollEvent,
- eUpdateSearchState,
-} from '../../utils/Events';
+import SearchService from '../../services/SearchService';
import {inputRef} from '../../utils/Refs';
-import {selection} from '../../utils';
-import Animated, {Easing} from 'react-native-reanimated';
-import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
-import {TextInput, Text} from 'react-native';
-import {br, normalize, SIZE, WEIGHT} from '../../utils/SizeUtils';
-import {db} from '../../utils/DB';
-import {DDS} from '../../services/DeviceDetection';
-import {ActionIcon} from '../ActionIcon';
-const {Value, timing, block} = Animated;
+import {normalize, SIZE, WEIGHT} from '../../utils/SizeUtils';
-let searchResult = [];
+export const SearchInput = (props) => {
+ const [state] = useTracked();
+ const {colors} = state;
+ const [searchState, setSearchState] = useState('Search all notes');
-let offsetY = 0;
-let timeoutAnimate = null;
-let animating = false;
-
-const _marginAnim = new Value(0);
-const _opacity = new Value(1);
-const _borderAnim = new Value(1.5);
-
-const animation = (margin, opacity, border) => {
- if (animating) return;
- animating = true;
- timing(_marginAnim, {
- toValue: margin,
- duration: 130,
- easing: Easing.inOut(Easing.ease),
- }).start();
- timing(_opacity, {
- toValue: opacity,
- duration: 140,
- easing: Easing.inOut(Easing.ease),
- }).start();
- timing(_borderAnim, {
- toValue: border,
- duration: 150,
- easing: Easing.inOut(Easing.ease),
- }).start();
- setTimeout(() => {
- animating = false;
- }, 200);
-};
-
-let timeout = null;
-
-export const Search = (props) => {
- const [state, dispatch] = useTracked();
- const {colors, searchResults} = state;
- const [text, setText] = useState('');
- const [focus, setFocus] = useState(false);
- const [searchState, setSearchState] = useState({
- noSearch: false,
- data: [],
- type: 'notes',
- color: null,
- placeholder: 'Search all notes',
- });
-
- const onScroll = (y) => {
- if (searchResults.results.length > 0) return;
-
- if (y < 30) {
- clearTimeout(timeoutAnimate);
- timeoutAnimate = null;
- animation(0, 1, 1.5);
- offsetY = y;
- return;
- }
- if (y > offsetY) {
- if (y - offsetY < 100) return;
- clearTimeout(timeoutAnimate);
- timeoutAnimate = null;
- timeoutAnimate = setTimeout(() => {
- animation(-65, 0, 0);
- }, 300);
- offsetY = y;
- } else {
- if (offsetY - y < 50) return;
- clearTimeout(timeoutAnimate);
- timeoutAnimate = null;
- timeoutAnimate = setTimeout(() => {
- animation(0, 1, 1.5);
- }, 300);
- offsetY = y;
- }
- };
-
- const updateSearchState = (state) => {
- setSearchState(state);
- };
-
- useEffect(() => {
- selection.data = searchState.data;
- selection.type = searchState.type;
- eSubscribeEvent(eScrollEvent, onScroll);
- eSubscribeEvent('showSearch', (hide) => {
- if (hide) {
- animation(-65, 0, 0);
- return;
- }
- animation(0, 1, 1.5);
- });
-
- eSubscribeEvent(eUpdateSearchState, updateSearchState);
- return () => {
- eUnSubscribeEvent(eScrollEvent, onScroll);
- eUnSubscribeEvent('showSearch', (hide) => {
- if (hide) {
- animation(-65, 0, 0);
- return;
- }
- animation(0, 1, 1.5);
- });
- eUnSubscribeEvent(eUpdateSearchState, updateSearchState);
- };
- }, [searchState]);
-
- const clearSearch = () => {
- if (searchResult && searchResult.length > 0) {
- searchResult = null;
- setText(null);
-
- inputRef.current?.setNativeProps({
- text: '',
- });
- dispatch({
- type: Actions.SEARCH_RESULTS,
- results: {
- results: [],
- type: null,
- keyword: null,
- },
- });
- } else {
- dispatch({
- type: Actions.SEARCH_RESULTS,
- results: {
- results: [],
- type: null,
- keyword: null,
- },
- });
- }
+ const updateSearchState = () => {
+ setSearchState(SearchService.getSearchInformation().placeholder);
+ console.log(SearchService.getSearchInformation().placeholder);
};
useEffect(() => {
- eSubscribeEvent(eClearSearch, clearSearch);
- return () => {
- eUnSubscribeEvent(eClearSearch, clearSearch);
- };
+ updateSearchState();
}, []);
- useEffect(() => {
- if (!text || text.length === 0) {
- clearSearch();
- }
- }, [text]);
-
- const onChangeText = (value) => {
- setText(value);
- };
-
- const onSubmitEditing = async () => {
- if (!text || text.length < 1) {
- clearSearch();
- return;
- }
-
- let type = searchState.type;
- if (!type) return;
-
- let data = searchState.data;
-
- searchResult = await db.lookup[type](
- data[0].data ? db.notes.all : data,
- text,
- );
- if (!searchResult || searchResult.length === 0) {
- ToastEvent.show('No search results found for ' + text, 'error');
- } else {
- dispatch({
- type: Actions.SEARCH_RESULTS,
- results: {
- type,
- results: searchResult,
- keyword: text,
- },
- });
- }
- };
-
- const onBlur = () => {
- if (searchResult && searchResult.length === 0) {
- clearSearch();
- }
- };
+ /* const clearSearch = () => {
+ searchResult = null;
+ inputRef.current?.setNativeProps({
+ text: '',
+ });
+ dispatch({
+ type: Actions.SEARCH_RESULTS,
+ results: [],
+ });
+ }; */
- const onFocus = () => {
- //setSearch(false);
+ const onChangeText = async (value) => {
+ await SearchService.search(value);
};
return (
- <Animated.View
- style={{
- opacity: _opacity,
- height: 60,
- justifyContent: 'center',
- marginTop: _marginAnim,
- paddingHorizontal: 12,
- }}>
- <Animated.View
- style={{
+ <View
+ style={[
+ {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
- width: '100%',
alignSelf: 'center',
- borderRadius: br,
- height: normalize(55),
- backgroundColor: focus
- ? searchState.color
- ? searchState.color
- : colors.shade
- : colors.nav,
- }}>
- <ActionIcon
- customStyle={{
- width: 50,
- height: 50,
- }}
- name="magnify"
- color={
- focus
- ? searchState.color
- ? searchState.color
- : colors.accent
- : colors.icon
- }
- size={SIZE.xxl}
- />
- <TextInput
- ref={inputRef}
- style={{
- fontFamily: WEIGHT.regular,
- color: colors.pri,
- fontSize: SIZE.md,
- flexGrow: 1,
- flex: 1,
- flexWrap: 'wrap',
- }}
- onChangeText={onChangeText}
- onSubmitEditing={onSubmitEditing}
- onFocus={() => {
- setFocus(true);
- onFocus();
- }}
- onBlur={() => {
- setFocus(false);
- onBlur();
- }}
- numberOfLines={1}
- placeholder={searchState.placeholder}
- placeholderTextColor={colors.icon}
- />
- <ActionIcon
- customStyle={{
- width: 50,
- height: 50,
- }}
- name="tune"
- color={colors.icon}
- size={SIZE.xxl}
- />
- </Animated.View>
- </Animated.View>
+ height: normalize(50),
+ borderRadius: 5,
+ width: '100%',
+ paddingHorizontal: 12,
+ },
+ props.customStyle,
+ ]}>
+ <TextInput
+ ref={inputRef}
+ style={{
+ fontFamily: WEIGHT.bold,
+ color: colors.pri,
+ fontSize: SIZE.xl,
+ flexGrow: 1,
+ flex: 1,
+ flexWrap: 'wrap',
+ paddingVertical:0,
+ padding:0
+ }}
+ onChangeText={onChangeText}
+ numberOfLines={1}
+ placeholder={searchState}
+ placeholderTextColor={colors.icon}
+ />
+ </View>
);
};