From e4783eaaa83f825e36eec6d127cf9566443fd166 Mon Sep 17 00:00:00 2001 From: Radek Czemerys Date: Thu, 15 Oct 2020 10:26:34 +0200 Subject: [PATCH] feature: use locked from application state for side menu --- src/AppStack.tsx | 2 +- src/lib/snjs_helper_hooks.ts | 8 ++-- src/screens/Notes/NoteList.tsx | 2 +- src/screens/Notes/Notes.tsx | 2 +- src/screens/Root.tsx | 2 +- .../Settings/Sections/EncryptionSection.tsx | 2 +- .../Settings/Sections/OptionsSection.tsx | 2 +- src/screens/Settings/Settings.tsx | 2 +- src/screens/SideMenu/SideMenuHero.tsx | 44 +++---------------- 9 files changed, 16 insertions(+), 50 deletions(-) diff --git a/src/AppStack.tsx b/src/AppStack.tsx index dba35c2e..72373c82 100644 --- a/src/AppStack.tsx +++ b/src/AppStack.tsx @@ -60,7 +60,7 @@ export const AppStackComponent = ( // Context const application = useContext(ApplicationContext); const theme = useContext(ThemeContext); - const isLocked = useIsLocked(); + const [isLocked] = useIsLocked(); const [hasEditor] = useHasEditor(); // State diff --git a/src/lib/snjs_helper_hooks.ts b/src/lib/snjs_helper_hooks.ts index 951d6669..52aac541 100644 --- a/src/lib/snjs_helper_hooks.ts +++ b/src/lib/snjs_helper_hooks.ts @@ -25,7 +25,7 @@ export const useSignedIn = ( // Context const application = React.useContext(ApplicationContext); - const isLocked = useIsLocked(); + const [isLocked] = useIsLocked(); // State const [signedIn, setSignedIn] = React.useState(false); @@ -59,7 +59,7 @@ export const useSignedIn = ( }; }, [application, signedInCallback, signedOutCallback, isLocked]); - return signedIn; + return [signedIn]; }; export const useOutOfSync = () => { @@ -97,7 +97,7 @@ export const useOutOfSync = () => { return removeSignedInObserver; }, [application]); - return outOfSync; + return [outOfSync]; }; export const useIsLocked = () => { @@ -130,7 +130,7 @@ export const useIsLocked = () => { }; }, [application]); - return isLocked; + return [isLocked]; }; export const useHasEditor = () => { diff --git a/src/screens/Notes/NoteList.tsx b/src/screens/Notes/NoteList.tsx index fbd3f7af..0e98dbae 100644 --- a/src/screens/Notes/NoteList.tsx +++ b/src/screens/Notes/NoteList.tsx @@ -50,7 +50,7 @@ type Props = { export const NoteList = (props: Props): JSX.Element => { // Context - const signedIn = useSignedIn(); + const [signedIn] = useSignedIn(); const application = useContext(ApplicationContext); const themeService = useContext(ThemeServiceContext); const theme = useContext(ThemeContext); diff --git a/src/screens/Notes/Notes.tsx b/src/screens/Notes/Notes.tsx index 13145e2d..1d44fdea 100644 --- a/src/screens/Notes/Notes.tsx +++ b/src/screens/Notes/Notes.tsx @@ -49,7 +49,7 @@ export const Notes = ({ * Update sync status */ const [loading, decrypting, refreshing, startRefreshing] = useSyncStatus(); - const signedIn = useSignedIn(); + const [signedIn] = useSignedIn(); // State const [sortBy, setSortBy] = useState(() => diff --git a/src/screens/Root.tsx b/src/screens/Root.tsx index 2f03f27b..85926900 100644 --- a/src/screens/Root.tsx +++ b/src/screens/Root.tsx @@ -25,7 +25,7 @@ export const Root = (): JSX.Element | null => { // Context const application = useContext(ApplicationContext); const theme = useContext(ThemeContext); - const isLocked = useIsLocked(); + const [isLocked] = useIsLocked(); // State const [, setWidth] = useState(undefined); diff --git a/src/screens/Settings/Sections/EncryptionSection.tsx b/src/screens/Settings/Sections/EncryptionSection.tsx index 987b5cd3..ab6f5e74 100644 --- a/src/screens/Settings/Sections/EncryptionSection.tsx +++ b/src/screens/Settings/Sections/EncryptionSection.tsx @@ -19,7 +19,7 @@ type Props = { export const EncryptionSection = (props: Props) => { // Context const application = useContext(ApplicationContext); - const isLocked = useIsLocked(); + const [isLocked] = useIsLocked(); // State const [protocolDisplayName, setProtocolDisplayName] = useState(''); diff --git a/src/screens/Settings/Sections/OptionsSection.tsx b/src/screens/Settings/Sections/OptionsSection.tsx index 8cf03dcc..89c5ac11 100644 --- a/src/screens/Settings/Sections/OptionsSection.tsx +++ b/src/screens/Settings/Sections/OptionsSection.tsx @@ -26,7 +26,7 @@ type Props = { export const OptionsSection = ({ title, encryptionAvailable }: Props) => { // Context const application = useContext(ApplicationContext); - const signedIn = useSignedIn(); + const [signedIn] = useSignedIn(); const navigation = useNavigation< ModalStackNavigationProp['navigation'] >(); diff --git a/src/screens/Settings/Settings.tsx b/src/screens/Settings/Settings.tsx index 201785eb..37b81ee1 100644 --- a/src/screens/Settings/Settings.tsx +++ b/src/screens/Settings/Settings.tsx @@ -43,7 +43,7 @@ export const Settings = (props: Props) => { props.navigation.goBack(); }, [props.navigation]); - const signedIn = useSignedIn(goBack); + const [signedIn] = useSignedIn(goBack); return ( = props => { const theme = useContext(ThemeContext); // State - const signedIn = useSignedIn(); - const [isLocked, setIsLocked] = React.useState(true); - const isOutOfSync = useOutOfSync(); + const [signedIn] = useSignedIn(); + const [isLocked] = useIsLocked(); + const [isOutOfSync] = useOutOfSync(); const [itemsLength, setItemsLength] = useState(0); useEffect(() => { @@ -45,40 +45,6 @@ export const SideMenuHero: React.FC = props => { return removeStreamItems; }, [application, itemsLength]); - useEffect(() => { - let mounted = true; - const getIsLocked = async () => { - const locked = await application?.isLocked(); - if (mounted) { - if (locked === undefined) { - setIsLocked(true); - } else { - setIsLocked(Boolean(locked)); - } - } - }; - - const removeSignedInObserver = application?.addEventObserver( - async event => { - if ( - event === ApplicationEvent.Launched || - event === ApplicationEvent.SignedIn || - event === ApplicationEvent.WillSync - ) { - setIsLocked(false); - } - if (event === ApplicationEvent.SignedOut) { - getIsLocked(); - } - } - ); - - return () => { - mounted = false; - removeSignedInObserver && removeSignedInObserver(); - }; - }, [application]); - const textData = useMemo(() => { const hasEncryption = application?.isEncryptionAvailable(); if (!signedIn) {