Skip to content

Commit

Permalink
Working Basic Version
Browse files Browse the repository at this point in the history
  • Loading branch information
KoalaSat committed Oct 25, 2022
1 parent e239af0 commit e2af989
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 43 deletions.
9 changes: 5 additions & 4 deletions frontend/Components/ConfigPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import { RelayPoolContext } from '../../Contexts/RelayPoolContext';

export const ConfigPage: React.FC = () => {
const theme = useTheme();
const { setPage, page, database } = useContext(AppContext);
const { setPrivateKey, relayPool } = useContext(RelayPoolContext);
const { setPage, database } = useContext(AppContext);
const { setPrivateKey, relayPool, publicKey } = useContext(RelayPoolContext);
const { t } = useTranslation('common');
const breadcrump = page.split('%');

const onPressBack: () => void = () => {
setPage(breadcrump.slice(0, -1).join('%'));
if (publicKey) {
setPage(`profile#${publicKey}`);
}
};

const onPressLogout: () => void = () => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/Components/HomePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const HomePage: React.FC = () => {
limit: 20,
};

if (notes.length > 0) {
if (notes.length >= 20) {
message = {
...message,
since: notes[0].created_at,
Expand Down
27 changes: 6 additions & 21 deletions frontend/Components/LandingPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const LandingPage: React.FC = () => {
const [totalPets, setTotalPets] = useState<number>();
const [inputValue, setInputValue] = useState<string>('');
const [loadedUsers, setLoadedUsers] = useState<number>();
const [loadedNotes, setLoadedNotes] = useState<number>();
const styles = StyleSheet.create({
tab: {
height: '100%',
Expand All @@ -43,7 +42,6 @@ export const LandingPage: React.FC = () => {
});

useEffect(() => {
// #1 STEP
if (relayPool && publicKey) {
setStatus(1);
initEvents();
Expand All @@ -55,20 +53,20 @@ export const LandingPage: React.FC = () => {
}, [relayPool, publicKey]);

useEffect(() => {
if (status > 3) {
if (status > 2) {
relayPool?.removeOn('event', 'landing');
setPage('home');
}
}, [status]);

useEffect(() => {
if (loadedUsers ?? loadedNotes) {
const timer = setTimeout(() => setStatus(4), 4000);
if (loadedUsers) {
const timer = setTimeout(() => setStatus(3), 4000);
return () => {
clearTimeout(timer);
};
}
}, [loadedUsers, loadedNotes]);
}, [loadedUsers]);

const initEvents: () => void = () => {
relayPool?.on('event', 'landing', (_relay: Relay, _subId?: string, event?: Event) => {
Expand All @@ -79,8 +77,6 @@ export const LandingPage: React.FC = () => {
} else if (event.kind === EventKind.meta) {
setLoadedUsers((prev) => (prev ? prev + 1 : 1));
if (loadedUsers && loadedUsers - 1 === totalPets) setStatus(3);
} else if (event.kind === EventKind.textNote) {
setLoadedNotes((prev) => (prev ? prev + 1 : 1));
}
}
});
Expand All @@ -95,7 +91,7 @@ export const LandingPage: React.FC = () => {
requestUserData(event);
});
} else {
setStatus(4);
setStatus(3);
}
}
};
Expand All @@ -107,16 +103,6 @@ export const LandingPage: React.FC = () => {
kinds: [EventKind.meta],
authors,
});
relayPool?.subscribe('main-channel', {
kinds: [EventKind.textNote, EventKind.recommendServer],
authors,
limit: 20,
});
relayPool?.subscribe('main-channel', {
kinds: [EventKind.textNote, EventKind.recommendServer],
authors: [publicKey],
limit: 10,
});
}
};

Expand All @@ -131,8 +117,7 @@ export const LandingPage: React.FC = () => {
0: t('landing.connect'),
1: t('landing.connecting'),
2: t('landing.loadingContacts'),
3: t('landing.loadingTimeline'),
4: t('landing.ready'),
3: t('landing.ready'),
};

return (
Expand Down
19 changes: 3 additions & 16 deletions frontend/Components/MainLayout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useState } from 'react';
import React, { useContext } from 'react';
import { Layout } from '@ui-kitten/components';
import { StyleSheet } from 'react-native';
import { AppContext } from '../../Contexts/AppContext';
Expand All @@ -9,13 +9,10 @@ import SendPage from '../SendPage';
import ContactsPage from '../ContactsPage';
import NotePage from '../NotePage';
import LandingPage from '../LandingPage';
import { RelayPoolContext } from '../../Contexts/RelayPoolContext';
import ConfigPage from '../ConfigPage';

export const MainLayout: React.FC = () => {
const { page } = useContext(AppContext);
const { relayPool } = useContext(RelayPoolContext);
const [lastPage, setLastPage] = useState<string>(page);

const styles = StyleSheet.create({
container: {
Expand All @@ -36,16 +33,6 @@ export const MainLayout: React.FC = () => {
const breadcrump: string[] = page.split('%');
const pageToDisplay: string = breadcrump[breadcrump.length - 1].split('#')[0];

const clearSubscriptions: () => boolean = () => {
if (relayPool && lastPage && lastPage !== page) {
relayPool.unsubscribeAll();
relayPool.removeOn('event', lastPage);
setLastPage(page);
}

return true;
};

const view: () => JSX.Element = () => {
if (page === '') {
return <Layout style={styles.container} level='4' />;
Expand All @@ -59,15 +46,15 @@ export const MainLayout: React.FC = () => {
return (
<>
<Layout style={styles.container} level='4'>
{clearSubscriptions() && (pagination[pageToDisplay] ?? <></>)}
{pagination[pageToDisplay]}
</Layout>
<NavigationBar />
</>
);
}
};

return view();
return <>{view()}</>;
};

export default MainLayout;
2 changes: 2 additions & 0 deletions frontend/Contexts/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const AppContextProvider = ({ children }: AppContextProviderProps): JSX.E
createInitDatabase(db).then(() => {
setLoadingDb(false);
});
} else {
setLoadingDb(false);
}
});
}, []);
Expand Down
12 changes: 11 additions & 1 deletion frontend/Contexts/RelayPoolContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ export const initialRelayPoolContext: RelayPoolContextProps = {
export const RelayPoolContextProvider = ({
children,
}: RelayPoolContextProviderProps): JSX.Element => {
const { database, loadingDb, setPage } = useContext(AppContext);
const { database, loadingDb, setPage, page } = useContext(AppContext);

const [publicKey, setPublicKey] = useState<string>();
const [privateKey, setPrivateKey] = useState<string>();
const [relayPool, setRelayPool] = useState<RelayPool>();
const [lastEventId, setLastEventId] = useState<string>();
const [lastPage, setLastPage] = useState<string>(page);

const loadRelayPool: () => void = () => {
if (database && privateKey) {
Expand Down Expand Up @@ -94,9 +95,18 @@ export const RelayPoolContextProvider = ({
}
}, [privateKey, loadingDb]);

useEffect(() => {
if (relayPool && lastPage !== page) {
relayPool.unsubscribeAll();
relayPool.removeOn('event', lastPage);
setLastPage(page);
}
}, [page]);

useEffect(() => {
EncryptedStorage.getItem('privateKey').then((result) => {
if (result && result !== '') {
loadRelayPool();
setPage('home');
setPrivateKey(result);
} else {
Expand Down

0 comments on commit e2af989

Please sign in to comment.