diff --git a/src/pages/create/index.tsx b/src/pages/create/index.tsx index e51a9d9..acde385 100644 --- a/src/pages/create/index.tsx +++ b/src/pages/create/index.tsx @@ -46,9 +46,7 @@ import { shorten, signEventForMetaFile, updateUsersAppData, - uploadToFileStorage, - isPromiseFulfilled, - isPromiseRejected + uploadToFileStorage } from '../../utils' import { Container } from '../../components/Container' import styles from './style.module.scss' @@ -86,6 +84,7 @@ import { faUpload } from '@fortawesome/free-solid-svg-icons' import { SigitFile } from '../../utils/file.ts' +import { checkNotifications } from '../../utils/notifications.ts' export const CreatePage = () => { const navigate = useNavigate() @@ -787,14 +786,7 @@ export const CreatePage = () => { setLoadingSpinnerDesc('Sending notifications to counterparties') const notifications = await Promise.allSettled(sendNotifications(meta)) - if (notifications.every(isPromiseFulfilled)) { - toast.success('Notifications sent successfully') - } else { - logRejectedNotifications(notifications) - toast.error( - 'Some notifications failed to publish. Please rebroadcast them again.' - ) - } + checkNotifications(notifications) navigate(appPrivateRoutes.sign, { state: { meta: meta } }) } else { const zip = new JSZip() @@ -850,15 +842,6 @@ export const CreatePage = () => { } } - const logRejectedNotifications = ( - notifications: PromiseSettledResult[] - ) => { - notifications - .filter(isPromiseRejected) - .map((res: PromiseRejectedResult) => (res.reason as Error).message) - .forEach((message: string) => console.log(message)) - } - const onDrawFieldsChange = (sigitFiles: SigitFile[]) => { setDrawnFiles(sigitFiles) } diff --git a/src/pages/sign/index.tsx b/src/pages/sign/index.tsx index 12e4c81..7158bff 100644 --- a/src/pages/sign/index.tsx +++ b/src/pages/sign/index.tsx @@ -53,6 +53,7 @@ import { SigitFile } from '../../utils/file.ts' import { ARRAY_BUFFER, DEFLATE } from '../../utils/const.ts' +import { checkNotifications } from '../../utils/notifications.ts' enum SignedStatus { Fully_Signed, User_Is_Next_Signer, @@ -663,59 +664,22 @@ export const SignPage = () => { // Handle the online flow: update users app data and send notifications const handleOnlineFlow = async (meta: Meta) => { - setLoadingSpinnerDesc('Updating users app data') - const updatedEvent = await updateUsersAppData(meta) - if (!updatedEvent) { + try { + setLoadingSpinnerDesc('Updating users app data') + const updatedEvent = await updateUsersAppData(meta) + if (!updatedEvent) { + throw new Error('There was an error updating user app data.') + } + setLoadingSpinnerDesc('Sending notifications') + + const notifications = await notifyUsers(meta) + checkNotifications(notifications) + } catch (error) { + console.error(error) + toast.error('There was an error finalising signatures.') + } finally { setIsLoading(false) - return } - - const userSet = new Set<`npub1${string}`>() - if (submittedBy && submittedBy !== usersPubkey) { - userSet.add(hexToNpub(submittedBy)) - } - - const usersNpub = hexToNpub(usersPubkey!) - const isLastSigner = checkIsLastSigner(signers) - if (isLastSigner) { - signers.forEach((signer) => { - if (signer !== usersNpub) { - userSet.add(signer) - } - }) - - viewers.forEach((viewer) => { - userSet.add(viewer) - }) - } else { - const currentSignerIndex = signers.indexOf(usersNpub) - const prevSigners = signers.slice(0, currentSignerIndex) - - prevSigners.forEach((signer) => { - userSet.add(signer) - }) - - const nextSigner = signers[currentSignerIndex + 1] - userSet.add(nextSigner) - } - - setLoadingSpinnerDesc('Sending notifications') - const users = Array.from(userSet) - console.log('users: ', users) - const publishedRelays = await Promise.all( - users.map(async (user) => sendNotification(npubToHex(user)!, meta)) - ) - console.log('published relays: ', publishedRelays) - // await Promise.all(promises) - // .then(() => { - // toast.success('Notifications sent successfully') - // setMeta(meta) - // }) - // .catch(() => { - // toast.error('Failed to publish notifications') - // }) - - setIsLoading(false) } // Check if the current user is the last signer @@ -873,6 +837,64 @@ export const SignPage = () => { } } + const getUsersToNotify = (): `npub1${string}`[] => { + const userSet = new Set<`npub1${string}`>() + if (submittedBy && submittedBy !== usersPubkey) { + userSet.add(hexToNpub(submittedBy)) + } + + const usersNpub = hexToNpub(usersPubkey!) + const isLastSigner = checkIsLastSigner(signers) + if (isLastSigner) { + signers.forEach((signer) => { + if (signer !== usersNpub) { + userSet.add(signer) + } + }) + + viewers.forEach((viewer) => userSet.add(viewer)) + } else { + const currentSignerIndex = signers.indexOf(usersNpub) + const prevSigners = signers.slice(0, currentSignerIndex) + + prevSigners.forEach((signer) => { + userSet.add(signer) + }) + + const nextSigner = signers[currentSignerIndex + 1] + userSet.add(nextSigner) + } + return Array.from(userSet) + } + + const notifyUsers = async (meta: Meta) => { + try { + const usersToNotify = getUsersToNotify() + return await Promise.allSettled( + usersToNotify.map(async (user) => + sendNotification(npubToHex(user)!, meta) + ) + ) + } catch (error) { + throw new Error('There was a problem sending notifications to users', { + cause: error + }) + } + } + + const handleRenotifyUsers = async () => { + try { + setIsLoading(true) + const notifications = await notifyUsers(meta!) + checkNotifications(notifications) + } catch (error) { + console.error(error) + toast.error('There was an error re-notifying users') + } finally { + setIsLoading(false) + } + } + if (authUrl) { return (