Skip to content

Commit

Permalink
Fix Load on init
Browse files Browse the repository at this point in the history
  • Loading branch information
KoalaSat committed Oct 29, 2022
1 parent 04bbafe commit 02a9150
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
7 changes: 4 additions & 3 deletions frontend/Components/ConfigPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { RelayPoolContext } from '../../Contexts/RelayPoolContext';

export const ConfigPage: React.FC = () => {
const theme = useTheme();
const { goToPage, goBack, database } = useContext(AppContext);
const { goToPage, goBack, database, init } = useContext(AppContext);
const { setPrivateKey, setPublicKey, relayPool } = useContext(RelayPoolContext);
const { t } = useTranslation('common');

Expand All @@ -31,10 +31,11 @@ export const ConfigPage: React.FC = () => {
const onPressLogout: () => void = () => {
if (database) {
relayPool?.unsubscribeAll();
setPrivateKey('');
setPublicKey('');
setPrivateKey(undefined);
setPublicKey(undefined);
dropTables(database).then(() => {
EncryptedStorage.removeItem('privateKey').then(() => {
init();
goToPage('landing', true);
});
});
Expand Down
4 changes: 3 additions & 1 deletion frontend/Components/ProfilePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export const ProfilePage: React.FC = () => {
setContactsIds(users.map((user) => user.id));
});
}
getNotes(database, { filters: { pubkey: userId } }).then(setNotes);
getNotes(database, { filters: { pubkey: userId } }).then((results) => {
if (results.length > 0) setNotes(results);
});
}
}, [lastEventId, database]);

Expand Down
9 changes: 7 additions & 2 deletions frontend/Contexts/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface AppContextProps {
page: string;
goToPage: (path: string, root?: boolean) => void;
goBack: () => void;
init: () => void;
loadingDb: boolean;
database: SQLiteDatabase | null;
}
Expand All @@ -19,6 +20,7 @@ export interface AppContextProviderProps {

export const initialAppContext: AppContextProps = {
page: '',
init: () => {},
goToPage: () => {},
goBack: () => {},
loadingDb: true,
Expand All @@ -30,7 +32,7 @@ export const AppContextProvider = ({ children }: AppContextProviderProps): JSX.E
const [database, setDatabase] = useState<SQLiteDatabase | null>(null);
const [loadingDb, setLoadingDb] = useState<boolean>(initialAppContext.loadingDb);

useEffect(() => {
const init: () => void = () => {
EncryptedStorage.getItem('privateKey').then((result) => {
const db = initDatabase();
setDatabase(db);
Expand All @@ -42,7 +44,9 @@ export const AppContextProvider = ({ children }: AppContextProviderProps): JSX.E
setLoadingDb(false);
}
});
}, []);
};

useEffect(init, []);

const goToPage: (path: string, root?: boolean) => void = (path, root) => {
if (page !== '' && !root) {
Expand All @@ -61,6 +65,7 @@ export const AppContextProvider = ({ children }: AppContextProviderProps): JSX.E
<AppContext.Provider
value={{
page,
init,
goToPage,
goBack,
loadingDb,
Expand Down
7 changes: 3 additions & 4 deletions frontend/Contexts/RelayPoolContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export interface RelayPoolContextProps {
relayPool?: RelayPool;
setRelayPool: (relayPool: RelayPool) => void;
publicKey?: string;
setPublicKey: (privateKey: string) => void;
setPublicKey: (privateKey: string | undefined) => void;
privateKey?: string;
setPrivateKey: (privateKey: string) => void;
setPrivateKey: (privateKey: string | undefined) => void;
lastEventId?: string;
setLastEventId: (lastEventId: string) => void;
}
Expand Down Expand Up @@ -84,7 +84,7 @@ export const RelayPoolContextProvider = ({
};

useEffect(() => {
if (privateKey) {
if (privateKey && privateKey !== '') {
setPublicKey(getPublickey(privateKey));
}
}, [privateKey]);
Expand All @@ -110,7 +110,6 @@ export const RelayPoolContextProvider = ({
setPrivateKey(result);
setPublicKey(getPublickey(result));
} else {
setPrivateKey('');
goToPage('landing', true);
}
});
Expand Down

0 comments on commit 02a9150

Please sign in to comment.