From 8c6a40bab7300e5c6da459da5f8091643e807e70 Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Thu, 15 Sep 2022 13:39:46 -0600 Subject: [PATCH] Enrich limit order notification --- common/notification.ts | 17 +- functions/src/create-notification.ts | 7 + .../scripts/backfill-contract-followers.ts | 8 +- web/pages/notifications.tsx | 233 +++++++++++++----- 4 files changed, 190 insertions(+), 75 deletions(-) diff --git a/common/notification.ts b/common/notification.ts index c34f5b9c..2f03467d 100644 --- a/common/notification.ts +++ b/common/notification.ts @@ -92,11 +92,6 @@ export type notification_reason_types = | 'your_contract_closed' | 'subsidized_your_market' -export type BettingStreakData = { - streak: number - bonusAmount: number -} - type notification_descriptions = { [key in notification_preference]: { simple: string @@ -241,3 +236,15 @@ export const NOTIFICATION_DESCRIPTIONS: notification_descriptions = { detailed: `Answers on markets that you're watching and that you're invested in`, }, } + +export type BettingStreakData = { + streak: number + bonusAmount: number +} + +export type BetFillData = { + betOutcome: string + creatorOutcome: string + probability: number + fillAmount: number +} diff --git a/functions/src/create-notification.ts b/functions/src/create-notification.ts index ba9fa5c4..390a8cd8 100644 --- a/functions/src/create-notification.ts +++ b/functions/src/create-notification.ts @@ -1,5 +1,6 @@ import * as admin from 'firebase-admin' import { + BetFillData, BettingStreakData, Notification, notification_reason_types, @@ -542,6 +543,12 @@ export const createBetFillNotification = async ( sourceContractTitle: contract.question, sourceContractSlug: contract.slug, sourceContractId: contract.id, + data: { + betOutcome: bet.outcome, + creatorOutcome: userBet.outcome, + fillAmount, + probability: userBet.limitProb, + } as BetFillData, } return await notificationRef.set(removeUndefinedProps(notification)) diff --git a/functions/src/scripts/backfill-contract-followers.ts b/functions/src/scripts/backfill-contract-followers.ts index 9b936654..9b5834bc 100644 --- a/functions/src/scripts/backfill-contract-followers.ts +++ b/functions/src/scripts/backfill-contract-followers.ts @@ -4,14 +4,14 @@ import { initAdmin } from './script-init' initAdmin() import { getValues } from '../utils' -import { Contract } from 'common/lib/contract' -import { Comment } from 'common/lib/comment' +import { Contract } from 'common/contract' +import { Comment } from 'common/comment' import { uniq } from 'lodash' -import { Bet } from 'common/lib/bet' +import { Bet } from 'common/bet' import { DEV_HOUSE_LIQUIDITY_PROVIDER_ID, HOUSE_LIQUIDITY_PROVIDER_ID, -} from 'common/lib/antes' +} from 'common/antes' const firestore = admin.firestore() diff --git a/web/pages/notifications.tsx b/web/pages/notifications.tsx index 008f5df1..bc5e8cc6 100644 --- a/web/pages/notifications.tsx +++ b/web/pages/notifications.tsx @@ -1,7 +1,11 @@ import { ControlledTabs } from 'web/components/layout/tabs' import React, { useEffect, useMemo, useState } from 'react' import Router, { useRouter } from 'next/router' -import { Notification, notification_source_types } from 'common/notification' +import { + BetFillData, + Notification, + notification_source_types, +} from 'common/notification' import { Avatar, EmptyAvatar } from 'web/components/avatar' import { Row } from 'web/components/layout/row' import { Page } from 'web/components/page' @@ -141,6 +145,7 @@ function RenderNotificationGroups(props: { ) : ( + ) + } + // TODO Add new notification components here if (justSummary) { return ( - -
-
- -
- - {sourceType && - reason && - getReasonForShowingNotification(notification, true)} - -
- -
-
-
-
-
+ + + ) } + return ( + +
+ +
+
+ ) +} + +function NotificationSummaryFrame(props: { + notification: Notification + subtitle: string + children: React.ReactNode +}) { + const { notification, subtitle, children } = props + const { sourceUserName, sourceUserUsername } = notification + return ( + +
+
+ +
+ {subtitle} +
{children}
+
+
+
+
+ ) +} + +function NotificationFrame(props: { + notification: Notification + highlighted: boolean + subtitle: string + children: React.ReactNode + isChildOfGroup?: boolean +}) { + const { notification, isChildOfGroup, highlighted, subtitle, children } = + props + const { + sourceType, + sourceUserName, + sourceUserAvatarUrl, + sourceUpdateType, + reason, + reasonText, + sourceUserUsername, + sourceText, + } = notification + const questionNeedsResolution = sourceUpdateType == 'closed' + return (
- {!questionNeedsResolution && ( - - )} - {getReasonForShowingNotification( - notification, - isChildOfGroup ?? false - )} + + {subtitle} {isChildOfGroup ? ( ) : ( @@ -822,9 +877,7 @@ function NotificationItem(props: { )}
-
- -
+
{children}
@@ -832,6 +885,66 @@ function NotificationItem(props: { ) } +function BetFillNotification(props: { + notification: Notification + highlighted: boolean + justSummary: boolean + isChildOfGroup?: boolean +}) { + const { notification, isChildOfGroup, highlighted, justSummary } = props + const { sourceText, data } = notification + const { creatorOutcome, probability } = (data as BetFillData) ?? {} + const subtitle = 'bet against you' + const amount = formatMoney(parseInt(sourceText ?? '0')) + const description = + creatorOutcome && probability ? ( + + of your{' '} + + {creatorOutcome}{' '} + + limit order at {Math.round(probability * 100)}% was filled + + ) : ( + of your limit order was filled + ) + + if (justSummary) { + return ( + + + {amount} + {description} + + + ) + } + + return ( + + + + {amount} + {description} + + + + ) +} + export const setNotificationsAsSeen = async (notifications: Notification[]) => { const unseenNotifications = notifications.filter((n) => !n.isSeen) return await Promise.all( @@ -1002,15 +1115,6 @@ function NotificationTextLabel(props: { return ( {formatMoney(parseInt(sourceText))} ) - } else if (sourceType === 'bet' && sourceText) { - return ( - <> - - {formatMoney(parseInt(sourceText))} - {' '} - of your limit order was filled - - ) } else if (sourceType === 'challenge' && sourceText) { return ( <> @@ -1074,9 +1178,6 @@ function getReasonForShowingNotification( else if (sourceSlug) reasonText = 'joined because you shared' else reasonText = 'joined because of you' break - case 'bet': - reasonText = 'bet against you' - break case 'challenge': reasonText = 'accepted your challenge' break