-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
123 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,20 @@ | ||
import { cookies } from 'next/headers'; | ||
|
||
import { EventHandler } from '@/components/events/Events'; | ||
import { Contacts } from '@/views/contacts/Contacts'; | ||
|
||
export default function Page() { | ||
return <Contacts />; | ||
const eventsUrl = process.env.EVENTS_URL; | ||
|
||
const cookieStore = cookies(); | ||
const accessToken = cookieStore.get('amboss_banco_access_token')?.value; | ||
|
||
return ( | ||
<> | ||
{accessToken && eventsUrl ? ( | ||
<EventHandler accessToken={accessToken} eventsUrl={eventsUrl} /> | ||
) : null} | ||
<Contacts /> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
'use client'; | ||
|
||
import { useApolloClient } from '@apollo/client'; | ||
import { | ||
EventStreamContentType, | ||
fetchEventSource, | ||
} from '@microsoft/fetch-event-source'; | ||
import { FC, useCallback, useEffect } from 'react'; | ||
|
||
import { useToast } from '../ui/use-toast'; | ||
|
||
class RetriableError extends Error {} | ||
class FatalError extends Error {} | ||
|
||
export const EventHandler: FC<{ | ||
accessToken: string; | ||
eventsUrl: string; | ||
}> = ({ accessToken, eventsUrl }) => { | ||
const client = useApolloClient(); | ||
const { toast } = useToast(); | ||
|
||
const showToast = useCallback( | ||
(sender: string | undefined) => { | ||
if (!sender) return; | ||
toast({ | ||
title: 'New Message', | ||
description: sender | ||
? `You have a new message from ${sender}` | ||
: undefined, | ||
}); | ||
}, | ||
[toast] | ||
); | ||
|
||
useEffect(() => { | ||
const startSource = async () => { | ||
try { | ||
await fetchEventSource(`${eventsUrl}/contacts`, { | ||
headers: { Authorization: `Bearer ${accessToken}` }, | ||
async onopen(response) { | ||
if ( | ||
response.ok && | ||
response.headers.get('content-type') === EventStreamContentType | ||
) { | ||
return; | ||
} else if ( | ||
response.status >= 400 && | ||
response.status < 500 && | ||
response.status !== 429 | ||
) { | ||
throw new FatalError(); | ||
} else { | ||
throw new RetriableError(); | ||
} | ||
}, | ||
onmessage(ev) { | ||
try { | ||
const parsed = JSON.parse(ev.data); | ||
showToast(parsed.sender_money_address); | ||
} catch (error) {} | ||
|
||
client.reFetchObservableQueries(); | ||
}, | ||
onclose() { | ||
throw new RetriableError(); | ||
}, | ||
onerror(err) { | ||
if (err instanceof FatalError) { | ||
throw err; | ||
} else { | ||
return 2000; | ||
} | ||
}, | ||
}); | ||
} catch (error) {} | ||
}; | ||
|
||
startSource(); | ||
}, [accessToken, client, showToast, eventsUrl]); | ||
|
||
return null; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,7 @@ export const GetWalletContactMessages = gql` | |
id | ||
contact_is_sender | ||
payload | ||
created_at | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters