88} from '@pushprotocol/pushchain-ui-kit' ;
99import PushMail from 'push-mail' ;
1010import { ReactNode , useEffect , useState } from 'react' ;
11- import { Email , Wallet } from '.. /common' ;
11+ import { Email , EMAIL_BOX , transformEmails , Wallet } from '@ /common' ;
1212
1313interface AppContextType {
1414 searchInput : string ;
@@ -27,15 +27,19 @@ interface AppContextType {
2727 inbox : Email [ ] ;
2828 } >
2929 > ;
30- currTab : 'inbox' | 'sent' ;
31- setCurrTab : React . Dispatch < React . SetStateAction < 'inbox' | 'sent' > > ;
30+ currTab : EMAIL_BOX ;
31+ setCurrTab : React . Dispatch < React . SetStateAction < EMAIL_BOX > > ;
3232 replyTo : Email | undefined ;
3333 setReplyTo : React . Dispatch < React . SetStateAction < Email | undefined > > ;
3434 account : string | null ;
3535 handleSendSignRequestToPushWallet : ( data : Uint8Array ) => Promise < Uint8Array > ;
3636 wallet : Wallet | null ;
3737 getEmails : ( ) => Promise < void > ;
3838 isLoading : boolean ;
39+ getSentEmails : ( ) => Promise < void > ;
40+ isSentEmailLoading : boolean ;
41+ getReceivedEmails : ( ) => Promise < void > ;
42+ isReceivedEmailLoading : boolean ;
3943}
4044
4145export const AppContext = createContext < AppContextType | undefined > ( undefined ) ;
@@ -44,7 +48,8 @@ export function AppProvider({ children }: { children: ReactNode }) {
4448 const [ searchInput , setSearchInput ] = useState < string > ( '' ) ;
4549 const [ selectedEmail , setSelectedEmail ] = useState < Email | null > ( null ) ;
4650 const [ pushNetwork , setPushNetwork ] = useState < PushNetwork | null > ( null ) ;
47- const [ currTab , setCurrTab ] = useState < 'inbox' | 'sent' > ( 'inbox' ) ;
51+ const [ pushEmail , setPushEmail ] = useState < PushMail | null > ( null ) ;
52+ const [ currTab , setCurrTab ] = useState < EMAIL_BOX > ( EMAIL_BOX . INBOX ) ;
4853 const [ emails , setEmails ] = useState < {
4954 sent : Email [ ] ;
5055 inbox : Email [ ] ;
@@ -55,45 +60,65 @@ export function AppProvider({ children }: { children: ReactNode }) {
5560 const [ replyTo , setReplyTo ] = useState < Email | undefined > ( undefined ) ;
5661 const [ wallet , setWallet ] = useState < Wallet | null > ( null ) ;
5762 const [ isLoading , setIsLoading ] = useState ( false ) ;
63+ const [ isSentEmailLoading , setIsSentEmailLoading ] = useState ( false ) ;
64+ const [ isReceivedEmailLoading , setIsReceivedEmailLoading ] = useState ( false ) ;
5865
5966 const { account, handleSendSignRequestToPushWallet } = usePushWalletContext ( ) ;
6067
61- const getEmails = async ( ) => {
62- if ( ! account ) return ;
63- setIsLoading ( true ) ;
64- const pushMail = await PushMail . initialize ( ENV . DEV ) ;
65- const [ sent , received ] = await Promise . all ( [
66- pushMail . getBySender ( account ) ,
67- pushMail . getByRecipient ( account ) ,
68- ] ) ;
68+ const getSentEmails = async ( ) => {
69+ if ( ! account || ! pushEmail || isLoading || isSentEmailLoading ) return ;
70+ setIsSentEmailLoading ( true ) ;
71+ try {
72+ const sent = await pushEmail . getBySender ( account ) ;
73+ setEmails ( ( prev ) => ( {
74+ ...prev ,
75+ sent : transformEmails ( sent ) ,
76+ } ) ) ;
77+ } catch ( err ) {
78+ console . log ( 'Error fetching Sent Emails' , err ) ;
79+ } finally {
80+ setIsSentEmailLoading ( false ) ;
81+ }
82+ } ;
6983
70- setIsLoading ( false ) ;
84+ const getReceivedEmails = async ( ) => {
85+ if ( ! account || ! pushEmail || isLoading || isReceivedEmailLoading ) return ;
86+ setIsReceivedEmailLoading ( true ) ;
87+ try {
88+ const received = await pushEmail . getByRecipient ( account ) ;
89+ setEmails ( ( prev ) => ( {
90+ ...prev ,
91+ inbox : transformEmails ( received ) ,
92+ } ) ) ;
93+ } catch ( err ) {
94+ console . log ( 'Error fetching Received Emails' , err ) ;
95+ } finally {
96+ setIsReceivedEmailLoading ( false ) ;
97+ }
98+ } ;
7199
72- setEmails ( {
73- sent : sent . map ( ( email : any ) => ( {
74- from : email . from ,
75- to : email . to ,
76- subject : email . subject ,
77- timestamp : email . ts ,
78- body : email . body . content ,
79- attachments : email . attachments ,
80- txHash : email . txHash ,
81- } ) ) ,
82- inbox : received . map ( ( email : any ) => ( {
83- from : email . from ,
84- to : email . to ,
85- subject : email . subject ,
86- timestamp : email . ts ,
87- body : email . body . content ,
88- attachments : email . attachments ,
89- txHash : email . txHash ,
90- } ) ) ,
91- } ) ;
100+ const getEmails = async ( ) => {
101+ if ( ! account || ! pushEmail || isLoading ) return ;
102+ setIsLoading ( true ) ;
103+ try {
104+ await Promise . all ( [ getSentEmails ( ) , getReceivedEmails ( ) ] ) ;
105+ } finally {
106+ setIsLoading ( false ) ;
107+ }
92108 } ;
93109
94110 useEffect ( ( ) => {
95111 getEmails ( ) ;
96- } , [ account ] ) ;
112+ } , [ account , pushEmail ] ) ;
113+
114+ useEffect ( ( ) => {
115+ const interval = setInterval ( ( ) => {
116+ getSentEmails ( ) ;
117+ getReceivedEmails ( ) ;
118+ } , 5 * 60 * 1000 ) ; // 5 minutes interval
119+
120+ return ( ) => clearInterval ( interval ) ;
121+ } ) ;
97122
98123 useEffect ( ( ) => {
99124 if ( account ) {
@@ -106,8 +131,9 @@ export function AppProvider({ children }: { children: ReactNode }) {
106131 const setNetwork = async ( ) => {
107132 try {
108133 const pushNetworkInstance = await PushNetwork . initialize ( ENV . DEV ) ;
109- console . log ( 'Push Network initialized:' , pushNetworkInstance ) ;
134+ const pushMail = await PushMail . initialize ( ENV . DEV ) ;
110135 setPushNetwork ( pushNetworkInstance ) ;
136+ setPushEmail ( pushMail ) ;
111137 } catch ( error ) {
112138 console . error ( 'Error initializing Push Network:' , error ) ;
113139 }
@@ -134,6 +160,10 @@ export function AppProvider({ children }: { children: ReactNode }) {
134160 wallet,
135161 getEmails,
136162 isLoading,
163+ getSentEmails,
164+ isSentEmailLoading,
165+ getReceivedEmails,
166+ isReceivedEmailLoading,
137167 } }
138168 >
139169 { children }
0 commit comments