Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
fix: use state change observer for detecting changed tag
Browse files Browse the repository at this point in the history
  • Loading branch information
antsgar committed Mar 4, 2021
1 parent efa437f commit 8f12696
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/screens/Notes/NoteList.tsx
@@ -1,4 +1,4 @@
import { AppStateEventType } from '@Lib/application_state';
import { AppStateEventType, AppStateType } from '@Lib/application_state';
import { useSignedIn } from '@Lib/snjs_helper_hooks';
import { useFocusEffect } from '@react-navigation/native';
import { ApplicationContext } from '@Root/ApplicationContext';
Expand Down Expand Up @@ -62,7 +62,6 @@ export const NoteList = (props: Props) => {
// Ref
const searchBoxInputRef = useRef<IosSearchBar>(null);
const noteListRef = useRef<FlatList>(null);
const selectedTag = useRef<SNTag | undefined>(undefined);

const dissmissKeybard = () => {
searchBoxInputRef.current?.blur();
Expand All @@ -80,17 +79,23 @@ export const NoteList = (props: Props) => {
return unsubscribeStateEventObserver;
}, [application]);

useEffect(() => {
const newSelectedTag = application?.getAppState().selectedTag;
const scrollListToTop = useCallback(() => {
if (props.notes && props.notes.length > 0) {
noteListRef.current?.scrollToIndex({ animated: false, index: 0 });
}
}, [props.notes]);

if (newSelectedTag !== selectedTag.current) {
selectedTag.current = application?.getAppState().selectedTag;
useEffect(() => {
const unsubscribeTagChangedEventObserver = application
?.getAppState()
.addStateChangeObserver(event => {
if (event === AppStateType.TagChanged) {
scrollListToTop();
}
});

if (props.notes && props.notes.length > 0) {
noteListRef.current?.scrollToIndex({ animated: false, index: 0 });
}
}
}, [application, props.notes]);
return unsubscribeTagChangedEventObserver;
}, [application, scrollListToTop]);

useEffect(() => {
/**
Expand Down

0 comments on commit 8f12696

Please sign in to comment.