Merge branch 'main' of https://github.com/marsteralex/manifold
This commit is contained in:
commit
f7a32a0913
43
.github/workflows/format.yml
vendored
Normal file
43
.github/workflows/format.yml
vendored
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
name: Reformat main
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
|
||||||
|
env:
|
||||||
|
FORCE_COLOR: 3
|
||||||
|
NEXT_TELEMETRY_DISABLED: 1
|
||||||
|
|
||||||
|
# mqp - i generated a personal token to use for these writes -- it's unclear
|
||||||
|
# why, but the default token didn't work, even when i gave it max permissions
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
prettify:
|
||||||
|
name: Auto-prettify
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.FORMATTER_ACCESS_TOKEN }}
|
||||||
|
- name: Restore cached node_modules
|
||||||
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
path: '**/node_modules'
|
||||||
|
key: ${{ runner.os }}-${{ matrix.node-version }}-nodemodules-${{ hashFiles('**/yarn.lock') }}
|
||||||
|
- name: Install missing dependencies
|
||||||
|
run: yarn install --prefer-offline --frozen-lockfile
|
||||||
|
- name: Run Prettier on web client
|
||||||
|
working-directory: web
|
||||||
|
run: yarn format
|
||||||
|
- name: Commit any Prettier changes
|
||||||
|
uses: stefanzweifel/git-auto-commit-action@v4
|
||||||
|
with:
|
||||||
|
commit_message: Auto-prettification
|
||||||
|
branch: ${{ github.head_ref }}
|
|
@ -14,18 +14,21 @@ export type Bet = {
|
||||||
probBefore: number
|
probBefore: number
|
||||||
probAfter: number
|
probAfter: number
|
||||||
|
|
||||||
sale?: {
|
|
||||||
amount: number // amount user makes from sale
|
|
||||||
betId: string // id of bet being sold
|
|
||||||
// TODO: add sale time?
|
|
||||||
}
|
|
||||||
|
|
||||||
fees: Fees
|
fees: Fees
|
||||||
|
|
||||||
isSold?: boolean // true if this BUY bet has been sold
|
|
||||||
isAnte?: boolean
|
isAnte?: boolean
|
||||||
isLiquidityProvision?: boolean
|
isLiquidityProvision?: boolean
|
||||||
isRedemption?: boolean
|
isRedemption?: boolean
|
||||||
|
challengeSlug?: string
|
||||||
|
|
||||||
|
// Props for bets in DPM contract below.
|
||||||
|
// A bet is either a BUY or a SELL that sells all of a previous buy.
|
||||||
|
isSold?: boolean // true if this BUY bet has been sold
|
||||||
|
// This field marks a SELL bet.
|
||||||
|
sale?: {
|
||||||
|
amount: number // amount user makes from sale
|
||||||
|
betId: string // id of BUY bet being sold
|
||||||
|
}
|
||||||
} & Partial<LimitProps>
|
} & Partial<LimitProps>
|
||||||
|
|
||||||
export type NumericBet = Bet & {
|
export type NumericBet = Bet & {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { maxBy } from 'lodash'
|
import { maxBy, sortBy, sum, sumBy } from 'lodash'
|
||||||
import { Bet, LimitBet } from './bet'
|
import { Bet, LimitBet } from './bet'
|
||||||
import {
|
import {
|
||||||
calculateCpmmSale,
|
calculateCpmmSale,
|
||||||
|
@ -133,10 +133,46 @@ export function resolvedPayout(contract: Contract, bet: Bet) {
|
||||||
: calculateDpmPayout(contract, bet, outcome)
|
: calculateDpmPayout(contract, bet, outcome)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getCpmmInvested(yourBets: Bet[]) {
|
||||||
|
const totalShares: { [outcome: string]: number } = {}
|
||||||
|
const totalSpent: { [outcome: string]: number } = {}
|
||||||
|
|
||||||
|
const sortedBets = sortBy(yourBets, 'createdTime')
|
||||||
|
for (const bet of sortedBets) {
|
||||||
|
const { outcome, shares, amount } = bet
|
||||||
|
if (amount > 0) {
|
||||||
|
totalShares[outcome] = (totalShares[outcome] ?? 0) + shares
|
||||||
|
totalSpent[outcome] = (totalSpent[outcome] ?? 0) + amount
|
||||||
|
} else if (amount < 0) {
|
||||||
|
const averagePrice = totalSpent[outcome] / totalShares[outcome]
|
||||||
|
totalShares[outcome] = totalShares[outcome] + shares
|
||||||
|
totalSpent[outcome] = totalSpent[outcome] + averagePrice * shares
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum(Object.values(totalSpent))
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDpmInvested(yourBets: Bet[]) {
|
||||||
|
const sortedBets = sortBy(yourBets, 'createdTime')
|
||||||
|
|
||||||
|
return sumBy(sortedBets, (bet) => {
|
||||||
|
const { amount, sale } = bet
|
||||||
|
|
||||||
|
if (sale) {
|
||||||
|
const originalBet = sortedBets.find((b) => b.id === sale.betId)
|
||||||
|
if (originalBet) return -originalBet.amount
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return amount
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export function getContractBetMetrics(contract: Contract, yourBets: Bet[]) {
|
export function getContractBetMetrics(contract: Contract, yourBets: Bet[]) {
|
||||||
const { resolution } = contract
|
const { resolution } = contract
|
||||||
|
const isCpmm = contract.mechanism === 'cpmm-1'
|
||||||
|
|
||||||
let currentInvested = 0
|
|
||||||
let totalInvested = 0
|
let totalInvested = 0
|
||||||
let payout = 0
|
let payout = 0
|
||||||
let loan = 0
|
let loan = 0
|
||||||
|
@ -162,7 +198,6 @@ export function getContractBetMetrics(contract: Contract, yourBets: Bet[]) {
|
||||||
saleValue -= amount
|
saleValue -= amount
|
||||||
}
|
}
|
||||||
|
|
||||||
currentInvested += amount
|
|
||||||
loan += loanAmount ?? 0
|
loan += loanAmount ?? 0
|
||||||
payout += resolution
|
payout += resolution
|
||||||
? calculatePayout(contract, bet, resolution)
|
? calculatePayout(contract, bet, resolution)
|
||||||
|
@ -174,12 +209,13 @@ export function getContractBetMetrics(contract: Contract, yourBets: Bet[]) {
|
||||||
const profit = payout + saleValue + redeemed - totalInvested
|
const profit = payout + saleValue + redeemed - totalInvested
|
||||||
const profitPercent = (profit / totalInvested) * 100
|
const profitPercent = (profit / totalInvested) * 100
|
||||||
|
|
||||||
|
const invested = isCpmm ? getCpmmInvested(yourBets) : getDpmInvested(yourBets)
|
||||||
const hasShares = Object.values(totalShares).some(
|
const hasShares = Object.values(totalShares).some(
|
||||||
(shares) => !floatingEqual(shares, 0)
|
(shares) => !floatingEqual(shares, 0)
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
invested: Math.max(0, currentInvested),
|
invested,
|
||||||
payout,
|
payout,
|
||||||
netPayout,
|
netPayout,
|
||||||
profit,
|
profit,
|
||||||
|
|
65
common/challenge.ts
Normal file
65
common/challenge.ts
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
import { IS_PRIVATE_MANIFOLD } from './envs/constants'
|
||||||
|
|
||||||
|
export type Challenge = {
|
||||||
|
// The link to send: https://manifold.markets/challenges/username/market-slug/{slug}
|
||||||
|
// Also functions as the unique id for the link.
|
||||||
|
slug: string
|
||||||
|
|
||||||
|
// The user that created the challenge.
|
||||||
|
creatorId: string
|
||||||
|
creatorUsername: string
|
||||||
|
creatorName: string
|
||||||
|
creatorAvatarUrl?: string
|
||||||
|
|
||||||
|
// Displayed to people claiming the challenge
|
||||||
|
message: string
|
||||||
|
|
||||||
|
// How much to put up
|
||||||
|
creatorAmount: number
|
||||||
|
|
||||||
|
// YES or NO for now
|
||||||
|
creatorOutcome: string
|
||||||
|
|
||||||
|
// Different than the creator
|
||||||
|
acceptorOutcome: string
|
||||||
|
acceptorAmount: number
|
||||||
|
|
||||||
|
// The probability the challenger thinks
|
||||||
|
creatorOutcomeProb: number
|
||||||
|
|
||||||
|
contractId: string
|
||||||
|
contractSlug: string
|
||||||
|
contractQuestion: string
|
||||||
|
contractCreatorUsername: string
|
||||||
|
|
||||||
|
createdTime: number
|
||||||
|
// If null, the link is valid forever
|
||||||
|
expiresTime: number | null
|
||||||
|
|
||||||
|
// How many times the challenge can be used
|
||||||
|
maxUses: number
|
||||||
|
|
||||||
|
// Used for simpler caching
|
||||||
|
acceptedByUserIds: string[]
|
||||||
|
// Successful redemptions of the link
|
||||||
|
acceptances: Acceptance[]
|
||||||
|
|
||||||
|
// TODO: will have to fill this on resolve contract
|
||||||
|
isResolved: boolean
|
||||||
|
resolutionOutcome?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Acceptance = {
|
||||||
|
// User that accepted the challenge
|
||||||
|
userId: string
|
||||||
|
userUsername: string
|
||||||
|
userName: string
|
||||||
|
userAvatarUrl: string
|
||||||
|
|
||||||
|
// The ID of the successful bet that tracks the money moved
|
||||||
|
betId: string
|
||||||
|
|
||||||
|
createdTime: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export const CHALLENGES_ENABLED = !IS_PRIVATE_MANIFOLD
|
|
@ -1,3 +1,5 @@
|
||||||
|
import type { JSONContent } from '@tiptap/core'
|
||||||
|
|
||||||
// Currently, comments are created after the bet, not atomically with the bet.
|
// Currently, comments are created after the bet, not atomically with the bet.
|
||||||
// They're uniquely identified by the pair contractId/betId.
|
// They're uniquely identified by the pair contractId/betId.
|
||||||
export type Comment = {
|
export type Comment = {
|
||||||
|
@ -9,11 +11,15 @@ export type Comment = {
|
||||||
replyToCommentId?: string
|
replyToCommentId?: string
|
||||||
userId: string
|
userId: string
|
||||||
|
|
||||||
text: string
|
/** @deprecated - content now stored as JSON in content*/
|
||||||
|
text?: string
|
||||||
|
content: JSONContent
|
||||||
createdTime: number
|
createdTime: number
|
||||||
|
|
||||||
// Denormalized, for rendering comments
|
// Denormalized, for rendering comments
|
||||||
userName: string
|
userName: string
|
||||||
userUsername: string
|
userUsername: string
|
||||||
userAvatarUrl?: string
|
userAvatarUrl?: string
|
||||||
|
contractSlug?: string
|
||||||
|
contractQuestion?: string
|
||||||
}
|
}
|
||||||
|
|
|
@ -139,7 +139,7 @@ export const OUTCOME_TYPES = [
|
||||||
] as const
|
] as const
|
||||||
|
|
||||||
export const MAX_QUESTION_LENGTH = 480
|
export const MAX_QUESTION_LENGTH = 480
|
||||||
export const MAX_DESCRIPTION_LENGTH = 10000
|
export const MAX_DESCRIPTION_LENGTH = 16000
|
||||||
export const MAX_TAG_LENGTH = 60
|
export const MAX_TAG_LENGTH = 60
|
||||||
|
|
||||||
export const CPMM_MIN_POOL_QTY = 0.01
|
export const CPMM_MIN_POOL_QTY = 0.01
|
||||||
|
|
|
@ -25,6 +25,10 @@ export function isAdmin(email: string) {
|
||||||
return ENV_CONFIG.adminEmails.includes(email)
|
return ENV_CONFIG.adminEmails.includes(email)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isManifoldId(userId: string) {
|
||||||
|
return userId === 'IPTOzEqrpkWmEzh6hwvAyY9PqFb2'
|
||||||
|
}
|
||||||
|
|
||||||
export const DOMAIN = ENV_CONFIG.domain
|
export const DOMAIN = ENV_CONFIG.domain
|
||||||
export const FIREBASE_CONFIG = ENV_CONFIG.firebaseConfig
|
export const FIREBASE_CONFIG = ENV_CONFIG.firebaseConfig
|
||||||
export const PROJECT_ID = ENV_CONFIG.firebaseConfig.projectId
|
export const PROJECT_ID = ENV_CONFIG.firebaseConfig.projectId
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { EnvConfig, PROD_CONFIG } from './prod'
|
||||||
|
|
||||||
export const DEV_CONFIG: EnvConfig = {
|
export const DEV_CONFIG: EnvConfig = {
|
||||||
...PROD_CONFIG,
|
...PROD_CONFIG,
|
||||||
|
domain: 'dev.manifold.markets',
|
||||||
firebaseConfig: {
|
firebaseConfig: {
|
||||||
apiKey: 'AIzaSyBoq3rzUa8Ekyo3ZaTnlycQYPRCA26VpOw',
|
apiKey: 'AIzaSyBoq3rzUa8Ekyo3ZaTnlycQYPRCA26VpOw',
|
||||||
authDomain: 'dev-mantic-markets.firebaseapp.com',
|
authDomain: 'dev-mantic-markets.firebaseapp.com',
|
||||||
|
|
|
@ -37,6 +37,7 @@ export type notification_source_types =
|
||||||
| 'group'
|
| 'group'
|
||||||
| 'user'
|
| 'user'
|
||||||
| 'bonus'
|
| 'bonus'
|
||||||
|
| 'challenge'
|
||||||
|
|
||||||
export type notification_source_update_types =
|
export type notification_source_update_types =
|
||||||
| 'created'
|
| 'created'
|
||||||
|
@ -64,3 +65,4 @@ export type notification_reason_types =
|
||||||
| 'tip_received'
|
| 'tip_received'
|
||||||
| 'bet_fill'
|
| 'bet_fill'
|
||||||
| 'user_joined_from_your_group_invite'
|
| 'user_joined_from_your_group_invite'
|
||||||
|
| 'challenge_accepted'
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
},
|
},
|
||||||
"sideEffects": false,
|
"sideEffects": false,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@tiptap/core": "2.0.0-beta.181",
|
||||||
"@tiptap/extension-image": "2.0.0-beta.30",
|
"@tiptap/extension-image": "2.0.0-beta.30",
|
||||||
"@tiptap/extension-link": "2.0.0-beta.43",
|
"@tiptap/extension-link": "2.0.0-beta.43",
|
||||||
"@tiptap/extension-mention": "2.0.0-beta.102",
|
"@tiptap/extension-mention": "2.0.0-beta.102",
|
||||||
|
|
|
@ -1,3 +1,40 @@
|
||||||
|
import { isEqual } from 'lodash'
|
||||||
|
|
||||||
export function filterDefined<T>(array: (T | null | undefined)[]) {
|
export function filterDefined<T>(array: (T | null | undefined)[]) {
|
||||||
return array.filter((item) => item !== null && item !== undefined) as T[]
|
return array.filter((item) => item !== null && item !== undefined) as T[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function buildArray<T>(
|
||||||
|
...params: (T | T[] | false | undefined | null)[]
|
||||||
|
) {
|
||||||
|
const array: T[] = []
|
||||||
|
|
||||||
|
for (const el of params) {
|
||||||
|
if (Array.isArray(el)) {
|
||||||
|
array.push(...el)
|
||||||
|
} else if (el) {
|
||||||
|
array.push(el)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array
|
||||||
|
}
|
||||||
|
|
||||||
|
export function groupConsecutive<T, U>(xs: T[], key: (x: T) => U) {
|
||||||
|
if (!xs.length) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
const result = []
|
||||||
|
let curr = { key: key(xs[0]), items: [xs[0]] }
|
||||||
|
for (const x of xs.slice(1)) {
|
||||||
|
const k = key(x)
|
||||||
|
if (!isEqual(key, curr.key)) {
|
||||||
|
result.push(curr)
|
||||||
|
curr = { key: k, items: [x] }
|
||||||
|
} else {
|
||||||
|
curr.items.push(x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.push(curr)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
|
@ -22,6 +22,8 @@ import { Image } from '@tiptap/extension-image'
|
||||||
import { Link } from '@tiptap/extension-link'
|
import { Link } from '@tiptap/extension-link'
|
||||||
import { Mention } from '@tiptap/extension-mention'
|
import { Mention } from '@tiptap/extension-mention'
|
||||||
import Iframe from './tiptap-iframe'
|
import Iframe from './tiptap-iframe'
|
||||||
|
import TiptapTweet from './tiptap-tweet-type'
|
||||||
|
import { uniq } from 'lodash'
|
||||||
|
|
||||||
export function parseTags(text: string) {
|
export function parseTags(text: string) {
|
||||||
const regex = /(?:^|\s)(?:[#][a-z0-9_]+)/gi
|
const regex = /(?:^|\s)(?:[#][a-z0-9_]+)/gi
|
||||||
|
@ -61,6 +63,15 @@ const checkAgainstQuery = (query: string, corpus: string) =>
|
||||||
export const searchInAny = (query: string, ...fields: string[]) =>
|
export const searchInAny = (query: string, ...fields: string[]) =>
|
||||||
fields.some((field) => checkAgainstQuery(query, field))
|
fields.some((field) => checkAgainstQuery(query, field))
|
||||||
|
|
||||||
|
/** @return user ids of all \@mentions */
|
||||||
|
export function parseMentions(data: JSONContent): string[] {
|
||||||
|
const mentions = data.content?.flatMap(parseMentions) ?? [] //dfs
|
||||||
|
if (data.type === 'mention' && data.attrs) {
|
||||||
|
mentions.push(data.attrs.id as string)
|
||||||
|
}
|
||||||
|
return uniq(mentions)
|
||||||
|
}
|
||||||
|
|
||||||
// can't just do [StarterKit, Image...] because it doesn't work with cjs imports
|
// can't just do [StarterKit, Image...] because it doesn't work with cjs imports
|
||||||
export const exhibitExts = [
|
export const exhibitExts = [
|
||||||
Blockquote,
|
Blockquote,
|
||||||
|
@ -84,6 +95,7 @@ export const exhibitExts = [
|
||||||
Link,
|
Link,
|
||||||
Mention,
|
Mention,
|
||||||
Iframe,
|
Iframe,
|
||||||
|
TiptapTweet,
|
||||||
]
|
]
|
||||||
|
|
||||||
export function richTextToString(text?: JSONContent) {
|
export function richTextToString(text?: JSONContent) {
|
||||||
|
|
37
common/util/tiptap-tweet-type.ts
Normal file
37
common/util/tiptap-tweet-type.ts
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
import { Node, mergeAttributes } from '@tiptap/core'
|
||||||
|
|
||||||
|
export interface TweetOptions {
|
||||||
|
tweetId: string
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is a version of the Tiptap Node config without addNodeView,
|
||||||
|
// since that would require bundling in tsx
|
||||||
|
export const TiptapTweetNode = {
|
||||||
|
name: 'tiptapTweet',
|
||||||
|
group: 'block',
|
||||||
|
atom: true,
|
||||||
|
|
||||||
|
addAttributes() {
|
||||||
|
return {
|
||||||
|
tweetId: {
|
||||||
|
default: null,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
parseHTML() {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
tag: 'tiptap-tweet',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
renderHTML(props: { HTMLAttributes: Record<string, any> }) {
|
||||||
|
return ['tiptap-tweet', mergeAttributes(props.HTMLAttributes)]
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore
|
||||||
|
export default Node.create<TweetOptions>(TiptapTweetNode)
|
|
@ -135,7 +135,8 @@ Requires no authorization.
|
||||||
// Market attributes. All times are in milliseconds since epoch
|
// Market attributes. All times are in milliseconds since epoch
|
||||||
closeTime?: number // Min of creator's chosen date, and resolutionTime
|
closeTime?: number // Min of creator's chosen date, and resolutionTime
|
||||||
question: string
|
question: string
|
||||||
description: string
|
description: JSONContent // Rich text content. See https://tiptap.dev/guide/output#option-1-json
|
||||||
|
textDescription: string // string description without formatting, images, or embeds
|
||||||
|
|
||||||
// A list of tags on each market. Any user can add tags to any market.
|
// A list of tags on each market. Any user can add tags to any market.
|
||||||
// This list also includes the predefined categories shown as filters on the home page.
|
// This list also includes the predefined categories shown as filters on the home page.
|
||||||
|
@ -162,6 +163,8 @@ Requires no authorization.
|
||||||
resolutionTime?: number
|
resolutionTime?: number
|
||||||
resolution?: string
|
resolution?: string
|
||||||
resolutionProbability?: number // Used for BINARY markets resolved to MKT
|
resolutionProbability?: number // Used for BINARY markets resolved to MKT
|
||||||
|
|
||||||
|
lastUpdatedTime?: number
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -528,6 +531,10 @@ $ curl https://manifold.markets/api/v0/bet -X POST -H 'Content-Type: application
|
||||||
"contractId":"{...}"}'
|
"contractId":"{...}"}'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `POST /v0/bet/cancel/[id]`
|
||||||
|
|
||||||
|
Cancel the limit order of a bet with the specified id. If the bet was unfilled, it will be cancelled so that no other bets will match with it. This is action irreversable.
|
||||||
|
|
||||||
### `POST /v0/market`
|
### `POST /v0/market`
|
||||||
|
|
||||||
Creates a new market on behalf of the authorized user.
|
Creates a new market on behalf of the authorized user.
|
||||||
|
@ -537,6 +544,7 @@ Parameters:
|
||||||
- `outcomeType`: Required. One of `BINARY`, `FREE_RESPONSE`, or `NUMERIC`.
|
- `outcomeType`: Required. One of `BINARY`, `FREE_RESPONSE`, or `NUMERIC`.
|
||||||
- `question`: Required. The headline question for the market.
|
- `question`: Required. The headline question for the market.
|
||||||
- `description`: Required. A long description describing the rules for the market.
|
- `description`: Required. A long description describing the rules for the market.
|
||||||
|
- Note: string descriptions do **not** turn into links, mentions, formatted text. Instead, rich text descriptions must be in [TipTap json](https://tiptap.dev/guide/output#option-1-json).
|
||||||
- `closeTime`: Required. The time at which the market will close, represented as milliseconds since the epoch.
|
- `closeTime`: Required. The time at which the market will close, represented as milliseconds since the epoch.
|
||||||
- `tags`: Optional. An array of string tags for the market.
|
- `tags`: Optional. An array of string tags for the market.
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,8 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@docusaurus/module-type-aliases": "2.0.0-beta.17",
|
"@docusaurus/module-type-aliases": "2.0.0-beta.17",
|
||||||
"@tsconfig/docusaurus": "^1.0.4"
|
"@tsconfig/docusaurus": "^1.0.4",
|
||||||
|
"@types/react": "^17.0.2"
|
||||||
},
|
},
|
||||||
"browserslist": {
|
"browserslist": {
|
||||||
"production": [
|
"production": [
|
||||||
|
|
|
@ -496,6 +496,28 @@
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"collectionGroup": "comments",
|
||||||
|
"fieldPath": "contractId",
|
||||||
|
"indexes": [
|
||||||
|
{
|
||||||
|
"order": "ASCENDING",
|
||||||
|
"queryScope": "COLLECTION"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"order": "DESCENDING",
|
||||||
|
"queryScope": "COLLECTION"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"arrayConfig": "CONTAINS",
|
||||||
|
"queryScope": "COLLECTION"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"order": "ASCENDING",
|
||||||
|
"queryScope": "COLLECTION_GROUP"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"collectionGroup": "comments",
|
"collectionGroup": "comments",
|
||||||
"fieldPath": "createdTime",
|
"fieldPath": "createdTime",
|
||||||
|
|
|
@ -20,17 +20,17 @@ service cloud.firestore {
|
||||||
|
|
||||||
match /users/{userId} {
|
match /users/{userId} {
|
||||||
allow read;
|
allow read;
|
||||||
allow update: if resource.data.id == request.auth.uid
|
allow update: if userId == request.auth.uid
|
||||||
&& request.resource.data.diff(resource.data).affectedKeys()
|
&& request.resource.data.diff(resource.data).affectedKeys()
|
||||||
.hasOnly(['bio', 'bannerUrl', 'website', 'twitterHandle', 'discordHandle', 'followedCategories', 'lastPingTime','shouldShowWelcome']);
|
.hasOnly(['bio', 'bannerUrl', 'website', 'twitterHandle', 'discordHandle', 'followedCategories', 'lastPingTime','shouldShowWelcome']);
|
||||||
// User referral rules
|
// User referral rules
|
||||||
allow update: if resource.data.id == request.auth.uid
|
allow update: if userId == request.auth.uid
|
||||||
&& request.resource.data.diff(resource.data).affectedKeys()
|
&& request.resource.data.diff(resource.data).affectedKeys()
|
||||||
.hasOnly(['referredByUserId', 'referredByContractId', 'referredByGroupId'])
|
.hasOnly(['referredByUserId', 'referredByContractId', 'referredByGroupId'])
|
||||||
// only one referral allowed per user
|
// only one referral allowed per user
|
||||||
&& !("referredByUserId" in resource.data)
|
&& !("referredByUserId" in resource.data)
|
||||||
// user can't refer themselves
|
// user can't refer themselves
|
||||||
&& !(resource.data.id == request.resource.data.referredByUserId);
|
&& !(userId == request.resource.data.referredByUserId);
|
||||||
// quid pro quos enabled (only once though so nbd) - bc I can't make this work:
|
// quid pro quos enabled (only once though so nbd) - bc I can't make this work:
|
||||||
// && (get(/databases/$(database)/documents/users/$(request.resource.data.referredByUserId)).referredByUserId == resource.data.id);
|
// && (get(/databases/$(database)/documents/users/$(request.resource.data.referredByUserId)).referredByUserId == resource.data.id);
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,17 @@ service cloud.firestore {
|
||||||
allow read;
|
allow read;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
match /{somePath=**}/challenges/{challengeId}{
|
||||||
|
allow read;
|
||||||
|
}
|
||||||
|
|
||||||
|
match /contracts/{contractId}/challenges/{challengeId}{
|
||||||
|
allow read;
|
||||||
|
allow create: if request.auth.uid == request.resource.data.creatorId;
|
||||||
|
// allow update if there have been no claims yet and if the challenge is still open
|
||||||
|
allow update: if request.auth.uid == resource.data.creatorId;
|
||||||
|
}
|
||||||
|
|
||||||
match /users/{userId}/follows/{followUserId} {
|
match /users/{userId}/follows/{followUserId} {
|
||||||
allow read;
|
allow read;
|
||||||
allow write: if request.auth.uid == userId;
|
allow write: if request.auth.uid == userId;
|
||||||
|
@ -49,8 +60,8 @@ service cloud.firestore {
|
||||||
}
|
}
|
||||||
|
|
||||||
match /private-users/{userId} {
|
match /private-users/{userId} {
|
||||||
allow read: if resource.data.id == request.auth.uid || isAdmin();
|
allow read: if userId == request.auth.uid || isAdmin();
|
||||||
allow update: if (resource.data.id == request.auth.uid || isAdmin())
|
allow update: if (userId == request.auth.uid || isAdmin())
|
||||||
&& request.resource.data.diff(resource.data).affectedKeys()
|
&& request.resource.data.diff(resource.data).affectedKeys()
|
||||||
.hasOnly(['apiKey', 'unsubscribedFromResolutionEmails', 'unsubscribedFromCommentEmails', 'unsubscribedFromAnswerEmails', 'notificationPreferences' ]);
|
.hasOnly(['apiKey', 'unsubscribedFromResolutionEmails', 'unsubscribedFromCommentEmails', 'unsubscribedFromAnswerEmails', 'notificationPreferences' ]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,8 +31,8 @@
|
||||||
"@tiptap/extension-link": "2.0.0-beta.43",
|
"@tiptap/extension-link": "2.0.0-beta.43",
|
||||||
"@tiptap/extension-mention": "2.0.0-beta.102",
|
"@tiptap/extension-mention": "2.0.0-beta.102",
|
||||||
"@tiptap/starter-kit": "2.0.0-beta.190",
|
"@tiptap/starter-kit": "2.0.0-beta.190",
|
||||||
"dayjs": "1.11.4",
|
|
||||||
"cors": "2.8.5",
|
"cors": "2.8.5",
|
||||||
|
"dayjs": "1.11.4",
|
||||||
"express": "4.18.1",
|
"express": "4.18.1",
|
||||||
"firebase-admin": "10.0.0",
|
"firebase-admin": "10.0.0",
|
||||||
"firebase-functions": "3.21.2",
|
"firebase-functions": "3.21.2",
|
||||||
|
|
167
functions/src/accept-challenge.ts
Normal file
167
functions/src/accept-challenge.ts
Normal file
|
@ -0,0 +1,167 @@
|
||||||
|
import { z } from 'zod'
|
||||||
|
import { APIError, newEndpoint, validate } from './api'
|
||||||
|
import { log } from './utils'
|
||||||
|
import { Contract, CPMMBinaryContract } from '../../common/contract'
|
||||||
|
import { User } from '../../common/user'
|
||||||
|
import * as admin from 'firebase-admin'
|
||||||
|
import { FieldValue } from 'firebase-admin/firestore'
|
||||||
|
import { removeUndefinedProps } from '../../common/util/object'
|
||||||
|
import { Acceptance, Challenge } from '../../common/challenge'
|
||||||
|
import { CandidateBet } from '../../common/new-bet'
|
||||||
|
import { createChallengeAcceptedNotification } from './create-notification'
|
||||||
|
import { noFees } from '../../common/fees'
|
||||||
|
import { formatMoney, formatPercent } from '../../common/util/format'
|
||||||
|
|
||||||
|
const bodySchema = z.object({
|
||||||
|
contractId: z.string(),
|
||||||
|
challengeSlug: z.string(),
|
||||||
|
outcomeType: z.literal('BINARY'),
|
||||||
|
closeTime: z.number().gte(Date.now()),
|
||||||
|
})
|
||||||
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
|
export const acceptchallenge = newEndpoint({}, async (req, auth) => {
|
||||||
|
const { challengeSlug, contractId } = validate(bodySchema, req.body)
|
||||||
|
|
||||||
|
const result = await firestore.runTransaction(async (trans) => {
|
||||||
|
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
||||||
|
const userDoc = firestore.doc(`users/${auth.uid}`)
|
||||||
|
const challengeDoc = firestore.doc(
|
||||||
|
`contracts/${contractId}/challenges/${challengeSlug}`
|
||||||
|
)
|
||||||
|
const [contractSnap, userSnap, challengeSnap] = await trans.getAll(
|
||||||
|
contractDoc,
|
||||||
|
userDoc,
|
||||||
|
challengeDoc
|
||||||
|
)
|
||||||
|
if (!contractSnap.exists) throw new APIError(400, 'Contract not found.')
|
||||||
|
if (!userSnap.exists) throw new APIError(400, 'User not found.')
|
||||||
|
if (!challengeSnap.exists) throw new APIError(400, 'Challenge not found.')
|
||||||
|
|
||||||
|
const anyContract = contractSnap.data() as Contract
|
||||||
|
const user = userSnap.data() as User
|
||||||
|
const challenge = challengeSnap.data() as Challenge
|
||||||
|
|
||||||
|
if (challenge.acceptances.length > 0)
|
||||||
|
throw new APIError(400, 'Challenge already accepted.')
|
||||||
|
|
||||||
|
const creatorDoc = firestore.doc(`users/${challenge.creatorId}`)
|
||||||
|
const creatorSnap = await trans.get(creatorDoc)
|
||||||
|
if (!creatorSnap.exists) throw new APIError(400, 'Creator not found.')
|
||||||
|
const creator = creatorSnap.data() as User
|
||||||
|
|
||||||
|
const {
|
||||||
|
creatorAmount,
|
||||||
|
acceptorOutcome,
|
||||||
|
creatorOutcome,
|
||||||
|
creatorOutcomeProb,
|
||||||
|
acceptorAmount,
|
||||||
|
} = challenge
|
||||||
|
|
||||||
|
if (user.balance < acceptorAmount)
|
||||||
|
throw new APIError(400, 'Insufficient balance.')
|
||||||
|
|
||||||
|
if (creator.balance < creatorAmount)
|
||||||
|
throw new APIError(400, 'Creator has insufficient balance.')
|
||||||
|
|
||||||
|
const contract = anyContract as CPMMBinaryContract
|
||||||
|
const shares = (1 / creatorOutcomeProb) * creatorAmount
|
||||||
|
const createdTime = Date.now()
|
||||||
|
const probOfYes =
|
||||||
|
creatorOutcome === 'YES' ? creatorOutcomeProb : 1 - creatorOutcomeProb
|
||||||
|
|
||||||
|
log(
|
||||||
|
'Creating challenge bet for',
|
||||||
|
user.username,
|
||||||
|
shares,
|
||||||
|
acceptorOutcome,
|
||||||
|
'shares',
|
||||||
|
'at',
|
||||||
|
formatPercent(creatorOutcomeProb),
|
||||||
|
'for',
|
||||||
|
formatMoney(acceptorAmount)
|
||||||
|
)
|
||||||
|
|
||||||
|
const yourNewBet: CandidateBet = removeUndefinedProps({
|
||||||
|
orderAmount: acceptorAmount,
|
||||||
|
amount: acceptorAmount,
|
||||||
|
shares,
|
||||||
|
isCancelled: false,
|
||||||
|
contractId: contract.id,
|
||||||
|
outcome: acceptorOutcome,
|
||||||
|
probBefore: probOfYes,
|
||||||
|
probAfter: probOfYes,
|
||||||
|
loanAmount: 0,
|
||||||
|
createdTime,
|
||||||
|
fees: noFees,
|
||||||
|
challengeSlug: challenge.slug,
|
||||||
|
})
|
||||||
|
|
||||||
|
const yourNewBetDoc = contractDoc.collection('bets').doc()
|
||||||
|
trans.create(yourNewBetDoc, {
|
||||||
|
id: yourNewBetDoc.id,
|
||||||
|
userId: user.id,
|
||||||
|
...yourNewBet,
|
||||||
|
})
|
||||||
|
|
||||||
|
trans.update(userDoc, { balance: FieldValue.increment(-yourNewBet.amount) })
|
||||||
|
|
||||||
|
const creatorNewBet: CandidateBet = removeUndefinedProps({
|
||||||
|
orderAmount: creatorAmount,
|
||||||
|
amount: creatorAmount,
|
||||||
|
shares,
|
||||||
|
isCancelled: false,
|
||||||
|
contractId: contract.id,
|
||||||
|
outcome: creatorOutcome,
|
||||||
|
probBefore: probOfYes,
|
||||||
|
probAfter: probOfYes,
|
||||||
|
loanAmount: 0,
|
||||||
|
createdTime,
|
||||||
|
fees: noFees,
|
||||||
|
challengeSlug: challenge.slug,
|
||||||
|
})
|
||||||
|
const creatorBetDoc = contractDoc.collection('bets').doc()
|
||||||
|
trans.create(creatorBetDoc, {
|
||||||
|
id: creatorBetDoc.id,
|
||||||
|
userId: creator.id,
|
||||||
|
...creatorNewBet,
|
||||||
|
})
|
||||||
|
|
||||||
|
trans.update(creatorDoc, {
|
||||||
|
balance: FieldValue.increment(-creatorNewBet.amount),
|
||||||
|
})
|
||||||
|
|
||||||
|
const volume = contract.volume + yourNewBet.amount + creatorNewBet.amount
|
||||||
|
trans.update(contractDoc, { volume })
|
||||||
|
|
||||||
|
trans.update(
|
||||||
|
challengeDoc,
|
||||||
|
removeUndefinedProps({
|
||||||
|
acceptedByUserIds: [user.id],
|
||||||
|
acceptances: [
|
||||||
|
{
|
||||||
|
userId: user.id,
|
||||||
|
betId: yourNewBetDoc.id,
|
||||||
|
createdTime,
|
||||||
|
amount: acceptorAmount,
|
||||||
|
userUsername: user.username,
|
||||||
|
userName: user.name,
|
||||||
|
userAvatarUrl: user.avatarUrl,
|
||||||
|
} as Acceptance,
|
||||||
|
],
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
await createChallengeAcceptedNotification(
|
||||||
|
user,
|
||||||
|
creator,
|
||||||
|
challenge,
|
||||||
|
acceptorAmount,
|
||||||
|
contract
|
||||||
|
)
|
||||||
|
log('Done, sent notification.')
|
||||||
|
return yourNewBetDoc
|
||||||
|
})
|
||||||
|
|
||||||
|
return { betId: result.id }
|
||||||
|
})
|
|
@ -3,7 +3,7 @@ import * as Amplitude from '@amplitude/node'
|
||||||
import { DEV_CONFIG } from '../../common/envs/dev'
|
import { DEV_CONFIG } from '../../common/envs/dev'
|
||||||
import { PROD_CONFIG } from '../../common/envs/prod'
|
import { PROD_CONFIG } from '../../common/envs/prod'
|
||||||
|
|
||||||
import { isProd } from './utils'
|
import { isProd, tryOrLogError } from './utils'
|
||||||
|
|
||||||
const key = isProd() ? PROD_CONFIG.amplitudeApiKey : DEV_CONFIG.amplitudeApiKey
|
const key = isProd() ? PROD_CONFIG.amplitudeApiKey : DEV_CONFIG.amplitudeApiKey
|
||||||
|
|
||||||
|
@ -15,10 +15,12 @@ export const track = async (
|
||||||
eventProperties?: any,
|
eventProperties?: any,
|
||||||
amplitudeProperties?: Partial<Amplitude.Event>
|
amplitudeProperties?: Partial<Amplitude.Event>
|
||||||
) => {
|
) => {
|
||||||
await amp.logEvent({
|
return await tryOrLogError(
|
||||||
event_type: eventName,
|
amp.logEvent({
|
||||||
user_id: userId,
|
event_type: eventName,
|
||||||
event_properties: eventProperties,
|
user_id: userId,
|
||||||
...amplitudeProperties,
|
event_properties: eventProperties,
|
||||||
})
|
...amplitudeProperties,
|
||||||
|
})
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,6 +78,19 @@ export const lookupUser = async (creds: Credentials): Promise<AuthedUser> => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const writeResponseError = (e: unknown, res: Response) => {
|
||||||
|
if (e instanceof APIError) {
|
||||||
|
const output: { [k: string]: unknown } = { message: e.message }
|
||||||
|
if (e.details != null) {
|
||||||
|
output.details = e.details
|
||||||
|
}
|
||||||
|
res.status(e.code).json(output)
|
||||||
|
} else {
|
||||||
|
error(e)
|
||||||
|
res.status(500).json({ message: 'An unknown error occurred.' })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const zTimestamp = () => {
|
export const zTimestamp = () => {
|
||||||
return z.preprocess((arg) => {
|
return z.preprocess((arg) => {
|
||||||
return typeof arg == 'number' ? new Date(arg) : undefined
|
return typeof arg == 'number' ? new Date(arg) : undefined
|
||||||
|
@ -131,16 +144,7 @@ export const newEndpoint = (endpointOpts: EndpointOptions, fn: Handler) => {
|
||||||
const authedUser = await lookupUser(await parseCredentials(req))
|
const authedUser = await lookupUser(await parseCredentials(req))
|
||||||
res.status(200).json(await fn(req, authedUser))
|
res.status(200).json(await fn(req, authedUser))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof APIError) {
|
writeResponseError(e, res)
|
||||||
const output: { [k: string]: unknown } = { message: e.message }
|
|
||||||
if (e.details != null) {
|
|
||||||
output.details = e.details
|
|
||||||
}
|
|
||||||
res.status(e.code).json(output)
|
|
||||||
} else {
|
|
||||||
error(e)
|
|
||||||
res.status(500).json({ message: 'An unknown error occurred.' })
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
} as EndpointDefinition
|
} as EndpointDefinition
|
||||||
|
|
|
@ -59,7 +59,7 @@ const descScehma: z.ZodType<JSONContent> = z.lazy(() =>
|
||||||
|
|
||||||
const bodySchema = z.object({
|
const bodySchema = z.object({
|
||||||
question: z.string().min(1).max(MAX_QUESTION_LENGTH),
|
question: z.string().min(1).max(MAX_QUESTION_LENGTH),
|
||||||
description: descScehma.optional(),
|
description: descScehma.or(z.string()).optional(),
|
||||||
tags: z.array(z.string().min(1).max(MAX_TAG_LENGTH)).optional(),
|
tags: z.array(z.string().min(1).max(MAX_TAG_LENGTH)).optional(),
|
||||||
closeTime: zTimestamp().refine(
|
closeTime: zTimestamp().refine(
|
||||||
(date) => date.getTime() > new Date().getTime(),
|
(date) => date.getTime() > new Date().getTime(),
|
||||||
|
@ -133,41 +133,7 @@ export const createmarket = newEndpoint({}, async (req, auth) => {
|
||||||
if (ante > user.balance)
|
if (ante > user.balance)
|
||||||
throw new APIError(400, `Balance must be at least ${ante}.`)
|
throw new APIError(400, `Balance must be at least ${ante}.`)
|
||||||
|
|
||||||
const slug = await getSlug(question)
|
let group: Group | null = null
|
||||||
const contractRef = firestore.collection('contracts').doc()
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
'creating contract for',
|
|
||||||
user.username,
|
|
||||||
'on',
|
|
||||||
question,
|
|
||||||
'ante:',
|
|
||||||
ante || 0
|
|
||||||
)
|
|
||||||
|
|
||||||
const contract = getNewContract(
|
|
||||||
contractRef.id,
|
|
||||||
slug,
|
|
||||||
user,
|
|
||||||
question,
|
|
||||||
outcomeType,
|
|
||||||
description ?? {},
|
|
||||||
initialProb ?? 0,
|
|
||||||
ante,
|
|
||||||
closeTime.getTime(),
|
|
||||||
tags ?? [],
|
|
||||||
NUMERIC_BUCKET_COUNT,
|
|
||||||
min ?? 0,
|
|
||||||
max ?? 0,
|
|
||||||
isLogScale ?? false,
|
|
||||||
answers ?? []
|
|
||||||
)
|
|
||||||
|
|
||||||
if (ante) await chargeUser(user.id, ante, true)
|
|
||||||
|
|
||||||
await contractRef.create(contract)
|
|
||||||
|
|
||||||
let group = null
|
|
||||||
if (groupId) {
|
if (groupId) {
|
||||||
const groupDocRef = firestore.collection('groups').doc(groupId)
|
const groupDocRef = firestore.collection('groups').doc(groupId)
|
||||||
const groupDoc = await groupDocRef.get()
|
const groupDoc = await groupDocRef.get()
|
||||||
|
@ -186,9 +152,60 @@ export const createmarket = newEndpoint({}, async (req, auth) => {
|
||||||
'User must be a member/creator of the group or group must be open to add markets to it.'
|
'User must be a member/creator of the group or group must be open to add markets to it.'
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
const slug = await getSlug(question)
|
||||||
|
const contractRef = firestore.collection('contracts').doc()
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
'creating contract for',
|
||||||
|
user.username,
|
||||||
|
'on',
|
||||||
|
question,
|
||||||
|
'ante:',
|
||||||
|
ante || 0
|
||||||
|
)
|
||||||
|
|
||||||
|
// convert string descriptions into JSONContent
|
||||||
|
const newDescription =
|
||||||
|
typeof description === 'string'
|
||||||
|
? {
|
||||||
|
type: 'doc',
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: 'paragraph',
|
||||||
|
content: [{ type: 'text', text: description }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
: description ?? {}
|
||||||
|
|
||||||
|
const contract = getNewContract(
|
||||||
|
contractRef.id,
|
||||||
|
slug,
|
||||||
|
user,
|
||||||
|
question,
|
||||||
|
outcomeType,
|
||||||
|
newDescription,
|
||||||
|
initialProb ?? 0,
|
||||||
|
ante,
|
||||||
|
closeTime.getTime(),
|
||||||
|
tags ?? [],
|
||||||
|
NUMERIC_BUCKET_COUNT,
|
||||||
|
min ?? 0,
|
||||||
|
max ?? 0,
|
||||||
|
isLogScale ?? false,
|
||||||
|
answers ?? []
|
||||||
|
)
|
||||||
|
|
||||||
|
if (ante) await chargeUser(user.id, ante, true)
|
||||||
|
|
||||||
|
await contractRef.create(contract)
|
||||||
|
|
||||||
|
if (group != null) {
|
||||||
if (!group.contractIds.includes(contractRef.id)) {
|
if (!group.contractIds.includes(contractRef.id)) {
|
||||||
await createGroupLinks(group, [contractRef.id], auth.uid)
|
await createGroupLinks(group, [contractRef.id], auth.uid)
|
||||||
await groupDocRef.update({
|
const groupDocRef = firestore.collection('groups').doc(group.id)
|
||||||
|
groupDocRef.update({
|
||||||
contractIds: uniq([...group.contractIds, contractRef.id]),
|
contractIds: uniq([...group.contractIds, contractRef.id]),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import {
|
||||||
} from '../../common/notification'
|
} from '../../common/notification'
|
||||||
import { User } from '../../common/user'
|
import { User } from '../../common/user'
|
||||||
import { Contract } from '../../common/contract'
|
import { Contract } from '../../common/contract'
|
||||||
import { getUserByUsername, getValues } from './utils'
|
import { getValues } from './utils'
|
||||||
import { Comment } from '../../common/comment'
|
import { Comment } from '../../common/comment'
|
||||||
import { uniq } from 'lodash'
|
import { uniq } from 'lodash'
|
||||||
import { Bet, LimitBet } from '../../common/bet'
|
import { Bet, LimitBet } from '../../common/bet'
|
||||||
|
@ -16,6 +16,8 @@ import { getContractBetMetrics } from '../../common/calculate'
|
||||||
import { removeUndefinedProps } from '../../common/util/object'
|
import { removeUndefinedProps } from '../../common/util/object'
|
||||||
import { TipTxn } from '../../common/txn'
|
import { TipTxn } from '../../common/txn'
|
||||||
import { Group, GROUP_CHAT_SLUG } from '../../common/group'
|
import { Group, GROUP_CHAT_SLUG } from '../../common/group'
|
||||||
|
import { Challenge } from '../../common/challenge'
|
||||||
|
import { richTextToString } from '../../common/util/parse'
|
||||||
const firestore = admin.firestore()
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
type user_to_reason_texts = {
|
type user_to_reason_texts = {
|
||||||
|
@ -32,7 +34,7 @@ export const createNotification = async (
|
||||||
miscData?: {
|
miscData?: {
|
||||||
contract?: Contract
|
contract?: Contract
|
||||||
relatedSourceType?: notification_source_types
|
relatedSourceType?: notification_source_types
|
||||||
relatedUserId?: string
|
recipients?: string[]
|
||||||
slug?: string
|
slug?: string
|
||||||
title?: string
|
title?: string
|
||||||
}
|
}
|
||||||
|
@ -40,7 +42,7 @@ export const createNotification = async (
|
||||||
const {
|
const {
|
||||||
contract: sourceContract,
|
contract: sourceContract,
|
||||||
relatedSourceType,
|
relatedSourceType,
|
||||||
relatedUserId,
|
recipients,
|
||||||
slug,
|
slug,
|
||||||
title,
|
title,
|
||||||
} = miscData ?? {}
|
} = miscData ?? {}
|
||||||
|
@ -127,7 +129,7 @@ export const createNotification = async (
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const notifyRepliedUsers = async (
|
const notifyRepliedUser = (
|
||||||
userToReasonTexts: user_to_reason_texts,
|
userToReasonTexts: user_to_reason_texts,
|
||||||
relatedUserId: string,
|
relatedUserId: string,
|
||||||
relatedSourceType: notification_source_types
|
relatedSourceType: notification_source_types
|
||||||
|
@ -144,7 +146,7 @@ export const createNotification = async (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const notifyFollowedUser = async (
|
const notifyFollowedUser = (
|
||||||
userToReasonTexts: user_to_reason_texts,
|
userToReasonTexts: user_to_reason_texts,
|
||||||
followedUserId: string
|
followedUserId: string
|
||||||
) => {
|
) => {
|
||||||
|
@ -154,21 +156,13 @@ export const createNotification = async (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const notifyTaggedUsers = async (
|
const notifyTaggedUsers = (
|
||||||
userToReasonTexts: user_to_reason_texts,
|
userToReasonTexts: user_to_reason_texts,
|
||||||
sourceText: string
|
userIds: (string | undefined)[]
|
||||||
) => {
|
) => {
|
||||||
const taggedUsers = sourceText.match(/@\w+/g)
|
userIds.forEach((id) => {
|
||||||
if (!taggedUsers) return
|
if (id && shouldGetNotification(id, userToReasonTexts))
|
||||||
// await all get tagged users:
|
userToReasonTexts[id] = {
|
||||||
const users = await Promise.all(
|
|
||||||
taggedUsers.map(async (username) => {
|
|
||||||
return await getUserByUsername(username.slice(1))
|
|
||||||
})
|
|
||||||
)
|
|
||||||
users.forEach((taggedUser) => {
|
|
||||||
if (taggedUser && shouldGetNotification(taggedUser.id, userToReasonTexts))
|
|
||||||
userToReasonTexts[taggedUser.id] = {
|
|
||||||
reason: 'tagged_user',
|
reason: 'tagged_user',
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -253,7 +247,7 @@ export const createNotification = async (
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const notifyUserAddedToGroup = async (
|
const notifyUserAddedToGroup = (
|
||||||
userToReasonTexts: user_to_reason_texts,
|
userToReasonTexts: user_to_reason_texts,
|
||||||
relatedUserId: string
|
relatedUserId: string
|
||||||
) => {
|
) => {
|
||||||
|
@ -275,11 +269,14 @@ export const createNotification = async (
|
||||||
const getUsersToNotify = async () => {
|
const getUsersToNotify = async () => {
|
||||||
const userToReasonTexts: user_to_reason_texts = {}
|
const userToReasonTexts: user_to_reason_texts = {}
|
||||||
// The following functions modify the userToReasonTexts object in place.
|
// The following functions modify the userToReasonTexts object in place.
|
||||||
if (sourceType === 'follow' && relatedUserId) {
|
if (sourceType === 'follow' && recipients?.[0]) {
|
||||||
await notifyFollowedUser(userToReasonTexts, relatedUserId)
|
notifyFollowedUser(userToReasonTexts, recipients[0])
|
||||||
} else if (sourceType === 'group' && relatedUserId) {
|
} else if (
|
||||||
if (sourceUpdateType === 'created')
|
sourceType === 'group' &&
|
||||||
await notifyUserAddedToGroup(userToReasonTexts, relatedUserId)
|
sourceUpdateType === 'created' &&
|
||||||
|
recipients
|
||||||
|
) {
|
||||||
|
recipients.forEach((r) => notifyUserAddedToGroup(userToReasonTexts, r))
|
||||||
}
|
}
|
||||||
|
|
||||||
// The following functions need sourceContract to be defined.
|
// The following functions need sourceContract to be defined.
|
||||||
|
@ -292,13 +289,9 @@ export const createNotification = async (
|
||||||
(sourceUpdateType === 'updated' || sourceUpdateType === 'resolved'))
|
(sourceUpdateType === 'updated' || sourceUpdateType === 'resolved'))
|
||||||
) {
|
) {
|
||||||
if (sourceType === 'comment') {
|
if (sourceType === 'comment') {
|
||||||
if (relatedUserId && relatedSourceType)
|
if (recipients?.[0] && relatedSourceType)
|
||||||
await notifyRepliedUsers(
|
notifyRepliedUser(userToReasonTexts, recipients[0], relatedSourceType)
|
||||||
userToReasonTexts,
|
if (sourceText) notifyTaggedUsers(userToReasonTexts, recipients ?? [])
|
||||||
relatedUserId,
|
|
||||||
relatedSourceType
|
|
||||||
)
|
|
||||||
if (sourceText) await notifyTaggedUsers(userToReasonTexts, sourceText)
|
|
||||||
}
|
}
|
||||||
await notifyContractCreator(userToReasonTexts, sourceContract)
|
await notifyContractCreator(userToReasonTexts, sourceContract)
|
||||||
await notifyOtherAnswerersOnContract(userToReasonTexts, sourceContract)
|
await notifyOtherAnswerersOnContract(userToReasonTexts, sourceContract)
|
||||||
|
@ -307,6 +300,7 @@ export const createNotification = async (
|
||||||
await notifyOtherCommentersOnContract(userToReasonTexts, sourceContract)
|
await notifyOtherCommentersOnContract(userToReasonTexts, sourceContract)
|
||||||
} else if (sourceType === 'contract' && sourceUpdateType === 'created') {
|
} else if (sourceType === 'contract' && sourceUpdateType === 'created') {
|
||||||
await notifyUsersFollowers(userToReasonTexts)
|
await notifyUsersFollowers(userToReasonTexts)
|
||||||
|
notifyTaggedUsers(userToReasonTexts, recipients ?? [])
|
||||||
} else if (sourceType === 'contract' && sourceUpdateType === 'closed') {
|
} else if (sourceType === 'contract' && sourceUpdateType === 'closed') {
|
||||||
await notifyContractCreator(userToReasonTexts, sourceContract, {
|
await notifyContractCreator(userToReasonTexts, sourceContract, {
|
||||||
force: true,
|
force: true,
|
||||||
|
@ -422,7 +416,7 @@ export const createGroupCommentNotification = async (
|
||||||
sourceUserName: fromUser.name,
|
sourceUserName: fromUser.name,
|
||||||
sourceUserUsername: fromUser.username,
|
sourceUserUsername: fromUser.username,
|
||||||
sourceUserAvatarUrl: fromUser.avatarUrl,
|
sourceUserAvatarUrl: fromUser.avatarUrl,
|
||||||
sourceText: comment.text,
|
sourceText: richTextToString(comment.content),
|
||||||
sourceSlug,
|
sourceSlug,
|
||||||
sourceTitle: `${group.name}`,
|
sourceTitle: `${group.name}`,
|
||||||
isSeenOnHref: sourceSlug,
|
isSeenOnHref: sourceSlug,
|
||||||
|
@ -478,3 +472,35 @@ export const createReferralNotification = async (
|
||||||
}
|
}
|
||||||
|
|
||||||
const groupPath = (groupSlug: string) => `/group/${groupSlug}`
|
const groupPath = (groupSlug: string) => `/group/${groupSlug}`
|
||||||
|
|
||||||
|
export const createChallengeAcceptedNotification = async (
|
||||||
|
challenger: User,
|
||||||
|
challengeCreator: User,
|
||||||
|
challenge: Challenge,
|
||||||
|
acceptedAmount: number,
|
||||||
|
contract: Contract
|
||||||
|
) => {
|
||||||
|
const notificationRef = firestore
|
||||||
|
.collection(`/users/${challengeCreator.id}/notifications`)
|
||||||
|
.doc()
|
||||||
|
const notification: Notification = {
|
||||||
|
id: notificationRef.id,
|
||||||
|
userId: challengeCreator.id,
|
||||||
|
reason: 'challenge_accepted',
|
||||||
|
createdTime: Date.now(),
|
||||||
|
isSeen: false,
|
||||||
|
sourceId: challenge.slug,
|
||||||
|
sourceType: 'challenge',
|
||||||
|
sourceUpdateType: 'updated',
|
||||||
|
sourceUserName: challenger.name,
|
||||||
|
sourceUserUsername: challenger.username,
|
||||||
|
sourceUserAvatarUrl: challenger.avatarUrl,
|
||||||
|
sourceText: acceptedAmount.toString(),
|
||||||
|
sourceContractCreatorUsername: contract.creatorUsername,
|
||||||
|
sourceContractTitle: contract.question,
|
||||||
|
sourceContractSlug: contract.slug,
|
||||||
|
sourceContractId: contract.id,
|
||||||
|
sourceSlug: `/challenges/${challengeCreator.username}/${challenge.contractSlug}/${challenge.slug}`,
|
||||||
|
}
|
||||||
|
return await notificationRef.set(removeUndefinedProps(notification))
|
||||||
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ import {
|
||||||
cleanDisplayName,
|
cleanDisplayName,
|
||||||
cleanUsername,
|
cleanUsername,
|
||||||
} from '../../common/util/clean-username'
|
} from '../../common/util/clean-username'
|
||||||
import { sendWelcomeEmail } from './emails'
|
import { sendPersonalFollowupEmail, sendWelcomeEmail } from './emails'
|
||||||
import { isWhitelisted } from '../../common/envs/constants'
|
import { isWhitelisted } from '../../common/envs/constants'
|
||||||
import {
|
import {
|
||||||
CATEGORIES_GROUP_SLUG_POSTFIX,
|
CATEGORIES_GROUP_SLUG_POSTFIX,
|
||||||
|
@ -96,9 +96,10 @@ export const createuser = newEndpoint(opts, async (req, auth) => {
|
||||||
|
|
||||||
await addUserToDefaultGroups(user)
|
await addUserToDefaultGroups(user)
|
||||||
await sendWelcomeEmail(user, privateUser)
|
await sendWelcomeEmail(user, privateUser)
|
||||||
|
await sendPersonalFollowupEmail(user, privateUser)
|
||||||
await track(auth.uid, 'create user', { username }, { ip: req.ip })
|
await track(auth.uid, 'create user', { username }, { ip: req.ip })
|
||||||
|
|
||||||
return user
|
return { user, privateUser }
|
||||||
})
|
})
|
||||||
|
|
||||||
const firestore = admin.firestore()
|
const firestore = admin.firestore()
|
||||||
|
|
|
@ -128,7 +128,20 @@
|
||||||
<div
|
<div
|
||||||
style="font-family:Arial, sans-serif;font-size:18px;letter-spacing:normal;line-height:1;text-align:left;color:#000000;">
|
style="font-family:Arial, sans-serif;font-size:18px;letter-spacing:normal;line-height:1;text-align:left;color:#000000;">
|
||||||
<p class="text-build-content"
|
<p class="text-build-content"
|
||||||
style="line-height: 24px; text-align: center; margin: 10px 0; margin-top: 10px; margin-bottom: 10px;"
|
style="line-height: 24px; margin: 10px 0; margin-top: 10px; margin-bottom: 10px;"
|
||||||
|
data-testid="4XoHRGw1Y"><span
|
||||||
|
style="color:#000000;font-family:Arial, Helvetica, sans-serif;font-size:18px;">
|
||||||
|
Hi {{name}},</span></p>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td align="left"
|
||||||
|
style="font-size:0px;padding:10px 25px;padding-top:0px;padding-bottom:0px;word-break:break-word;">
|
||||||
|
<div
|
||||||
|
style="font-family:Arial, sans-serif;font-size:18px;letter-spacing:normal;line-height:1;text-align:left;color:#000000;">
|
||||||
|
<p class="text-build-content"
|
||||||
|
style="line-height: 24px; margin: 10px 0; margin-top: 10px; margin-bottom: 10px;"
|
||||||
data-testid="4XoHRGw1Y"><span
|
data-testid="4XoHRGw1Y"><span
|
||||||
style="color:#000000;font-family:Arial, Helvetica, sans-serif;font-size:18px;">Thanks for
|
style="color:#000000;font-family:Arial, Helvetica, sans-serif;font-size:18px;">Thanks for
|
||||||
using Manifold Markets. Running low
|
using Manifold Markets. Running low
|
||||||
|
@ -161,6 +174,51 @@
|
||||||
</table>
|
</table>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td align="left"
|
||||||
|
style="font-size:0px;padding:15px 25px 0px 25px;padding-top:15px;padding-right:25px;padding-bottom:0px;padding-left:25px;word-break:break-word;">
|
||||||
|
<div
|
||||||
|
style="font-family:Arial, sans-serif;font-size:18px;letter-spacing:normal;line-height:1;text-align:left;color:#000000;">
|
||||||
|
<p class="text-build-content" style="line-height: 23px; margin: 10px 0; margin-top: 10px;"
|
||||||
|
data-testid="3Q8BP69fq"><span style="font-family:Arial, sans-serif;font-size:18px;">Did
|
||||||
|
you know, besides making correct predictions, there are
|
||||||
|
plenty of other ways to earn mana?</span></p>
|
||||||
|
<ul>
|
||||||
|
<li style="line-height:23px;"><span
|
||||||
|
style="font-family:Arial, sans-serif;font-size:18px;">Receiving
|
||||||
|
tips on comments</span></li>
|
||||||
|
<li style="line-height:23px;"><span
|
||||||
|
style="font-family:Arial, sans-serif;font-size:18px;">Unique
|
||||||
|
trader bonus for each user who bets on your
|
||||||
|
markets</span></li>
|
||||||
|
<li style="line-height:23px;"><span style="font-family:Arial, sans-serif;font-size:18px;"><a
|
||||||
|
class="link-build-content" style="color:inherit;; text-decoration: none;"
|
||||||
|
target="_blank" href="https://manifold.markets/referrals"><span
|
||||||
|
style="color:#55575d;font-family:Arial;font-size:18px;"><u>Referring
|
||||||
|
friends</u></span></a></span></li>
|
||||||
|
<li style="line-height:23px;"><a class="link-build-content"
|
||||||
|
style="color:inherit;; text-decoration: none;" target="_blank"
|
||||||
|
href="https://manifold.markets/group/bugs?s=most-traded"><span
|
||||||
|
style="color:#55575d;font-family:Arial;font-size:18px;"><u>Reporting
|
||||||
|
bugs</u></span></a><span style="font-family:Arial, sans-serif;font-size:18px;">
|
||||||
|
and </span><a class="link-build-content" style="color:inherit;; text-decoration: none;"
|
||||||
|
target="_blank"
|
||||||
|
href="https://manifold.markets/group/manifold-features-25bad7c7792e/chat?s=most-traded"><span
|
||||||
|
style="color:#55575d;font-family:Arial;font-size:18px;"><u>giving
|
||||||
|
feedback</u></span></a></li>
|
||||||
|
</ul>
|
||||||
|
<p class="text-build-content" data-testid="3Q8BP69fq" style="margin: 10px 0;"> </p>
|
||||||
|
<p class="text-build-content" data-testid="3Q8BP69fq" style="margin: 10px 0;"><span
|
||||||
|
style="color:#000000;font-family:Arial;font-size:18px;">Cheers,</span>
|
||||||
|
</p>
|
||||||
|
<p class="text-build-content" data-testid="3Q8BP69fq" style="margin: 10px 0;"><span
|
||||||
|
style="color:#000000;font-family:Arial;font-size:18px;">David
|
||||||
|
from Manifold</span></p>
|
||||||
|
<p class="text-build-content" data-testid="3Q8BP69fq"
|
||||||
|
style="margin: 10px 0; margin-bottom: 10px;"> </p>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td align="left"
|
<td align="left"
|
||||||
style="font-size:0px;padding:15px 25px 0px 25px;padding-top:15px;padding-right:25px;padding-bottom:0px;padding-left:25px;word-break:break-word;">
|
style="font-size:0px;padding:15px 25px 0px 25px;padding-top:15px;padding-right:25px;padding-bottom:0px;padding-left:25px;word-break:break-word;">
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -1,3 +1,5 @@
|
||||||
|
import * as dayjs from 'dayjs'
|
||||||
|
|
||||||
import { DOMAIN } from '../../common/envs/constants'
|
import { DOMAIN } from '../../common/envs/constants'
|
||||||
import { Answer } from '../../common/answer'
|
import { Answer } from '../../common/answer'
|
||||||
import { Bet } from '../../common/bet'
|
import { Bet } from '../../common/bet'
|
||||||
|
@ -14,9 +16,10 @@ import {
|
||||||
import { getValueFromBucket } from '../../common/calculate-dpm'
|
import { getValueFromBucket } from '../../common/calculate-dpm'
|
||||||
import { formatNumericProbability } from '../../common/pseudo-numeric'
|
import { formatNumericProbability } from '../../common/pseudo-numeric'
|
||||||
|
|
||||||
import { sendTemplateEmail } from './send-email'
|
import { sendTemplateEmail, sendTextEmail } from './send-email'
|
||||||
import { getPrivateUser, getUser } from './utils'
|
import { getPrivateUser, getUser } from './utils'
|
||||||
import { getFunctionUrl } from '../../common/api'
|
import { getFunctionUrl } from '../../common/api'
|
||||||
|
import { richTextToString } from '../../common/util/parse'
|
||||||
|
|
||||||
const UNSUBSCRIBE_ENDPOINT = getFunctionUrl('unsubscribe')
|
const UNSUBSCRIBE_ENDPOINT = getFunctionUrl('unsubscribe')
|
||||||
|
|
||||||
|
@ -73,9 +76,8 @@ export const sendMarketResolutionEmail = async (
|
||||||
|
|
||||||
// Modify template here:
|
// Modify template here:
|
||||||
// https://app.mailgun.com/app/sending/domains/mg.manifold.markets/templates/edit/market-resolved/initial
|
// https://app.mailgun.com/app/sending/domains/mg.manifold.markets/templates/edit/market-resolved/initial
|
||||||
// Mailgun username: james@mantic.markets
|
|
||||||
|
|
||||||
await sendTemplateEmail(
|
return await sendTemplateEmail(
|
||||||
privateUser.email,
|
privateUser.email,
|
||||||
subject,
|
subject,
|
||||||
'market-resolved',
|
'market-resolved',
|
||||||
|
@ -151,7 +153,7 @@ export const sendWelcomeEmail = async (
|
||||||
const emailType = 'generic'
|
const emailType = 'generic'
|
||||||
const unsubscribeLink = `${UNSUBSCRIBE_ENDPOINT}?id=${userId}&type=${emailType}`
|
const unsubscribeLink = `${UNSUBSCRIBE_ENDPOINT}?id=${userId}&type=${emailType}`
|
||||||
|
|
||||||
await sendTemplateEmail(
|
return await sendTemplateEmail(
|
||||||
privateUser.email,
|
privateUser.email,
|
||||||
'Welcome to Manifold Markets!',
|
'Welcome to Manifold Markets!',
|
||||||
'welcome',
|
'welcome',
|
||||||
|
@ -165,6 +167,43 @@ export const sendWelcomeEmail = async (
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const sendPersonalFollowupEmail = async (
|
||||||
|
user: User,
|
||||||
|
privateUser: PrivateUser
|
||||||
|
) => {
|
||||||
|
if (!privateUser || !privateUser.email) return
|
||||||
|
|
||||||
|
const { name } = user
|
||||||
|
const firstName = name.split(' ')[0]
|
||||||
|
|
||||||
|
const emailBody = `Hi ${firstName},
|
||||||
|
|
||||||
|
Thanks for signing up! I'm one of the cofounders of Manifold Markets, and was wondering how you've found your exprience on the platform so far?
|
||||||
|
|
||||||
|
If you haven't already, I encourage you to try creating your own prediction market (https://manifold.markets/create) and joining our Discord chat (https://discord.com/invite/eHQBNBqXuh).
|
||||||
|
|
||||||
|
Feel free to reply to this email with any questions or concerns you have.
|
||||||
|
|
||||||
|
Cheers,
|
||||||
|
|
||||||
|
James
|
||||||
|
Cofounder of Manifold Markets
|
||||||
|
https://manifold.markets
|
||||||
|
`
|
||||||
|
|
||||||
|
const sendTime = dayjs().add(4, 'hours').toString()
|
||||||
|
|
||||||
|
await sendTextEmail(
|
||||||
|
privateUser.email,
|
||||||
|
'How are you finding Manifold?',
|
||||||
|
emailBody,
|
||||||
|
{
|
||||||
|
from: 'James from Manifold <james@manifold.markets>',
|
||||||
|
'o:deliverytime': sendTime,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export const sendOneWeekBonusEmail = async (
|
export const sendOneWeekBonusEmail = async (
|
||||||
user: User,
|
user: User,
|
||||||
privateUser: PrivateUser
|
privateUser: PrivateUser
|
||||||
|
@ -182,7 +221,7 @@ export const sendOneWeekBonusEmail = async (
|
||||||
const emailType = 'generic'
|
const emailType = 'generic'
|
||||||
const unsubscribeLink = `${UNSUBSCRIBE_ENDPOINT}?id=${userId}&type=${emailType}`
|
const unsubscribeLink = `${UNSUBSCRIBE_ENDPOINT}?id=${userId}&type=${emailType}`
|
||||||
|
|
||||||
await sendTemplateEmail(
|
return await sendTemplateEmail(
|
||||||
privateUser.email,
|
privateUser.email,
|
||||||
'Manifold Markets one week anniversary gift',
|
'Manifold Markets one week anniversary gift',
|
||||||
'one-week',
|
'one-week',
|
||||||
|
@ -197,6 +236,37 @@ export const sendOneWeekBonusEmail = async (
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const sendCreatorGuideEmail = async (
|
||||||
|
user: User,
|
||||||
|
privateUser: PrivateUser
|
||||||
|
) => {
|
||||||
|
if (
|
||||||
|
!privateUser ||
|
||||||
|
!privateUser.email ||
|
||||||
|
privateUser.unsubscribedFromGenericEmails
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
const { name, id: userId } = user
|
||||||
|
const firstName = name.split(' ')[0]
|
||||||
|
|
||||||
|
const emailType = 'generic'
|
||||||
|
const unsubscribeLink = `${UNSUBSCRIBE_ENDPOINT}?id=${userId}&type=${emailType}`
|
||||||
|
|
||||||
|
return await sendTemplateEmail(
|
||||||
|
privateUser.email,
|
||||||
|
'Market creation guide',
|
||||||
|
'creating-market',
|
||||||
|
{
|
||||||
|
name: firstName,
|
||||||
|
unsubscribeLink,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: 'David from Manifold <david@manifold.markets>',
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export const sendThankYouEmail = async (
|
export const sendThankYouEmail = async (
|
||||||
user: User,
|
user: User,
|
||||||
privateUser: PrivateUser
|
privateUser: PrivateUser
|
||||||
|
@ -214,7 +284,7 @@ export const sendThankYouEmail = async (
|
||||||
const emailType = 'generic'
|
const emailType = 'generic'
|
||||||
const unsubscribeLink = `${UNSUBSCRIBE_ENDPOINT}?id=${userId}&type=${emailType}`
|
const unsubscribeLink = `${UNSUBSCRIBE_ENDPOINT}?id=${userId}&type=${emailType}`
|
||||||
|
|
||||||
await sendTemplateEmail(
|
return await sendTemplateEmail(
|
||||||
privateUser.email,
|
privateUser.email,
|
||||||
'Thanks for your Manifold purchase',
|
'Thanks for your Manifold purchase',
|
||||||
'thank-you',
|
'thank-you',
|
||||||
|
@ -249,7 +319,7 @@ export const sendMarketCloseEmail = async (
|
||||||
const emailType = 'market-resolve'
|
const emailType = 'market-resolve'
|
||||||
const unsubscribeUrl = `${UNSUBSCRIBE_ENDPOINT}?id=${userId}&type=${emailType}`
|
const unsubscribeUrl = `${UNSUBSCRIBE_ENDPOINT}?id=${userId}&type=${emailType}`
|
||||||
|
|
||||||
await sendTemplateEmail(
|
return await sendTemplateEmail(
|
||||||
privateUser.email,
|
privateUser.email,
|
||||||
'Your market has closed',
|
'Your market has closed',
|
||||||
'market-close',
|
'market-close',
|
||||||
|
@ -291,7 +361,8 @@ export const sendNewCommentEmail = async (
|
||||||
const unsubscribeUrl = `${UNSUBSCRIBE_ENDPOINT}?id=${userId}&type=${emailType}`
|
const unsubscribeUrl = `${UNSUBSCRIBE_ENDPOINT}?id=${userId}&type=${emailType}`
|
||||||
|
|
||||||
const { name: commentorName, avatarUrl: commentorAvatarUrl } = commentCreator
|
const { name: commentorName, avatarUrl: commentorAvatarUrl } = commentCreator
|
||||||
const { text } = comment
|
const { content } = comment
|
||||||
|
const text = richTextToString(content)
|
||||||
|
|
||||||
let betDescription = ''
|
let betDescription = ''
|
||||||
if (bet) {
|
if (bet) {
|
||||||
|
@ -307,7 +378,7 @@ export const sendNewCommentEmail = async (
|
||||||
if (contract.outcomeType === 'FREE_RESPONSE' && answerId && answerText) {
|
if (contract.outcomeType === 'FREE_RESPONSE' && answerId && answerText) {
|
||||||
const answerNumber = `#${answerId}`
|
const answerNumber = `#${answerId}`
|
||||||
|
|
||||||
await sendTemplateEmail(
|
return await sendTemplateEmail(
|
||||||
privateUser.email,
|
privateUser.email,
|
||||||
subject,
|
subject,
|
||||||
'market-answer-comment',
|
'market-answer-comment',
|
||||||
|
@ -330,7 +401,7 @@ export const sendNewCommentEmail = async (
|
||||||
bet.outcome
|
bet.outcome
|
||||||
)}`
|
)}`
|
||||||
}
|
}
|
||||||
await sendTemplateEmail(
|
return await sendTemplateEmail(
|
||||||
privateUser.email,
|
privateUser.email,
|
||||||
subject,
|
subject,
|
||||||
'market-comment',
|
'market-comment',
|
||||||
|
@ -375,7 +446,7 @@ export const sendNewAnswerEmail = async (
|
||||||
const subject = `New answer on ${question}`
|
const subject = `New answer on ${question}`
|
||||||
const from = `${name} <info@manifold.markets>`
|
const from = `${name} <info@manifold.markets>`
|
||||||
|
|
||||||
await sendTemplateEmail(
|
return await sendTemplateEmail(
|
||||||
privateUser.email,
|
privateUser.email,
|
||||||
subject,
|
subject,
|
||||||
'market-answer',
|
'market-answer',
|
||||||
|
|
33
functions/src/get-custom-token.ts
Normal file
33
functions/src/get-custom-token.ts
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
import * as admin from 'firebase-admin'
|
||||||
|
import {
|
||||||
|
APIError,
|
||||||
|
EndpointDefinition,
|
||||||
|
lookupUser,
|
||||||
|
parseCredentials,
|
||||||
|
writeResponseError,
|
||||||
|
} from './api'
|
||||||
|
|
||||||
|
const opts = {
|
||||||
|
method: 'GET',
|
||||||
|
minInstances: 1,
|
||||||
|
concurrency: 100,
|
||||||
|
memory: '2GiB',
|
||||||
|
cpu: 1,
|
||||||
|
} as const
|
||||||
|
|
||||||
|
export const getcustomtoken: EndpointDefinition = {
|
||||||
|
opts,
|
||||||
|
handler: async (req, res) => {
|
||||||
|
try {
|
||||||
|
const credentials = await parseCredentials(req)
|
||||||
|
if (credentials.kind != 'jwt') {
|
||||||
|
throw new APIError(403, 'API keys cannot mint custom tokens.')
|
||||||
|
}
|
||||||
|
const user = await lookupUser(credentials)
|
||||||
|
const token = await admin.auth().createCustomToken(user.uid)
|
||||||
|
res.status(200).json({ token: token })
|
||||||
|
} catch (e) {
|
||||||
|
writeResponseError(e, res)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
|
@ -64,6 +64,8 @@ import { resolvemarket } from './resolve-market'
|
||||||
import { unsubscribe } from './unsubscribe'
|
import { unsubscribe } from './unsubscribe'
|
||||||
import { stripewebhook, createcheckoutsession } from './stripe'
|
import { stripewebhook, createcheckoutsession } from './stripe'
|
||||||
import { getcurrentuser } from './get-current-user'
|
import { getcurrentuser } from './get-current-user'
|
||||||
|
import { acceptchallenge } from './accept-challenge'
|
||||||
|
import { getcustomtoken } from './get-custom-token'
|
||||||
|
|
||||||
const toCloudFunction = ({ opts, handler }: EndpointDefinition) => {
|
const toCloudFunction = ({ opts, handler }: EndpointDefinition) => {
|
||||||
return onRequest(opts, handler as any)
|
return onRequest(opts, handler as any)
|
||||||
|
@ -87,6 +89,8 @@ const unsubscribeFunction = toCloudFunction(unsubscribe)
|
||||||
const stripeWebhookFunction = toCloudFunction(stripewebhook)
|
const stripeWebhookFunction = toCloudFunction(stripewebhook)
|
||||||
const createCheckoutSessionFunction = toCloudFunction(createcheckoutsession)
|
const createCheckoutSessionFunction = toCloudFunction(createcheckoutsession)
|
||||||
const getCurrentUserFunction = toCloudFunction(getcurrentuser)
|
const getCurrentUserFunction = toCloudFunction(getcurrentuser)
|
||||||
|
const acceptChallenge = toCloudFunction(acceptchallenge)
|
||||||
|
const getCustomTokenFunction = toCloudFunction(getcustomtoken)
|
||||||
|
|
||||||
export {
|
export {
|
||||||
healthFunction as health,
|
healthFunction as health,
|
||||||
|
@ -108,4 +112,6 @@ export {
|
||||||
stripeWebhookFunction as stripewebhook,
|
stripeWebhookFunction as stripewebhook,
|
||||||
createCheckoutSessionFunction as createcheckoutsession,
|
createCheckoutSessionFunction as createcheckoutsession,
|
||||||
getCurrentUserFunction as getcurrentuser,
|
getCurrentUserFunction as getcurrentuser,
|
||||||
|
acceptChallenge as acceptchallenge,
|
||||||
|
getCustomTokenFunction as getcustomtoken,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
import * as functions from 'firebase-functions'
|
import * as functions from 'firebase-functions'
|
||||||
import * as admin from 'firebase-admin'
|
import * as admin from 'firebase-admin'
|
||||||
import { uniq } from 'lodash'
|
import { compact, uniq } from 'lodash'
|
||||||
|
|
||||||
import { getContract, getUser, getValues } from './utils'
|
import { getContract, getUser, getValues } from './utils'
|
||||||
import { Comment } from '../../common/comment'
|
import { Comment } from '../../common/comment'
|
||||||
import { sendNewCommentEmail } from './emails'
|
import { sendNewCommentEmail } from './emails'
|
||||||
import { Bet } from '../../common/bet'
|
import { Bet } from '../../common/bet'
|
||||||
import { Answer } from '../../common/answer'
|
import { Answer } from '../../common/answer'
|
||||||
import { createNotification } from './create-notification'
|
import { createNotification } from './create-notification'
|
||||||
|
import { parseMentions, richTextToString } from '../../common/util/parse'
|
||||||
|
|
||||||
const firestore = admin.firestore()
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
|
@ -24,6 +24,11 @@ export const onCreateCommentOnContract = functions
|
||||||
if (!contract)
|
if (!contract)
|
||||||
throw new Error('Could not find contract corresponding with comment')
|
throw new Error('Could not find contract corresponding with comment')
|
||||||
|
|
||||||
|
await change.ref.update({
|
||||||
|
contractSlug: contract.slug,
|
||||||
|
contractQuestion: contract.question,
|
||||||
|
})
|
||||||
|
|
||||||
const comment = change.data() as Comment
|
const comment = change.data() as Comment
|
||||||
const lastCommentTime = comment.createdTime
|
const lastCommentTime = comment.createdTime
|
||||||
|
|
||||||
|
@ -68,18 +73,22 @@ export const onCreateCommentOnContract = functions
|
||||||
? 'answer'
|
? 'answer'
|
||||||
: undefined
|
: undefined
|
||||||
|
|
||||||
const relatedUserId = comment.replyToCommentId
|
const repliedUserId = comment.replyToCommentId
|
||||||
? comments.find((c) => c.id === comment.replyToCommentId)?.userId
|
? comments.find((c) => c.id === comment.replyToCommentId)?.userId
|
||||||
: answer?.userId
|
: answer?.userId
|
||||||
|
|
||||||
|
const recipients = uniq(
|
||||||
|
compact([...parseMentions(comment.content), repliedUserId])
|
||||||
|
)
|
||||||
|
|
||||||
await createNotification(
|
await createNotification(
|
||||||
comment.id,
|
comment.id,
|
||||||
'comment',
|
'comment',
|
||||||
'created',
|
'created',
|
||||||
commentCreator,
|
commentCreator,
|
||||||
eventId,
|
eventId,
|
||||||
comment.text,
|
richTextToString(comment.content),
|
||||||
{ contract, relatedSourceType, relatedUserId }
|
{ contract, relatedSourceType, recipients }
|
||||||
)
|
)
|
||||||
|
|
||||||
const recipientUserIds = uniq([
|
const recipientUserIds = uniq([
|
||||||
|
|
|
@ -1,12 +1,17 @@
|
||||||
import * as functions from 'firebase-functions'
|
import * as functions from 'firebase-functions'
|
||||||
import { getUser } from './utils'
|
import * as admin from 'firebase-admin'
|
||||||
|
|
||||||
|
import { getPrivateUser, getUser } from './utils'
|
||||||
import { createNotification } from './create-notification'
|
import { createNotification } from './create-notification'
|
||||||
import { Contract } from '../../common/contract'
|
import { Contract } from '../../common/contract'
|
||||||
import { richTextToString } from '../../common/util/parse'
|
import { parseMentions, richTextToString } from '../../common/util/parse'
|
||||||
import { JSONContent } from '@tiptap/core'
|
import { JSONContent } from '@tiptap/core'
|
||||||
|
import { User } from 'common/user'
|
||||||
|
import { sendCreatorGuideEmail } from './emails'
|
||||||
|
|
||||||
export const onCreateContract = functions.firestore
|
export const onCreateContract = functions
|
||||||
.document('contracts/{contractId}')
|
.runWith({ secrets: ['MAILGUN_KEY'] })
|
||||||
|
.firestore.document('contracts/{contractId}')
|
||||||
.onCreate(async (snapshot, context) => {
|
.onCreate(async (snapshot, context) => {
|
||||||
const contract = snapshot.data() as Contract
|
const contract = snapshot.data() as Contract
|
||||||
const { eventId } = context
|
const { eventId } = context
|
||||||
|
@ -14,13 +19,35 @@ export const onCreateContract = functions.firestore
|
||||||
const contractCreator = await getUser(contract.creatorId)
|
const contractCreator = await getUser(contract.creatorId)
|
||||||
if (!contractCreator) throw new Error('Could not find contract creator')
|
if (!contractCreator) throw new Error('Could not find contract creator')
|
||||||
|
|
||||||
|
const desc = contract.description as JSONContent
|
||||||
|
const mentioned = parseMentions(desc)
|
||||||
|
|
||||||
await createNotification(
|
await createNotification(
|
||||||
contract.id,
|
contract.id,
|
||||||
'contract',
|
'contract',
|
||||||
'created',
|
'created',
|
||||||
contractCreator,
|
contractCreator,
|
||||||
eventId,
|
eventId,
|
||||||
richTextToString(contract.description as JSONContent),
|
richTextToString(desc),
|
||||||
{ contract }
|
{ contract, recipients: mentioned }
|
||||||
)
|
)
|
||||||
|
|
||||||
|
await sendGuideEmail(contractCreator)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
|
const sendGuideEmail = async (contractCreator: User) => {
|
||||||
|
const query = await firestore
|
||||||
|
.collection(`contracts`)
|
||||||
|
.where('creatorId', '==', contractCreator.id)
|
||||||
|
.limit(2)
|
||||||
|
.get()
|
||||||
|
|
||||||
|
if (query.size >= 2) return
|
||||||
|
|
||||||
|
const privateUser = await getPrivateUser(contractCreator.id)
|
||||||
|
if (!privateUser) return
|
||||||
|
|
||||||
|
await sendCreatorGuideEmail(contractCreator, privateUser)
|
||||||
|
}
|
||||||
|
|
|
@ -12,19 +12,17 @@ export const onCreateGroup = functions.firestore
|
||||||
const groupCreator = await getUser(group.creatorId)
|
const groupCreator = await getUser(group.creatorId)
|
||||||
if (!groupCreator) throw new Error('Could not find group creator')
|
if (!groupCreator) throw new Error('Could not find group creator')
|
||||||
// create notifications for all members of the group
|
// create notifications for all members of the group
|
||||||
for (const memberId of group.memberIds) {
|
await createNotification(
|
||||||
await createNotification(
|
group.id,
|
||||||
group.id,
|
'group',
|
||||||
'group',
|
'created',
|
||||||
'created',
|
groupCreator,
|
||||||
groupCreator,
|
eventId,
|
||||||
eventId,
|
group.about,
|
||||||
group.about,
|
{
|
||||||
{
|
recipients: group.memberIds,
|
||||||
relatedUserId: memberId,
|
slug: group.slug,
|
||||||
slug: group.slug,
|
title: group.name,
|
||||||
title: group.name,
|
}
|
||||||
}
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
|
@ -30,7 +30,7 @@ export const onFollowUser = functions.firestore
|
||||||
followingUser,
|
followingUser,
|
||||||
eventId,
|
eventId,
|
||||||
'',
|
'',
|
||||||
{ relatedUserId: follow.userId }
|
{ recipients: [follow.userId] }
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -82,7 +82,22 @@ export const placebet = newEndpoint({}, async (req, auth) => {
|
||||||
(outcomeType == 'BINARY' || outcomeType === 'PSEUDO_NUMERIC') &&
|
(outcomeType == 'BINARY' || outcomeType === 'PSEUDO_NUMERIC') &&
|
||||||
mechanism == 'cpmm-1'
|
mechanism == 'cpmm-1'
|
||||||
) {
|
) {
|
||||||
const { outcome, limitProb } = validate(binarySchema, req.body)
|
// eslint-disable-next-line prefer-const
|
||||||
|
let { outcome, limitProb } = validate(binarySchema, req.body)
|
||||||
|
|
||||||
|
if (limitProb !== undefined && outcomeType === 'BINARY') {
|
||||||
|
const isRounded = floatingEqual(
|
||||||
|
Math.round(limitProb * 100),
|
||||||
|
limitProb * 100
|
||||||
|
)
|
||||||
|
if (!isRounded)
|
||||||
|
throw new APIError(
|
||||||
|
400,
|
||||||
|
'limitProb must be in increments of 0.01 (i.e. whole percentage points)'
|
||||||
|
)
|
||||||
|
|
||||||
|
limitProb = Math.round(limitProb * 100) / 100
|
||||||
|
}
|
||||||
|
|
||||||
const unfilledBetsSnap = await trans.get(
|
const unfilledBetsSnap = await trans.get(
|
||||||
getUnfilledBetsQuery(contractDoc)
|
getUnfilledBetsQuery(contractDoc)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import * as admin from 'firebase-admin'
|
import * as admin from 'firebase-admin'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { difference, uniq, mapValues, groupBy, sumBy } from 'lodash'
|
import { difference, mapValues, groupBy, sumBy } from 'lodash'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Contract,
|
Contract,
|
||||||
|
@ -18,10 +18,12 @@ import {
|
||||||
groupPayoutsByUser,
|
groupPayoutsByUser,
|
||||||
Payout,
|
Payout,
|
||||||
} from '../../common/payouts'
|
} from '../../common/payouts'
|
||||||
import { isAdmin } from '../../common/envs/constants'
|
import { isManifoldId } from '../../common/envs/constants'
|
||||||
import { removeUndefinedProps } from '../../common/util/object'
|
import { removeUndefinedProps } from '../../common/util/object'
|
||||||
import { LiquidityProvision } from '../../common/liquidity-provision'
|
import { LiquidityProvision } from '../../common/liquidity-provision'
|
||||||
import { APIError, newEndpoint, validate } from './api'
|
import { APIError, newEndpoint, validate } from './api'
|
||||||
|
import { getContractBetMetrics } from '../../common/calculate'
|
||||||
|
import { floatingEqual } from '../../common/util/math'
|
||||||
|
|
||||||
const bodySchema = z.object({
|
const bodySchema = z.object({
|
||||||
contractId: z.string(),
|
contractId: z.string(),
|
||||||
|
@ -82,7 +84,7 @@ export const resolvemarket = newEndpoint(opts, async (req, auth) => {
|
||||||
req.body
|
req.body
|
||||||
)
|
)
|
||||||
|
|
||||||
if (creatorId !== auth.uid && !isAdmin(auth.uid))
|
if (creatorId !== auth.uid && !isManifoldId(auth.uid))
|
||||||
throw new APIError(403, 'User is not creator of contract')
|
throw new APIError(403, 'User is not creator of contract')
|
||||||
|
|
||||||
if (contract.resolution) throw new APIError(400, 'Contract already resolved')
|
if (contract.resolution) throw new APIError(400, 'Contract already resolved')
|
||||||
|
@ -162,7 +164,7 @@ export const resolvemarket = newEndpoint(opts, async (req, auth) => {
|
||||||
const userPayoutsWithoutLoans = groupPayoutsByUser(payouts)
|
const userPayoutsWithoutLoans = groupPayoutsByUser(payouts)
|
||||||
|
|
||||||
await sendResolutionEmails(
|
await sendResolutionEmails(
|
||||||
openBets,
|
bets,
|
||||||
userPayoutsWithoutLoans,
|
userPayoutsWithoutLoans,
|
||||||
creator,
|
creator,
|
||||||
creatorPayout,
|
creatorPayout,
|
||||||
|
@ -188,7 +190,7 @@ const processPayouts = async (payouts: Payout[], isDeposit = false) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const sendResolutionEmails = async (
|
const sendResolutionEmails = async (
|
||||||
openBets: Bet[],
|
bets: Bet[],
|
||||||
userPayouts: { [userId: string]: number },
|
userPayouts: { [userId: string]: number },
|
||||||
creator: User,
|
creator: User,
|
||||||
creatorPayout: number,
|
creatorPayout: number,
|
||||||
|
@ -197,14 +199,15 @@ const sendResolutionEmails = async (
|
||||||
resolutionProbability?: number,
|
resolutionProbability?: number,
|
||||||
resolutions?: { [outcome: string]: number }
|
resolutions?: { [outcome: string]: number }
|
||||||
) => {
|
) => {
|
||||||
const nonWinners = difference(
|
|
||||||
uniq(openBets.map(({ userId }) => userId)),
|
|
||||||
Object.keys(userPayouts)
|
|
||||||
)
|
|
||||||
const investedByUser = mapValues(
|
const investedByUser = mapValues(
|
||||||
groupBy(openBets, (bet) => bet.userId),
|
groupBy(bets, (bet) => bet.userId),
|
||||||
(bets) => sumBy(bets, (bet) => bet.amount)
|
(bets) => getContractBetMetrics(contract, bets).invested
|
||||||
)
|
)
|
||||||
|
const investedUsers = Object.keys(investedByUser).filter(
|
||||||
|
(userId) => !floatingEqual(investedByUser[userId], 0)
|
||||||
|
)
|
||||||
|
|
||||||
|
const nonWinners = difference(investedUsers, Object.keys(userPayouts))
|
||||||
const emailPayouts = [
|
const emailPayouts = [
|
||||||
...Object.entries(userPayouts),
|
...Object.entries(userPayouts),
|
||||||
...nonWinners.map((userId) => [userId, 0] as const),
|
...nonWinners.map((userId) => [userId, 0] as const),
|
||||||
|
|
70
functions/src/scripts/denormalize-comment-contract-data.ts
Normal file
70
functions/src/scripts/denormalize-comment-contract-data.ts
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
// Filling in the contract-based fields on comments.
|
||||||
|
|
||||||
|
import * as admin from 'firebase-admin'
|
||||||
|
import { initAdmin } from './script-init'
|
||||||
|
import {
|
||||||
|
DocumentCorrespondence,
|
||||||
|
findDiffs,
|
||||||
|
describeDiff,
|
||||||
|
applyDiff,
|
||||||
|
} from './denormalize'
|
||||||
|
import { DocumentSnapshot, Transaction } from 'firebase-admin/firestore'
|
||||||
|
|
||||||
|
initAdmin()
|
||||||
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
|
async function getContractsById(transaction: Transaction) {
|
||||||
|
const contracts = await transaction.get(firestore.collection('contracts'))
|
||||||
|
const results = Object.fromEntries(contracts.docs.map((doc) => [doc.id, doc]))
|
||||||
|
console.log(`Found ${contracts.size} contracts.`)
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getCommentsByContractId(transaction: Transaction) {
|
||||||
|
const comments = await transaction.get(
|
||||||
|
firestore.collectionGroup('comments').where('contractId', '!=', null)
|
||||||
|
)
|
||||||
|
const results = new Map<string, DocumentSnapshot[]>()
|
||||||
|
comments.forEach((doc) => {
|
||||||
|
const contractId = doc.get('contractId')
|
||||||
|
const contractComments = results.get(contractId) || []
|
||||||
|
contractComments.push(doc)
|
||||||
|
results.set(contractId, contractComments)
|
||||||
|
})
|
||||||
|
console.log(`Found ${comments.size} comments on ${results.size} contracts.`)
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
async function denormalize() {
|
||||||
|
let hasMore = true
|
||||||
|
while (hasMore) {
|
||||||
|
hasMore = await admin.firestore().runTransaction(async (transaction) => {
|
||||||
|
const [contractsById, commentsByContractId] = await Promise.all([
|
||||||
|
getContractsById(transaction),
|
||||||
|
getCommentsByContractId(transaction),
|
||||||
|
])
|
||||||
|
const mapping = Object.entries(contractsById).map(
|
||||||
|
([id, doc]): DocumentCorrespondence => {
|
||||||
|
return [doc, commentsByContractId.get(id) || []]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const slugDiffs = findDiffs(mapping, 'slug', 'contractSlug')
|
||||||
|
const qDiffs = findDiffs(mapping, 'question', 'contractQuestion')
|
||||||
|
console.log(`Found ${slugDiffs.length} comments with mismatched slugs.`)
|
||||||
|
console.log(`Found ${qDiffs.length} comments with mismatched questions.`)
|
||||||
|
const diffs = slugDiffs.concat(qDiffs)
|
||||||
|
diffs.slice(0, 500).forEach((d) => {
|
||||||
|
console.log(describeDiff(d))
|
||||||
|
applyDiff(transaction, d)
|
||||||
|
})
|
||||||
|
if (diffs.length > 500) {
|
||||||
|
console.log(`Applying first 500 because of Firestore limit...`)
|
||||||
|
}
|
||||||
|
return diffs.length > 500
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (require.main === module) {
|
||||||
|
denormalize().catch((e) => console.error(e))
|
||||||
|
}
|
27
functions/src/scripts/set-avatar-cache-headers.ts
Normal file
27
functions/src/scripts/set-avatar-cache-headers.ts
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
import { initAdmin } from './script-init'
|
||||||
|
import { log } from '../utils'
|
||||||
|
|
||||||
|
const app = initAdmin()
|
||||||
|
const ONE_YEAR_SECS = 60 * 60 * 24 * 365
|
||||||
|
const AVATAR_EXTENSION_RE = /\.(gif|tiff|jpe?g|png|webp)$/i
|
||||||
|
|
||||||
|
const processAvatars = async () => {
|
||||||
|
const storage = app.storage()
|
||||||
|
const bucket = storage.bucket(`${app.options.projectId}.appspot.com`)
|
||||||
|
const [files] = await bucket.getFiles({ prefix: 'user-images' })
|
||||||
|
log(`${files.length} avatar images to process.`)
|
||||||
|
for (const file of files) {
|
||||||
|
if (AVATAR_EXTENSION_RE.test(file.name)) {
|
||||||
|
log(`Updating metadata for ${file.name}.`)
|
||||||
|
await file.setMetadata({
|
||||||
|
cacheControl: `public, max-age=${ONE_YEAR_SECS}`,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
log(`Skipping ${file.name} because it probably isn't an avatar.`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (require.main === module) {
|
||||||
|
processAvatars().catch((e) => console.error(e))
|
||||||
|
}
|
|
@ -1,27 +1,35 @@
|
||||||
import * as mailgun from 'mailgun-js'
|
import * as mailgun from 'mailgun-js'
|
||||||
|
import { tryOrLogError } from './utils'
|
||||||
|
|
||||||
const initMailgun = () => {
|
const initMailgun = () => {
|
||||||
const apiKey = process.env.MAILGUN_KEY as string
|
const apiKey = process.env.MAILGUN_KEY as string
|
||||||
return mailgun({ apiKey, domain: 'mg.manifold.markets' })
|
return mailgun({ apiKey, domain: 'mg.manifold.markets' })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const sendTextEmail = (to: string, subject: string, text: string) => {
|
export const sendTextEmail = async (
|
||||||
|
to: string,
|
||||||
|
subject: string,
|
||||||
|
text: string,
|
||||||
|
options?: Partial<mailgun.messages.SendData>
|
||||||
|
) => {
|
||||||
const data: mailgun.messages.SendData = {
|
const data: mailgun.messages.SendData = {
|
||||||
from: 'Manifold Markets <info@manifold.markets>',
|
...options,
|
||||||
|
from: options?.from ?? 'Manifold Markets <info@manifold.markets>',
|
||||||
to,
|
to,
|
||||||
subject,
|
subject,
|
||||||
text,
|
text,
|
||||||
// Don't rewrite urls in plaintext emails
|
// Don't rewrite urls in plaintext emails
|
||||||
'o:tracking-clicks': 'htmlonly',
|
'o:tracking-clicks': 'htmlonly',
|
||||||
}
|
}
|
||||||
const mg = initMailgun()
|
const mg = initMailgun().messages()
|
||||||
return mg.messages().send(data, (error) => {
|
const result = await tryOrLogError(mg.send(data))
|
||||||
if (error) console.log('Error sending email', error)
|
if (result != null) {
|
||||||
else console.log('Sent text email', to, subject)
|
console.log('Sent text email', to, subject)
|
||||||
})
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
export const sendTemplateEmail = (
|
export const sendTemplateEmail = async (
|
||||||
to: string,
|
to: string,
|
||||||
subject: string,
|
subject: string,
|
||||||
templateId: string,
|
templateId: string,
|
||||||
|
@ -35,11 +43,13 @@ export const sendTemplateEmail = (
|
||||||
subject,
|
subject,
|
||||||
template: templateId,
|
template: templateId,
|
||||||
'h:X-Mailgun-Variables': JSON.stringify(templateData),
|
'h:X-Mailgun-Variables': JSON.stringify(templateData),
|
||||||
|
'o:tag': templateId,
|
||||||
|
'o:tracking': true,
|
||||||
}
|
}
|
||||||
const mg = initMailgun()
|
const mg = initMailgun().messages()
|
||||||
|
const result = await tryOrLogError(mg.send(data))
|
||||||
return mg.messages().send(data, (error) => {
|
if (result != null) {
|
||||||
if (error) console.log('Error sending email', error)
|
console.log('Sent template email', templateId, to, subject)
|
||||||
else console.log('Sent template email', templateId, to, subject)
|
}
|
||||||
})
|
return result
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@ import { resolvemarket } from './resolve-market'
|
||||||
import { unsubscribe } from './unsubscribe'
|
import { unsubscribe } from './unsubscribe'
|
||||||
import { stripewebhook, createcheckoutsession } from './stripe'
|
import { stripewebhook, createcheckoutsession } from './stripe'
|
||||||
import { getcurrentuser } from './get-current-user'
|
import { getcurrentuser } from './get-current-user'
|
||||||
|
import { getcustomtoken } from './get-custom-token'
|
||||||
|
|
||||||
type Middleware = (req: Request, res: Response, next: NextFunction) => void
|
type Middleware = (req: Request, res: Response, next: NextFunction) => void
|
||||||
const app = express()
|
const app = express()
|
||||||
|
@ -64,6 +65,7 @@ addJsonEndpointRoute('/resolvemarket', resolvemarket)
|
||||||
addJsonEndpointRoute('/unsubscribe', unsubscribe)
|
addJsonEndpointRoute('/unsubscribe', unsubscribe)
|
||||||
addJsonEndpointRoute('/createcheckoutsession', createcheckoutsession)
|
addJsonEndpointRoute('/createcheckoutsession', createcheckoutsession)
|
||||||
addJsonEndpointRoute('/getcurrentuser', getcurrentuser)
|
addJsonEndpointRoute('/getcurrentuser', getcurrentuser)
|
||||||
|
addEndpointRoute('/getcustomtoken', getcustomtoken)
|
||||||
addEndpointRoute('/stripewebhook', stripewebhook, express.raw())
|
addEndpointRoute('/stripewebhook', stripewebhook, express.raw())
|
||||||
|
|
||||||
app.listen(PORT)
|
app.listen(PORT)
|
||||||
|
|
|
@ -42,6 +42,15 @@ export const writeAsync = async (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const tryOrLogError = async <T>(task: Promise<T>) => {
|
||||||
|
try {
|
||||||
|
return await task
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const isProd = () => {
|
export const isProd = () => {
|
||||||
return admin.instanceId().app.options.projectId === 'mantic-markets'
|
return admin.instanceId().app.options.projectId === 'mantic-markets'
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,32 +1,35 @@
|
||||||
|
# Installing
|
||||||
|
1. `yarn install`
|
||||||
|
2. `yarn start`
|
||||||
|
3. `Y` to `Set up and develop “~path/to/the/repo/manifold”? [Y/n]`
|
||||||
|
4. `Manifold Markets` to `Which scope should contain your project? [Y/n] `
|
||||||
|
5. `Y` to `Link to existing project? [Y/n] `
|
||||||
|
6. `opengraph-image` to `What’s the name of your existing project?`
|
||||||
|
|
||||||
# Quickstart
|
# Quickstart
|
||||||
|
|
||||||
1. To get started: `yarn install`
|
1. To test locally: `yarn start`
|
||||||
2. To test locally: `yarn start`
|
|
||||||
The local image preview is broken for some reason; but the service works.
|
The local image preview is broken for some reason; but the service works.
|
||||||
E.g. try `http://localhost:3000/manifold.png`
|
E.g. try `http://localhost:3000/manifold.png`
|
||||||
3. To deploy: push to Github
|
2. To deploy: push to Github
|
||||||
|
- note: (Not `dev` because that's reserved for Vercel)
|
||||||
For more info, see Contributing.md
|
- note2: (Or `cd .. && vercel --prod`, I think)
|
||||||
|
|
||||||
- note2: You may have to configure Vercel the first time:
|
|
||||||
|
|
||||||
```
|
|
||||||
$ yarn start
|
|
||||||
yarn run v1.22.10
|
|
||||||
$ cd .. && vercel dev
|
|
||||||
Vercel CLI 23.1.2 dev (beta) — https://vercel.com/feedback
|
|
||||||
? Set up and develop “~/Code/mantic”? [Y/n] y
|
|
||||||
? Which scope should contain your project? Mantic Markets
|
|
||||||
? Found project “mantic/mantic”. Link to it? [Y/n] n
|
|
||||||
? Link to different existing project? [Y/n] y
|
|
||||||
? What’s the name of your existing project? manifold-og-image
|
|
||||||
```
|
|
||||||
|
|
||||||
- note2: (Not `dev` because that's reserved for Vercel)
|
|
||||||
- note3: (Or `cd .. && vercel --prod`, I think)
|
|
||||||
|
|
||||||
|
For more info, see Contributing.md
|
||||||
(Everything below is from the original repo)
|
(Everything below is from the original repo)
|
||||||
|
|
||||||
|
# Development
|
||||||
|
- Code of interest is contained in the `api/_lib` directory, i.e. `template.ts` is the page that renders the UI.
|
||||||
|
- Edit `parseRequest(req: IncomingMessage)` in `parser.ts` to add/edit query parameters.
|
||||||
|
- Note: When testing a remote branch on vercel, the og-image previews that apps load will point to
|
||||||
|
`https://manifold-og-image.vercel.app/m.png?question=etc.`, (see relevant code in `SEO.tsx`) and not your remote branch.
|
||||||
|
You have to find your opengraph-image branch's url and replace the part before `m.png` with it.
|
||||||
|
- You can also preview the image locally, e.g. `http://localhost:3000/m.png?question=etc.`
|
||||||
|
- Every time you change the template code you'll have to change the query parameter slightly as the image will likely be cached.
|
||||||
|
- You can find your remote branch's opengraph-image url by click `Visit Preview` on Github:
|
||||||
|
![](../../../../../Desktop/Screen Shot 2022-08-01 at 2.56.42 PM.png)
|
||||||
|
|
||||||
|
|
||||||
# [Open Graph Image as a Service](https://og-image.vercel.app)
|
# [Open Graph Image as a Service](https://og-image.vercel.app)
|
||||||
|
|
||||||
<a href="https://twitter.com/vercel">
|
<a href="https://twitter.com/vercel">
|
||||||
|
|
203
og-image/api/_lib/challenge-template.ts
Normal file
203
og-image/api/_lib/challenge-template.ts
Normal file
|
@ -0,0 +1,203 @@
|
||||||
|
import { sanitizeHtml } from './sanitizer'
|
||||||
|
import { ParsedRequest } from './types'
|
||||||
|
|
||||||
|
function getCss(theme: string, fontSize: string) {
|
||||||
|
let background = 'white'
|
||||||
|
let foreground = 'black'
|
||||||
|
let radial = 'lightgray'
|
||||||
|
|
||||||
|
if (theme === 'dark') {
|
||||||
|
background = 'black'
|
||||||
|
foreground = 'white'
|
||||||
|
radial = 'dimgray'
|
||||||
|
}
|
||||||
|
// To use Readex Pro: `font-family: 'Readex Pro', sans-serif;`
|
||||||
|
return `
|
||||||
|
@import url('https://fonts.googleapis.com/css2?family=Major+Mono+Display&family=Readex+Pro:wght@400;700&display=swap');
|
||||||
|
|
||||||
|
body {
|
||||||
|
background: ${background};
|
||||||
|
background-image: radial-gradient(circle at 25px 25px, ${radial} 2%, transparent 0%), radial-gradient(circle at 75px 75px, ${radial} 2%, transparent 0%);
|
||||||
|
background-size: 100px 100px;
|
||||||
|
height: 100vh;
|
||||||
|
font-family: "Readex Pro", sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
code {
|
||||||
|
color: #D400FF;
|
||||||
|
font-family: 'Vera';
|
||||||
|
white-space: pre-wrap;
|
||||||
|
letter-spacing: -5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
code:before, code:after {
|
||||||
|
content: '\`';
|
||||||
|
}
|
||||||
|
|
||||||
|
.logo-wrapper {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
align-content: center;
|
||||||
|
justify-content: center;
|
||||||
|
justify-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.logo {
|
||||||
|
margin: 0 75px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.plus {
|
||||||
|
color: #BBB;
|
||||||
|
font-family: Times New Roman, Verdana;
|
||||||
|
font-size: 100px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.spacer {
|
||||||
|
margin: 150px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.emoji {
|
||||||
|
height: 1em;
|
||||||
|
width: 1em;
|
||||||
|
margin: 0 .05em 0 .1em;
|
||||||
|
vertical-align: -0.1em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.heading {
|
||||||
|
font-family: 'Major Mono Display', monospace;
|
||||||
|
font-size: ${sanitizeHtml(fontSize)};
|
||||||
|
font-style: normal;
|
||||||
|
color: ${foreground};
|
||||||
|
line-height: 1.8;
|
||||||
|
}
|
||||||
|
|
||||||
|
.font-major-mono {
|
||||||
|
font-family: "Major Mono Display", monospace;
|
||||||
|
}
|
||||||
|
|
||||||
|
.text-primary {
|
||||||
|
color: #11b981;
|
||||||
|
}
|
||||||
|
`
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getChallengeHtml(parsedReq: ParsedRequest) {
|
||||||
|
const {
|
||||||
|
theme,
|
||||||
|
fontSize,
|
||||||
|
question,
|
||||||
|
creatorName,
|
||||||
|
creatorAvatarUrl,
|
||||||
|
challengerAmount,
|
||||||
|
challengerOutcome,
|
||||||
|
creatorAmount,
|
||||||
|
creatorOutcome,
|
||||||
|
acceptedName,
|
||||||
|
acceptedAvatarUrl,
|
||||||
|
} = parsedReq
|
||||||
|
const MAX_QUESTION_CHARS = 78
|
||||||
|
const truncatedQuestion =
|
||||||
|
question.length > MAX_QUESTION_CHARS
|
||||||
|
? question.slice(0, MAX_QUESTION_CHARS) + '...'
|
||||||
|
: question
|
||||||
|
const hideAvatar = creatorAvatarUrl ? '' : 'hidden'
|
||||||
|
const hideAcceptedAvatar = acceptedAvatarUrl ? '' : 'hidden'
|
||||||
|
const accepted = acceptedName !== ''
|
||||||
|
return `<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>Generated Image</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<script src="https://cdn.tailwindcss.com"></script>
|
||||||
|
</head>
|
||||||
|
<style>
|
||||||
|
${getCss(theme, fontSize)}
|
||||||
|
</style>
|
||||||
|
<body>
|
||||||
|
<div class="px-24">
|
||||||
|
|
||||||
|
|
||||||
|
<div class="flex flex-col justify-between gap-16 pt-2">
|
||||||
|
<div class="flex flex-col text-indigo-700 mt-4 text-5xl leading-tight text-center">
|
||||||
|
${truncatedQuestion}
|
||||||
|
</div>
|
||||||
|
<div class="flex flex-row grid grid-cols-3">
|
||||||
|
<div class="flex flex-col justify-center items-center ${
|
||||||
|
creatorOutcome === 'YES' ? 'text-primary' : 'text-red-500'
|
||||||
|
}">
|
||||||
|
|
||||||
|
<!-- Creator user column-->
|
||||||
|
<div class="flex flex-col align-bottom gap-6 items-center justify-center">
|
||||||
|
<p class="text-gray-900 text-4xl">${creatorName}</p>
|
||||||
|
<img
|
||||||
|
class="h-36 w-36 rounded-full bg-white flex items-center justify-center ${hideAvatar}"
|
||||||
|
src="${creatorAvatarUrl}"
|
||||||
|
alt=""
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="flex flex-row justify-center items-center gap-3 mt-6">
|
||||||
|
<div class="text-5xl">${'M$' + creatorAmount}</div>
|
||||||
|
<div class="text-4xl">${'on'}</div>
|
||||||
|
<div class="text-5xl ">${creatorOutcome}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- VS-->
|
||||||
|
<div class="flex flex-col text-gray-900 text-6xl mt-8 text-center">
|
||||||
|
VS
|
||||||
|
</div>
|
||||||
|
<div class="flex flex-col justify-center items-center ${
|
||||||
|
challengerOutcome === 'YES' ? 'text-primary' : 'text-red-500'
|
||||||
|
}">
|
||||||
|
|
||||||
|
<!-- Unaccepted user column-->
|
||||||
|
<div class="flex flex-col align-bottom gap-6 items-center justify-center
|
||||||
|
${accepted ? 'hidden' : ''}">
|
||||||
|
<p class="text-gray-900 text-4xl">You</p>
|
||||||
|
<img
|
||||||
|
class="h-36 w-36 rounded-full bg-white flex items-center justify-center "
|
||||||
|
src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png"
|
||||||
|
alt=""
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<!-- Accepted user column-->
|
||||||
|
<div class="flex flex-col align-bottom gap-6 items-center justify-center">
|
||||||
|
<p class="text-gray-900 text-4xl">${acceptedName}</p>
|
||||||
|
<img
|
||||||
|
class="h-36 w-36 rounded-full bg-white flex items-center justify-center ${hideAcceptedAvatar}"
|
||||||
|
src="${acceptedAvatarUrl}"
|
||||||
|
alt=""
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="flex flex-row justify-center items-center gap-3 mt-6">
|
||||||
|
<div class="text-5xl">${'M$' + challengerAmount}</div>
|
||||||
|
<div class="text-4xl">${'on'}</div>
|
||||||
|
<div class="text-5xl ">${challengerOutcome}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<!-- Manifold logo -->
|
||||||
|
<div class="flex flex-row justify-center absolute bottom-4 left-[24rem]">
|
||||||
|
<a class="flex flex-row gap-3" href="/">
|
||||||
|
<img
|
||||||
|
class="sm:h-12 sm:w-12"
|
||||||
|
src="https://manifold.markets/logo.png"
|
||||||
|
width="40"
|
||||||
|
height="40"
|
||||||
|
alt=''
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
class="hidden sm:flex font-major-mono lowercase mt-1 sm:text-3xl md:whitespace-nowrap"
|
||||||
|
>
|
||||||
|
Manifold Markets
|
||||||
|
</div></a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>`
|
||||||
|
}
|
|
@ -16,10 +16,19 @@ export function parseRequest(req: IncomingMessage) {
|
||||||
// Attributes for Manifold card:
|
// Attributes for Manifold card:
|
||||||
question,
|
question,
|
||||||
probability,
|
probability,
|
||||||
|
numericValue,
|
||||||
metadata,
|
metadata,
|
||||||
creatorName,
|
creatorName,
|
||||||
creatorUsername,
|
creatorUsername,
|
||||||
creatorAvatarUrl,
|
creatorAvatarUrl,
|
||||||
|
|
||||||
|
// Challenge attributes:
|
||||||
|
challengerAmount,
|
||||||
|
challengerOutcome,
|
||||||
|
creatorAmount,
|
||||||
|
creatorOutcome,
|
||||||
|
acceptedName,
|
||||||
|
acceptedAvatarUrl,
|
||||||
} = query || {}
|
} = query || {}
|
||||||
|
|
||||||
if (Array.isArray(fontSize)) {
|
if (Array.isArray(fontSize)) {
|
||||||
|
@ -63,10 +72,17 @@ export function parseRequest(req: IncomingMessage) {
|
||||||
question:
|
question:
|
||||||
getString(question) || 'Will you create a prediction market on Manifold?',
|
getString(question) || 'Will you create a prediction market on Manifold?',
|
||||||
probability: getString(probability),
|
probability: getString(probability),
|
||||||
|
numericValue: getString(numericValue) || '',
|
||||||
metadata: getString(metadata) || 'Jan 1 • M$ 123 pool',
|
metadata: getString(metadata) || 'Jan 1 • M$ 123 pool',
|
||||||
creatorName: getString(creatorName) || 'Manifold Markets',
|
creatorName: getString(creatorName) || 'Manifold Markets',
|
||||||
creatorUsername: getString(creatorUsername) || 'ManifoldMarkets',
|
creatorUsername: getString(creatorUsername) || 'ManifoldMarkets',
|
||||||
creatorAvatarUrl: getString(creatorAvatarUrl) || '',
|
creatorAvatarUrl: getString(creatorAvatarUrl) || '',
|
||||||
|
challengerAmount: getString(challengerAmount) || '',
|
||||||
|
challengerOutcome: getString(challengerOutcome) || '',
|
||||||
|
creatorAmount: getString(creatorAmount) || '',
|
||||||
|
creatorOutcome: getString(creatorOutcome) || '',
|
||||||
|
acceptedName: getString(acceptedName) || '',
|
||||||
|
acceptedAvatarUrl: getString(acceptedAvatarUrl) || '',
|
||||||
}
|
}
|
||||||
parsedRequest.images = getDefaultImages(parsedRequest.images)
|
parsedRequest.images = getDefaultImages(parsedRequest.images)
|
||||||
return parsedRequest
|
return parsedRequest
|
||||||
|
|
|
@ -91,6 +91,7 @@ export function getHtml(parsedReq: ParsedRequest) {
|
||||||
creatorName,
|
creatorName,
|
||||||
creatorUsername,
|
creatorUsername,
|
||||||
creatorAvatarUrl,
|
creatorAvatarUrl,
|
||||||
|
numericValue,
|
||||||
} = parsedReq
|
} = parsedReq
|
||||||
const MAX_QUESTION_CHARS = 100
|
const MAX_QUESTION_CHARS = 100
|
||||||
const truncatedQuestion =
|
const truncatedQuestion =
|
||||||
|
@ -126,7 +127,7 @@ export function getHtml(parsedReq: ParsedRequest) {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Mantic logo -->
|
<!-- Manifold logo -->
|
||||||
<div class="absolute right-24 top-8">
|
<div class="absolute right-24 top-8">
|
||||||
<a class="flex flex-row gap-3" href="/"
|
<a class="flex flex-row gap-3" href="/"
|
||||||
><img
|
><img
|
||||||
|
@ -150,6 +151,12 @@ export function getHtml(parsedReq: ParsedRequest) {
|
||||||
<div class="flex flex-col text-primary">
|
<div class="flex flex-col text-primary">
|
||||||
<div class="text-8xl">${probability}</div>
|
<div class="text-8xl">${probability}</div>
|
||||||
<div class="text-4xl">${probability !== '' ? 'chance' : ''}</div>
|
<div class="text-4xl">${probability !== '' ? 'chance' : ''}</div>
|
||||||
|
<span class='text-blue-500 text-center'>
|
||||||
|
<div class="text-8xl ">${
|
||||||
|
numericValue !== '' && probability === '' ? numericValue : ''
|
||||||
|
}</div>
|
||||||
|
<div class="text-4xl">${numericValue !== '' ? 'expected' : ''}</div>
|
||||||
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
@ -1,21 +1,29 @@
|
||||||
export type FileType = "png" | "jpeg";
|
export type FileType = 'png' | 'jpeg'
|
||||||
export type Theme = "light" | "dark";
|
export type Theme = 'light' | 'dark'
|
||||||
|
|
||||||
export interface ParsedRequest {
|
export interface ParsedRequest {
|
||||||
fileType: FileType;
|
fileType: FileType
|
||||||
text: string;
|
text: string
|
||||||
theme: Theme;
|
theme: Theme
|
||||||
md: boolean;
|
md: boolean
|
||||||
fontSize: string;
|
fontSize: string
|
||||||
images: string[];
|
images: string[]
|
||||||
widths: string[];
|
widths: string[]
|
||||||
heights: string[];
|
heights: string[]
|
||||||
|
|
||||||
// Attributes for Manifold card:
|
// Attributes for Manifold card:
|
||||||
question: string;
|
question: string
|
||||||
probability: string;
|
probability: string
|
||||||
metadata: string;
|
numericValue: string
|
||||||
creatorName: string;
|
metadata: string
|
||||||
creatorUsername: string;
|
creatorName: string
|
||||||
creatorAvatarUrl: string;
|
creatorUsername: string
|
||||||
|
creatorAvatarUrl: string
|
||||||
|
// Challenge attributes:
|
||||||
|
challengerAmount: string
|
||||||
|
challengerOutcome: string
|
||||||
|
creatorAmount: string
|
||||||
|
creatorOutcome: string
|
||||||
|
acceptedName: string
|
||||||
|
acceptedAvatarUrl: string
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,36 +1,38 @@
|
||||||
import { IncomingMessage, ServerResponse } from "http";
|
import { IncomingMessage, ServerResponse } from 'http'
|
||||||
import { parseRequest } from "./_lib/parser";
|
import { parseRequest } from './_lib/parser'
|
||||||
import { getScreenshot } from "./_lib/chromium";
|
import { getScreenshot } from './_lib/chromium'
|
||||||
import { getHtml } from "./_lib/template";
|
import { getHtml } from './_lib/template'
|
||||||
|
import { getChallengeHtml } from './_lib/challenge-template'
|
||||||
|
|
||||||
const isDev = !process.env.AWS_REGION;
|
const isDev = !process.env.AWS_REGION
|
||||||
const isHtmlDebug = process.env.OG_HTML_DEBUG === "1";
|
const isHtmlDebug = process.env.OG_HTML_DEBUG === '1'
|
||||||
|
|
||||||
export default async function handler(
|
export default async function handler(
|
||||||
req: IncomingMessage,
|
req: IncomingMessage,
|
||||||
res: ServerResponse
|
res: ServerResponse
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const parsedReq = parseRequest(req);
|
const parsedReq = parseRequest(req)
|
||||||
const html = getHtml(parsedReq);
|
let html = getHtml(parsedReq)
|
||||||
|
if (parsedReq.challengerOutcome) html = getChallengeHtml(parsedReq)
|
||||||
if (isHtmlDebug) {
|
if (isHtmlDebug) {
|
||||||
res.setHeader("Content-Type", "text/html");
|
res.setHeader('Content-Type', 'text/html')
|
||||||
res.end(html);
|
res.end(html)
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
const { fileType } = parsedReq;
|
const { fileType } = parsedReq
|
||||||
const file = await getScreenshot(html, fileType, isDev);
|
const file = await getScreenshot(html, fileType, isDev)
|
||||||
res.statusCode = 200;
|
res.statusCode = 200
|
||||||
res.setHeader("Content-Type", `image/${fileType}`);
|
res.setHeader('Content-Type', `image/${fileType}`)
|
||||||
res.setHeader(
|
res.setHeader(
|
||||||
"Cache-Control",
|
'Cache-Control',
|
||||||
`public, immutable, no-transform, s-maxage=31536000, max-age=31536000`
|
`public, immutable, no-transform, s-maxage=31536000, max-age=31536000`
|
||||||
);
|
)
|
||||||
res.end(file);
|
res.end(file)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
res.statusCode = 500;
|
res.statusCode = 500
|
||||||
res.setHeader("Content-Type", "text/html");
|
res.setHeader('Content-Type', 'text/html')
|
||||||
res.end("<h1>Internal Error</h1><p>Sorry, there was a problem</p>");
|
res.end('<h1>Internal Error</h1><p>Sorry, there was a problem</p>')
|
||||||
console.error(e);
|
console.error(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@typescript-eslint/eslint-plugin": "5.25.0",
|
"@typescript-eslint/eslint-plugin": "5.25.0",
|
||||||
"@typescript-eslint/parser": "5.25.0",
|
"@typescript-eslint/parser": "5.25.0",
|
||||||
|
"@types/node": "16.11.11",
|
||||||
"concurrently": "6.5.1",
|
"concurrently": "6.5.1",
|
||||||
"eslint": "8.15.0",
|
"eslint": "8.15.0",
|
||||||
"eslint-plugin-lodash": "^7.4.0",
|
"eslint-plugin-lodash": "^7.4.0",
|
||||||
|
|
|
@ -5,6 +5,7 @@ module.exports = {
|
||||||
'plugin:@typescript-eslint/recommended',
|
'plugin:@typescript-eslint/recommended',
|
||||||
'plugin:react-hooks/recommended',
|
'plugin:react-hooks/recommended',
|
||||||
'plugin:@next/next/recommended',
|
'plugin:@next/next/recommended',
|
||||||
|
'prettier',
|
||||||
],
|
],
|
||||||
rules: {
|
rules: {
|
||||||
'@typescript-eslint/no-empty-function': 'off',
|
'@typescript-eslint/no-empty-function': 'off',
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { ReactNode } from 'react'
|
import { ReactNode } from 'react'
|
||||||
import Head from 'next/head'
|
import Head from 'next/head'
|
||||||
|
import { Challenge } from 'common/challenge'
|
||||||
|
|
||||||
export type OgCardProps = {
|
export type OgCardProps = {
|
||||||
question: string
|
question: string
|
||||||
|
@ -8,27 +9,51 @@ export type OgCardProps = {
|
||||||
creatorName: string
|
creatorName: string
|
||||||
creatorUsername: string
|
creatorUsername: string
|
||||||
creatorAvatarUrl?: string
|
creatorAvatarUrl?: string
|
||||||
|
numericValue?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildCardUrl(props: OgCardProps) {
|
function buildCardUrl(props: OgCardProps, challenge?: Challenge) {
|
||||||
|
const {
|
||||||
|
creatorAmount,
|
||||||
|
acceptances,
|
||||||
|
acceptorAmount,
|
||||||
|
creatorOutcome,
|
||||||
|
acceptorOutcome,
|
||||||
|
} = challenge || {}
|
||||||
|
const { userName, userAvatarUrl } = acceptances?.[0] ?? {}
|
||||||
|
|
||||||
const probabilityParam =
|
const probabilityParam =
|
||||||
props.probability === undefined
|
props.probability === undefined
|
||||||
? ''
|
? ''
|
||||||
: `&probability=${encodeURIComponent(props.probability ?? '')}`
|
: `&probability=${encodeURIComponent(props.probability ?? '')}`
|
||||||
|
|
||||||
|
const numericValueParam =
|
||||||
|
props.numericValue === undefined
|
||||||
|
? ''
|
||||||
|
: `&numericValue=${encodeURIComponent(props.numericValue ?? '')}`
|
||||||
|
|
||||||
const creatorAvatarUrlParam =
|
const creatorAvatarUrlParam =
|
||||||
props.creatorAvatarUrl === undefined
|
props.creatorAvatarUrl === undefined
|
||||||
? ''
|
? ''
|
||||||
: `&creatorAvatarUrl=${encodeURIComponent(props.creatorAvatarUrl ?? '')}`
|
: `&creatorAvatarUrl=${encodeURIComponent(props.creatorAvatarUrl ?? '')}`
|
||||||
|
|
||||||
|
const challengeUrlParams = challenge
|
||||||
|
? `&creatorAmount=${creatorAmount}&creatorOutcome=${creatorOutcome}` +
|
||||||
|
`&challengerAmount=${acceptorAmount}&challengerOutcome=${acceptorOutcome}` +
|
||||||
|
`&acceptedName=${userName ?? ''}&acceptedAvatarUrl=${userAvatarUrl ?? ''}`
|
||||||
|
: ''
|
||||||
|
|
||||||
// URL encode each of the props, then add them as query params
|
// URL encode each of the props, then add them as query params
|
||||||
return (
|
return (
|
||||||
`https://manifold-og-image.vercel.app/m.png` +
|
`https://manifold-og-image.vercel.app/m.png` +
|
||||||
`?question=${encodeURIComponent(props.question)}` +
|
`?question=${encodeURIComponent(props.question)}` +
|
||||||
probabilityParam +
|
probabilityParam +
|
||||||
|
numericValueParam +
|
||||||
`&metadata=${encodeURIComponent(props.metadata)}` +
|
`&metadata=${encodeURIComponent(props.metadata)}` +
|
||||||
`&creatorName=${encodeURIComponent(props.creatorName)}` +
|
`&creatorName=${encodeURIComponent(props.creatorName)}` +
|
||||||
creatorAvatarUrlParam +
|
creatorAvatarUrlParam +
|
||||||
`&creatorUsername=${encodeURIComponent(props.creatorUsername)}`
|
`&creatorUsername=${encodeURIComponent(props.creatorUsername)}` +
|
||||||
|
challengeUrlParams
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,8 +63,9 @@ export function SEO(props: {
|
||||||
url?: string
|
url?: string
|
||||||
children?: ReactNode
|
children?: ReactNode
|
||||||
ogCardProps?: OgCardProps
|
ogCardProps?: OgCardProps
|
||||||
|
challenge?: Challenge
|
||||||
}) {
|
}) {
|
||||||
const { title, description, url, children, ogCardProps } = props
|
const { title, description, url, children, ogCardProps, challenge } = props
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Head>
|
<Head>
|
||||||
|
@ -71,13 +97,13 @@ export function SEO(props: {
|
||||||
<>
|
<>
|
||||||
<meta
|
<meta
|
||||||
property="og:image"
|
property="og:image"
|
||||||
content={buildCardUrl(ogCardProps)}
|
content={buildCardUrl(ogCardProps, challenge)}
|
||||||
key="image1"
|
key="image1"
|
||||||
/>
|
/>
|
||||||
<meta name="twitter:card" content="summary_large_image" key="card" />
|
<meta name="twitter:card" content="summary_large_image" key="card" />
|
||||||
<meta
|
<meta
|
||||||
name="twitter:image"
|
name="twitter:image"
|
||||||
content={buildCardUrl(ogCardProps)}
|
content={buildCardUrl(ogCardProps, challenge)}
|
||||||
key="image2"
|
key="image2"
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
|
|
|
@ -26,6 +26,7 @@ import { Bet } from 'common/bet'
|
||||||
import { track } from 'web/lib/service/analytics'
|
import { track } from 'web/lib/service/analytics'
|
||||||
import { SignUpPrompt } from '../sign-up-prompt'
|
import { SignUpPrompt } from '../sign-up-prompt'
|
||||||
import { isIOS } from 'web/lib/util/device'
|
import { isIOS } from 'web/lib/util/device'
|
||||||
|
import { AlertBox } from '../alert-box'
|
||||||
|
|
||||||
export function AnswerBetPanel(props: {
|
export function AnswerBetPanel(props: {
|
||||||
answer: Answer
|
answer: Answer
|
||||||
|
@ -113,6 +114,8 @@ export function AnswerBetPanel(props: {
|
||||||
const currentReturn = betAmount ? (currentPayout - betAmount) / betAmount : 0
|
const currentReturn = betAmount ? (currentPayout - betAmount) / betAmount : 0
|
||||||
const currentReturnPercent = formatPercent(currentReturn)
|
const currentReturnPercent = formatPercent(currentReturn)
|
||||||
|
|
||||||
|
const bankrollFraction = (betAmount ?? 0) / (user?.balance ?? 1e9)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Col className={clsx('px-2 pb-2 pt-4 sm:pt-0', className)}>
|
<Col className={clsx('px-2 pb-2 pt-4 sm:pt-0', className)}>
|
||||||
<Row className="items-center justify-between self-stretch">
|
<Row className="items-center justify-between self-stretch">
|
||||||
|
@ -139,6 +142,22 @@ export function AnswerBetPanel(props: {
|
||||||
disabled={isSubmitting}
|
disabled={isSubmitting}
|
||||||
inputRef={inputRef}
|
inputRef={inputRef}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{(betAmount ?? 0) > 10 &&
|
||||||
|
bankrollFraction >= 0.5 &&
|
||||||
|
bankrollFraction <= 1 ? (
|
||||||
|
<AlertBox
|
||||||
|
title="Whoa, there!"
|
||||||
|
text={`You might not want to spend ${formatPercent(
|
||||||
|
bankrollFraction
|
||||||
|
)} of your balance on a single bet. \n\nCurrent balance: ${formatMoney(
|
||||||
|
user?.balance ?? 0
|
||||||
|
)}`}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
''
|
||||||
|
)}
|
||||||
|
|
||||||
<Col className="mt-3 w-full gap-3">
|
<Col className="mt-3 w-full gap-3">
|
||||||
<Row className="items-center justify-between text-sm">
|
<Row className="items-center justify-between text-sm">
|
||||||
<div className="text-gray-500">Probability</div>
|
<div className="text-gray-500">Probability</div>
|
||||||
|
|
|
@ -1,26 +1,23 @@
|
||||||
import { MAX_ANSWER_LENGTH } from 'common/answer'
|
import { MAX_ANSWER_LENGTH } from 'common/answer'
|
||||||
import { useState } from 'react'
|
|
||||||
import Textarea from 'react-expanding-textarea'
|
import Textarea from 'react-expanding-textarea'
|
||||||
import { XIcon } from '@heroicons/react/solid'
|
import { XIcon } from '@heroicons/react/solid'
|
||||||
|
|
||||||
import { Col } from '../layout/col'
|
import { Col } from '../layout/col'
|
||||||
import { Row } from '../layout/row'
|
import { Row } from '../layout/row'
|
||||||
|
|
||||||
export function MultipleChoiceAnswers(props: {
|
export function MultipleChoiceAnswers(props: {
|
||||||
|
answers: string[]
|
||||||
setAnswers: (answers: string[]) => void
|
setAnswers: (answers: string[]) => void
|
||||||
}) {
|
}) {
|
||||||
const [answers, setInternalAnswers] = useState(['', '', ''])
|
const { answers, setAnswers } = props
|
||||||
|
|
||||||
const setAnswer = (i: number, answer: string) => {
|
const setAnswer = (i: number, answer: string) => {
|
||||||
const newAnswers = setElement(answers, i, answer)
|
const newAnswers = setElement(answers, i, answer)
|
||||||
setInternalAnswers(newAnswers)
|
setAnswers(newAnswers)
|
||||||
props.setAnswers(newAnswers)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const removeAnswer = (i: number) => {
|
const removeAnswer = (i: number) => {
|
||||||
const newAnswers = answers.slice(0, i).concat(answers.slice(i + 1))
|
const newAnswers = answers.slice(0, i).concat(answers.slice(i + 1))
|
||||||
setInternalAnswers(newAnswers)
|
setAnswers(newAnswers)
|
||||||
props.setAnswers(newAnswers)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const addAnswer = () => setAnswer(answers.length, '')
|
const addAnswer = () => setAnswer(answers.length, '')
|
||||||
|
@ -28,7 +25,7 @@ export function MultipleChoiceAnswers(props: {
|
||||||
return (
|
return (
|
||||||
<Col>
|
<Col>
|
||||||
{answers.map((answer, i) => (
|
{answers.map((answer, i) => (
|
||||||
<Row className="mb-2 items-center align-middle">
|
<Row className="mb-2 items-center gap-2 align-middle">
|
||||||
{i + 1}.{' '}
|
{i + 1}.{' '}
|
||||||
<Textarea
|
<Textarea
|
||||||
value={answer}
|
value={answer}
|
||||||
|
@ -40,17 +37,22 @@ export function MultipleChoiceAnswers(props: {
|
||||||
/>
|
/>
|
||||||
{answers.length > 2 && (
|
{answers.length > 2 && (
|
||||||
<button
|
<button
|
||||||
className="btn btn-xs btn-outline ml-2"
|
|
||||||
onClick={() => removeAnswer(i)}
|
onClick={() => removeAnswer(i)}
|
||||||
|
type="button"
|
||||||
|
className="inline-flex items-center rounded-full border border-gray-300 bg-white p-1 text-xs font-medium text-gray-700 shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
|
||||||
>
|
>
|
||||||
<XIcon className="h-4 w-4 flex-shrink-0" />
|
<XIcon className="h-5 w-5" aria-hidden="true" />
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
</Row>
|
</Row>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
<Row className="justify-end">
|
<Row className="justify-end">
|
||||||
<button className="btn btn-outline btn-xs" onClick={addAnswer}>
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={addAnswer}
|
||||||
|
className="inline-flex items-center rounded border border-gray-300 bg-white px-2.5 py-1.5 text-xs font-medium text-gray-700 shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
|
||||||
|
>
|
||||||
Add answer
|
Add answer
|
||||||
</button>
|
</button>
|
||||||
</Row>
|
</Row>
|
||||||
|
|
|
@ -1,23 +1,24 @@
|
||||||
import { createContext, useEffect } from 'react'
|
import { ReactNode, createContext, useEffect } from 'react'
|
||||||
import { User } from 'common/user'
|
|
||||||
import { onIdTokenChanged } from 'firebase/auth'
|
import { onIdTokenChanged } from 'firebase/auth'
|
||||||
import {
|
import {
|
||||||
|
UserAndPrivateUser,
|
||||||
auth,
|
auth,
|
||||||
listenForUser,
|
listenForUser,
|
||||||
getUser,
|
listenForPrivateUser,
|
||||||
|
getUserAndPrivateUser,
|
||||||
setCachedReferralInfoForUser,
|
setCachedReferralInfoForUser,
|
||||||
} from 'web/lib/firebase/users'
|
} from 'web/lib/firebase/users'
|
||||||
import { deleteAuthCookies, setAuthCookies } from 'web/lib/firebase/auth'
|
import { deleteTokenCookies, setTokenCookies } from 'web/lib/firebase/auth'
|
||||||
import { createUser } from 'web/lib/firebase/api'
|
import { createUser } from 'web/lib/firebase/api'
|
||||||
import { randomString } from 'common/util/random'
|
import { randomString } from 'common/util/random'
|
||||||
import { identifyUser, setUserProperty } from 'web/lib/service/analytics'
|
import { identifyUser, setUserProperty } from 'web/lib/service/analytics'
|
||||||
import { useStateCheckEquality } from 'web/hooks/use-state-check-equality'
|
import { useStateCheckEquality } from 'web/hooks/use-state-check-equality'
|
||||||
|
|
||||||
// Either we haven't looked up the logged in user yet (undefined), or we know
|
// Either we haven't looked up the logged in user yet (undefined), or we know
|
||||||
// the user is not logged in (null), or we know the user is logged in (User).
|
// the user is not logged in (null), or we know the user is logged in.
|
||||||
type AuthUser = undefined | null | User
|
type AuthUser = undefined | null | UserAndPrivateUser
|
||||||
|
|
||||||
const CACHED_USER_KEY = 'CACHED_USER_KEY'
|
const CACHED_USER_KEY = 'CACHED_USER_KEY_V2'
|
||||||
|
|
||||||
const ensureDeviceToken = () => {
|
const ensureDeviceToken = () => {
|
||||||
let deviceToken = localStorage.getItem('device-token')
|
let deviceToken = localStorage.getItem('device-token')
|
||||||
|
@ -28,48 +29,72 @@ const ensureDeviceToken = () => {
|
||||||
return deviceToken
|
return deviceToken
|
||||||
}
|
}
|
||||||
|
|
||||||
export const AuthContext = createContext<AuthUser>(null)
|
export const AuthContext = createContext<AuthUser>(undefined)
|
||||||
|
|
||||||
export function AuthProvider({ children }: any) {
|
export function AuthProvider(props: {
|
||||||
const [authUser, setAuthUser] = useStateCheckEquality<AuthUser>(undefined)
|
children: ReactNode
|
||||||
|
serverUser?: AuthUser
|
||||||
|
}) {
|
||||||
|
const { children, serverUser } = props
|
||||||
|
const [authUser, setAuthUser] = useStateCheckEquality<AuthUser>(serverUser)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const cachedUser = localStorage.getItem(CACHED_USER_KEY)
|
if (serverUser === undefined) {
|
||||||
setAuthUser(cachedUser && JSON.parse(cachedUser))
|
const cachedUser = localStorage.getItem(CACHED_USER_KEY)
|
||||||
}, [setAuthUser])
|
setAuthUser(cachedUser && JSON.parse(cachedUser))
|
||||||
|
}
|
||||||
|
}, [setAuthUser, serverUser])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return onIdTokenChanged(auth, async (fbUser) => {
|
return onIdTokenChanged(auth, async (fbUser) => {
|
||||||
if (fbUser) {
|
if (fbUser) {
|
||||||
setAuthCookies(await fbUser.getIdToken(), fbUser.refreshToken)
|
setTokenCookies({
|
||||||
let user = await getUser(fbUser.uid)
|
id: await fbUser.getIdToken(),
|
||||||
if (!user) {
|
refresh: fbUser.refreshToken,
|
||||||
|
})
|
||||||
|
let current = await getUserAndPrivateUser(fbUser.uid)
|
||||||
|
if (!current.user || !current.privateUser) {
|
||||||
const deviceToken = ensureDeviceToken()
|
const deviceToken = ensureDeviceToken()
|
||||||
user = (await createUser({ deviceToken })) as User
|
current = (await createUser({ deviceToken })) as UserAndPrivateUser
|
||||||
}
|
}
|
||||||
setAuthUser(user)
|
setAuthUser(current)
|
||||||
// Persist to local storage, to reduce login blink next time.
|
// Persist to local storage, to reduce login blink next time.
|
||||||
// Note: Cap on localStorage size is ~5mb
|
// Note: Cap on localStorage size is ~5mb
|
||||||
localStorage.setItem(CACHED_USER_KEY, JSON.stringify(user))
|
localStorage.setItem(CACHED_USER_KEY, JSON.stringify(current))
|
||||||
setCachedReferralInfoForUser(user)
|
setCachedReferralInfoForUser(current.user)
|
||||||
} else {
|
} else {
|
||||||
// User logged out; reset to null
|
// User logged out; reset to null
|
||||||
deleteAuthCookies()
|
deleteTokenCookies()
|
||||||
setAuthUser(null)
|
setAuthUser(null)
|
||||||
localStorage.removeItem(CACHED_USER_KEY)
|
localStorage.removeItem(CACHED_USER_KEY)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}, [setAuthUser])
|
}, [setAuthUser])
|
||||||
|
|
||||||
const authUserId = authUser?.id
|
const uid = authUser?.user.id
|
||||||
const authUsername = authUser?.username
|
const username = authUser?.user.username
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (authUserId && authUsername) {
|
if (uid && username) {
|
||||||
identifyUser(authUserId)
|
identifyUser(uid)
|
||||||
setUserProperty('username', authUsername)
|
setUserProperty('username', username)
|
||||||
return listenForUser(authUserId, setAuthUser)
|
const userListener = listenForUser(uid, (user) =>
|
||||||
|
setAuthUser((authUser) => {
|
||||||
|
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
|
||||||
|
return { ...authUser!, user: user! }
|
||||||
|
})
|
||||||
|
)
|
||||||
|
const privateUserListener = listenForPrivateUser(uid, (privateUser) => {
|
||||||
|
setAuthUser((authUser) => {
|
||||||
|
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
|
||||||
|
return { ...authUser!, privateUser: privateUser! }
|
||||||
|
})
|
||||||
|
})
|
||||||
|
return () => {
|
||||||
|
userListener()
|
||||||
|
privateUserListener()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, [authUserId, authUsername, setAuthUser])
|
}, [uid, username, setAuthUser])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AuthContext.Provider value={authUser}>{children}</AuthContext.Provider>
|
<AuthContext.Provider value={authUser}>{children}</AuthContext.Provider>
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import Router from 'next/router'
|
import Router from 'next/router'
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import { MouseEvent } from 'react'
|
import { MouseEvent, useState } from 'react'
|
||||||
import { UserCircleIcon, UserIcon, UsersIcon } from '@heroicons/react/solid'
|
import { UserCircleIcon, UserIcon, UsersIcon } from '@heroicons/react/solid'
|
||||||
|
|
||||||
export function Avatar(props: {
|
export function Avatar(props: {
|
||||||
|
@ -10,7 +10,8 @@ export function Avatar(props: {
|
||||||
size?: number | 'xs' | 'sm'
|
size?: number | 'xs' | 'sm'
|
||||||
className?: string
|
className?: string
|
||||||
}) {
|
}) {
|
||||||
const { username, avatarUrl, noLink, size, className } = props
|
const { username, noLink, size, className } = props
|
||||||
|
const [avatarUrl, setAvatarUrl] = useState(props.avatarUrl)
|
||||||
const s = size == 'xs' ? 6 : size === 'sm' ? 8 : size || 10
|
const s = size == 'xs' ? 6 : size === 'sm' ? 8 : size || 10
|
||||||
|
|
||||||
const onClick =
|
const onClick =
|
||||||
|
@ -35,6 +36,11 @@ export function Avatar(props: {
|
||||||
src={avatarUrl}
|
src={avatarUrl}
|
||||||
onClick={onClick}
|
onClick={onClick}
|
||||||
alt={username}
|
alt={username}
|
||||||
|
onError={() => {
|
||||||
|
// If the image doesn't load, clear the avatarUrl to show the default
|
||||||
|
// Mostly for localhost, when getting a 403 from googleusercontent
|
||||||
|
setAvatarUrl('')
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<UserCircleIcon
|
<UserCircleIcon
|
||||||
|
@ -47,14 +53,21 @@ export function Avatar(props: {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function EmptyAvatar(props: { size?: number; multi?: boolean }) {
|
export function EmptyAvatar(props: {
|
||||||
const { size = 8, multi } = props
|
className?: string
|
||||||
|
size?: number
|
||||||
|
multi?: boolean
|
||||||
|
}) {
|
||||||
|
const { className, size = 8, multi } = props
|
||||||
const insize = size - 3
|
const insize = size - 3
|
||||||
const Icon = multi ? UsersIcon : UserIcon
|
const Icon = multi ? UsersIcon : UserIcon
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={`flex flex-shrink-0 h-${size} w-${size} items-center justify-center rounded-full bg-gray-200`}
|
className={clsx(
|
||||||
|
`flex flex-shrink-0 h-${size} w-${size} items-center justify-center rounded-full bg-gray-200`,
|
||||||
|
className
|
||||||
|
)}
|
||||||
>
|
>
|
||||||
<Icon className={`h-${insize} w-${insize} text-gray-500`} aria-hidden />
|
<Icon className={`h-${insize} w-${insize} text-gray-500`} aria-hidden />
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -16,8 +16,7 @@ import {
|
||||||
import { getBinaryBetStats, getBinaryCpmmBetInfo } from 'common/new-bet'
|
import { getBinaryBetStats, getBinaryCpmmBetInfo } from 'common/new-bet'
|
||||||
import { User } from 'web/lib/firebase/users'
|
import { User } from 'web/lib/firebase/users'
|
||||||
import { Bet, LimitBet } from 'common/bet'
|
import { Bet, LimitBet } from 'common/bet'
|
||||||
import { APIError, placeBet } from 'web/lib/firebase/api'
|
import { APIError, placeBet, sellShares } from 'web/lib/firebase/api'
|
||||||
import { sellShares } from 'web/lib/firebase/api'
|
|
||||||
import { AmountInput, BuyAmountInput } from './amount-input'
|
import { AmountInput, BuyAmountInput } from './amount-input'
|
||||||
import { InfoTooltip } from './info-tooltip'
|
import { InfoTooltip } from './info-tooltip'
|
||||||
import {
|
import {
|
||||||
|
@ -255,6 +254,7 @@ function BuyPanel(props: {
|
||||||
const resultProb = getCpmmProbability(newPool, newP)
|
const resultProb = getCpmmProbability(newPool, newP)
|
||||||
const probStayedSame =
|
const probStayedSame =
|
||||||
formatPercent(resultProb) === formatPercent(initialProb)
|
formatPercent(resultProb) === formatPercent(initialProb)
|
||||||
|
const probChange = Math.abs(resultProb - initialProb)
|
||||||
|
|
||||||
const currentPayout = newBet.shares
|
const currentPayout = newBet.shares
|
||||||
|
|
||||||
|
@ -306,6 +306,19 @@ function BuyPanel(props: {
|
||||||
''
|
''
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{(betAmount ?? 0) > 10 && probChange >= 0.3 ? (
|
||||||
|
<AlertBox
|
||||||
|
title="Whoa, there!"
|
||||||
|
text={`Are you sure you want to move the market ${
|
||||||
|
isPseudoNumeric && contract.isLogScale
|
||||||
|
? 'this much'
|
||||||
|
: format(probChange)
|
||||||
|
}?`}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
''
|
||||||
|
)}
|
||||||
|
|
||||||
<Col className="mt-3 w-full gap-3">
|
<Col className="mt-3 w-full gap-3">
|
||||||
<Row className="items-center justify-between text-sm">
|
<Row className="items-center justify-between text-sm">
|
||||||
<div className="text-gray-500">
|
<div className="text-gray-500">
|
||||||
|
@ -351,7 +364,7 @@ function BuyPanel(props: {
|
||||||
{user && (
|
{user && (
|
||||||
<button
|
<button
|
||||||
className={clsx(
|
className={clsx(
|
||||||
'btn flex-1',
|
'btn mb-2 flex-1',
|
||||||
betDisabled
|
betDisabled
|
||||||
? 'btn-disabled'
|
? 'btn-disabled'
|
||||||
: outcome === 'YES'
|
: outcome === 'YES'
|
||||||
|
@ -435,8 +448,6 @@ function LimitOrderPanel(props: {
|
||||||
const yesAmount = shares * (yesLimitProb ?? 1)
|
const yesAmount = shares * (yesLimitProb ?? 1)
|
||||||
const noAmount = shares * (1 - (noLimitProb ?? 0))
|
const noAmount = shares * (1 - (noLimitProb ?? 0))
|
||||||
|
|
||||||
const profitIfBothFilled = shares - (yesAmount + noAmount)
|
|
||||||
|
|
||||||
function onBetChange(newAmount: number | undefined) {
|
function onBetChange(newAmount: number | undefined) {
|
||||||
setWasSubmitted(false)
|
setWasSubmitted(false)
|
||||||
setBetAmount(newAmount)
|
setBetAmount(newAmount)
|
||||||
|
@ -485,6 +496,8 @@ function LimitOrderPanel(props: {
|
||||||
setIsSubmitting(false)
|
setIsSubmitting(false)
|
||||||
setWasSubmitted(true)
|
setWasSubmitted(true)
|
||||||
setBetAmount(undefined)
|
setBetAmount(undefined)
|
||||||
|
setLowLimitProb(undefined)
|
||||||
|
setHighLimitProb(undefined)
|
||||||
if (onBuySuccess) onBuySuccess()
|
if (onBuySuccess) onBuySuccess()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -544,6 +557,8 @@ function LimitOrderPanel(props: {
|
||||||
)
|
)
|
||||||
const noReturnPercent = formatPercent(noReturn)
|
const noReturnPercent = formatPercent(noReturn)
|
||||||
|
|
||||||
|
const profitIfBothFilled = shares - (yesAmount + noAmount) - yesFees - noFees
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Col className={hidden ? 'hidden' : ''}>
|
<Col className={hidden ? 'hidden' : ''}>
|
||||||
<Row className="mt-1 items-center gap-4">
|
<Row className="mt-1 items-center gap-4">
|
||||||
|
|
|
@ -1,5 +1,14 @@
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
import { groupBy, mapValues, sortBy, partition, sumBy } from 'lodash'
|
import {
|
||||||
|
Dictionary,
|
||||||
|
keyBy,
|
||||||
|
groupBy,
|
||||||
|
mapValues,
|
||||||
|
sortBy,
|
||||||
|
partition,
|
||||||
|
sumBy,
|
||||||
|
uniq,
|
||||||
|
} from 'lodash'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
import { useEffect, useMemo, useState } from 'react'
|
import { useEffect, useMemo, useState } from 'react'
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
|
@ -19,6 +28,7 @@ import {
|
||||||
Contract,
|
Contract,
|
||||||
contractPath,
|
contractPath,
|
||||||
getBinaryProbPercent,
|
getBinaryProbPercent,
|
||||||
|
getContractFromId,
|
||||||
} from 'web/lib/firebase/contracts'
|
} from 'web/lib/firebase/contracts'
|
||||||
import { Row } from './layout/row'
|
import { Row } from './layout/row'
|
||||||
import { UserLink } from './user-page'
|
import { UserLink } from './user-page'
|
||||||
|
@ -41,10 +51,12 @@ import { trackLatency } from 'web/lib/firebase/tracking'
|
||||||
import { NumericContract } from 'common/contract'
|
import { NumericContract } from 'common/contract'
|
||||||
import { formatNumericProbability } from 'common/pseudo-numeric'
|
import { formatNumericProbability } from 'common/pseudo-numeric'
|
||||||
import { useUser } from 'web/hooks/use-user'
|
import { useUser } from 'web/hooks/use-user'
|
||||||
|
import { useUserBets } from 'web/hooks/use-user-bets'
|
||||||
import { SellSharesModal } from './sell-modal'
|
import { SellSharesModal } from './sell-modal'
|
||||||
import { useUnfilledBets } from 'web/hooks/use-bets'
|
import { useUnfilledBets } from 'web/hooks/use-bets'
|
||||||
import { LimitBet } from 'common/bet'
|
import { LimitBet } from 'common/bet'
|
||||||
import { floatingEqual } from 'common/util/math'
|
import { floatingEqual } from 'common/util/math'
|
||||||
|
import { filterDefined } from 'common/util/array'
|
||||||
import { Pagination } from './pagination'
|
import { Pagination } from './pagination'
|
||||||
import { LimitOrderTable } from './limit-bets'
|
import { LimitOrderTable } from './limit-bets'
|
||||||
|
|
||||||
|
@ -52,25 +64,35 @@ type BetSort = 'newest' | 'profit' | 'closeTime' | 'value'
|
||||||
type BetFilter = 'open' | 'limit_bet' | 'sold' | 'closed' | 'resolved' | 'all'
|
type BetFilter = 'open' | 'limit_bet' | 'sold' | 'closed' | 'resolved' | 'all'
|
||||||
|
|
||||||
const CONTRACTS_PER_PAGE = 50
|
const CONTRACTS_PER_PAGE = 50
|
||||||
|
const JUNE_1_2022 = new Date('2022-06-01T00:00:00.000Z').valueOf()
|
||||||
|
|
||||||
export function BetsList(props: {
|
export function BetsList(props: { user: User }) {
|
||||||
user: User
|
const { user } = props
|
||||||
bets: Bet[] | undefined
|
|
||||||
contractsById: { [id: string]: Contract } | undefined
|
|
||||||
hideBetsBefore?: number
|
|
||||||
}) {
|
|
||||||
const { user, bets: allBets, contractsById, hideBetsBefore } = props
|
|
||||||
|
|
||||||
const signedInUser = useUser()
|
const signedInUser = useUser()
|
||||||
const isYourBets = user.id === signedInUser?.id
|
const isYourBets = user.id === signedInUser?.id
|
||||||
|
const hideBetsBefore = isYourBets ? 0 : JUNE_1_2022
|
||||||
|
const userBets = useUserBets(user.id, { includeRedemptions: true })
|
||||||
|
const [contractsById, setContractsById] = useState<
|
||||||
|
Dictionary<Contract> | undefined
|
||||||
|
>()
|
||||||
|
|
||||||
// Hide bets before 06-01-2022 if this isn't your own profile
|
// Hide bets before 06-01-2022 if this isn't your own profile
|
||||||
// NOTE: This means public profits also begin on 06-01-2022 as well.
|
// NOTE: This means public profits also begin on 06-01-2022 as well.
|
||||||
const bets = useMemo(
|
const bets = useMemo(
|
||||||
() => allBets?.filter((bet) => bet.createdTime >= (hideBetsBefore ?? 0)),
|
() => userBets?.filter((bet) => bet.createdTime >= (hideBetsBefore ?? 0)),
|
||||||
[allBets, hideBetsBefore]
|
[userBets, hideBetsBefore]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (bets) {
|
||||||
|
const contractIds = uniq(bets.map((b) => b.contractId))
|
||||||
|
Promise.all(contractIds.map(getContractFromId)).then((contracts) => {
|
||||||
|
setContractsById(keyBy(filterDefined(contracts), 'id'))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, [bets])
|
||||||
|
|
||||||
const [sort, setSort] = useState<BetSort>('newest')
|
const [sort, setSort] = useState<BetSort>('newest')
|
||||||
const [filter, setFilter] = useState<BetFilter>('open')
|
const [filter, setFilter] = useState<BetFilter>('open')
|
||||||
const [page, setPage] = useState(0)
|
const [page, setPage] = useState(0)
|
||||||
|
@ -406,95 +428,105 @@ export function BetsSummary(props: {
|
||||||
: 'NO'
|
: 'NO'
|
||||||
: 'YES'
|
: 'YES'
|
||||||
|
|
||||||
|
const canSell =
|
||||||
|
isYourBets &&
|
||||||
|
isCpmm &&
|
||||||
|
(isBinary || isPseudoNumeric) &&
|
||||||
|
!isClosed &&
|
||||||
|
!resolution &&
|
||||||
|
hasShares &&
|
||||||
|
sharesOutcome &&
|
||||||
|
user
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Row className={clsx('flex-wrap gap-4 sm:flex-nowrap sm:gap-6', className)}>
|
<Col className={clsx(className, 'gap-4')}>
|
||||||
{!isCpmm && (
|
<Row className="flex-wrap gap-4 sm:flex-nowrap sm:gap-6">
|
||||||
<Col>
|
<Col>
|
||||||
<div className="whitespace-nowrap text-sm text-gray-500">
|
<div className="whitespace-nowrap text-sm text-gray-500">
|
||||||
Invested
|
Invested
|
||||||
</div>
|
</div>
|
||||||
<div className="whitespace-nowrap">{formatMoney(invested)}</div>
|
<div className="whitespace-nowrap">{formatMoney(invested)}</div>
|
||||||
</Col>
|
</Col>
|
||||||
)}
|
|
||||||
{resolution ? (
|
|
||||||
<Col>
|
<Col>
|
||||||
<div className="text-sm text-gray-500">Payout</div>
|
<div className="whitespace-nowrap text-sm text-gray-500">Profit</div>
|
||||||
<div className="whitespace-nowrap">
|
<div className="whitespace-nowrap">
|
||||||
{formatMoney(payout)} <ProfitBadge profitPercent={profitPercent} />
|
{formatMoney(profit)} <ProfitBadge profitPercent={profitPercent} />
|
||||||
</div>
|
</div>
|
||||||
</Col>
|
</Col>
|
||||||
) : isBinary ? (
|
{canSell && (
|
||||||
<>
|
<>
|
||||||
<Col>
|
<button
|
||||||
<div className="whitespace-nowrap text-sm text-gray-500">
|
className="btn btn-sm self-end"
|
||||||
Payout if <YesLabel />
|
onClick={() => setShowSellModal(true)}
|
||||||
</div>
|
>
|
||||||
<div className="whitespace-nowrap">{formatMoney(yesWinnings)}</div>
|
Sell
|
||||||
</Col>
|
</button>
|
||||||
<Col>
|
{showSellModal && (
|
||||||
<div className="whitespace-nowrap text-sm text-gray-500">
|
<SellSharesModal
|
||||||
Payout if <NoLabel />
|
contract={contract}
|
||||||
</div>
|
user={user}
|
||||||
<div className="whitespace-nowrap">{formatMoney(noWinnings)}</div>
|
userBets={bets}
|
||||||
</Col>
|
shares={totalShares[sharesOutcome]}
|
||||||
</>
|
sharesOutcome={sharesOutcome}
|
||||||
) : isPseudoNumeric ? (
|
setOpen={setShowSellModal}
|
||||||
<>
|
/>
|
||||||
<Col>
|
|
||||||
<div className="whitespace-nowrap text-sm text-gray-500">
|
|
||||||
Payout if {'>='} {formatLargeNumber(contract.max)}
|
|
||||||
</div>
|
|
||||||
<div className="whitespace-nowrap">{formatMoney(yesWinnings)}</div>
|
|
||||||
</Col>
|
|
||||||
<Col>
|
|
||||||
<div className="whitespace-nowrap text-sm text-gray-500">
|
|
||||||
Payout if {'<='} {formatLargeNumber(contract.min)}
|
|
||||||
</div>
|
|
||||||
<div className="whitespace-nowrap">{formatMoney(noWinnings)}</div>
|
|
||||||
</Col>
|
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
<Col>
|
|
||||||
<div className="whitespace-nowrap text-sm text-gray-500">
|
|
||||||
Current value
|
|
||||||
</div>
|
|
||||||
<div className="whitespace-nowrap">{formatMoney(payout)}</div>
|
|
||||||
</Col>
|
|
||||||
)}
|
|
||||||
<Col>
|
|
||||||
<div className="whitespace-nowrap text-sm text-gray-500">Profit</div>
|
|
||||||
<div className="whitespace-nowrap">
|
|
||||||
{formatMoney(profit)} <ProfitBadge profitPercent={profitPercent} />
|
|
||||||
{isYourBets &&
|
|
||||||
isCpmm &&
|
|
||||||
(isBinary || isPseudoNumeric) &&
|
|
||||||
!isClosed &&
|
|
||||||
!resolution &&
|
|
||||||
hasShares &&
|
|
||||||
sharesOutcome &&
|
|
||||||
user && (
|
|
||||||
<>
|
|
||||||
<button
|
|
||||||
className="btn btn-sm ml-2"
|
|
||||||
onClick={() => setShowSellModal(true)}
|
|
||||||
>
|
|
||||||
Sell
|
|
||||||
</button>
|
|
||||||
{showSellModal && (
|
|
||||||
<SellSharesModal
|
|
||||||
contract={contract}
|
|
||||||
user={user}
|
|
||||||
userBets={bets}
|
|
||||||
shares={totalShares[sharesOutcome]}
|
|
||||||
sharesOutcome={sharesOutcome}
|
|
||||||
setOpen={setShowSellModal}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
</>
|
||||||
</Col>
|
)}
|
||||||
</Row>
|
</Row>
|
||||||
|
<Row className="flex-wrap-none gap-4">
|
||||||
|
{resolution ? (
|
||||||
|
<Col>
|
||||||
|
<div className="text-sm text-gray-500">Payout</div>
|
||||||
|
<div className="whitespace-nowrap">
|
||||||
|
{formatMoney(payout)}{' '}
|
||||||
|
<ProfitBadge profitPercent={profitPercent} />
|
||||||
|
</div>
|
||||||
|
</Col>
|
||||||
|
) : isBinary ? (
|
||||||
|
<>
|
||||||
|
<Col>
|
||||||
|
<div className="whitespace-nowrap text-sm text-gray-500">
|
||||||
|
Payout if <YesLabel />
|
||||||
|
</div>
|
||||||
|
<div className="whitespace-nowrap">
|
||||||
|
{formatMoney(yesWinnings)}
|
||||||
|
</div>
|
||||||
|
</Col>
|
||||||
|
<Col>
|
||||||
|
<div className="whitespace-nowrap text-sm text-gray-500">
|
||||||
|
Payout if <NoLabel />
|
||||||
|
</div>
|
||||||
|
<div className="whitespace-nowrap">{formatMoney(noWinnings)}</div>
|
||||||
|
</Col>
|
||||||
|
</>
|
||||||
|
) : isPseudoNumeric ? (
|
||||||
|
<>
|
||||||
|
<Col>
|
||||||
|
<div className="whitespace-nowrap text-sm text-gray-500">
|
||||||
|
Payout if {'>='} {formatLargeNumber(contract.max)}
|
||||||
|
</div>
|
||||||
|
<div className="whitespace-nowrap">
|
||||||
|
{formatMoney(yesWinnings)}
|
||||||
|
</div>
|
||||||
|
</Col>
|
||||||
|
<Col>
|
||||||
|
<div className="whitespace-nowrap text-sm text-gray-500">
|
||||||
|
Payout if {'<='} {formatLargeNumber(contract.min)}
|
||||||
|
</div>
|
||||||
|
<div className="whitespace-nowrap">{formatMoney(noWinnings)}</div>
|
||||||
|
</Col>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<Col>
|
||||||
|
<div className="whitespace-nowrap text-sm text-gray-500">
|
||||||
|
Current value
|
||||||
|
</div>
|
||||||
|
<div className="whitespace-nowrap">{formatMoney(payout)}</div>
|
||||||
|
</Col>
|
||||||
|
)}
|
||||||
|
</Row>
|
||||||
|
</Col>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,16 @@ export function Button(props: {
|
||||||
className?: string
|
className?: string
|
||||||
onClick?: () => void
|
onClick?: () => void
|
||||||
children?: ReactNode
|
children?: ReactNode
|
||||||
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl'
|
size?: '2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl'
|
||||||
color?: 'green' | 'red' | 'blue' | 'indigo' | 'yellow' | 'gray' | 'gray-white'
|
color?:
|
||||||
|
| 'green'
|
||||||
|
| 'red'
|
||||||
|
| 'blue'
|
||||||
|
| 'indigo'
|
||||||
|
| 'yellow'
|
||||||
|
| 'gray'
|
||||||
|
| 'gradient'
|
||||||
|
| 'gray-white'
|
||||||
type?: 'button' | 'reset' | 'submit'
|
type?: 'button' | 'reset' | 'submit'
|
||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
}) {
|
}) {
|
||||||
|
@ -21,11 +29,13 @@ export function Button(props: {
|
||||||
} = props
|
} = props
|
||||||
|
|
||||||
const sizeClasses = {
|
const sizeClasses = {
|
||||||
|
'2xs': 'px-2 py-1 text-xs',
|
||||||
xs: 'px-2.5 py-1.5 text-sm',
|
xs: 'px-2.5 py-1.5 text-sm',
|
||||||
sm: 'px-3 py-2 text-sm',
|
sm: 'px-3 py-2 text-sm',
|
||||||
md: 'px-4 py-2 text-sm',
|
md: 'px-4 py-2 text-sm',
|
||||||
lg: 'px-4 py-2 text-base',
|
lg: 'px-4 py-2 text-base',
|
||||||
xl: 'px-6 py-3 text-base',
|
xl: 'px-6 py-3 text-base',
|
||||||
|
'2xl': 'px-6 py-3 text-xl',
|
||||||
}[size]
|
}[size]
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -39,10 +49,11 @@ export function Button(props: {
|
||||||
color === 'yellow' && 'bg-yellow-400 text-white hover:bg-yellow-500',
|
color === 'yellow' && 'bg-yellow-400 text-white hover:bg-yellow-500',
|
||||||
color === 'blue' && 'bg-blue-400 text-white hover:bg-blue-500',
|
color === 'blue' && 'bg-blue-400 text-white hover:bg-blue-500',
|
||||||
color === 'indigo' && 'bg-indigo-500 text-white hover:bg-indigo-600',
|
color === 'indigo' && 'bg-indigo-500 text-white hover:bg-indigo-600',
|
||||||
color === 'gray' &&
|
color === 'gray' && 'bg-gray-100 text-gray-600 hover:bg-gray-200',
|
||||||
'bg-greyscale-1 text-greyscale-7 hover:bg-greyscale-2',
|
color === 'gradient' &&
|
||||||
|
'bg-gradient-to-r from-indigo-500 to-blue-500 text-white hover:from-indigo-700 hover:to-blue-700',
|
||||||
color === 'gray-white' &&
|
color === 'gray-white' &&
|
||||||
'text-greyscale-6 hover:bg-greyscale-2 bg-white',
|
'border-none bg-white text-gray-500 shadow-none hover:bg-gray-200',
|
||||||
className
|
className
|
||||||
)}
|
)}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
|
|
125
web/components/challenges/accept-challenge-button.tsx
Normal file
125
web/components/challenges/accept-challenge-button.tsx
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
import { User } from 'common/user'
|
||||||
|
import { Contract } from 'common/contract'
|
||||||
|
import { Challenge } from 'common/challenge'
|
||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
import { SignUpPrompt } from 'web/components/sign-up-prompt'
|
||||||
|
import { acceptChallenge, APIError } from 'web/lib/firebase/api'
|
||||||
|
import { Modal } from 'web/components/layout/modal'
|
||||||
|
import { Col } from 'web/components/layout/col'
|
||||||
|
import { Title } from 'web/components/title'
|
||||||
|
import { Row } from 'web/components/layout/row'
|
||||||
|
import { formatMoney } from 'common/util/format'
|
||||||
|
import { Button } from 'web/components/button'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
export function AcceptChallengeButton(props: {
|
||||||
|
user: User | null | undefined
|
||||||
|
contract: Contract
|
||||||
|
challenge: Challenge
|
||||||
|
}) {
|
||||||
|
const { user, challenge, contract } = props
|
||||||
|
const [open, setOpen] = useState(false)
|
||||||
|
const [errorText, setErrorText] = useState('')
|
||||||
|
const [loading, setLoading] = useState(false)
|
||||||
|
const { acceptorAmount, creatorAmount } = challenge
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setErrorText('')
|
||||||
|
}, [open])
|
||||||
|
|
||||||
|
if (!user) return <SignUpPrompt label="Accept this bet" className="mt-4" />
|
||||||
|
|
||||||
|
const iAcceptChallenge = () => {
|
||||||
|
setLoading(true)
|
||||||
|
if (user.id === challenge.creatorId) {
|
||||||
|
setErrorText('You cannot accept your own challenge!')
|
||||||
|
setLoading(false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
acceptChallenge({
|
||||||
|
contractId: contract.id,
|
||||||
|
challengeSlug: challenge.slug,
|
||||||
|
outcomeType: contract.outcomeType,
|
||||||
|
closeTime: contract.closeTime,
|
||||||
|
})
|
||||||
|
.then((r) => {
|
||||||
|
console.log('accepted challenge. Result:', r)
|
||||||
|
setLoading(false)
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
setLoading(false)
|
||||||
|
if (e instanceof APIError) {
|
||||||
|
setErrorText(e.toString())
|
||||||
|
} else {
|
||||||
|
console.error(e)
|
||||||
|
setErrorText('Error accepting challenge')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Modal open={open} setOpen={(newOpen) => setOpen(newOpen)} size={'sm'}>
|
||||||
|
<Col className="gap-4 rounded-md bg-white px-8 py-6">
|
||||||
|
<Col className={'gap-4'}>
|
||||||
|
<div className={'flex flex-row justify-start '}>
|
||||||
|
<Title text={"So you're in?"} className={'!my-2'} />
|
||||||
|
</div>
|
||||||
|
<Col className="w-full items-center justify-start gap-2">
|
||||||
|
<Row className={'w-full justify-start gap-20'}>
|
||||||
|
<span className={'min-w-[4rem] font-bold'}>Cost to you:</span>{' '}
|
||||||
|
<span className={'text-red-500'}>
|
||||||
|
{formatMoney(acceptorAmount)}
|
||||||
|
</span>
|
||||||
|
</Row>
|
||||||
|
<Col className={'w-full items-center justify-start'}>
|
||||||
|
<Row className={'w-full justify-start gap-10'}>
|
||||||
|
<span className={'min-w-[4rem] font-bold'}>
|
||||||
|
Potential payout:
|
||||||
|
</span>{' '}
|
||||||
|
<Row className={'items-center justify-center'}>
|
||||||
|
<span className={'text-primary'}>
|
||||||
|
{formatMoney(creatorAmount + acceptorAmount)}
|
||||||
|
</span>
|
||||||
|
</Row>
|
||||||
|
</Row>
|
||||||
|
</Col>
|
||||||
|
</Col>
|
||||||
|
<Row className={'mt-4 justify-end gap-4'}>
|
||||||
|
<Button
|
||||||
|
color={'gray'}
|
||||||
|
disabled={loading}
|
||||||
|
onClick={() => setOpen(false)}
|
||||||
|
className={clsx('whitespace-nowrap')}
|
||||||
|
>
|
||||||
|
I'm out
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
color={'indigo'}
|
||||||
|
disabled={loading}
|
||||||
|
onClick={() => iAcceptChallenge()}
|
||||||
|
className={clsx('min-w-[6rem] whitespace-nowrap')}
|
||||||
|
>
|
||||||
|
I'm in
|
||||||
|
</Button>
|
||||||
|
</Row>
|
||||||
|
<Row>
|
||||||
|
<span className={'text-error'}>{errorText}</span>
|
||||||
|
</Row>
|
||||||
|
</Col>
|
||||||
|
</Col>
|
||||||
|
</Modal>
|
||||||
|
|
||||||
|
{challenge.creatorId != user.id && (
|
||||||
|
<Button
|
||||||
|
color="gradient"
|
||||||
|
size="2xl"
|
||||||
|
onClick={() => setOpen(true)}
|
||||||
|
className={clsx('whitespace-nowrap')}
|
||||||
|
>
|
||||||
|
Accept bet
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
259
web/components/challenges/create-challenge-modal.tsx
Normal file
259
web/components/challenges/create-challenge-modal.tsx
Normal file
|
@ -0,0 +1,259 @@
|
||||||
|
import clsx from 'clsx'
|
||||||
|
import dayjs from 'dayjs'
|
||||||
|
import React, { useEffect, useState } from 'react'
|
||||||
|
import { LinkIcon, SwitchVerticalIcon } from '@heroicons/react/outline'
|
||||||
|
import toast from 'react-hot-toast'
|
||||||
|
|
||||||
|
import { Col } from '../layout/col'
|
||||||
|
import { Row } from '../layout/row'
|
||||||
|
import { Title } from '../title'
|
||||||
|
import { User } from 'common/user'
|
||||||
|
import { Modal } from 'web/components/layout/modal'
|
||||||
|
import { Button } from '../button'
|
||||||
|
import { createChallenge, getChallengeUrl } from 'web/lib/firebase/challenges'
|
||||||
|
import { BinaryContract } from 'common/contract'
|
||||||
|
import { SiteLink } from 'web/components/site-link'
|
||||||
|
import { formatMoney } from 'common/util/format'
|
||||||
|
import { NoLabel, YesLabel } from '../outcome-label'
|
||||||
|
import { QRCode } from '../qr-code'
|
||||||
|
import { copyToClipboard } from 'web/lib/util/copy'
|
||||||
|
import { AmountInput } from '../amount-input'
|
||||||
|
import { getProbability } from 'common/calculate'
|
||||||
|
import { track } from 'web/lib/service/analytics'
|
||||||
|
|
||||||
|
type challengeInfo = {
|
||||||
|
amount: number
|
||||||
|
expiresTime: number | null
|
||||||
|
message: string
|
||||||
|
outcome: 'YES' | 'NO' | number
|
||||||
|
acceptorAmount: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export function CreateChallengeModal(props: {
|
||||||
|
user: User | null | undefined
|
||||||
|
contract: BinaryContract
|
||||||
|
isOpen: boolean
|
||||||
|
setOpen: (open: boolean) => void
|
||||||
|
}) {
|
||||||
|
const { user, contract, isOpen, setOpen } = props
|
||||||
|
const [challengeSlug, setChallengeSlug] = useState('')
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Modal open={isOpen} setOpen={setOpen}>
|
||||||
|
<Col className="gap-4 rounded-md bg-white px-8 py-6">
|
||||||
|
{/*// add a sign up to challenge button?*/}
|
||||||
|
{user && (
|
||||||
|
<CreateChallengeForm
|
||||||
|
user={user}
|
||||||
|
contract={contract}
|
||||||
|
onCreate={async (newChallenge) => {
|
||||||
|
const challenge = await createChallenge({
|
||||||
|
creator: user,
|
||||||
|
creatorAmount: newChallenge.amount,
|
||||||
|
expiresTime: newChallenge.expiresTime,
|
||||||
|
message: newChallenge.message,
|
||||||
|
acceptorAmount: newChallenge.acceptorAmount,
|
||||||
|
outcome: newChallenge.outcome,
|
||||||
|
contract: contract,
|
||||||
|
})
|
||||||
|
if (challenge) {
|
||||||
|
setChallengeSlug(getChallengeUrl(challenge))
|
||||||
|
track('challenge created', {
|
||||||
|
creator: user.username,
|
||||||
|
amount: newChallenge.amount,
|
||||||
|
contractId: contract.id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
challengeSlug={challengeSlug}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</Col>
|
||||||
|
</Modal>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function CreateChallengeForm(props: {
|
||||||
|
user: User
|
||||||
|
contract: BinaryContract
|
||||||
|
onCreate: (m: challengeInfo) => Promise<void>
|
||||||
|
challengeSlug: string
|
||||||
|
}) {
|
||||||
|
const { user, onCreate, contract, challengeSlug } = props
|
||||||
|
const [isCreating, setIsCreating] = useState(false)
|
||||||
|
const [finishedCreating, setFinishedCreating] = useState(false)
|
||||||
|
const [error, setError] = useState<string>('')
|
||||||
|
const [editingAcceptorAmount, setEditingAcceptorAmount] = useState(false)
|
||||||
|
const defaultExpire = 'week'
|
||||||
|
|
||||||
|
const defaultMessage = `${user.name} is challenging you to a bet! Do you think ${contract.question}`
|
||||||
|
|
||||||
|
const [challengeInfo, setChallengeInfo] = useState<challengeInfo>({
|
||||||
|
expiresTime: dayjs().add(2, defaultExpire).valueOf(),
|
||||||
|
outcome: 'YES',
|
||||||
|
amount: 100,
|
||||||
|
acceptorAmount: 100,
|
||||||
|
message: defaultMessage,
|
||||||
|
})
|
||||||
|
useEffect(() => {
|
||||||
|
setError('')
|
||||||
|
}, [challengeInfo])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{!finishedCreating && (
|
||||||
|
<form
|
||||||
|
onSubmit={(e) => {
|
||||||
|
e.preventDefault()
|
||||||
|
if (user.balance < challengeInfo.amount) {
|
||||||
|
setError('You do not have enough mana to create this challenge')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setIsCreating(true)
|
||||||
|
onCreate(challengeInfo).finally(() => setIsCreating(false))
|
||||||
|
setFinishedCreating(true)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Title className="!mt-2" text="Challenge bet " />
|
||||||
|
|
||||||
|
<div className="mb-8">
|
||||||
|
Challenge a friend to bet on{' '}
|
||||||
|
<span className="underline">{contract.question}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="mt-2 flex flex-col flex-wrap justify-center gap-x-5 gap-y-2">
|
||||||
|
<div>You'll bet:</div>
|
||||||
|
<Row
|
||||||
|
className={
|
||||||
|
'form-control w-full max-w-xs items-center justify-between gap-4 pr-3'
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<AmountInput
|
||||||
|
amount={challengeInfo.amount || undefined}
|
||||||
|
onChange={(newAmount) =>
|
||||||
|
setChallengeInfo((m: challengeInfo) => {
|
||||||
|
return {
|
||||||
|
...m,
|
||||||
|
amount: newAmount ?? 0,
|
||||||
|
acceptorAmount: editingAcceptorAmount
|
||||||
|
? m.acceptorAmount
|
||||||
|
: newAmount ?? 0,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
error={undefined}
|
||||||
|
label={'M$'}
|
||||||
|
inputClassName="w-24"
|
||||||
|
/>
|
||||||
|
<span className={''}>on</span>
|
||||||
|
{challengeInfo.outcome === 'YES' ? <YesLabel /> : <NoLabel />}
|
||||||
|
</Row>
|
||||||
|
<Row className={'mt-3 max-w-xs justify-end'}>
|
||||||
|
<Button
|
||||||
|
color={'gray-white'}
|
||||||
|
onClick={() =>
|
||||||
|
setChallengeInfo((m: challengeInfo) => {
|
||||||
|
return {
|
||||||
|
...m,
|
||||||
|
outcome: m.outcome === 'YES' ? 'NO' : 'YES',
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<SwitchVerticalIcon className={'h-6 w-6'} />
|
||||||
|
</Button>
|
||||||
|
</Row>
|
||||||
|
<Row className={'items-center'}>If they bet:</Row>
|
||||||
|
<Row className={'max-w-xs items-center justify-between gap-4 pr-3'}>
|
||||||
|
<div className={'w-32 sm:mr-1'}>
|
||||||
|
<AmountInput
|
||||||
|
amount={challengeInfo.acceptorAmount || undefined}
|
||||||
|
onChange={(newAmount) => {
|
||||||
|
setEditingAcceptorAmount(true)
|
||||||
|
|
||||||
|
setChallengeInfo((m: challengeInfo) => {
|
||||||
|
return {
|
||||||
|
...m,
|
||||||
|
acceptorAmount: newAmount ?? 0,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}}
|
||||||
|
error={undefined}
|
||||||
|
label={'M$'}
|
||||||
|
inputClassName="w-24"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<span>on</span>
|
||||||
|
{challengeInfo.outcome === 'YES' ? <NoLabel /> : <YesLabel />}
|
||||||
|
</Row>
|
||||||
|
</div>
|
||||||
|
<Button
|
||||||
|
size="2xs"
|
||||||
|
color="gray"
|
||||||
|
onClick={() => {
|
||||||
|
setEditingAcceptorAmount(true)
|
||||||
|
|
||||||
|
const p = getProbability(contract)
|
||||||
|
const prob = challengeInfo.outcome === 'YES' ? p : 1 - p
|
||||||
|
const { amount } = challengeInfo
|
||||||
|
const acceptorAmount = Math.round(amount / prob - amount)
|
||||||
|
setChallengeInfo({ ...challengeInfo, acceptorAmount })
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Use market odds
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<div className="mt-8">
|
||||||
|
If the challenge is accepted, whoever is right will earn{' '}
|
||||||
|
<span className="font-semibold">
|
||||||
|
{formatMoney(
|
||||||
|
challengeInfo.acceptorAmount + challengeInfo.amount || 0
|
||||||
|
)}
|
||||||
|
</span>{' '}
|
||||||
|
in total.
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Row className="mt-8 items-center">
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
color={'gradient'}
|
||||||
|
size="xl"
|
||||||
|
className={clsx(
|
||||||
|
'whitespace-nowrap drop-shadow-md',
|
||||||
|
isCreating ? 'disabled' : ''
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
Create challenge bet
|
||||||
|
</Button>
|
||||||
|
</Row>
|
||||||
|
<Row className={'text-error'}>{error} </Row>
|
||||||
|
</form>
|
||||||
|
)}
|
||||||
|
{finishedCreating && (
|
||||||
|
<>
|
||||||
|
<Title className="!my-0" text="Challenge Created!" />
|
||||||
|
|
||||||
|
<div>Share the challenge using the link.</div>
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
copyToClipboard(challengeSlug)
|
||||||
|
toast('Link copied to clipboard!')
|
||||||
|
}}
|
||||||
|
className={'btn btn-outline mb-4 whitespace-nowrap normal-case'}
|
||||||
|
>
|
||||||
|
<LinkIcon className={'mr-2 h-5 w-5'} />
|
||||||
|
Copy link
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<QRCode url={challengeSlug} className="self-center" />
|
||||||
|
<Row className={'gap-1 text-gray-500'}>
|
||||||
|
See your other
|
||||||
|
<SiteLink className={'underline'} href={'/challenges'}>
|
||||||
|
challenges
|
||||||
|
</SiteLink>
|
||||||
|
</Row>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
|
@ -22,7 +22,10 @@ export function ChoicesToggleGroup(props: {
|
||||||
} = props
|
} = props
|
||||||
return (
|
return (
|
||||||
<RadioGroup
|
<RadioGroup
|
||||||
className={clsx(className, 'flex flex-row flex-wrap items-center gap-3')}
|
className={clsx(
|
||||||
|
className,
|
||||||
|
'flex flex-row flex-wrap items-center gap-2 sm:gap-3'
|
||||||
|
)}
|
||||||
value={currentChoice.toString()}
|
value={currentChoice.toString()}
|
||||||
onChange={setChoice}
|
onChange={setChoice}
|
||||||
>
|
>
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
|
||||||
import { Comment } from 'common/comment'
|
import { Comment } from 'common/comment'
|
||||||
import { Contract } from 'common/contract'
|
import { groupConsecutive } from 'common/util/array'
|
||||||
import { contractPath } from 'web/lib/firebase/contracts'
|
import { getUsersComments } from 'web/lib/firebase/comments'
|
||||||
import { SiteLink } from './site-link'
|
import { SiteLink } from './site-link'
|
||||||
import { Row } from './layout/row'
|
import { Row } from './layout/row'
|
||||||
import { Avatar } from './avatar'
|
import { Avatar } from './avatar'
|
||||||
|
@ -8,49 +10,82 @@ import { RelativeTimestamp } from './relative-timestamp'
|
||||||
import { UserLink } from './user-page'
|
import { UserLink } from './user-page'
|
||||||
import { User } from 'common/user'
|
import { User } from 'common/user'
|
||||||
import { Col } from './layout/col'
|
import { Col } from './layout/col'
|
||||||
import { Linkify } from './linkify'
|
import { Content } from './editor'
|
||||||
import { groupBy } from 'lodash'
|
import { Pagination } from './pagination'
|
||||||
|
import { LoadingIndicator } from './loading-indicator'
|
||||||
|
|
||||||
export function UserCommentsList(props: {
|
const COMMENTS_PER_PAGE = 50
|
||||||
user: User
|
|
||||||
comments: Comment[]
|
|
||||||
contractsById: { [id: string]: Contract }
|
|
||||||
}) {
|
|
||||||
const { comments, contractsById } = props
|
|
||||||
|
|
||||||
// we don't show comments in groups here atm, just comments on contracts
|
type ContractComment = Comment & {
|
||||||
const contractComments = comments.filter((c) => c.contractId)
|
contractId: string
|
||||||
const commentsByContract = groupBy(contractComments, 'contractId')
|
contractSlug: string
|
||||||
|
contractQuestion: string
|
||||||
|
}
|
||||||
|
|
||||||
|
function contractPath(slug: string) {
|
||||||
|
// by convention this includes the contract creator username, but we don't
|
||||||
|
// have that handy, so we just put /market/
|
||||||
|
return `/market/${slug}`
|
||||||
|
}
|
||||||
|
|
||||||
|
export function UserCommentsList(props: { user: User }) {
|
||||||
|
const { user } = props
|
||||||
|
const [comments, setComments] = useState<ContractComment[] | undefined>()
|
||||||
|
const [page, setPage] = useState(0)
|
||||||
|
const start = page * COMMENTS_PER_PAGE
|
||||||
|
const end = start + COMMENTS_PER_PAGE
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
getUsersComments(user.id).then((cs) => {
|
||||||
|
// we don't show comments in groups here atm, just comments on contracts
|
||||||
|
setComments(cs.filter((c) => c.contractId) as ContractComment[])
|
||||||
|
})
|
||||||
|
}, [user.id])
|
||||||
|
|
||||||
|
if (comments == null) {
|
||||||
|
return <LoadingIndicator />
|
||||||
|
}
|
||||||
|
|
||||||
|
const pageComments = groupConsecutive(comments.slice(start, end), (c) => {
|
||||||
|
return { question: c.contractQuestion, slug: c.contractSlug }
|
||||||
|
})
|
||||||
return (
|
return (
|
||||||
<Col className={'bg-white'}>
|
<Col className={'bg-white'}>
|
||||||
{Object.entries(commentsByContract).map(([contractId, comments]) => {
|
{pageComments.map(({ key, items }, i) => {
|
||||||
const contract = contractsById[contractId]
|
|
||||||
return (
|
return (
|
||||||
<div key={contractId} className={'border-width-1 border-b p-5'}>
|
<div key={start + i} className="border-b p-5">
|
||||||
<SiteLink
|
<SiteLink
|
||||||
className={'mb-2 block text-sm text-indigo-700'}
|
className="mb-2 block pb-2 font-medium text-indigo-700"
|
||||||
href={contractPath(contract)}
|
href={contractPath(key.slug)}
|
||||||
>
|
>
|
||||||
{contract.question}
|
{key.question}
|
||||||
</SiteLink>
|
</SiteLink>
|
||||||
{comments.map((comment) => (
|
<Col className="gap-6">
|
||||||
<ProfileComment
|
{items.map((comment) => (
|
||||||
key={comment.id}
|
<ProfileComment
|
||||||
comment={comment}
|
key={comment.id}
|
||||||
className="relative flex items-start space-x-3 pb-6"
|
comment={comment}
|
||||||
/>
|
className="relative flex items-start space-x-3"
|
||||||
))}
|
/>
|
||||||
|
))}
|
||||||
|
</Col>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
})}
|
})}
|
||||||
|
<Pagination
|
||||||
|
page={page}
|
||||||
|
itemsPerPage={COMMENTS_PER_PAGE}
|
||||||
|
totalItems={comments.length}
|
||||||
|
setPage={setPage}
|
||||||
|
/>
|
||||||
</Col>
|
</Col>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function ProfileComment(props: { comment: Comment; className?: string }) {
|
function ProfileComment(props: { comment: Comment; className?: string }) {
|
||||||
const { comment, className } = props
|
const { comment, className } = props
|
||||||
const { text, userUsername, userName, userAvatarUrl, createdTime } = comment
|
const { text, content, userUsername, userName, userAvatarUrl, createdTime } =
|
||||||
|
comment
|
||||||
// TODO: find and attach relevant bets by comment betId at some point
|
// TODO: find and attach relevant bets by comment betId at some point
|
||||||
return (
|
return (
|
||||||
<Row className={className}>
|
<Row className={className}>
|
||||||
|
@ -64,7 +99,7 @@ function ProfileComment(props: { comment: Comment; className?: string }) {
|
||||||
/>{' '}
|
/>{' '}
|
||||||
<RelativeTimestamp time={createdTime} />
|
<RelativeTimestamp time={createdTime} />
|
||||||
</p>
|
</p>
|
||||||
<Linkify text={text} />
|
<Content content={content || text} smallImage />
|
||||||
</div>
|
</div>
|
||||||
</Row>
|
</Row>
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,26 +1,43 @@
|
||||||
/* eslint-disable react-hooks/exhaustive-deps */
|
/* eslint-disable react-hooks/exhaustive-deps */
|
||||||
import algoliasearch from 'algoliasearch/lite'
|
import algoliasearch, { SearchIndex } from 'algoliasearch/lite'
|
||||||
|
import { SearchOptions } from '@algolia/client-search'
|
||||||
|
|
||||||
import { Contract } from 'common/contract'
|
import { Contract } from 'common/contract'
|
||||||
import { Sort, useQueryAndSortParams } from '../hooks/use-sort-and-query-params'
|
import { User } from 'common/user'
|
||||||
|
import { Sort, useQuery, useSort } from '../hooks/use-sort-and-query-params'
|
||||||
import {
|
import {
|
||||||
ContractHighlightOptions,
|
ContractHighlightOptions,
|
||||||
ContractsGrid,
|
ContractsGrid,
|
||||||
} from './contract/contracts-list'
|
} from './contract/contracts-grid'
|
||||||
|
import { ShowTime } from './contract/contract-details'
|
||||||
import { Row } from './layout/row'
|
import { Row } from './layout/row'
|
||||||
import { useEffect, useMemo, useState } from 'react'
|
import { useEffect, useRef, useMemo, useState } from 'react'
|
||||||
import { Spacer } from './layout/spacer'
|
import { unstable_batchedUpdates } from 'react-dom'
|
||||||
import { ENV, IS_PRIVATE_MANIFOLD } from 'common/envs/constants'
|
import { ENV, IS_PRIVATE_MANIFOLD } from 'common/envs/constants'
|
||||||
import { useUser } from 'web/hooks/use-user'
|
|
||||||
import { useFollows } from 'web/hooks/use-follows'
|
import { useFollows } from 'web/hooks/use-follows'
|
||||||
import { track, trackCallback } from 'web/lib/service/analytics'
|
import { track, trackCallback } from 'web/lib/service/analytics'
|
||||||
import ContractSearchFirestore from 'web/pages/contract-search-firestore'
|
import ContractSearchFirestore from 'web/pages/contract-search-firestore'
|
||||||
import { useMemberGroups } from 'web/hooks/use-group'
|
import { useMemberGroups } from 'web/hooks/use-group'
|
||||||
import { Group, NEW_USER_GROUP_SLUGS } from 'common/group'
|
import { NEW_USER_GROUP_SLUGS } from 'common/group'
|
||||||
import { PillButton } from './buttons/pill-button'
|
import { PillButton } from './buttons/pill-button'
|
||||||
import { range, sortBy } from 'lodash'
|
import { debounce, sortBy } from 'lodash'
|
||||||
import { DEFAULT_CATEGORY_GROUPS } from 'common/categories'
|
import { DEFAULT_CATEGORY_GROUPS } from 'common/categories'
|
||||||
import { Col } from './layout/col'
|
import { Col } from './layout/col'
|
||||||
|
import { safeLocalStorage } from 'web/lib/util/local'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
|
// TODO: this obviously doesn't work with SSR, common sense would suggest
|
||||||
|
// that we should save things like this in cookies so the server has them
|
||||||
|
|
||||||
|
const MARKETS_SORT = 'markets_sort'
|
||||||
|
|
||||||
|
function setSavedSort(s: Sort) {
|
||||||
|
safeLocalStorage()?.setItem(MARKETS_SORT, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSavedSort() {
|
||||||
|
return safeLocalStorage()?.getItem(MARKETS_SORT) as Sort | null | undefined
|
||||||
|
}
|
||||||
|
|
||||||
const searchClient = algoliasearch(
|
const searchClient = algoliasearch(
|
||||||
'GJQPAYENIF',
|
'GJQPAYENIF',
|
||||||
|
@ -40,44 +57,176 @@ const sortOptions = [
|
||||||
{ label: 'Close date', value: 'close-date' },
|
{ label: 'Close date', value: 'close-date' },
|
||||||
{ label: 'Resolve date', value: 'resolve-date' },
|
{ label: 'Resolve date', value: 'resolve-date' },
|
||||||
]
|
]
|
||||||
export const DEFAULT_SORT = 'score'
|
|
||||||
|
|
||||||
type filter = 'personal' | 'open' | 'closed' | 'resolved' | 'all'
|
type filter = 'personal' | 'open' | 'closed' | 'resolved' | 'all'
|
||||||
|
|
||||||
|
type SearchParameters = {
|
||||||
|
index: SearchIndex
|
||||||
|
query: string
|
||||||
|
numericFilters: SearchOptions['numericFilters']
|
||||||
|
facetFilters: SearchOptions['facetFilters']
|
||||||
|
showTime?: ShowTime
|
||||||
|
}
|
||||||
|
|
||||||
|
type AdditionalFilter = {
|
||||||
|
creatorId?: string
|
||||||
|
tag?: string
|
||||||
|
excludeContractIds?: string[]
|
||||||
|
groupSlug?: string
|
||||||
|
}
|
||||||
|
|
||||||
export function ContractSearch(props: {
|
export function ContractSearch(props: {
|
||||||
querySortOptions?: {
|
user?: User | null
|
||||||
defaultSort: Sort
|
defaultSort?: Sort
|
||||||
defaultFilter?: filter
|
defaultFilter?: filter
|
||||||
shouldLoadFromStorage?: boolean
|
additionalFilter?: AdditionalFilter
|
||||||
}
|
|
||||||
additionalFilter?: {
|
|
||||||
creatorId?: string
|
|
||||||
tag?: string
|
|
||||||
excludeContractIds?: string[]
|
|
||||||
groupSlug?: string
|
|
||||||
}
|
|
||||||
highlightOptions?: ContractHighlightOptions
|
highlightOptions?: ContractHighlightOptions
|
||||||
onContractClick?: (contract: Contract) => void
|
onContractClick?: (contract: Contract) => void
|
||||||
showPlaceHolder?: boolean
|
|
||||||
hideOrderSelector?: boolean
|
hideOrderSelector?: boolean
|
||||||
overrideGridClassName?: string
|
overrideGridClassName?: string
|
||||||
cardHideOptions?: {
|
cardHideOptions?: {
|
||||||
hideGroupLink?: boolean
|
hideGroupLink?: boolean
|
||||||
hideQuickBet?: boolean
|
hideQuickBet?: boolean
|
||||||
}
|
}
|
||||||
|
headerClassName?: string
|
||||||
|
useQuerySortLocalStorage?: boolean
|
||||||
|
useQuerySortUrlParams?: boolean
|
||||||
}) {
|
}) {
|
||||||
const {
|
const {
|
||||||
querySortOptions,
|
user,
|
||||||
|
defaultSort,
|
||||||
|
defaultFilter,
|
||||||
additionalFilter,
|
additionalFilter,
|
||||||
onContractClick,
|
onContractClick,
|
||||||
overrideGridClassName,
|
overrideGridClassName,
|
||||||
hideOrderSelector,
|
hideOrderSelector,
|
||||||
showPlaceHolder,
|
|
||||||
cardHideOptions,
|
cardHideOptions,
|
||||||
highlightOptions,
|
highlightOptions,
|
||||||
|
headerClassName,
|
||||||
|
useQuerySortLocalStorage,
|
||||||
|
useQuerySortUrlParams,
|
||||||
} = props
|
} = props
|
||||||
|
|
||||||
const user = useUser()
|
const [numPages, setNumPages] = useState(1)
|
||||||
|
const [pages, setPages] = useState<Contract[][]>([])
|
||||||
|
const [showTime, setShowTime] = useState<ShowTime | undefined>()
|
||||||
|
|
||||||
|
const searchParameters = useRef<SearchParameters | undefined>()
|
||||||
|
const requestId = useRef(0)
|
||||||
|
|
||||||
|
const performQuery = async (freshQuery?: boolean) => {
|
||||||
|
if (searchParameters.current === undefined) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const params = searchParameters.current
|
||||||
|
const id = ++requestId.current
|
||||||
|
const requestedPage = freshQuery ? 0 : pages.length
|
||||||
|
if (freshQuery || requestedPage < numPages) {
|
||||||
|
const results = await params.index.search(params.query, {
|
||||||
|
facetFilters: params.facetFilters,
|
||||||
|
numericFilters: params.numericFilters,
|
||||||
|
page: requestedPage,
|
||||||
|
hitsPerPage: 20,
|
||||||
|
})
|
||||||
|
// if there's a more recent request, forget about this one
|
||||||
|
if (id === requestId.current) {
|
||||||
|
const newPage = results.hits as any as Contract[]
|
||||||
|
// this spooky looking function is the easiest way to get react to
|
||||||
|
// batch this and not do multiple renders. we can throw it out in react 18.
|
||||||
|
// see https://github.com/reactwg/react-18/discussions/21
|
||||||
|
unstable_batchedUpdates(() => {
|
||||||
|
setShowTime(params.showTime)
|
||||||
|
setNumPages(results.nbPages)
|
||||||
|
if (freshQuery) {
|
||||||
|
setPages([newPage])
|
||||||
|
window.scrollTo(0, 0)
|
||||||
|
} else {
|
||||||
|
setPages((pages) => [...pages, newPage])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const onSearchParametersChanged = useRef(
|
||||||
|
debounce((params) => {
|
||||||
|
searchParameters.current = params
|
||||||
|
performQuery(true)
|
||||||
|
}, 100)
|
||||||
|
).current
|
||||||
|
|
||||||
|
const contracts = pages
|
||||||
|
.flat()
|
||||||
|
.filter((c) => !additionalFilter?.excludeContractIds?.includes(c.id))
|
||||||
|
|
||||||
|
if (IS_PRIVATE_MANIFOLD || process.env.NEXT_PUBLIC_FIREBASE_EMULATE) {
|
||||||
|
return <ContractSearchFirestore additionalFilter={additionalFilter} />
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Col className="h-full">
|
||||||
|
<ContractSearchControls
|
||||||
|
className={headerClassName}
|
||||||
|
defaultSort={defaultSort}
|
||||||
|
defaultFilter={defaultFilter}
|
||||||
|
additionalFilter={additionalFilter}
|
||||||
|
hideOrderSelector={hideOrderSelector}
|
||||||
|
useQuerySortLocalStorage={useQuerySortLocalStorage}
|
||||||
|
useQuerySortUrlParams={useQuerySortUrlParams}
|
||||||
|
user={user}
|
||||||
|
onSearchParametersChanged={onSearchParametersChanged}
|
||||||
|
/>
|
||||||
|
<ContractsGrid
|
||||||
|
contracts={pages.length === 0 ? undefined : contracts}
|
||||||
|
loadMore={performQuery}
|
||||||
|
showTime={showTime}
|
||||||
|
onContractClick={onContractClick}
|
||||||
|
overrideGridClassName={overrideGridClassName}
|
||||||
|
highlightOptions={highlightOptions}
|
||||||
|
cardHideOptions={cardHideOptions}
|
||||||
|
/>
|
||||||
|
</Col>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function ContractSearchControls(props: {
|
||||||
|
className?: string
|
||||||
|
defaultSort?: Sort
|
||||||
|
defaultFilter?: filter
|
||||||
|
additionalFilter?: AdditionalFilter
|
||||||
|
hideOrderSelector?: boolean
|
||||||
|
onSearchParametersChanged: (params: SearchParameters) => void
|
||||||
|
useQuerySortLocalStorage?: boolean
|
||||||
|
useQuerySortUrlParams?: boolean
|
||||||
|
user?: User | null
|
||||||
|
}) {
|
||||||
|
const {
|
||||||
|
className,
|
||||||
|
defaultSort,
|
||||||
|
defaultFilter,
|
||||||
|
additionalFilter,
|
||||||
|
hideOrderSelector,
|
||||||
|
onSearchParametersChanged,
|
||||||
|
useQuerySortLocalStorage,
|
||||||
|
useQuerySortUrlParams,
|
||||||
|
user,
|
||||||
|
} = props
|
||||||
|
|
||||||
|
const savedSort = useQuerySortLocalStorage ? getSavedSort() : null
|
||||||
|
const initialSort = savedSort ?? defaultSort ?? 'score'
|
||||||
|
const querySortOpts = { useUrl: !!useQuerySortUrlParams }
|
||||||
|
const [sort, setSort] = useSort(initialSort, querySortOpts)
|
||||||
|
const [query, setQuery] = useQuery('', querySortOpts)
|
||||||
|
const [filter, setFilter] = useState<filter>(defaultFilter ?? 'open')
|
||||||
|
const [pillFilter, setPillFilter] = useState<string | undefined>(undefined)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (useQuerySortLocalStorage) {
|
||||||
|
setSavedSort(sort)
|
||||||
|
}
|
||||||
|
}, [sort])
|
||||||
|
|
||||||
|
const follows = useFollows(user?.id)
|
||||||
const memberGroups = (useMemberGroups(user?.id) ?? []).filter(
|
const memberGroups = (useMemberGroups(user?.id) ?? []).filter(
|
||||||
(group) => !NEW_USER_GROUP_SLUGS.includes(group.slug)
|
(group) => !NEW_USER_GROUP_SLUGS.includes(group.slug)
|
||||||
)
|
)
|
||||||
|
@ -91,31 +240,8 @@ export function ContractSearch(props: {
|
||||||
(group) => group.contractIds.length
|
(group) => group.contractIds.length
|
||||||
).reverse()
|
).reverse()
|
||||||
|
|
||||||
const defaultPillGroups = DEFAULT_CATEGORY_GROUPS as Group[]
|
const pillGroups: { name: string; slug: string }[] =
|
||||||
|
memberPillGroups.length > 0 ? memberPillGroups : DEFAULT_CATEGORY_GROUPS
|
||||||
const pillGroups =
|
|
||||||
memberPillGroups.length > 0 ? memberPillGroups : defaultPillGroups
|
|
||||||
|
|
||||||
const follows = useFollows(user?.id)
|
|
||||||
|
|
||||||
const { shouldLoadFromStorage, defaultSort } = querySortOptions ?? {}
|
|
||||||
const { query, setQuery, sort, setSort } = useQueryAndSortParams({
|
|
||||||
defaultSort,
|
|
||||||
shouldLoadFromStorage,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [filter, setFilter] = useState<filter>(
|
|
||||||
querySortOptions?.defaultFilter ?? 'open'
|
|
||||||
)
|
|
||||||
const pillsEnabled = !additionalFilter && !query
|
|
||||||
|
|
||||||
const [pillFilter, setPillFilter] = useState<string | undefined>(undefined)
|
|
||||||
|
|
||||||
const selectPill = (pill: string | undefined) => () => {
|
|
||||||
setPillFilter(pill)
|
|
||||||
setPage(0)
|
|
||||||
track('select search category', { category: pill ?? 'all' })
|
|
||||||
}
|
|
||||||
|
|
||||||
const additionalFilters = [
|
const additionalFilters = [
|
||||||
additionalFilter?.creatorId
|
additionalFilter?.creatorId
|
||||||
|
@ -162,6 +288,27 @@ export function ContractSearch(props: {
|
||||||
filter === 'closed' ? `closeTime <= ${Date.now()}` : '',
|
filter === 'closed' ? `closeTime <= ${Date.now()}` : '',
|
||||||
].filter((f) => f)
|
].filter((f) => f)
|
||||||
|
|
||||||
|
const selectPill = (pill: string | undefined) => () => {
|
||||||
|
setPillFilter(pill)
|
||||||
|
track('select search category', { category: pill ?? 'all' })
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateQuery = (newQuery: string) => {
|
||||||
|
setQuery(newQuery)
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectFilter = (newFilter: filter) => {
|
||||||
|
if (newFilter === filter) return
|
||||||
|
setFilter(newFilter)
|
||||||
|
track('select search filter', { filter: newFilter })
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectSort = (newSort: Sort) => {
|
||||||
|
if (newSort === sort) return
|
||||||
|
setSort(newSort)
|
||||||
|
track('select search sort', { sort: newSort })
|
||||||
|
}
|
||||||
|
|
||||||
const indexName = `${indexPrefix}contracts-${sort}`
|
const indexName = `${indexPrefix}contracts-${sort}`
|
||||||
const index = useMemo(() => searchClient.initIndex(indexName), [indexName])
|
const index = useMemo(() => searchClient.initIndex(indexName), [indexName])
|
||||||
const searchIndex = useMemo(
|
const searchIndex = useMemo(
|
||||||
|
@ -169,100 +316,28 @@ export function ContractSearch(props: {
|
||||||
[searchIndexName]
|
[searchIndexName]
|
||||||
)
|
)
|
||||||
|
|
||||||
const [page, setPage] = useState(0)
|
|
||||||
const [numPages, setNumPages] = useState(1)
|
|
||||||
const [hitsByPage, setHitsByPage] = useState<{ [page: string]: Contract[] }>(
|
|
||||||
{}
|
|
||||||
)
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let wasMostRecentQuery = true
|
onSearchParametersChanged({
|
||||||
const algoliaIndex = query ? searchIndex : index
|
index: query ? searchIndex : index,
|
||||||
|
query: query,
|
||||||
algoliaIndex
|
numericFilters: numericFilters,
|
||||||
.search(query, {
|
facetFilters: facetFilters,
|
||||||
facetFilters,
|
showTime:
|
||||||
numericFilters,
|
sort === 'close-date' || sort === 'resolve-date' ? sort : undefined,
|
||||||
page,
|
})
|
||||||
hitsPerPage: 20,
|
}, [query, index, searchIndex, filter, JSON.stringify(facetFilters)])
|
||||||
})
|
|
||||||
.then((results) => {
|
|
||||||
if (!wasMostRecentQuery) return
|
|
||||||
|
|
||||||
if (page === 0) {
|
|
||||||
setHitsByPage({
|
|
||||||
[0]: results.hits as any as Contract[],
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
setHitsByPage((hitsByPage) => ({
|
|
||||||
...hitsByPage,
|
|
||||||
[page]: results.hits,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
setNumPages(results.nbPages)
|
|
||||||
})
|
|
||||||
return () => {
|
|
||||||
wasMostRecentQuery = false
|
|
||||||
}
|
|
||||||
// Note numeric filters are unique based on current time, so can't compare
|
|
||||||
// them by value.
|
|
||||||
}, [query, page, index, searchIndex, JSON.stringify(facetFilters), filter])
|
|
||||||
|
|
||||||
const loadMore = () => {
|
|
||||||
if (page >= numPages - 1) return
|
|
||||||
|
|
||||||
const haveLoadedCurrentPage = hitsByPage[page]
|
|
||||||
if (haveLoadedCurrentPage) setPage(page + 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
const hits = range(0, page + 1)
|
|
||||||
.map((p) => hitsByPage[p] ?? [])
|
|
||||||
.flat()
|
|
||||||
|
|
||||||
const contracts = hits.filter(
|
|
||||||
(c) => !additionalFilter?.excludeContractIds?.includes(c.id)
|
|
||||||
)
|
|
||||||
|
|
||||||
const showTime =
|
|
||||||
sort === 'close-date' || sort === 'resolve-date' ? sort : undefined
|
|
||||||
|
|
||||||
const updateQuery = (newQuery: string) => {
|
|
||||||
setQuery(newQuery)
|
|
||||||
setPage(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
const selectFilter = (newFilter: filter) => {
|
|
||||||
if (newFilter === filter) return
|
|
||||||
setFilter(newFilter)
|
|
||||||
setPage(0)
|
|
||||||
trackCallback('select search filter', { filter: newFilter })
|
|
||||||
}
|
|
||||||
|
|
||||||
const selectSort = (newSort: Sort) => {
|
|
||||||
if (newSort === sort) return
|
|
||||||
|
|
||||||
setPage(0)
|
|
||||||
setSort(newSort)
|
|
||||||
track('select sort', { sort: newSort })
|
|
||||||
}
|
|
||||||
|
|
||||||
if (IS_PRIVATE_MANIFOLD || process.env.NEXT_PUBLIC_FIREBASE_EMULATE) {
|
|
||||||
return (
|
|
||||||
<ContractSearchFirestore
|
|
||||||
querySortOptions={querySortOptions}
|
|
||||||
additionalFilter={additionalFilter}
|
|
||||||
/>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Col>
|
<Col
|
||||||
|
className={clsx('bg-base-200 sticky top-0 z-20 gap-3 pb-3', className)}
|
||||||
|
>
|
||||||
<Row className="gap-1 sm:gap-2">
|
<Row className="gap-1 sm:gap-2">
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
value={query}
|
value={query}
|
||||||
onChange={(e) => updateQuery(e.target.value)}
|
onChange={(e) => updateQuery(e.target.value)}
|
||||||
placeholder={showPlaceHolder ? `Search ${filter} markets` : ''}
|
onBlur={trackCallback('search', { query })}
|
||||||
|
placeholder={'Search'}
|
||||||
className="input input-bordered w-full"
|
className="input input-bordered w-full"
|
||||||
/>
|
/>
|
||||||
{!query && (
|
{!query && (
|
||||||
|
@ -292,9 +367,7 @@ export function ContractSearch(props: {
|
||||||
)}
|
)}
|
||||||
</Row>
|
</Row>
|
||||||
|
|
||||||
<Spacer h={3} />
|
{!additionalFilter && !query && (
|
||||||
|
|
||||||
{pillsEnabled && (
|
|
||||||
<Row className="scrollbar-hide items-start gap-2 overflow-x-auto">
|
<Row className="scrollbar-hide items-start gap-2 overflow-x-auto">
|
||||||
<PillButton
|
<PillButton
|
||||||
key={'all'}
|
key={'all'}
|
||||||
|
@ -334,25 +407,6 @@ export function ContractSearch(props: {
|
||||||
})}
|
})}
|
||||||
</Row>
|
</Row>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<Spacer h={3} />
|
|
||||||
|
|
||||||
{filter === 'personal' &&
|
|
||||||
(follows ?? []).length === 0 &&
|
|
||||||
memberGroupSlugs.length === 0 ? (
|
|
||||||
<>You're not following anyone, nor in any of your own groups yet.</>
|
|
||||||
) : (
|
|
||||||
<ContractsGrid
|
|
||||||
contracts={contracts}
|
|
||||||
loadMore={loadMore}
|
|
||||||
hasMore={true}
|
|
||||||
showTime={showTime}
|
|
||||||
onContractClick={onContractClick}
|
|
||||||
overrideGridClassName={overrideGridClassName}
|
|
||||||
highlightOptions={highlightOptions}
|
|
||||||
cardHideOptions={cardHideOptions}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</Col>
|
</Col>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
44
web/components/contract/contract-card-preview.tsx
Normal file
44
web/components/contract/contract-card-preview.tsx
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
import { Contract } from 'common/contract'
|
||||||
|
import { getBinaryProbPercent } from 'web/lib/firebase/contracts'
|
||||||
|
import { richTextToString } from 'common/util/parse'
|
||||||
|
import { contractTextDetails } from 'web/components/contract/contract-details'
|
||||||
|
import { getFormattedMappedValue } from 'common/pseudo-numeric'
|
||||||
|
import { getProbability } from 'common/calculate'
|
||||||
|
|
||||||
|
export const getOpenGraphProps = (contract: Contract) => {
|
||||||
|
const {
|
||||||
|
resolution,
|
||||||
|
question,
|
||||||
|
creatorName,
|
||||||
|
creatorUsername,
|
||||||
|
outcomeType,
|
||||||
|
creatorAvatarUrl,
|
||||||
|
description: desc,
|
||||||
|
} = contract
|
||||||
|
const probPercent =
|
||||||
|
outcomeType === 'BINARY' ? getBinaryProbPercent(contract) : undefined
|
||||||
|
|
||||||
|
const numericValue =
|
||||||
|
outcomeType === 'PSEUDO_NUMERIC'
|
||||||
|
? getFormattedMappedValue(contract)(getProbability(contract))
|
||||||
|
: undefined
|
||||||
|
|
||||||
|
const stringDesc = typeof desc === 'string' ? desc : richTextToString(desc)
|
||||||
|
|
||||||
|
const description = resolution
|
||||||
|
? `Resolved ${resolution}. ${stringDesc}`
|
||||||
|
: probPercent
|
||||||
|
? `${probPercent} chance. ${stringDesc}`
|
||||||
|
: stringDesc
|
||||||
|
|
||||||
|
return {
|
||||||
|
question,
|
||||||
|
probability: probPercent,
|
||||||
|
metadata: contractTextDetails(contract),
|
||||||
|
creatorName,
|
||||||
|
creatorUsername,
|
||||||
|
creatorAvatarUrl,
|
||||||
|
description,
|
||||||
|
numericValue,
|
||||||
|
}
|
||||||
|
}
|
|
@ -31,6 +31,7 @@ import { useUser } from 'web/hooks/use-user'
|
||||||
import { track } from '@amplitude/analytics-browser'
|
import { track } from '@amplitude/analytics-browser'
|
||||||
import { trackCallback } from 'web/lib/service/analytics'
|
import { trackCallback } from 'web/lib/service/analytics'
|
||||||
import { getMappedValue } from 'common/pseudo-numeric'
|
import { getMappedValue } from 'common/pseudo-numeric'
|
||||||
|
import { Tooltip } from '../tooltip'
|
||||||
|
|
||||||
export function ContractCard(props: {
|
export function ContractCard(props: {
|
||||||
contract: Contract
|
contract: Contract
|
||||||
|
@ -65,114 +66,118 @@ export function ContractCard(props: {
|
||||||
!hideQuickBet
|
!hideQuickBet
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<Row
|
||||||
<Col
|
className={clsx(
|
||||||
|
'relative gap-3 self-start rounded-lg bg-white shadow-md hover:cursor-pointer hover:bg-gray-100',
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<Col className="group relative flex-1 gap-3 py-4 pb-12 pl-6">
|
||||||
|
{onClick ? (
|
||||||
|
<a
|
||||||
|
className="absolute top-0 left-0 right-0 bottom-0"
|
||||||
|
href={contractPath(contract)}
|
||||||
|
onClick={(e) => {
|
||||||
|
// Let the browser handle the link click (opens in new tab).
|
||||||
|
if (e.ctrlKey || e.metaKey) return
|
||||||
|
|
||||||
|
e.preventDefault()
|
||||||
|
track('click market card', {
|
||||||
|
slug: contract.slug,
|
||||||
|
contractId: contract.id,
|
||||||
|
})
|
||||||
|
onClick()
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<Link href={contractPath(contract)}>
|
||||||
|
<a
|
||||||
|
onClick={trackCallback('click market card', {
|
||||||
|
slug: contract.slug,
|
||||||
|
contractId: contract.id,
|
||||||
|
})}
|
||||||
|
className="absolute top-0 left-0 right-0 bottom-0"
|
||||||
|
/>
|
||||||
|
</Link>
|
||||||
|
)}
|
||||||
|
<AvatarDetails
|
||||||
|
contract={contract}
|
||||||
|
className={'hidden md:inline-flex'}
|
||||||
|
/>
|
||||||
|
<p
|
||||||
|
className="break-words font-semibold text-indigo-700 group-hover:underline group-hover:decoration-indigo-400 group-hover:decoration-2"
|
||||||
|
style={{ /* For iOS safari */ wordBreak: 'break-word' }}
|
||||||
|
>
|
||||||
|
{question}
|
||||||
|
</p>
|
||||||
|
|
||||||
|
{(outcomeType === 'FREE_RESPONSE' ||
|
||||||
|
outcomeType === 'MULTIPLE_CHOICE') &&
|
||||||
|
(resolution ? (
|
||||||
|
<FreeResponseOutcomeLabel
|
||||||
|
contract={contract}
|
||||||
|
resolution={resolution}
|
||||||
|
truncate={'long'}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<FreeResponseTopAnswer contract={contract} truncate="long" />
|
||||||
|
))}
|
||||||
|
</Col>
|
||||||
|
{showQuickBet ? (
|
||||||
|
<QuickBet contract={contract} user={user} />
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
{outcomeType === 'BINARY' && (
|
||||||
|
<BinaryResolutionOrChance
|
||||||
|
className="items-center self-center pr-5"
|
||||||
|
contract={contract}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{outcomeType === 'PSEUDO_NUMERIC' && (
|
||||||
|
<PseudoNumericResolutionOrExpectation
|
||||||
|
className="items-center self-center pr-5"
|
||||||
|
contract={contract}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{outcomeType === 'NUMERIC' && (
|
||||||
|
<NumericResolutionOrExpectation
|
||||||
|
className="items-center self-center pr-5"
|
||||||
|
contract={contract}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{(outcomeType === 'FREE_RESPONSE' ||
|
||||||
|
outcomeType === 'MULTIPLE_CHOICE') && (
|
||||||
|
<FreeResponseResolutionOrChance
|
||||||
|
className="items-center self-center pr-5 text-gray-600"
|
||||||
|
contract={contract}
|
||||||
|
truncate="long"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<ProbBar contract={contract} />
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
<Row
|
||||||
className={clsx(
|
className={clsx(
|
||||||
'relative gap-3 rounded-lg bg-white py-4 pl-6 pr-5 shadow-md hover:cursor-pointer hover:bg-gray-100',
|
'absolute bottom-3 gap-2 truncate px-5 md:gap-0',
|
||||||
className
|
showQuickBet ? 'w-[85%]' : 'w-full'
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<Row>
|
<AvatarDetails
|
||||||
<Col className="relative flex-1 gap-3 pr-1">
|
contract={contract}
|
||||||
<div
|
short={true}
|
||||||
className={clsx(
|
className={'block md:hidden'}
|
||||||
'peer absolute -left-6 -top-4 -bottom-4 right-0 z-10'
|
/>
|
||||||
)}
|
<MiscDetails
|
||||||
>
|
contract={contract}
|
||||||
{onClick ? (
|
showHotVolume={showHotVolume}
|
||||||
<a
|
showTime={showTime}
|
||||||
className="absolute top-0 left-0 right-0 bottom-0"
|
hideGroupLink={hideGroupLink}
|
||||||
href={contractPath(contract)}
|
/>
|
||||||
onClick={(e) => {
|
</Row>
|
||||||
// Let the browser handle the link click (opens in new tab).
|
</Row>
|
||||||
if (e.ctrlKey || e.metaKey) return
|
|
||||||
|
|
||||||
e.preventDefault()
|
|
||||||
track('click market card', {
|
|
||||||
slug: contract.slug,
|
|
||||||
contractId: contract.id,
|
|
||||||
})
|
|
||||||
onClick()
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
) : (
|
|
||||||
<Link href={contractPath(contract)}>
|
|
||||||
<a
|
|
||||||
onClick={trackCallback('click market card', {
|
|
||||||
slug: contract.slug,
|
|
||||||
contractId: contract.id,
|
|
||||||
})}
|
|
||||||
className="absolute top-0 left-0 right-0 bottom-0"
|
|
||||||
/>
|
|
||||||
</Link>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
<AvatarDetails contract={contract} />
|
|
||||||
<p
|
|
||||||
className="break-words font-semibold text-indigo-700 peer-hover:underline peer-hover:decoration-indigo-400 peer-hover:decoration-2"
|
|
||||||
style={{ /* For iOS safari */ wordBreak: 'break-word' }}
|
|
||||||
>
|
|
||||||
{question}
|
|
||||||
</p>
|
|
||||||
|
|
||||||
{(outcomeType === 'FREE_RESPONSE' ||
|
|
||||||
outcomeType === 'MULTIPLE_CHOICE') &&
|
|
||||||
(resolution ? (
|
|
||||||
<FreeResponseOutcomeLabel
|
|
||||||
contract={contract}
|
|
||||||
resolution={resolution}
|
|
||||||
truncate={'long'}
|
|
||||||
/>
|
|
||||||
) : (
|
|
||||||
<FreeResponseTopAnswer contract={contract} truncate="long" />
|
|
||||||
))}
|
|
||||||
|
|
||||||
<MiscDetails
|
|
||||||
contract={contract}
|
|
||||||
showHotVolume={showHotVolume}
|
|
||||||
showTime={showTime}
|
|
||||||
hideGroupLink={hideGroupLink}
|
|
||||||
/>
|
|
||||||
</Col>
|
|
||||||
{showQuickBet ? (
|
|
||||||
<QuickBet contract={contract} user={user} />
|
|
||||||
) : (
|
|
||||||
<Col className="m-auto pl-2">
|
|
||||||
{outcomeType === 'BINARY' && (
|
|
||||||
<BinaryResolutionOrChance
|
|
||||||
className="items-center"
|
|
||||||
contract={contract}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{outcomeType === 'PSEUDO_NUMERIC' && (
|
|
||||||
<PseudoNumericResolutionOrExpectation
|
|
||||||
className="items-center"
|
|
||||||
contract={contract}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{outcomeType === 'NUMERIC' && (
|
|
||||||
<NumericResolutionOrExpectation
|
|
||||||
className="items-center"
|
|
||||||
contract={contract}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{(outcomeType === 'FREE_RESPONSE' ||
|
|
||||||
outcomeType === 'MULTIPLE_CHOICE') && (
|
|
||||||
<FreeResponseResolutionOrChance
|
|
||||||
className="self-end text-gray-600"
|
|
||||||
contract={contract}
|
|
||||||
truncate="long"
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
<ProbBar contract={contract} />
|
|
||||||
</Col>
|
|
||||||
)}
|
|
||||||
</Row>
|
|
||||||
</Col>
|
|
||||||
</div>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -332,22 +337,19 @@ export function PseudoNumericResolutionOrExpectation(props: {
|
||||||
{resolution === 'CANCEL' ? (
|
{resolution === 'CANCEL' ? (
|
||||||
<CancelLabel />
|
<CancelLabel />
|
||||||
) : (
|
) : (
|
||||||
<div
|
<Tooltip className={textColor} text={value.toFixed(2)}>
|
||||||
className={clsx('tooltip', textColor)}
|
|
||||||
data-tip={value.toFixed(2)}
|
|
||||||
>
|
|
||||||
{formatLargeNumber(value)}
|
{formatLargeNumber(value)}
|
||||||
</div>
|
</Tooltip>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
<div
|
<Tooltip
|
||||||
className={clsx('tooltip text-3xl', textColor)}
|
className={clsx('text-3xl', textColor)}
|
||||||
data-tip={value.toFixed(2)}
|
text={value.toFixed(2)}
|
||||||
>
|
>
|
||||||
{formatLargeNumber(value)}
|
{formatLargeNumber(value)}
|
||||||
</div>
|
</Tooltip>
|
||||||
<div className={clsx('text-base', textColor)}>expected</div>
|
<div className={clsx('text-base', textColor)}>expected</div>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
|
@ -13,6 +13,7 @@ import { TextEditor, useTextEditor } from 'web/components/editor'
|
||||||
import { Button } from '../button'
|
import { Button } from '../button'
|
||||||
import { Spacer } from '../layout/spacer'
|
import { Spacer } from '../layout/spacer'
|
||||||
import { Editor, Content as ContentType } from '@tiptap/react'
|
import { Editor, Content as ContentType } from '@tiptap/react'
|
||||||
|
import { insertContent } from '../editor/utils'
|
||||||
|
|
||||||
export function ContractDescription(props: {
|
export function ContractDescription(props: {
|
||||||
contract: Contract
|
contract: Contract
|
||||||
|
@ -94,12 +95,8 @@ function RichEditContract(props: { contract: Contract; isAdmin?: boolean }) {
|
||||||
size="xs"
|
size="xs"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setEditing(true)
|
setEditing(true)
|
||||||
editor
|
editor?.commands.focus('end')
|
||||||
?.chain()
|
insertContent(editor, `<p>${editTimestamp()}</p>`)
|
||||||
.setContent(contract.description)
|
|
||||||
.focus('end')
|
|
||||||
.insertContent(`<p>${editTimestamp()}</p>`)
|
|
||||||
.run()
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Edit description
|
Edit description
|
||||||
|
@ -131,7 +128,7 @@ function EditQuestion(props: {
|
||||||
|
|
||||||
function joinContent(oldContent: ContentType, newContent: string) {
|
function joinContent(oldContent: ContentType, newContent: string) {
|
||||||
const editor = new Editor({ content: oldContent, extensions: exhibitExts })
|
const editor = new Editor({ content: oldContent, extensions: exhibitExts })
|
||||||
editor.chain().focus('end').insertContent(newContent).run()
|
insertContent(editor, newContent)
|
||||||
return editor.getJSON()
|
return editor.getJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,13 +5,13 @@ import {
|
||||||
TrendingUpIcon,
|
TrendingUpIcon,
|
||||||
UserGroupIcon,
|
UserGroupIcon,
|
||||||
} from '@heroicons/react/outline'
|
} from '@heroicons/react/outline'
|
||||||
|
|
||||||
import { Row } from '../layout/row'
|
import { Row } from '../layout/row'
|
||||||
import { formatMoney } from 'common/util/format'
|
import { formatMoney } from 'common/util/format'
|
||||||
import { UserLink } from '../user-page'
|
import { UserLink } from '../user-page'
|
||||||
import {
|
import {
|
||||||
Contract,
|
Contract,
|
||||||
contractMetrics,
|
contractMetrics,
|
||||||
contractPath,
|
|
||||||
updateContract,
|
updateContract,
|
||||||
} from 'web/lib/firebase/contracts'
|
} from 'web/lib/firebase/contracts'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
|
@ -24,17 +24,17 @@ import { Bet } from 'common/bet'
|
||||||
import NewContractBadge from '../new-contract-badge'
|
import NewContractBadge from '../new-contract-badge'
|
||||||
import { UserFollowButton } from '../follow-button'
|
import { UserFollowButton } from '../follow-button'
|
||||||
import { DAY_MS } from 'common/util/time'
|
import { DAY_MS } from 'common/util/time'
|
||||||
import { ShareIconButton } from 'web/components/share-icon-button'
|
|
||||||
import { useUser } from 'web/hooks/use-user'
|
import { useUser } from 'web/hooks/use-user'
|
||||||
import { Editor } from '@tiptap/react'
|
import { Editor } from '@tiptap/react'
|
||||||
import { exhibitExts } from 'common/util/parse'
|
import { exhibitExts } from 'common/util/parse'
|
||||||
import { ENV_CONFIG } from 'common/envs/constants'
|
|
||||||
import { Button } from 'web/components/button'
|
import { Button } from 'web/components/button'
|
||||||
import { Modal } from 'web/components/layout/modal'
|
import { Modal } from 'web/components/layout/modal'
|
||||||
import { Col } from 'web/components/layout/col'
|
import { Col } from 'web/components/layout/col'
|
||||||
import { ContractGroupsList } from 'web/components/groups/contract-groups-list'
|
import { ContractGroupsList } from 'web/components/groups/contract-groups-list'
|
||||||
import { SiteLink } from 'web/components/site-link'
|
import { SiteLink } from 'web/components/site-link'
|
||||||
import { groupPath } from 'web/lib/firebase/groups'
|
import { groupPath } from 'web/lib/firebase/groups'
|
||||||
|
import { insertContent } from '../editor/utils'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
export type ShowTime = 'resolve-date' | 'close-date'
|
export type ShowTime = 'resolve-date' | 'close-date'
|
||||||
|
|
||||||
|
@ -58,13 +58,13 @@ export function MiscDetails(props: {
|
||||||
const isNew = createdTime > Date.now() - DAY_MS && !isResolved
|
const isNew = createdTime > Date.now() - DAY_MS && !isResolved
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Row className="items-center gap-3 text-sm text-gray-400">
|
<Row className="items-center gap-3 truncate text-sm text-gray-400">
|
||||||
{showHotVolume ? (
|
{showHotVolume ? (
|
||||||
<Row className="gap-0.5">
|
<Row className="gap-0.5">
|
||||||
<TrendingUpIcon className="h-5 w-5" /> {formatMoney(volume24Hours)}
|
<TrendingUpIcon className="h-5 w-5" /> {formatMoney(volume24Hours)}
|
||||||
</Row>
|
</Row>
|
||||||
) : showTime === 'close-date' ? (
|
) : showTime === 'close-date' ? (
|
||||||
<Row className="gap-0.5">
|
<Row className="gap-0.5 whitespace-nowrap">
|
||||||
<ClockIcon className="h-5 w-5" />
|
<ClockIcon className="h-5 w-5" />
|
||||||
{(closeTime || 0) < Date.now() ? 'Closed' : 'Closes'}{' '}
|
{(closeTime || 0) < Date.now() ? 'Closed' : 'Closes'}{' '}
|
||||||
{fromNow(closeTime || 0)}
|
{fromNow(closeTime || 0)}
|
||||||
|
@ -84,30 +84,33 @@ export function MiscDetails(props: {
|
||||||
{!hideGroupLink && groupLinks && groupLinks.length > 0 && (
|
{!hideGroupLink && groupLinks && groupLinks.length > 0 && (
|
||||||
<SiteLink
|
<SiteLink
|
||||||
href={groupPath(groupLinks[0].slug)}
|
href={groupPath(groupLinks[0].slug)}
|
||||||
className="text-sm text-gray-400"
|
className="truncate text-sm text-gray-400"
|
||||||
>
|
>
|
||||||
<Row className={'line-clamp-1 flex-wrap items-center '}>
|
{groupLinks[0].name}
|
||||||
<UserGroupIcon className="mx-1 mb-0.5 inline h-4 w-4 shrink-0" />
|
|
||||||
{groupLinks[0].name}
|
|
||||||
</Row>
|
|
||||||
</SiteLink>
|
</SiteLink>
|
||||||
)}
|
)}
|
||||||
</Row>
|
</Row>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function AvatarDetails(props: { contract: Contract }) {
|
export function AvatarDetails(props: {
|
||||||
const { contract } = props
|
contract: Contract
|
||||||
|
className?: string
|
||||||
|
short?: boolean
|
||||||
|
}) {
|
||||||
|
const { contract, short, className } = props
|
||||||
const { creatorName, creatorUsername } = contract
|
const { creatorName, creatorUsername } = contract
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Row className="items-center gap-2 text-sm text-gray-400">
|
<Row
|
||||||
|
className={clsx('items-center gap-2 text-sm text-gray-400', className)}
|
||||||
|
>
|
||||||
<Avatar
|
<Avatar
|
||||||
username={creatorUsername}
|
username={creatorUsername}
|
||||||
avatarUrl={contract.creatorAvatarUrl}
|
avatarUrl={contract.creatorAvatarUrl}
|
||||||
size={6}
|
size={6}
|
||||||
/>
|
/>
|
||||||
<UserLink name={creatorName} username={creatorUsername} />
|
<UserLink name={creatorName} username={creatorUsername} short={short} />
|
||||||
</Row>
|
</Row>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -147,6 +150,15 @@ export function ContractDetails(props: {
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
const [open, setOpen] = useState(false)
|
const [open, setOpen] = useState(false)
|
||||||
|
|
||||||
|
const groupInfo = (
|
||||||
|
<Row>
|
||||||
|
<UserGroupIcon className="mx-1 inline h-5 w-5 shrink-0" />
|
||||||
|
<span className="truncate">
|
||||||
|
{groupToDisplay ? groupToDisplay.name : 'No group'}
|
||||||
|
</span>
|
||||||
|
</Row>
|
||||||
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Row className="flex-1 flex-wrap items-center gap-x-4 gap-y-2 text-sm text-gray-500">
|
<Row className="flex-1 flex-wrap items-center gap-x-4 gap-y-2 text-sm text-gray-500">
|
||||||
<Row className="items-center gap-2">
|
<Row className="items-center gap-2">
|
||||||
|
@ -168,19 +180,18 @@ export function ContractDetails(props: {
|
||||||
{!disabled && <UserFollowButton userId={creatorId} small />}
|
{!disabled && <UserFollowButton userId={creatorId} small />}
|
||||||
</Row>
|
</Row>
|
||||||
<Row>
|
<Row>
|
||||||
<Button
|
{disabled ? (
|
||||||
size={'xs'}
|
groupInfo
|
||||||
className={'max-w-[200px]'}
|
) : (
|
||||||
color={'gray-white'}
|
<Button
|
||||||
onClick={() => setOpen(!open)}
|
size={'xs'}
|
||||||
>
|
className={'max-w-[200px]'}
|
||||||
<Row>
|
color={'gray-white'}
|
||||||
<UserGroupIcon className="mx-1 inline h-5 w-5 shrink-0" />
|
onClick={() => setOpen(!open)}
|
||||||
<span className={'line-clamp-1'}>
|
>
|
||||||
{groupToDisplay ? groupToDisplay.name : 'No group'}
|
{groupInfo}
|
||||||
</span>
|
</Button>
|
||||||
</Row>
|
)}
|
||||||
</Button>
|
|
||||||
</Row>
|
</Row>
|
||||||
<Modal open={open} setOpen={setOpen} size={'md'}>
|
<Modal open={open} setOpen={setOpen} size={'md'}>
|
||||||
<Col
|
<Col
|
||||||
|
@ -204,7 +215,7 @@ export function ContractDetails(props: {
|
||||||
<>
|
<>
|
||||||
<DateTimeTooltip
|
<DateTimeTooltip
|
||||||
text="Market resolved:"
|
text="Market resolved:"
|
||||||
time={contract.resolutionTime}
|
time={dayjs(contract.resolutionTime)}
|
||||||
>
|
>
|
||||||
{resolvedDate}
|
{resolvedDate}
|
||||||
</DateTimeTooltip>
|
</DateTimeTooltip>
|
||||||
|
@ -228,14 +239,6 @@ export function ContractDetails(props: {
|
||||||
|
|
||||||
<div className="whitespace-nowrap">{volumeLabel}</div>
|
<div className="whitespace-nowrap">{volumeLabel}</div>
|
||||||
</Row>
|
</Row>
|
||||||
<ShareIconButton
|
|
||||||
copyPayload={`https://${ENV_CONFIG.domain}${contractPath(contract)}${
|
|
||||||
user?.username && contract.creatorUsername !== user?.username
|
|
||||||
? '?referrer=' + user?.username
|
|
||||||
: ''
|
|
||||||
}`}
|
|
||||||
toastClassName={'sm:-left-40 -left-24 min-w-[250%]'}
|
|
||||||
/>
|
|
||||||
|
|
||||||
{!disabled && <ContractInfoDialog contract={contract} bets={bets} />}
|
{!disabled && <ContractInfoDialog contract={contract} bets={bets} />}
|
||||||
</Row>
|
</Row>
|
||||||
|
@ -268,13 +271,16 @@ function EditableCloseDate(props: {
|
||||||
}) {
|
}) {
|
||||||
const { closeTime, contract, isCreator } = props
|
const { closeTime, contract, isCreator } = props
|
||||||
|
|
||||||
|
const dayJsCloseTime = dayjs(closeTime)
|
||||||
|
const dayJsNow = dayjs()
|
||||||
|
|
||||||
const [isEditingCloseTime, setIsEditingCloseTime] = useState(false)
|
const [isEditingCloseTime, setIsEditingCloseTime] = useState(false)
|
||||||
const [closeDate, setCloseDate] = useState(
|
const [closeDate, setCloseDate] = useState(
|
||||||
closeTime && dayjs(closeTime).format('YYYY-MM-DDTHH:mm')
|
closeTime && dayJsCloseTime.format('YYYY-MM-DDTHH:mm')
|
||||||
)
|
)
|
||||||
|
|
||||||
const isSameYear = dayjs(closeTime).isSame(dayjs(), 'year')
|
const isSameYear = dayJsCloseTime.isSame(dayJsNow, 'year')
|
||||||
const isSameDay = dayjs(closeTime).isSame(dayjs(), 'day')
|
const isSameDay = dayJsCloseTime.isSame(dayJsNow, 'day')
|
||||||
|
|
||||||
const onSave = () => {
|
const onSave = () => {
|
||||||
const newCloseTime = dayjs(closeDate).valueOf()
|
const newCloseTime = dayjs(closeDate).valueOf()
|
||||||
|
@ -284,12 +290,11 @@ function EditableCloseDate(props: {
|
||||||
const formattedCloseDate = dayjs(newCloseTime).format('YYYY-MM-DD h:mm a')
|
const formattedCloseDate = dayjs(newCloseTime).format('YYYY-MM-DD h:mm a')
|
||||||
|
|
||||||
const editor = new Editor({ content, extensions: exhibitExts })
|
const editor = new Editor({ content, extensions: exhibitExts })
|
||||||
editor
|
editor.commands.focus('end')
|
||||||
.chain()
|
insertContent(
|
||||||
.focus('end')
|
editor,
|
||||||
.insertContent('<br /><br />')
|
`<br><p>Close date updated to ${formattedCloseDate}</p>`
|
||||||
.insertContent(`Close date updated to ${formattedCloseDate}`)
|
)
|
||||||
.run()
|
|
||||||
|
|
||||||
updateContract(contract.id, {
|
updateContract(contract.id, {
|
||||||
closeTime: newCloseTime,
|
closeTime: newCloseTime,
|
||||||
|
@ -316,11 +321,11 @@ function EditableCloseDate(props: {
|
||||||
) : (
|
) : (
|
||||||
<DateTimeTooltip
|
<DateTimeTooltip
|
||||||
text={closeTime > Date.now() ? 'Trading ends:' : 'Trading ended:'}
|
text={closeTime > Date.now() ? 'Trading ends:' : 'Trading ended:'}
|
||||||
time={closeTime}
|
time={dayJsCloseTime}
|
||||||
>
|
>
|
||||||
{isSameYear
|
{isSameYear
|
||||||
? dayjs(closeTime).format('MMM D')
|
? dayJsCloseTime.format('MMM D')
|
||||||
: dayjs(closeTime).format('MMM D, YYYY')}
|
: dayJsCloseTime.format('MMM D, YYYY')}
|
||||||
{isSameDay && <> ({fromNow(closeTime)})</>}
|
{isSameDay && <> ({fromNow(closeTime)})</>}
|
||||||
</DateTimeTooltip>
|
</DateTimeTooltip>
|
||||||
)}
|
)}
|
||||||
|
|
|
@ -7,16 +7,12 @@ import { Bet } from 'common/bet'
|
||||||
|
|
||||||
import { Contract } from 'common/contract'
|
import { Contract } from 'common/contract'
|
||||||
import { formatMoney } from 'common/util/format'
|
import { formatMoney } from 'common/util/format'
|
||||||
import { contractPath, contractPool } from 'web/lib/firebase/contracts'
|
import { contractPool } from 'web/lib/firebase/contracts'
|
||||||
import { LiquidityPanel } from '../liquidity-panel'
|
import { LiquidityPanel } from '../liquidity-panel'
|
||||||
import { Col } from '../layout/col'
|
import { Col } from '../layout/col'
|
||||||
import { Modal } from '../layout/modal'
|
import { Modal } from '../layout/modal'
|
||||||
import { Row } from '../layout/row'
|
|
||||||
import { ShareEmbedButton } from '../share-embed-button'
|
|
||||||
import { Title } from '../title'
|
import { Title } from '../title'
|
||||||
import { TweetButton } from '../tweet-button'
|
|
||||||
import { InfoTooltip } from '../info-tooltip'
|
import { InfoTooltip } from '../info-tooltip'
|
||||||
import { DuplicateContractButton } from '../copy-contract-button'
|
|
||||||
|
|
||||||
export const contractDetailsButtonClassName =
|
export const contractDetailsButtonClassName =
|
||||||
'group flex items-center rounded-md px-3 py-2 text-sm font-medium cursor-pointer hover:bg-gray-100 text-gray-400 hover:text-gray-500'
|
'group flex items-center rounded-md px-3 py-2 text-sm font-medium cursor-pointer hover:bg-gray-100 text-gray-400 hover:text-gray-500'
|
||||||
|
@ -61,20 +57,6 @@ export function ContractInfoDialog(props: { contract: Contract; bets: Bet[] }) {
|
||||||
<Col className="gap-4 rounded bg-white p-6">
|
<Col className="gap-4 rounded bg-white p-6">
|
||||||
<Title className="!mt-0 !mb-0" text="Market info" />
|
<Title className="!mt-0 !mb-0" text="Market info" />
|
||||||
|
|
||||||
<div>Share</div>
|
|
||||||
|
|
||||||
<Row className="justify-start gap-4">
|
|
||||||
<TweetButton
|
|
||||||
className="self-start"
|
|
||||||
tweetText={getTweetText(contract)}
|
|
||||||
/>
|
|
||||||
<ShareEmbedButton contract={contract} toastClassName={'-left-20'} />
|
|
||||||
<DuplicateContractButton contract={contract} />
|
|
||||||
</Row>
|
|
||||||
<div />
|
|
||||||
|
|
||||||
<div>Stats</div>
|
|
||||||
|
|
||||||
<table className="table-compact table-zebra table w-full text-gray-500">
|
<table className="table-compact table-zebra table w-full text-gray-500">
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
|
@ -150,14 +132,3 @@ export function ContractInfoDialog(props: { contract: Contract; bets: Bet[] }) {
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const getTweetText = (contract: Contract) => {
|
|
||||||
const { question, resolution } = contract
|
|
||||||
|
|
||||||
const tweetDescription = resolution ? `\n\nResolved ${resolution}!` : ''
|
|
||||||
|
|
||||||
const timeParam = `${Date.now()}`.substring(7)
|
|
||||||
const url = `https://manifold.markets${contractPath(contract)}?t=${timeParam}`
|
|
||||||
|
|
||||||
return `${question}\n\n${url}${tweetDescription}`
|
|
||||||
}
|
|
||||||
|
|
|
@ -107,7 +107,6 @@ export function ContractTopTrades(props: {
|
||||||
comment={commentsById[topCommentId]}
|
comment={commentsById[topCommentId]}
|
||||||
tips={tips[topCommentId]}
|
tips={tips[topCommentId]}
|
||||||
betsBySameUser={[betsById[topCommentId]]}
|
betsBySameUser={[betsById[topCommentId]]}
|
||||||
truncate={false}
|
|
||||||
smallAvatar={false}
|
smallAvatar={false}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
import React from 'react'
|
||||||
|
import clsx from 'clsx'
|
||||||
|
|
||||||
import { tradingAllowed } from 'web/lib/firebase/contracts'
|
import { tradingAllowed } from 'web/lib/firebase/contracts'
|
||||||
import { Col } from '../layout/col'
|
import { Col } from '../layout/col'
|
||||||
import { Spacer } from '../layout/spacer'
|
import { Spacer } from '../layout/spacer'
|
||||||
|
@ -5,11 +8,9 @@ import { ContractProbGraph } from './contract-prob-graph'
|
||||||
import { useUser } from 'web/hooks/use-user'
|
import { useUser } from 'web/hooks/use-user'
|
||||||
import { Row } from '../layout/row'
|
import { Row } from '../layout/row'
|
||||||
import { Linkify } from '../linkify'
|
import { Linkify } from '../linkify'
|
||||||
import clsx from 'clsx'
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
FreeResponseResolutionOrChance,
|
|
||||||
BinaryResolutionOrChance,
|
BinaryResolutionOrChance,
|
||||||
|
FreeResponseResolutionOrChance,
|
||||||
NumericResolutionOrExpectation,
|
NumericResolutionOrExpectation,
|
||||||
PseudoNumericResolutionOrExpectation,
|
PseudoNumericResolutionOrExpectation,
|
||||||
} from './contract-card'
|
} from './contract-card'
|
||||||
|
@ -19,8 +20,8 @@ import { AnswersGraph } from '../answers/answers-graph'
|
||||||
import { Contract, CPMMBinaryContract } from 'common/contract'
|
import { Contract, CPMMBinaryContract } from 'common/contract'
|
||||||
import { ContractDescription } from './contract-description'
|
import { ContractDescription } from './contract-description'
|
||||||
import { ContractDetails } from './contract-details'
|
import { ContractDetails } from './contract-details'
|
||||||
import { ShareMarket } from '../share-market'
|
|
||||||
import { NumericGraph } from './numeric-graph'
|
import { NumericGraph } from './numeric-graph'
|
||||||
|
import { ShareRow } from './share-row'
|
||||||
|
|
||||||
export const ContractOverview = (props: {
|
export const ContractOverview = (props: {
|
||||||
contract: Contract
|
contract: Contract
|
||||||
|
@ -32,6 +33,7 @@ export const ContractOverview = (props: {
|
||||||
|
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
const isCreator = user?.id === creatorId
|
const isCreator = user?.id === creatorId
|
||||||
|
|
||||||
const isBinary = outcomeType === 'BINARY'
|
const isBinary = outcomeType === 'BINARY'
|
||||||
const isPseudoNumeric = outcomeType === 'PSEUDO_NUMERIC'
|
const isPseudoNumeric = outcomeType === 'PSEUDO_NUMERIC'
|
||||||
|
|
||||||
|
@ -116,8 +118,7 @@ export const ContractOverview = (props: {
|
||||||
<AnswersGraph contract={contract} bets={bets} />
|
<AnswersGraph contract={contract} bets={bets} />
|
||||||
)}
|
)}
|
||||||
{outcomeType === 'NUMERIC' && <NumericGraph contract={contract} />}
|
{outcomeType === 'NUMERIC' && <NumericGraph contract={contract} />}
|
||||||
{(contract.description || isCreator) && <Spacer h={6} />}
|
<ShareRow user={user} contract={contract} />
|
||||||
{isCreator && <ShareMarket className="px-2" contract={contract} />}
|
|
||||||
<ContractDescription
|
<ContractDescription
|
||||||
className="px-2"
|
className="px-2"
|
||||||
contract={contract}
|
contract={contract}
|
||||||
|
|
|
@ -8,18 +8,17 @@ import { Spacer } from '../layout/spacer'
|
||||||
import { Tabs } from '../layout/tabs'
|
import { Tabs } from '../layout/tabs'
|
||||||
import { Col } from '../layout/col'
|
import { Col } from '../layout/col'
|
||||||
import { CommentTipMap } from 'web/hooks/use-tip-txns'
|
import { CommentTipMap } from 'web/hooks/use-tip-txns'
|
||||||
import { LiquidityProvision } from 'common/liquidity-provision'
|
|
||||||
import { useComments } from 'web/hooks/use-comments'
|
import { useComments } from 'web/hooks/use-comments'
|
||||||
|
import { useLiquidity } from 'web/hooks/use-liquidity'
|
||||||
|
|
||||||
export function ContractTabs(props: {
|
export function ContractTabs(props: {
|
||||||
contract: Contract
|
contract: Contract
|
||||||
user: User | null | undefined
|
user: User | null | undefined
|
||||||
bets: Bet[]
|
bets: Bet[]
|
||||||
liquidityProvisions: LiquidityProvision[]
|
|
||||||
comments: Comment[]
|
comments: Comment[]
|
||||||
tips: CommentTipMap
|
tips: CommentTipMap
|
||||||
}) {
|
}) {
|
||||||
const { contract, user, bets, tips, liquidityProvisions } = props
|
const { contract, user, bets, tips } = props
|
||||||
const { outcomeType } = contract
|
const { outcomeType } = contract
|
||||||
|
|
||||||
const userBets = user && bets.filter((bet) => bet.userId === user.id)
|
const userBets = user && bets.filter((bet) => bet.userId === user.id)
|
||||||
|
@ -27,6 +26,9 @@ export function ContractTabs(props: {
|
||||||
(bet) => !bet.isAnte && !bet.isRedemption && bet.amount !== 0
|
(bet) => !bet.isAnte && !bet.isRedemption && bet.amount !== 0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const liquidityProvisions =
|
||||||
|
useLiquidity(contract.id)?.filter((l) => !l.isAnte && l.amount > 0) ?? []
|
||||||
|
|
||||||
// Load comments here, so the badge count will be correct
|
// Load comments here, so the badge count will be correct
|
||||||
const updatedComments = useComments(contract.id)
|
const updatedComments = useComments(contract.id)
|
||||||
const comments = updatedComments ?? props.comments
|
const comments = updatedComments ?? props.comments
|
||||||
|
|
|
@ -5,9 +5,10 @@ import { SiteLink } from '../site-link'
|
||||||
import { ContractCard } from './contract-card'
|
import { ContractCard } from './contract-card'
|
||||||
import { ShowTime } from './contract-details'
|
import { ShowTime } from './contract-details'
|
||||||
import { ContractSearch } from '../contract-search'
|
import { ContractSearch } from '../contract-search'
|
||||||
import { useIsVisible } from 'web/hooks/use-is-visible'
|
import { useCallback } from 'react'
|
||||||
import { useEffect, useState } from 'react'
|
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
|
import { LoadingIndicator } from '../loading-indicator'
|
||||||
|
import { VisibilityObserver } from '../visibility-observer'
|
||||||
|
|
||||||
export type ContractHighlightOptions = {
|
export type ContractHighlightOptions = {
|
||||||
contractIds?: string[]
|
contractIds?: string[]
|
||||||
|
@ -15,9 +16,8 @@ export type ContractHighlightOptions = {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ContractsGrid(props: {
|
export function ContractsGrid(props: {
|
||||||
contracts: Contract[]
|
contracts: Contract[] | undefined
|
||||||
loadMore: () => void
|
loadMore?: () => void
|
||||||
hasMore: boolean
|
|
||||||
showTime?: ShowTime
|
showTime?: ShowTime
|
||||||
onContractClick?: (contract: Contract) => void
|
onContractClick?: (contract: Contract) => void
|
||||||
overrideGridClassName?: string
|
overrideGridClassName?: string
|
||||||
|
@ -30,7 +30,6 @@ export function ContractsGrid(props: {
|
||||||
const {
|
const {
|
||||||
contracts,
|
contracts,
|
||||||
showTime,
|
showTime,
|
||||||
hasMore,
|
|
||||||
loadMore,
|
loadMore,
|
||||||
onContractClick,
|
onContractClick,
|
||||||
overrideGridClassName,
|
overrideGridClassName,
|
||||||
|
@ -38,16 +37,19 @@ export function ContractsGrid(props: {
|
||||||
highlightOptions,
|
highlightOptions,
|
||||||
} = props
|
} = props
|
||||||
const { hideQuickBet, hideGroupLink } = cardHideOptions || {}
|
const { hideQuickBet, hideGroupLink } = cardHideOptions || {}
|
||||||
|
|
||||||
const { contractIds, highlightClassName } = highlightOptions || {}
|
const { contractIds, highlightClassName } = highlightOptions || {}
|
||||||
const [elem, setElem] = useState<HTMLElement | null>(null)
|
const onVisibilityUpdated = useCallback(
|
||||||
const isBottomVisible = useIsVisible(elem)
|
(visible) => {
|
||||||
|
if (visible && loadMore) {
|
||||||
|
loadMore()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[loadMore]
|
||||||
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
if (contracts === undefined) {
|
||||||
if (isBottomVisible && hasMore) {
|
return <LoadingIndicator />
|
||||||
loadMore()
|
}
|
||||||
}
|
|
||||||
}, [isBottomVisible, hasMore, loadMore])
|
|
||||||
|
|
||||||
if (contracts.length === 0) {
|
if (contracts.length === 0) {
|
||||||
return (
|
return (
|
||||||
|
@ -87,21 +89,25 @@ export function ContractsGrid(props: {
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
<div ref={setElem} className="relative -top-96 h-1" />
|
<VisibilityObserver
|
||||||
|
onVisibilityUpdated={onVisibilityUpdated}
|
||||||
|
className="relative -top-96 h-1"
|
||||||
|
/>
|
||||||
</Col>
|
</Col>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function CreatorContractsList(props: { creator: User }) {
|
export function CreatorContractsList(props: {
|
||||||
const { creator } = props
|
user: User | null | undefined
|
||||||
|
creator: User
|
||||||
|
}) {
|
||||||
|
const { user, creator } = props
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ContractSearch
|
<ContractSearch
|
||||||
querySortOptions={{
|
user={user}
|
||||||
defaultSort: 'newest',
|
defaultSort="newest"
|
||||||
defaultFilter: 'all',
|
defaultFilter="all"
|
||||||
shouldLoadFromStorage: false,
|
|
||||||
}}
|
|
||||||
additionalFilter={{
|
additionalFilter={{
|
||||||
creatorId: creator.id,
|
creatorId: creator.id,
|
||||||
}}
|
}}
|
|
@ -138,7 +138,7 @@ export function QuickBet(props: {
|
||||||
return (
|
return (
|
||||||
<Col
|
<Col
|
||||||
className={clsx(
|
className={clsx(
|
||||||
'relative -my-4 -mr-5 min-w-[5.5rem] justify-center gap-2 pr-5 pl-1 align-middle'
|
'relative min-w-[5.5rem] justify-center gap-2 pr-5 pl-1 align-middle'
|
||||||
// Use this for colored QuickBet panes
|
// Use this for colored QuickBet panes
|
||||||
// `bg-opacity-10 bg-${color}`
|
// `bg-opacity-10 bg-${color}`
|
||||||
)}
|
)}
|
||||||
|
@ -319,7 +319,7 @@ function getProb(contract: Contract) {
|
||||||
? getBinaryProb(contract)
|
? getBinaryProb(contract)
|
||||||
: outcomeType === 'PSEUDO_NUMERIC'
|
: outcomeType === 'PSEUDO_NUMERIC'
|
||||||
? getProbability(contract)
|
? getProbability(contract)
|
||||||
: outcomeType === 'FREE_RESPONSE'
|
: outcomeType === 'FREE_RESPONSE' || outcomeType === 'MULTIPLE_CHOICE'
|
||||||
? getOutcomeProbability(contract, getTopAnswer(contract)?.id || '')
|
? getOutcomeProbability(contract, getTopAnswer(contract)?.id || '')
|
||||||
: outcomeType === 'NUMERIC'
|
: outcomeType === 'NUMERIC'
|
||||||
? getNumericScale(contract)
|
? getNumericScale(contract)
|
||||||
|
|
82
web/components/contract/share-modal.tsx
Normal file
82
web/components/contract/share-modal.tsx
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
import { LinkIcon } from '@heroicons/react/outline'
|
||||||
|
import toast from 'react-hot-toast'
|
||||||
|
|
||||||
|
import { Contract } from 'common/contract'
|
||||||
|
import { contractPath } from 'web/lib/firebase/contracts'
|
||||||
|
import { Col } from '../layout/col'
|
||||||
|
import { Modal } from '../layout/modal'
|
||||||
|
import { Row } from '../layout/row'
|
||||||
|
import { ShareEmbedButton } from '../share-embed-button'
|
||||||
|
import { Title } from '../title'
|
||||||
|
import { TweetButton } from '../tweet-button'
|
||||||
|
import { DuplicateContractButton } from '../copy-contract-button'
|
||||||
|
import { Button } from '../button'
|
||||||
|
import { copyToClipboard } from 'web/lib/util/copy'
|
||||||
|
import { track } from 'web/lib/service/analytics'
|
||||||
|
import { ENV_CONFIG } from 'common/envs/constants'
|
||||||
|
import { REFERRAL_AMOUNT, User } from 'common/user'
|
||||||
|
import { SiteLink } from '../site-link'
|
||||||
|
import { formatMoney } from 'common/util/format'
|
||||||
|
|
||||||
|
export function ShareModal(props: {
|
||||||
|
contract: Contract
|
||||||
|
user: User | undefined | null
|
||||||
|
isOpen: boolean
|
||||||
|
setOpen: (open: boolean) => void
|
||||||
|
}) {
|
||||||
|
const { contract, user, isOpen, setOpen } = props
|
||||||
|
|
||||||
|
const linkIcon = <LinkIcon className="mr-2 h-6 w-6" aria-hidden="true" />
|
||||||
|
|
||||||
|
const shareUrl = `https://${ENV_CONFIG.domain}${contractPath(contract)}${
|
||||||
|
user?.username && contract.creatorUsername !== user?.username
|
||||||
|
? '?referrer=' + user?.username
|
||||||
|
: ''
|
||||||
|
}`
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Modal open={isOpen} setOpen={setOpen} size="md">
|
||||||
|
<Col className="gap-4 rounded bg-white p-4">
|
||||||
|
<Title className="!mt-0 !mb-2" text="Share this market" />
|
||||||
|
<p>
|
||||||
|
Earn{' '}
|
||||||
|
<SiteLink href="/referrals">
|
||||||
|
{formatMoney(REFERRAL_AMOUNT)} referral bonus
|
||||||
|
</SiteLink>{' '}
|
||||||
|
if a new user signs up using the link!
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
size="2xl"
|
||||||
|
color="gradient"
|
||||||
|
className={'mb-2 flex max-w-xs self-center'}
|
||||||
|
onClick={() => {
|
||||||
|
copyToClipboard(shareUrl)
|
||||||
|
toast.success('Link copied!', {
|
||||||
|
icon: linkIcon,
|
||||||
|
})
|
||||||
|
track('copy share link')
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{linkIcon} Copy link
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<Row className="z-0 justify-start gap-4 self-center">
|
||||||
|
<TweetButton
|
||||||
|
className="self-start"
|
||||||
|
tweetText={getTweetText(contract, shareUrl)}
|
||||||
|
/>
|
||||||
|
<ShareEmbedButton contract={contract} />
|
||||||
|
<DuplicateContractButton contract={contract} />
|
||||||
|
</Row>
|
||||||
|
</Col>
|
||||||
|
</Modal>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const getTweetText = (contract: Contract, url: string) => {
|
||||||
|
const { question, resolution } = contract
|
||||||
|
const tweetDescription = resolution ? `\n\nResolved ${resolution}!` : ''
|
||||||
|
|
||||||
|
return `${question}\n\n${url}${tweetDescription}`
|
||||||
|
}
|
67
web/components/contract/share-row.tsx
Normal file
67
web/components/contract/share-row.tsx
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
import clsx from 'clsx'
|
||||||
|
import { ShareIcon } from '@heroicons/react/outline'
|
||||||
|
|
||||||
|
import { Row } from '../layout/row'
|
||||||
|
import { Contract } from 'web/lib/firebase/contracts'
|
||||||
|
import { useState } from 'react'
|
||||||
|
import { Button } from 'web/components/button'
|
||||||
|
import { CreateChallengeModal } from '../challenges/create-challenge-modal'
|
||||||
|
import { User } from 'common/user'
|
||||||
|
import { CHALLENGES_ENABLED } from 'common/challenge'
|
||||||
|
import { ShareModal } from './share-modal'
|
||||||
|
import { withTracking } from 'web/lib/service/analytics'
|
||||||
|
|
||||||
|
export function ShareRow(props: {
|
||||||
|
contract: Contract
|
||||||
|
user: User | undefined | null
|
||||||
|
}) {
|
||||||
|
const { user, contract } = props
|
||||||
|
const { outcomeType, resolution } = contract
|
||||||
|
|
||||||
|
const showChallenge =
|
||||||
|
user && outcomeType === 'BINARY' && !resolution && CHALLENGES_ENABLED
|
||||||
|
|
||||||
|
const [isOpen, setIsOpen] = useState(false)
|
||||||
|
const [isShareOpen, setShareOpen] = useState(false)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Row className="mt-2">
|
||||||
|
<Button
|
||||||
|
size="lg"
|
||||||
|
color="gray-white"
|
||||||
|
className={'flex'}
|
||||||
|
onClick={() => {
|
||||||
|
setShareOpen(true)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<ShareIcon className={clsx('mr-2 h-[24px] w-5')} aria-hidden="true" />
|
||||||
|
Share
|
||||||
|
<ShareModal
|
||||||
|
isOpen={isShareOpen}
|
||||||
|
setOpen={setShareOpen}
|
||||||
|
contract={contract}
|
||||||
|
user={user}
|
||||||
|
/>
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
{showChallenge && (
|
||||||
|
<Button
|
||||||
|
size="lg"
|
||||||
|
color="gray-white"
|
||||||
|
onClick={withTracking(
|
||||||
|
() => setIsOpen(true),
|
||||||
|
'click challenge button'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
⚔️ Challenge
|
||||||
|
<CreateChallengeModal
|
||||||
|
isOpen={isOpen}
|
||||||
|
setOpen={setIsOpen}
|
||||||
|
user={user}
|
||||||
|
contract={contract}
|
||||||
|
/>
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</Row>
|
||||||
|
)
|
||||||
|
}
|
|
@ -2,7 +2,6 @@ import React, { Fragment } from 'react'
|
||||||
import { LinkIcon } from '@heroicons/react/outline'
|
import { LinkIcon } from '@heroicons/react/outline'
|
||||||
import { Menu, Transition } from '@headlessui/react'
|
import { Menu, Transition } from '@headlessui/react'
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
|
|
||||||
import { copyToClipboard } from 'web/lib/util/copy'
|
import { copyToClipboard } from 'web/lib/util/copy'
|
||||||
import { ToastClipboard } from 'web/components/toast-clipboard'
|
import { ToastClipboard } from 'web/components/toast-clipboard'
|
||||||
import { track } from 'web/lib/service/analytics'
|
import { track } from 'web/lib/service/analytics'
|
||||||
|
@ -14,6 +13,8 @@ export function CopyLinkButton(props: {
|
||||||
tracking?: string
|
tracking?: string
|
||||||
buttonClassName?: string
|
buttonClassName?: string
|
||||||
toastClassName?: string
|
toastClassName?: string
|
||||||
|
icon?: React.ComponentType<{ className?: string }>
|
||||||
|
label?: string
|
||||||
}) {
|
}) {
|
||||||
const { url, displayUrl, tracking, buttonClassName, toastClassName } = props
|
const { url, displayUrl, tracking, buttonClassName, toastClassName } = props
|
||||||
|
|
||||||
|
|
|
@ -1,35 +1,28 @@
|
||||||
import React from 'react'
|
import dayjs, { Dayjs } from 'dayjs'
|
||||||
import dayjs from 'dayjs'
|
|
||||||
import utc from 'dayjs/plugin/utc'
|
import utc from 'dayjs/plugin/utc'
|
||||||
import timezone from 'dayjs/plugin/timezone'
|
import timezone from 'dayjs/plugin/timezone'
|
||||||
import advanced from 'dayjs/plugin/advancedFormat'
|
import advanced from 'dayjs/plugin/advancedFormat'
|
||||||
import { ClientRender } from './client-render'
|
import { Tooltip } from './tooltip'
|
||||||
|
|
||||||
dayjs.extend(utc)
|
dayjs.extend(utc)
|
||||||
dayjs.extend(timezone)
|
dayjs.extend(timezone)
|
||||||
dayjs.extend(advanced)
|
dayjs.extend(advanced)
|
||||||
|
|
||||||
export function DateTimeTooltip(props: {
|
export function DateTimeTooltip(props: {
|
||||||
time: number
|
time: Dayjs
|
||||||
text?: string
|
text?: string
|
||||||
|
className?: string
|
||||||
children?: React.ReactNode
|
children?: React.ReactNode
|
||||||
|
noTap?: boolean
|
||||||
}) {
|
}) {
|
||||||
const { time, text } = props
|
const { className, time, text, noTap } = props
|
||||||
|
|
||||||
const formattedTime = dayjs(time).format('MMM DD, YYYY hh:mm a z')
|
const formattedTime = time.format('MMM DD, YYYY hh:mm a z')
|
||||||
const toolTip = text ? `${text} ${formattedTime}` : formattedTime
|
const toolTip = text ? `${text} ${formattedTime}` : formattedTime
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<Tooltip className={className} text={toolTip} noTap={noTap}>
|
||||||
<ClientRender>
|
{props.children}
|
||||||
<span
|
</Tooltip>
|
||||||
className="tooltip hidden cursor-default sm:inline-block"
|
|
||||||
data-tip={toolTip}
|
|
||||||
>
|
|
||||||
{props.children}
|
|
||||||
</span>
|
|
||||||
</ClientRender>
|
|
||||||
<span className="whitespace-nowrap sm:hidden">{props.children}</span>
|
|
||||||
</>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ import Placeholder from '@tiptap/extension-placeholder'
|
||||||
import {
|
import {
|
||||||
useEditor,
|
useEditor,
|
||||||
EditorContent,
|
EditorContent,
|
||||||
FloatingMenu,
|
|
||||||
JSONContent,
|
JSONContent,
|
||||||
Content,
|
Content,
|
||||||
Editor,
|
Editor,
|
||||||
|
@ -11,28 +10,42 @@ import {
|
||||||
import StarterKit from '@tiptap/starter-kit'
|
import StarterKit from '@tiptap/starter-kit'
|
||||||
import { Image } from '@tiptap/extension-image'
|
import { Image } from '@tiptap/extension-image'
|
||||||
import { Link } from '@tiptap/extension-link'
|
import { Link } from '@tiptap/extension-link'
|
||||||
import { Mention } from '@tiptap/extension-mention'
|
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { Linkify } from './linkify'
|
import { Linkify } from './linkify'
|
||||||
import { uploadImage } from 'web/lib/firebase/storage'
|
import { uploadImage } from 'web/lib/firebase/storage'
|
||||||
import { useMutation } from 'react-query'
|
import { useMutation } from 'react-query'
|
||||||
import { exhibitExts } from 'common/util/parse'
|
|
||||||
import { FileUploadButton } from './file-upload-button'
|
import { FileUploadButton } from './file-upload-button'
|
||||||
import { linkClass } from './site-link'
|
import { linkClass } from './site-link'
|
||||||
import { useUsers } from 'web/hooks/use-users'
|
import { useUsers } from 'web/hooks/use-users'
|
||||||
import { mentionSuggestion } from './editor/mention-suggestion'
|
import { mentionSuggestion } from './editor/mention-suggestion'
|
||||||
import { DisplayMention } from './editor/mention'
|
import { DisplayMention } from './editor/mention'
|
||||||
import Iframe from 'common/util/tiptap-iframe'
|
import Iframe from 'common/util/tiptap-iframe'
|
||||||
import { CodeIcon, PhotographIcon } from '@heroicons/react/solid'
|
import TiptapTweet from './editor/tiptap-tweet'
|
||||||
import { Modal } from './layout/modal'
|
import { EmbedModal } from './editor/embed-modal'
|
||||||
import { Col } from './layout/col'
|
import {
|
||||||
import { Button } from './button'
|
CodeIcon,
|
||||||
import { Row } from './layout/row'
|
PhotographIcon,
|
||||||
import { Spacer } from './layout/spacer'
|
PresentationChartLineIcon,
|
||||||
|
} from '@heroicons/react/solid'
|
||||||
|
import { MarketModal } from './editor/market-modal'
|
||||||
|
import { insertContent } from './editor/utils'
|
||||||
|
import { Tooltip } from './tooltip'
|
||||||
|
|
||||||
|
const DisplayImage = Image.configure({
|
||||||
|
HTMLAttributes: {
|
||||||
|
class: 'max-h-60',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const DisplayLink = Link.configure({
|
||||||
|
HTMLAttributes: {
|
||||||
|
class: clsx('no-underline !text-indigo-700', linkClass),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
const proseClass = clsx(
|
const proseClass = clsx(
|
||||||
'prose prose-p:my-0 prose-li:my-0 prose-blockquote:not-italic max-w-none prose-quoteless leading-relaxed',
|
'prose prose-p:my-0 prose-ul:my-0 prose-ol:my-0 prose-li:my-0 prose-blockquote:not-italic max-w-none prose-quoteless leading-relaxed',
|
||||||
'font-light prose-a:font-light prose-blockquote:font-light'
|
'font-light prose-a:font-light prose-blockquote:font-light'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -41,14 +54,16 @@ export function useTextEditor(props: {
|
||||||
max?: number
|
max?: number
|
||||||
defaultValue?: Content
|
defaultValue?: Content
|
||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
|
simple?: boolean
|
||||||
}) {
|
}) {
|
||||||
const { placeholder, max, defaultValue = '', disabled } = props
|
const { placeholder, max, defaultValue = '', disabled, simple } = props
|
||||||
|
|
||||||
const users = useUsers()
|
const users = useUsers()
|
||||||
|
|
||||||
const editorClass = clsx(
|
const editorClass = clsx(
|
||||||
proseClass,
|
proseClass,
|
||||||
'min-h-[6em] resize-none outline-none border-none pt-3 px-4 focus:ring-0'
|
!simple && 'min-h-[6em]',
|
||||||
|
'outline-none pt-2 px-4'
|
||||||
)
|
)
|
||||||
|
|
||||||
const editor = useEditor(
|
const editor = useEditor(
|
||||||
|
@ -56,24 +71,22 @@ export function useTextEditor(props: {
|
||||||
editorProps: { attributes: { class: editorClass } },
|
editorProps: { attributes: { class: editorClass } },
|
||||||
extensions: [
|
extensions: [
|
||||||
StarterKit.configure({
|
StarterKit.configure({
|
||||||
heading: { levels: [1, 2, 3] },
|
heading: simple ? false : { levels: [1, 2, 3] },
|
||||||
|
horizontalRule: simple ? false : {},
|
||||||
}),
|
}),
|
||||||
Placeholder.configure({
|
Placeholder.configure({
|
||||||
placeholder,
|
placeholder,
|
||||||
emptyEditorClass:
|
emptyEditorClass:
|
||||||
'before:content-[attr(data-placeholder)] before:text-slate-500 before:float-left before:h-0',
|
'before:content-[attr(data-placeholder)] before:text-slate-500 before:float-left before:h-0 cursor-text',
|
||||||
}),
|
}),
|
||||||
CharacterCount.configure({ limit: max }),
|
CharacterCount.configure({ limit: max }),
|
||||||
Image,
|
simple ? DisplayImage : Image,
|
||||||
Link.configure({
|
DisplayLink,
|
||||||
HTMLAttributes: {
|
|
||||||
class: clsx('no-underline !text-indigo-700', linkClass),
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
DisplayMention.configure({
|
DisplayMention.configure({
|
||||||
suggestion: mentionSuggestion(users),
|
suggestion: mentionSuggestion(users),
|
||||||
}),
|
}),
|
||||||
Iframe,
|
Iframe,
|
||||||
|
TiptapTweet,
|
||||||
],
|
],
|
||||||
content: defaultValue,
|
content: defaultValue,
|
||||||
},
|
},
|
||||||
|
@ -97,7 +110,7 @@ export function useTextEditor(props: {
|
||||||
// If the pasted content is iframe code, directly inject it
|
// If the pasted content is iframe code, directly inject it
|
||||||
const text = event.clipboardData?.getData('text/plain').trim() ?? ''
|
const text = event.clipboardData?.getData('text/plain').trim() ?? ''
|
||||||
if (isValidIframe(text)) {
|
if (isValidIframe(text)) {
|
||||||
editor.chain().insertContent(text).run()
|
insertContent(editor, text)
|
||||||
return true // Prevent the code from getting pasted as text
|
return true // Prevent the code from getting pasted as text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,67 +133,68 @@ function isValidIframe(text: string) {
|
||||||
export function TextEditor(props: {
|
export function TextEditor(props: {
|
||||||
editor: Editor | null
|
editor: Editor | null
|
||||||
upload: ReturnType<typeof useUploadMutation>
|
upload: ReturnType<typeof useUploadMutation>
|
||||||
|
children?: React.ReactNode // additional toolbar buttons
|
||||||
}) {
|
}) {
|
||||||
const { editor, upload } = props
|
const { editor, upload, children } = props
|
||||||
const [iframeOpen, setIframeOpen] = useState(false)
|
const [iframeOpen, setIframeOpen] = useState(false)
|
||||||
|
const [marketOpen, setMarketOpen] = useState(false)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* hide placeholder when focused */}
|
{/* hide placeholder when focused */}
|
||||||
<div className="relative w-full [&:focus-within_p.is-empty]:before:content-none">
|
<div className="relative w-full [&:focus-within_p.is-empty]:before:content-none">
|
||||||
{editor && (
|
<div className="rounded-lg border border-gray-300 bg-white shadow-sm focus-within:border-indigo-500 focus-within:ring-1 focus-within:ring-indigo-500">
|
||||||
<FloatingMenu
|
|
||||||
editor={editor}
|
|
||||||
className={clsx(proseClass, '-ml-2 mr-2 w-full text-slate-300 ')}
|
|
||||||
>
|
|
||||||
Type <em>*markdown*</em>. Paste or{' '}
|
|
||||||
<FileUploadButton
|
|
||||||
className="link text-blue-300"
|
|
||||||
onFiles={upload.mutate}
|
|
||||||
>
|
|
||||||
upload
|
|
||||||
</FileUploadButton>{' '}
|
|
||||||
images!
|
|
||||||
</FloatingMenu>
|
|
||||||
)}
|
|
||||||
<div className="overflow-hidden rounded-lg border border-gray-300 shadow-sm focus-within:border-indigo-500 focus-within:ring-1 focus-within:ring-indigo-500">
|
|
||||||
<EditorContent editor={editor} />
|
<EditorContent editor={editor} />
|
||||||
{/* Spacer element to match the height of the toolbar */}
|
{/* Toolbar, with buttons for images and embeds */}
|
||||||
<div className="py-2" aria-hidden="true">
|
<div className="flex h-9 items-center gap-5 pl-4 pr-1">
|
||||||
{/* Matches height of button in toolbar (1px border + 36px content height) */}
|
<Tooltip className="flex items-center" text="Add image" noTap>
|
||||||
<div className="py-px">
|
|
||||||
<div className="h-9" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Toolbar, with buttons for image and embeds */}
|
|
||||||
<div className="absolute inset-x-0 bottom-0 flex justify-between py-2 pl-3 pr-2">
|
|
||||||
<div className="flex items-center space-x-5">
|
|
||||||
<div className="flex items-center">
|
|
||||||
<FileUploadButton
|
<FileUploadButton
|
||||||
onFiles={upload.mutate}
|
onFiles={upload.mutate}
|
||||||
className="-m-2.5 flex h-10 w-10 items-center justify-center rounded-full text-gray-400 hover:text-gray-500"
|
className="-m-2.5 flex h-10 w-10 items-center justify-center rounded-full text-gray-400 hover:text-gray-500"
|
||||||
>
|
>
|
||||||
<PhotographIcon className="h-5 w-5" aria-hidden="true" />
|
<PhotographIcon className="h-5 w-5" aria-hidden="true" />
|
||||||
<span className="sr-only">Upload an image</span>
|
|
||||||
</FileUploadButton>
|
</FileUploadButton>
|
||||||
</div>
|
</Tooltip>
|
||||||
<div className="flex items-center">
|
<Tooltip className="flex items-center" text="Add embed" noTap>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => setIframeOpen(true)}
|
onClick={() => setIframeOpen(true)}
|
||||||
className="-m-2.5 flex h-10 w-10 items-center justify-center rounded-full text-gray-400 hover:text-gray-500"
|
className="-m-2.5 flex h-10 w-10 items-center justify-center rounded-full text-gray-400 hover:text-gray-500"
|
||||||
>
|
>
|
||||||
<IframeModal
|
<EmbedModal
|
||||||
editor={editor}
|
editor={editor}
|
||||||
open={iframeOpen}
|
open={iframeOpen}
|
||||||
setOpen={setIframeOpen}
|
setOpen={setIframeOpen}
|
||||||
/>
|
/>
|
||||||
<CodeIcon className="h-5 w-5" aria-hidden="true" />
|
<CodeIcon className="h-5 w-5" aria-hidden="true" />
|
||||||
<span className="sr-only">Embed an iframe</span>
|
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</Tooltip>
|
||||||
|
<Tooltip className="flex items-center" text="Add market" noTap>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setMarketOpen(true)}
|
||||||
|
className="-m-2.5 flex h-10 w-10 items-center justify-center rounded-full text-gray-400 hover:text-gray-500"
|
||||||
|
>
|
||||||
|
<MarketModal
|
||||||
|
editor={editor}
|
||||||
|
open={marketOpen}
|
||||||
|
setOpen={setMarketOpen}
|
||||||
|
/>
|
||||||
|
<PresentationChartLineIcon
|
||||||
|
className="h-5 w-5"
|
||||||
|
aria-hidden="true"
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
</Tooltip>
|
||||||
|
{/* Spacer that also focuses editor on click */}
|
||||||
|
<div
|
||||||
|
className="grow cursor-text self-stretch"
|
||||||
|
onMouseDown={() =>
|
||||||
|
editor?.chain().focus('end').createParagraphNear().run()
|
||||||
|
}
|
||||||
|
aria-hidden
|
||||||
|
/>
|
||||||
|
{children}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -192,65 +206,6 @@ export function TextEditor(props: {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function IframeModal(props: {
|
|
||||||
editor: Editor | null
|
|
||||||
open: boolean
|
|
||||||
setOpen: (open: boolean) => void
|
|
||||||
}) {
|
|
||||||
const { editor, open, setOpen } = props
|
|
||||||
const [embedCode, setEmbedCode] = useState('')
|
|
||||||
const valid = isValidIframe(embedCode)
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Modal open={open} setOpen={setOpen}>
|
|
||||||
<Col className="gap-2 rounded bg-white p-6">
|
|
||||||
<label
|
|
||||||
htmlFor="embed"
|
|
||||||
className="block text-sm font-medium text-gray-700"
|
|
||||||
>
|
|
||||||
Embed a market, Youtube video, etc.
|
|
||||||
</label>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
name="embed"
|
|
||||||
id="embed"
|
|
||||||
className="block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm"
|
|
||||||
placeholder='e.g. <iframe src="..."></iframe>'
|
|
||||||
value={embedCode}
|
|
||||||
onChange={(e) => setEmbedCode(e.target.value)}
|
|
||||||
/>
|
|
||||||
|
|
||||||
{/* Preview the embed if it's valid */}
|
|
||||||
{valid ? <RichContent content={embedCode} /> : <Spacer h={2} />}
|
|
||||||
|
|
||||||
<Row className="gap-2">
|
|
||||||
<Button
|
|
||||||
disabled={!valid}
|
|
||||||
onClick={() => {
|
|
||||||
if (editor && valid) {
|
|
||||||
editor.chain().insertContent(embedCode).run()
|
|
||||||
setEmbedCode('')
|
|
||||||
setOpen(false)
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
Embed
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
color="gray"
|
|
||||||
onClick={() => {
|
|
||||||
setEmbedCode('')
|
|
||||||
setOpen(false)
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
Cancel
|
|
||||||
</Button>
|
|
||||||
</Row>
|
|
||||||
</Col>
|
|
||||||
</Modal>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const useUploadMutation = (editor: Editor | null) =>
|
const useUploadMutation = (editor: Editor | null) =>
|
||||||
useMutation(
|
useMutation(
|
||||||
(files: File[]) =>
|
(files: File[]) =>
|
||||||
|
@ -269,14 +224,20 @@ const useUploadMutation = (editor: Editor | null) =>
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
function RichContent(props: { content: JSONContent | string }) {
|
export function RichContent(props: {
|
||||||
const { content } = props
|
content: JSONContent | string
|
||||||
|
smallImage?: boolean
|
||||||
|
}) {
|
||||||
|
const { content, smallImage } = props
|
||||||
const editor = useEditor({
|
const editor = useEditor({
|
||||||
editorProps: { attributes: { class: proseClass } },
|
editorProps: { attributes: { class: proseClass } },
|
||||||
extensions: [
|
extensions: [
|
||||||
// replace tiptap's Mention with ours, to add style and link
|
StarterKit,
|
||||||
...exhibitExts.filter((ex) => ex.name !== Mention.name),
|
smallImage ? DisplayImage : Image,
|
||||||
|
DisplayLink,
|
||||||
DisplayMention,
|
DisplayMention,
|
||||||
|
Iframe,
|
||||||
|
TiptapTweet,
|
||||||
],
|
],
|
||||||
content,
|
content,
|
||||||
editable: false,
|
editable: false,
|
||||||
|
@ -287,13 +248,16 @@ function RichContent(props: { content: JSONContent | string }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// backwards compatibility: we used to store content as strings
|
// backwards compatibility: we used to store content as strings
|
||||||
export function Content(props: { content: JSONContent | string }) {
|
export function Content(props: {
|
||||||
|
content: JSONContent | string
|
||||||
|
smallImage?: boolean
|
||||||
|
}) {
|
||||||
const { content } = props
|
const { content } = props
|
||||||
return typeof content === 'string' ? (
|
return typeof content === 'string' ? (
|
||||||
<div className="whitespace-pre-line font-light leading-relaxed">
|
<div className="whitespace-pre-line font-light leading-relaxed">
|
||||||
<Linkify text={content} />
|
<Linkify text={content} />
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<RichContent content={content} />
|
<RichContent {...props} />
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
130
web/components/editor/embed-modal.tsx
Normal file
130
web/components/editor/embed-modal.tsx
Normal file
|
@ -0,0 +1,130 @@
|
||||||
|
import { Editor } from '@tiptap/react'
|
||||||
|
import { DOMAIN } from 'common/envs/constants'
|
||||||
|
import { useState } from 'react'
|
||||||
|
import { Button } from '../button'
|
||||||
|
import { RichContent } from '../editor'
|
||||||
|
import { Col } from '../layout/col'
|
||||||
|
import { Modal } from '../layout/modal'
|
||||||
|
import { Row } from '../layout/row'
|
||||||
|
import { Spacer } from '../layout/spacer'
|
||||||
|
|
||||||
|
type EmbedPattern = {
|
||||||
|
// Regex should have a single capture group.
|
||||||
|
regex: RegExp
|
||||||
|
rewrite: (text: string) => string
|
||||||
|
}
|
||||||
|
|
||||||
|
const embedPatterns: EmbedPattern[] = [
|
||||||
|
{
|
||||||
|
regex: /^(<iframe.*<\/iframe>)$/,
|
||||||
|
rewrite: (text: string) => text,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
regex: /^https?:\/\/manifold\.markets\/([^\/]+\/[^\/]+)/,
|
||||||
|
rewrite: (slug) =>
|
||||||
|
`<iframe src="https://manifold.markets/embed/${slug}"></iframe>`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
regex: /^https?:\/\/twitter\.com\/.*\/status\/(\d+)/,
|
||||||
|
// Hack: append a leading 't', to prevent tweetId from being interpreted as a number.
|
||||||
|
// If it's a number, there may be numeric precision issues.
|
||||||
|
rewrite: (id) => `<tiptap-tweet tweetid="t${id}"></tiptap-tweet>`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
regex: /^https?:\/\/www\.youtube\.com\/watch\?v=([^&]+)/,
|
||||||
|
rewrite: (id) =>
|
||||||
|
`<iframe src="https://www.youtube.com/embed/${id}"></iframe>`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
regex: /^https?:\/\/www\.metaculus\.com\/questions\/(\d+)/,
|
||||||
|
rewrite: (id) =>
|
||||||
|
`<iframe src="https://www.metaculus.com/questions/embed/${id}"></iframe>`,
|
||||||
|
},
|
||||||
|
// Twitch is a bit annoying, since it requires the `&parent=DOMAIN` to match
|
||||||
|
{
|
||||||
|
// Twitch: https://www.twitch.tv/videos/1445087149
|
||||||
|
regex: /^https?:\/\/www\.twitch\.tv\/videos\/(\d+)/,
|
||||||
|
rewrite: (id) =>
|
||||||
|
`<iframe src="https://player.twitch.tv/?video=${id}&parent=${DOMAIN}"></iframe>`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// Twitch: https://www.twitch.tv/sirsalty
|
||||||
|
regex: /^https?:\/\/www\.twitch\.tv\/([^\/]+)/,
|
||||||
|
rewrite: (channel) =>
|
||||||
|
`<iframe src="https://player.twitch.tv/?channel=${channel}&parent=${DOMAIN}"></iframe>`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
regex: /^(https?:\/\/.*)/,
|
||||||
|
rewrite: (url) => `<iframe src="${url}"></iframe>`,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
function embedCode(text: string) {
|
||||||
|
for (const pattern of embedPatterns) {
|
||||||
|
const match = text.match(pattern.regex)
|
||||||
|
if (match) {
|
||||||
|
return pattern.rewrite(match[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
export function EmbedModal(props: {
|
||||||
|
editor: Editor | null
|
||||||
|
open: boolean
|
||||||
|
setOpen: (open: boolean) => void
|
||||||
|
}) {
|
||||||
|
const { editor, open, setOpen } = props
|
||||||
|
const [input, setInput] = useState('')
|
||||||
|
const embed = embedCode(input)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Modal open={open} setOpen={setOpen}>
|
||||||
|
<Col className="gap-2 rounded bg-white p-6">
|
||||||
|
<label
|
||||||
|
htmlFor="embed"
|
||||||
|
className="block text-sm font-medium text-gray-700"
|
||||||
|
>
|
||||||
|
Embed a Youtube video, Tweet, or other link
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
name="embed"
|
||||||
|
id="embed"
|
||||||
|
className="block w-full rounded-md border-gray-300 shadow-sm placeholder:text-gray-300 focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm"
|
||||||
|
placeholder="e.g. https://www.youtube.com/watch?v=dQw4w9WgXcQ"
|
||||||
|
value={input}
|
||||||
|
onChange={(e) => setInput(e.target.value)}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{/* Preview the embed if it's valid */}
|
||||||
|
{embed ? <RichContent content={embed} /> : <Spacer h={2} />}
|
||||||
|
|
||||||
|
<Row className="gap-2">
|
||||||
|
<Button
|
||||||
|
disabled={!embed}
|
||||||
|
onClick={() => {
|
||||||
|
if (editor && embed) {
|
||||||
|
editor.chain().insertContent(embed).run()
|
||||||
|
console.log('editorjson', editor.getJSON())
|
||||||
|
setInput('')
|
||||||
|
setOpen(false)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Embed
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
color="gray"
|
||||||
|
onClick={() => {
|
||||||
|
setInput('')
|
||||||
|
setOpen(false)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
</Row>
|
||||||
|
</Col>
|
||||||
|
</Modal>
|
||||||
|
)
|
||||||
|
}
|
84
web/components/editor/market-modal.tsx
Normal file
84
web/components/editor/market-modal.tsx
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
import { Editor } from '@tiptap/react'
|
||||||
|
import { Contract } from 'common/contract'
|
||||||
|
import { useState } from 'react'
|
||||||
|
import { Button } from '../button'
|
||||||
|
import { ContractSearch } from '../contract-search'
|
||||||
|
import { Col } from '../layout/col'
|
||||||
|
import { Modal } from '../layout/modal'
|
||||||
|
import { Row } from '../layout/row'
|
||||||
|
import { LoadingIndicator } from '../loading-indicator'
|
||||||
|
import { embedCode } from '../share-embed-button'
|
||||||
|
import { insertContent } from './utils'
|
||||||
|
|
||||||
|
export function MarketModal(props: {
|
||||||
|
editor: Editor | null
|
||||||
|
open: boolean
|
||||||
|
setOpen: (open: boolean) => void
|
||||||
|
}) {
|
||||||
|
const { editor, open, setOpen } = props
|
||||||
|
|
||||||
|
const [contracts, setContracts] = useState<Contract[]>([])
|
||||||
|
const [loading, setLoading] = useState(false)
|
||||||
|
|
||||||
|
async function addContract(contract: Contract) {
|
||||||
|
if (contracts.map((c) => c.id).includes(contract.id)) {
|
||||||
|
setContracts(contracts.filter((c) => c.id !== contract.id))
|
||||||
|
} else setContracts([...contracts, contract])
|
||||||
|
}
|
||||||
|
|
||||||
|
async function doneAddingContracts() {
|
||||||
|
setLoading(true)
|
||||||
|
insertContent(editor, ...contracts.map(embedCode))
|
||||||
|
setLoading(false)
|
||||||
|
setOpen(false)
|
||||||
|
setContracts([])
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Modal open={open} setOpen={setOpen} className={'sm:p-0'} size={'lg'}>
|
||||||
|
<Col className="h-[85vh] w-full gap-4 rounded-md bg-white">
|
||||||
|
<Row className="p-8 pb-0">
|
||||||
|
<div className={'text-xl text-indigo-700'}>Embed a market</div>
|
||||||
|
|
||||||
|
{!loading && (
|
||||||
|
<Row className="grow justify-end gap-4">
|
||||||
|
{contracts.length > 0 && (
|
||||||
|
<Button onClick={doneAddingContracts} color={'indigo'}>
|
||||||
|
Embed {contracts.length} question
|
||||||
|
{contracts.length > 1 && 's'}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
<Button onClick={() => setContracts([])} color="gray">
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
</Row>
|
||||||
|
)}
|
||||||
|
</Row>
|
||||||
|
|
||||||
|
{loading && (
|
||||||
|
<div className="w-full justify-center">
|
||||||
|
<LoadingIndicator />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="overflow-y-scroll sm:px-8">
|
||||||
|
<ContractSearch
|
||||||
|
hideOrderSelector
|
||||||
|
onContractClick={addContract}
|
||||||
|
overrideGridClassName={
|
||||||
|
'flex grid grid-cols-1 sm:grid-cols-2 flex-col gap-3 p-1'
|
||||||
|
}
|
||||||
|
cardHideOptions={{ hideGroupLink: true, hideQuickBet: true }}
|
||||||
|
highlightOptions={{
|
||||||
|
contractIds: contracts.map((c) => c.id),
|
||||||
|
highlightClassName:
|
||||||
|
'!bg-indigo-100 outline outline-2 outline-indigo-300',
|
||||||
|
}}
|
||||||
|
additionalFilter={{}} /* hide pills */
|
||||||
|
headerClassName="bg-white"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</Col>
|
||||||
|
</Modal>
|
||||||
|
)
|
||||||
|
}
|
|
@ -11,7 +11,7 @@ const name = 'mention-component'
|
||||||
|
|
||||||
const MentionComponent = (props: any) => {
|
const MentionComponent = (props: any) => {
|
||||||
return (
|
return (
|
||||||
<NodeViewWrapper className={clsx(name, 'not-prose inline text-indigo-700')}>
|
<NodeViewWrapper className={clsx(name, 'not-prose text-indigo-700')}>
|
||||||
<Linkify text={'@' + props.node.attrs.label} />
|
<Linkify text={'@' + props.node.attrs.label} />
|
||||||
</NodeViewWrapper>
|
</NodeViewWrapper>
|
||||||
)
|
)
|
||||||
|
@ -25,5 +25,6 @@ const MentionComponent = (props: any) => {
|
||||||
export const DisplayMention = Mention.extend({
|
export const DisplayMention = Mention.extend({
|
||||||
parseHTML: () => [{ tag: name }],
|
parseHTML: () => [{ tag: name }],
|
||||||
renderHTML: ({ HTMLAttributes }) => [name, mergeAttributes(HTMLAttributes)],
|
renderHTML: ({ HTMLAttributes }) => [name, mergeAttributes(HTMLAttributes)],
|
||||||
addNodeView: () => ReactNodeViewRenderer(MentionComponent),
|
addNodeView: () =>
|
||||||
|
ReactNodeViewRenderer(MentionComponent, { className: 'inline-block' }),
|
||||||
})
|
})
|
||||||
|
|
13
web/components/editor/tiptap-tweet.tsx
Normal file
13
web/components/editor/tiptap-tweet.tsx
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import { Node } from '@tiptap/core'
|
||||||
|
import { ReactNodeViewRenderer } from '@tiptap/react'
|
||||||
|
import { TiptapTweetNode } from 'common/util/tiptap-tweet-type'
|
||||||
|
import WrappedTwitterTweetEmbed from './tweet-embed'
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore
|
||||||
|
export default Node.create<TweetOptions>({
|
||||||
|
...TiptapTweetNode,
|
||||||
|
addNodeView() {
|
||||||
|
return ReactNodeViewRenderer(WrappedTwitterTweetEmbed)
|
||||||
|
},
|
||||||
|
})
|
19
web/components/editor/tweet-embed.tsx
Normal file
19
web/components/editor/tweet-embed.tsx
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import { NodeViewWrapper } from '@tiptap/react'
|
||||||
|
import { TwitterTweetEmbed } from 'react-twitter-embed'
|
||||||
|
|
||||||
|
export default function WrappedTwitterTweetEmbed(props: {
|
||||||
|
node: {
|
||||||
|
attrs: {
|
||||||
|
tweetId: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}): JSX.Element {
|
||||||
|
// Remove the leading 't' from the tweet id
|
||||||
|
const tweetId = props.node.attrs.tweetId.slice(1)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<NodeViewWrapper className="tiptap-tweet">
|
||||||
|
<TwitterTweetEmbed tweetId={tweetId} />
|
||||||
|
</NodeViewWrapper>
|
||||||
|
)
|
||||||
|
}
|
13
web/components/editor/utils.ts
Normal file
13
web/components/editor/utils.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import { Editor, Content } from '@tiptap/react'
|
||||||
|
|
||||||
|
export function insertContent(editor: Editor | null, ...contents: Content[]) {
|
||||||
|
if (!editor) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let e = editor.chain()
|
||||||
|
for (const content of contents) {
|
||||||
|
e = e.createParagraphNear().insertContent(content)
|
||||||
|
}
|
||||||
|
e.run()
|
||||||
|
}
|
|
@ -26,7 +26,10 @@ export function ContractActivity(props: {
|
||||||
|
|
||||||
const contract = useContractWithPreload(props.contract) ?? props.contract
|
const contract = useContractWithPreload(props.contract) ?? props.contract
|
||||||
const comments = props.comments
|
const comments = props.comments
|
||||||
const updatedBets = useBets(contract.id)
|
const updatedBets = useBets(contract.id, {
|
||||||
|
filterChallenges: false,
|
||||||
|
filterRedemptions: true,
|
||||||
|
})
|
||||||
const bets = (updatedBets ?? props.bets).filter(
|
const bets = (updatedBets ?? props.bets).filter(
|
||||||
(bet) => !bet.isRedemption && bet.amount !== 0
|
(bet) => !bet.isRedemption && bet.amount !== 0
|
||||||
)
|
)
|
||||||
|
|
|
@ -7,6 +7,7 @@ import { fromNow } from 'web/lib/util/time'
|
||||||
import { ToastClipboard } from 'web/components/toast-clipboard'
|
import { ToastClipboard } from 'web/components/toast-clipboard'
|
||||||
import { LinkIcon } from '@heroicons/react/outline'
|
import { LinkIcon } from '@heroicons/react/outline'
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
|
import dayjs from 'dayjs'
|
||||||
|
|
||||||
export function CopyLinkDateTimeComponent(props: {
|
export function CopyLinkDateTimeComponent(props: {
|
||||||
prefix: string
|
prefix: string
|
||||||
|
@ -17,6 +18,7 @@ export function CopyLinkDateTimeComponent(props: {
|
||||||
}) {
|
}) {
|
||||||
const { prefix, slug, elementId, createdTime, className } = props
|
const { prefix, slug, elementId, createdTime, className } = props
|
||||||
const [showToast, setShowToast] = useState(false)
|
const [showToast, setShowToast] = useState(false)
|
||||||
|
const time = dayjs(createdTime)
|
||||||
|
|
||||||
function copyLinkToComment(
|
function copyLinkToComment(
|
||||||
event: React.MouseEvent<HTMLAnchorElement, MouseEvent>
|
event: React.MouseEvent<HTMLAnchorElement, MouseEvent>
|
||||||
|
@ -30,7 +32,7 @@ export function CopyLinkDateTimeComponent(props: {
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<div className={clsx('inline', className)}>
|
<div className={clsx('inline', className)}>
|
||||||
<DateTimeTooltip time={createdTime}>
|
<DateTimeTooltip time={time} noTap>
|
||||||
<Link href={`/${prefix}/${slug}#${elementId}`} passHref={true}>
|
<Link href={`/${prefix}/${slug}#${elementId}`} passHref={true}>
|
||||||
<a
|
<a
|
||||||
onClick={(event) => copyLinkToComment(event)}
|
onClick={(event) => copyLinkToComment(event)}
|
||||||
|
|
|
@ -31,9 +31,9 @@ export function FeedAnswerCommentGroup(props: {
|
||||||
const { answer, contract, comments, tips, bets, user } = props
|
const { answer, contract, comments, tips, bets, user } = props
|
||||||
const { username, avatarUrl, name, text } = answer
|
const { username, avatarUrl, name, text } = answer
|
||||||
|
|
||||||
const [replyToUsername, setReplyToUsername] = useState('')
|
const [replyToUser, setReplyToUser] =
|
||||||
|
useState<Pick<User, 'id' | 'username'>>()
|
||||||
const [showReply, setShowReply] = useState(false)
|
const [showReply, setShowReply] = useState(false)
|
||||||
const [inputRef, setInputRef] = useState<HTMLTextAreaElement | null>(null)
|
|
||||||
const [highlighted, setHighlighted] = useState(false)
|
const [highlighted, setHighlighted] = useState(false)
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
|
@ -70,9 +70,14 @@ export function FeedAnswerCommentGroup(props: {
|
||||||
|
|
||||||
const scrollAndOpenReplyInput = useEvent(
|
const scrollAndOpenReplyInput = useEvent(
|
||||||
(comment?: Comment, answer?: Answer) => {
|
(comment?: Comment, answer?: Answer) => {
|
||||||
setReplyToUsername(comment?.userUsername ?? answer?.username ?? '')
|
setReplyToUser(
|
||||||
|
comment
|
||||||
|
? { id: comment.userId, username: comment.userUsername }
|
||||||
|
: answer
|
||||||
|
? { id: answer.userId, username: answer.username }
|
||||||
|
: undefined
|
||||||
|
)
|
||||||
setShowReply(true)
|
setShowReply(true)
|
||||||
inputRef?.focus()
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -80,7 +85,7 @@ export function FeedAnswerCommentGroup(props: {
|
||||||
// Only show one comment input for a bet at a time
|
// Only show one comment input for a bet at a time
|
||||||
if (
|
if (
|
||||||
betsByCurrentUser.length > 1 &&
|
betsByCurrentUser.length > 1 &&
|
||||||
inputRef?.textContent?.length === 0 &&
|
// inputRef?.textContent?.length === 0 && //TODO: editor.isEmpty
|
||||||
betsByCurrentUser.sort((a, b) => b.createdTime - a.createdTime)[0]
|
betsByCurrentUser.sort((a, b) => b.createdTime - a.createdTime)[0]
|
||||||
?.outcome !== answer.number.toString()
|
?.outcome !== answer.number.toString()
|
||||||
)
|
)
|
||||||
|
@ -89,10 +94,6 @@ export function FeedAnswerCommentGroup(props: {
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [betsByCurrentUser.length, user, answer.number])
|
}, [betsByCurrentUser.length, user, answer.number])
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (showReply && inputRef) inputRef.focus()
|
|
||||||
}, [inputRef, showReply])
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (router.asPath.endsWith(`#${answerElementId}`)) {
|
if (router.asPath.endsWith(`#${answerElementId}`)) {
|
||||||
setHighlighted(true)
|
setHighlighted(true)
|
||||||
|
@ -154,7 +155,6 @@ export function FeedAnswerCommentGroup(props: {
|
||||||
commentsList={commentsList}
|
commentsList={commentsList}
|
||||||
betsByUserId={betsByUserId}
|
betsByUserId={betsByUserId}
|
||||||
smallAvatar={true}
|
smallAvatar={true}
|
||||||
truncate={false}
|
|
||||||
bets={bets}
|
bets={bets}
|
||||||
tips={tips}
|
tips={tips}
|
||||||
scrollAndOpenReplyInput={scrollAndOpenReplyInput}
|
scrollAndOpenReplyInput={scrollAndOpenReplyInput}
|
||||||
|
@ -172,12 +172,8 @@ export function FeedAnswerCommentGroup(props: {
|
||||||
betsByCurrentUser={betsByCurrentUser}
|
betsByCurrentUser={betsByCurrentUser}
|
||||||
commentsByCurrentUser={commentsByCurrentUser}
|
commentsByCurrentUser={commentsByCurrentUser}
|
||||||
parentAnswerOutcome={answer.number.toString()}
|
parentAnswerOutcome={answer.number.toString()}
|
||||||
replyToUsername={replyToUsername}
|
replyToUser={replyToUser}
|
||||||
setRef={setInputRef}
|
onSubmitComment={() => setShowReply(false)}
|
||||||
onSubmitComment={() => {
|
|
||||||
setShowReply(false)
|
|
||||||
setReplyToUsername('')
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
|
@ -10,11 +10,14 @@ import { UsersIcon } from '@heroicons/react/solid'
|
||||||
import { formatMoney, formatPercent } from 'common/util/format'
|
import { formatMoney, formatPercent } from 'common/util/format'
|
||||||
import { OutcomeLabel } from 'web/components/outcome-label'
|
import { OutcomeLabel } from 'web/components/outcome-label'
|
||||||
import { RelativeTimestamp } from 'web/components/relative-timestamp'
|
import { RelativeTimestamp } from 'web/components/relative-timestamp'
|
||||||
import React, { Fragment } from 'react'
|
import React, { Fragment, useEffect } from 'react'
|
||||||
import { uniqBy, partition, sumBy, groupBy } from 'lodash'
|
import { uniqBy, partition, sumBy, groupBy } from 'lodash'
|
||||||
import { JoinSpans } from 'web/components/join-spans'
|
import { JoinSpans } from 'web/components/join-spans'
|
||||||
import { UserLink } from '../user-page'
|
import { UserLink } from '../user-page'
|
||||||
import { formatNumericProbability } from 'common/pseudo-numeric'
|
import { formatNumericProbability } from 'common/pseudo-numeric'
|
||||||
|
import { SiteLink } from 'web/components/site-link'
|
||||||
|
import { getChallenge, getChallengeUrl } from 'web/lib/firebase/challenges'
|
||||||
|
import { Challenge } from 'common/challenge'
|
||||||
|
|
||||||
export function FeedBet(props: {
|
export function FeedBet(props: {
|
||||||
contract: Contract
|
contract: Contract
|
||||||
|
@ -33,38 +36,33 @@ export function FeedBet(props: {
|
||||||
const isSelf = user?.id === userId
|
const isSelf = user?.id === userId
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<Row className={'flex w-full items-center gap-2 pt-3'}>
|
||||||
<Row className={'flex w-full gap-2 pt-3'}>
|
{isSelf ? (
|
||||||
{isSelf ? (
|
<Avatar
|
||||||
<Avatar
|
className={clsx(smallAvatar && 'ml-1')}
|
||||||
className={clsx(smallAvatar && 'ml-1')}
|
size={smallAvatar ? 'sm' : undefined}
|
||||||
size={smallAvatar ? 'sm' : undefined}
|
avatarUrl={user.avatarUrl}
|
||||||
avatarUrl={user.avatarUrl}
|
username={user.username}
|
||||||
username={user.username}
|
/>
|
||||||
/>
|
) : bettor ? (
|
||||||
) : bettor ? (
|
<Avatar
|
||||||
<Avatar
|
className={clsx(smallAvatar && 'ml-1')}
|
||||||
className={clsx(smallAvatar && 'ml-1')}
|
size={smallAvatar ? 'sm' : undefined}
|
||||||
size={smallAvatar ? 'sm' : undefined}
|
avatarUrl={bettor.avatarUrl}
|
||||||
avatarUrl={bettor.avatarUrl}
|
username={bettor.username}
|
||||||
username={bettor.username}
|
/>
|
||||||
/>
|
) : (
|
||||||
) : (
|
<EmptyAvatar className="mx-1" />
|
||||||
<div className="relative px-1">
|
)}
|
||||||
<EmptyAvatar />
|
<BetStatusText
|
||||||
</div>
|
bet={bet}
|
||||||
)}
|
contract={contract}
|
||||||
<div className={'min-w-0 flex-1 py-1.5'}>
|
isSelf={isSelf}
|
||||||
<BetStatusText
|
bettor={bettor}
|
||||||
bet={bet}
|
hideOutcome={hideOutcome}
|
||||||
contract={contract}
|
className="flex-1"
|
||||||
isSelf={isSelf}
|
/>
|
||||||
bettor={bettor}
|
</Row>
|
||||||
hideOutcome={hideOutcome}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</Row>
|
|
||||||
</>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,12 +72,21 @@ export function BetStatusText(props: {
|
||||||
isSelf: boolean
|
isSelf: boolean
|
||||||
bettor?: User
|
bettor?: User
|
||||||
hideOutcome?: boolean
|
hideOutcome?: boolean
|
||||||
|
className?: string
|
||||||
}) {
|
}) {
|
||||||
const { bet, contract, bettor, isSelf, hideOutcome } = props
|
const { bet, contract, bettor, isSelf, hideOutcome, className } = props
|
||||||
const { outcomeType } = contract
|
const { outcomeType } = contract
|
||||||
const isPseudoNumeric = outcomeType === 'PSEUDO_NUMERIC'
|
const isPseudoNumeric = outcomeType === 'PSEUDO_NUMERIC'
|
||||||
const isFreeResponse = outcomeType === 'FREE_RESPONSE'
|
const isFreeResponse = outcomeType === 'FREE_RESPONSE'
|
||||||
const { amount, outcome, createdTime } = bet
|
const { amount, outcome, createdTime, challengeSlug } = bet
|
||||||
|
const [challenge, setChallenge] = React.useState<Challenge>()
|
||||||
|
useEffect(() => {
|
||||||
|
if (challengeSlug) {
|
||||||
|
getChallenge(challengeSlug, contract.id).then((c) => {
|
||||||
|
setChallenge(c)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, [challengeSlug, contract.id])
|
||||||
|
|
||||||
const bought = amount >= 0 ? 'bought' : 'sold'
|
const bought = amount >= 0 ? 'bought' : 'sold'
|
||||||
const outOfTotalAmount =
|
const outOfTotalAmount =
|
||||||
|
@ -112,7 +119,7 @@ export function BetStatusText(props: {
|
||||||
: formatPercent(bet.limitProb ?? bet.probAfter)
|
: formatPercent(bet.limitProb ?? bet.probAfter)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="text-sm text-gray-500">
|
<div className={clsx('text-sm text-gray-500', className)}>
|
||||||
{bettor ? (
|
{bettor ? (
|
||||||
<UserLink name={bettor.name} username={bettor.username} />
|
<UserLink name={bettor.name} username={bettor.username} />
|
||||||
) : (
|
) : (
|
||||||
|
@ -133,6 +140,14 @@ export function BetStatusText(props: {
|
||||||
{fromProb === toProb
|
{fromProb === toProb
|
||||||
? `at ${fromProb}`
|
? `at ${fromProb}`
|
||||||
: `from ${fromProb} to ${toProb}`}
|
: `from ${fromProb} to ${toProb}`}
|
||||||
|
{challengeSlug && (
|
||||||
|
<SiteLink
|
||||||
|
href={challenge ? getChallengeUrl(challenge) : ''}
|
||||||
|
className={'mx-1'}
|
||||||
|
>
|
||||||
|
[challenge]
|
||||||
|
</SiteLink>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
<RelativeTimestamp time={createdTime} />
|
<RelativeTimestamp time={createdTime} />
|
||||||
|
|
|
@ -13,25 +13,22 @@ import { Avatar } from 'web/components/avatar'
|
||||||
import { UserLink } from 'web/components/user-page'
|
import { UserLink } from 'web/components/user-page'
|
||||||
import { OutcomeLabel } from 'web/components/outcome-label'
|
import { OutcomeLabel } from 'web/components/outcome-label'
|
||||||
import { CopyLinkDateTimeComponent } from 'web/components/feed/copy-link-date-time'
|
import { CopyLinkDateTimeComponent } from 'web/components/feed/copy-link-date-time'
|
||||||
import { contractPath } from 'web/lib/firebase/contracts'
|
|
||||||
import { firebaseLogin } from 'web/lib/firebase/users'
|
import { firebaseLogin } from 'web/lib/firebase/users'
|
||||||
import {
|
import {
|
||||||
createCommentOnContract,
|
createCommentOnContract,
|
||||||
MAX_COMMENT_LENGTH,
|
MAX_COMMENT_LENGTH,
|
||||||
} from 'web/lib/firebase/comments'
|
} from 'web/lib/firebase/comments'
|
||||||
import Textarea from 'react-expanding-textarea'
|
|
||||||
import { Linkify } from 'web/components/linkify'
|
|
||||||
import { SiteLink } from 'web/components/site-link'
|
|
||||||
import { BetStatusText } from 'web/components/feed/feed-bets'
|
import { BetStatusText } from 'web/components/feed/feed-bets'
|
||||||
import { Col } from 'web/components/layout/col'
|
import { Col } from 'web/components/layout/col'
|
||||||
import { getProbability } from 'common/calculate'
|
import { getProbability } from 'common/calculate'
|
||||||
import { LoadingIndicator } from 'web/components/loading-indicator'
|
import { LoadingIndicator } from 'web/components/loading-indicator'
|
||||||
import { PaperAirplaneIcon } from '@heroicons/react/outline'
|
import { PaperAirplaneIcon } from '@heroicons/react/outline'
|
||||||
import { track } from 'web/lib/service/analytics'
|
import { track } from 'web/lib/service/analytics'
|
||||||
import { useEvent } from 'web/hooks/use-event'
|
|
||||||
import { Tipper } from '../tipper'
|
import { Tipper } from '../tipper'
|
||||||
import { CommentTipMap, CommentTips } from 'web/hooks/use-tip-txns'
|
import { CommentTipMap, CommentTips } from 'web/hooks/use-tip-txns'
|
||||||
import { useWindowSize } from 'web/hooks/use-window-size'
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
||||||
|
import { Content, TextEditor, useTextEditor } from '../editor'
|
||||||
|
import { Editor } from '@tiptap/react'
|
||||||
|
|
||||||
export function FeedCommentThread(props: {
|
export function FeedCommentThread(props: {
|
||||||
contract: Contract
|
contract: Contract
|
||||||
|
@ -39,20 +36,12 @@ export function FeedCommentThread(props: {
|
||||||
tips: CommentTipMap
|
tips: CommentTipMap
|
||||||
parentComment: Comment
|
parentComment: Comment
|
||||||
bets: Bet[]
|
bets: Bet[]
|
||||||
truncate?: boolean
|
|
||||||
smallAvatar?: boolean
|
smallAvatar?: boolean
|
||||||
}) {
|
}) {
|
||||||
const {
|
const { contract, comments, bets, tips, smallAvatar, parentComment } = props
|
||||||
contract,
|
|
||||||
comments,
|
|
||||||
bets,
|
|
||||||
tips,
|
|
||||||
truncate,
|
|
||||||
smallAvatar,
|
|
||||||
parentComment,
|
|
||||||
} = props
|
|
||||||
const [showReply, setShowReply] = useState(false)
|
const [showReply, setShowReply] = useState(false)
|
||||||
const [replyToUsername, setReplyToUsername] = useState('')
|
const [replyToUser, setReplyToUser] =
|
||||||
|
useState<{ id: string; username: string }>()
|
||||||
const betsByUserId = groupBy(bets, (bet) => bet.userId)
|
const betsByUserId = groupBy(bets, (bet) => bet.userId)
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
const commentsList = comments.filter(
|
const commentsList = comments.filter(
|
||||||
|
@ -60,15 +49,12 @@ export function FeedCommentThread(props: {
|
||||||
parentComment.id && comment.replyToCommentId === parentComment.id
|
parentComment.id && comment.replyToCommentId === parentComment.id
|
||||||
)
|
)
|
||||||
commentsList.unshift(parentComment)
|
commentsList.unshift(parentComment)
|
||||||
const [inputRef, setInputRef] = useState<HTMLTextAreaElement | null>(null)
|
|
||||||
function scrollAndOpenReplyInput(comment: Comment) {
|
function scrollAndOpenReplyInput(comment: Comment) {
|
||||||
setReplyToUsername(comment.userUsername)
|
setReplyToUser({ id: comment.userId, username: comment.userUsername })
|
||||||
setShowReply(true)
|
setShowReply(true)
|
||||||
inputRef?.focus()
|
|
||||||
}
|
}
|
||||||
useEffect(() => {
|
|
||||||
if (showReply && inputRef) inputRef.focus()
|
|
||||||
}, [inputRef, showReply])
|
|
||||||
return (
|
return (
|
||||||
<Col className={'w-full gap-3 pr-1'}>
|
<Col className={'w-full gap-3 pr-1'}>
|
||||||
<span
|
<span
|
||||||
|
@ -81,7 +67,6 @@ export function FeedCommentThread(props: {
|
||||||
betsByUserId={betsByUserId}
|
betsByUserId={betsByUserId}
|
||||||
tips={tips}
|
tips={tips}
|
||||||
smallAvatar={smallAvatar}
|
smallAvatar={smallAvatar}
|
||||||
truncate={truncate}
|
|
||||||
bets={bets}
|
bets={bets}
|
||||||
scrollAndOpenReplyInput={scrollAndOpenReplyInput}
|
scrollAndOpenReplyInput={scrollAndOpenReplyInput}
|
||||||
/>
|
/>
|
||||||
|
@ -98,13 +83,9 @@ export function FeedCommentThread(props: {
|
||||||
(c) => c.userId === user?.id
|
(c) => c.userId === user?.id
|
||||||
)}
|
)}
|
||||||
parentCommentId={parentComment.id}
|
parentCommentId={parentComment.id}
|
||||||
replyToUsername={replyToUsername}
|
replyToUser={replyToUser}
|
||||||
parentAnswerOutcome={comments[0].answerOutcome}
|
parentAnswerOutcome={comments[0].answerOutcome}
|
||||||
setRef={setInputRef}
|
onSubmitComment={() => setShowReply(false)}
|
||||||
onSubmitComment={() => {
|
|
||||||
setShowReply(false)
|
|
||||||
setReplyToUsername('')
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
</Col>
|
</Col>
|
||||||
)}
|
)}
|
||||||
|
@ -121,14 +102,12 @@ export function CommentRepliesList(props: {
|
||||||
bets: Bet[]
|
bets: Bet[]
|
||||||
treatFirstIndexEqually?: boolean
|
treatFirstIndexEqually?: boolean
|
||||||
smallAvatar?: boolean
|
smallAvatar?: boolean
|
||||||
truncate?: boolean
|
|
||||||
}) {
|
}) {
|
||||||
const {
|
const {
|
||||||
contract,
|
contract,
|
||||||
commentsList,
|
commentsList,
|
||||||
betsByUserId,
|
betsByUserId,
|
||||||
tips,
|
tips,
|
||||||
truncate,
|
|
||||||
smallAvatar,
|
smallAvatar,
|
||||||
bets,
|
bets,
|
||||||
scrollAndOpenReplyInput,
|
scrollAndOpenReplyInput,
|
||||||
|
@ -168,7 +147,6 @@ export function CommentRepliesList(props: {
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
smallAvatar={smallAvatar}
|
smallAvatar={smallAvatar}
|
||||||
truncate={truncate}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
@ -182,7 +160,6 @@ export function FeedComment(props: {
|
||||||
tips: CommentTips
|
tips: CommentTips
|
||||||
betsBySameUser: Bet[]
|
betsBySameUser: Bet[]
|
||||||
probAtCreatedTime?: number
|
probAtCreatedTime?: number
|
||||||
truncate?: boolean
|
|
||||||
smallAvatar?: boolean
|
smallAvatar?: boolean
|
||||||
onReplyClick?: (comment: Comment) => void
|
onReplyClick?: (comment: Comment) => void
|
||||||
}) {
|
}) {
|
||||||
|
@ -192,10 +169,10 @@ export function FeedComment(props: {
|
||||||
tips,
|
tips,
|
||||||
betsBySameUser,
|
betsBySameUser,
|
||||||
probAtCreatedTime,
|
probAtCreatedTime,
|
||||||
truncate,
|
|
||||||
onReplyClick,
|
onReplyClick,
|
||||||
} = props
|
} = props
|
||||||
const { text, userUsername, userName, userAvatarUrl, createdTime } = comment
|
const { text, content, userUsername, userName, userAvatarUrl, createdTime } =
|
||||||
|
comment
|
||||||
let betOutcome: string | undefined,
|
let betOutcome: string | undefined,
|
||||||
bought: string | undefined,
|
bought: string | undefined,
|
||||||
money: string | undefined
|
money: string | undefined
|
||||||
|
@ -276,11 +253,9 @@ export function FeedComment(props: {
|
||||||
elementId={comment.id}
|
elementId={comment.id}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<TruncatedComment
|
<div className="mt-2 text-[15px] text-gray-700">
|
||||||
comment={text}
|
<Content content={content || text} smallImage />
|
||||||
moreHref={contractPath(contract)}
|
</div>
|
||||||
shouldTruncate={truncate}
|
|
||||||
/>
|
|
||||||
<Row className="mt-2 items-center gap-6 text-xs text-gray-500">
|
<Row className="mt-2 items-center gap-6 text-xs text-gray-500">
|
||||||
<Tipper comment={comment} tips={tips ?? {}} />
|
<Tipper comment={comment} tips={tips ?? {}} />
|
||||||
{onReplyClick && (
|
{onReplyClick && (
|
||||||
|
@ -345,8 +320,7 @@ export function CommentInput(props: {
|
||||||
contract: Contract
|
contract: Contract
|
||||||
betsByCurrentUser: Bet[]
|
betsByCurrentUser: Bet[]
|
||||||
commentsByCurrentUser: Comment[]
|
commentsByCurrentUser: Comment[]
|
||||||
replyToUsername?: string
|
replyToUser?: { id: string; username: string }
|
||||||
setRef?: (ref: HTMLTextAreaElement) => void
|
|
||||||
// Reply to a free response answer
|
// Reply to a free response answer
|
||||||
parentAnswerOutcome?: string
|
parentAnswerOutcome?: string
|
||||||
// Reply to another comment
|
// Reply to another comment
|
||||||
|
@ -359,12 +333,18 @@ export function CommentInput(props: {
|
||||||
commentsByCurrentUser,
|
commentsByCurrentUser,
|
||||||
parentAnswerOutcome,
|
parentAnswerOutcome,
|
||||||
parentCommentId,
|
parentCommentId,
|
||||||
replyToUsername,
|
replyToUser,
|
||||||
onSubmitComment,
|
onSubmitComment,
|
||||||
setRef,
|
|
||||||
} = props
|
} = props
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
const [comment, setComment] = useState('')
|
const { editor, upload } = useTextEditor({
|
||||||
|
simple: true,
|
||||||
|
max: MAX_COMMENT_LENGTH,
|
||||||
|
placeholder:
|
||||||
|
!!parentCommentId || !!parentAnswerOutcome
|
||||||
|
? 'Write a reply...'
|
||||||
|
: 'Write a comment...',
|
||||||
|
})
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||||
|
|
||||||
const mostRecentCommentableBet = getMostRecentCommentableBet(
|
const mostRecentCommentableBet = getMostRecentCommentableBet(
|
||||||
|
@ -380,18 +360,17 @@ export function CommentInput(props: {
|
||||||
track('sign in to comment')
|
track('sign in to comment')
|
||||||
return await firebaseLogin()
|
return await firebaseLogin()
|
||||||
}
|
}
|
||||||
if (!comment || isSubmitting) return
|
if (!editor || editor.isEmpty || isSubmitting) return
|
||||||
setIsSubmitting(true)
|
setIsSubmitting(true)
|
||||||
await createCommentOnContract(
|
await createCommentOnContract(
|
||||||
contract.id,
|
contract.id,
|
||||||
comment,
|
editor.getJSON(),
|
||||||
user,
|
user,
|
||||||
betId,
|
betId,
|
||||||
parentAnswerOutcome,
|
parentAnswerOutcome,
|
||||||
parentCommentId
|
parentCommentId
|
||||||
)
|
)
|
||||||
onSubmitComment?.()
|
onSubmitComment?.()
|
||||||
setComment('')
|
|
||||||
setIsSubmitting(false)
|
setIsSubmitting(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -415,8 +394,8 @@ export function CommentInput(props: {
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className={'min-w-0 flex-1'}>
|
<div className={'min-w-0 flex-1'}>
|
||||||
<div className="pl-0.5 text-sm text-gray-500">
|
<div className="pl-0.5 text-sm">
|
||||||
<div className={'mb-1'}>
|
<div className="mb-1 text-gray-500">
|
||||||
{mostRecentCommentableBet && (
|
{mostRecentCommentableBet && (
|
||||||
<BetStatusText
|
<BetStatusText
|
||||||
contract={contract}
|
contract={contract}
|
||||||
|
@ -446,14 +425,12 @@ export function CommentInput(props: {
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<CommentInputTextArea
|
<CommentInputTextArea
|
||||||
commentText={comment}
|
editor={editor}
|
||||||
setComment={setComment}
|
upload={upload}
|
||||||
isReply={!!parentCommentId || !!parentAnswerOutcome}
|
replyToUser={replyToUser}
|
||||||
replyToUsername={replyToUsername ?? ''}
|
|
||||||
user={user}
|
user={user}
|
||||||
submitComment={submitComment}
|
submitComment={submitComment}
|
||||||
isSubmitting={isSubmitting}
|
isSubmitting={isSubmitting}
|
||||||
setRef={setRef}
|
|
||||||
presetId={id}
|
presetId={id}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
@ -465,94 +442,93 @@ export function CommentInput(props: {
|
||||||
|
|
||||||
export function CommentInputTextArea(props: {
|
export function CommentInputTextArea(props: {
|
||||||
user: User | undefined | null
|
user: User | undefined | null
|
||||||
isReply: boolean
|
replyToUser?: { id: string; username: string }
|
||||||
replyToUsername: string
|
editor: Editor | null
|
||||||
commentText: string
|
upload: Parameters<typeof TextEditor>[0]['upload']
|
||||||
setComment: (text: string) => void
|
|
||||||
submitComment: (id?: string) => void
|
submitComment: (id?: string) => void
|
||||||
isSubmitting: boolean
|
isSubmitting: boolean
|
||||||
setRef?: (ref: HTMLTextAreaElement) => void
|
submitOnEnter?: boolean
|
||||||
presetId?: string
|
presetId?: string
|
||||||
enterToSubmitOnDesktop?: boolean
|
|
||||||
}) {
|
}) {
|
||||||
const {
|
const {
|
||||||
isReply,
|
|
||||||
setRef,
|
|
||||||
user,
|
user,
|
||||||
commentText,
|
editor,
|
||||||
setComment,
|
upload,
|
||||||
submitComment,
|
submitComment,
|
||||||
presetId,
|
presetId,
|
||||||
isSubmitting,
|
isSubmitting,
|
||||||
replyToUsername,
|
submitOnEnter,
|
||||||
enterToSubmitOnDesktop,
|
replyToUser,
|
||||||
} = props
|
} = props
|
||||||
const { width } = useWindowSize()
|
const isMobile = (useWindowSize().width ?? 0) < 768 // TODO: base off input device (keybord vs touch)
|
||||||
const memoizedSetComment = useEvent(setComment)
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!replyToUsername || !user || replyToUsername === user.username) return
|
editor?.setEditable(!isSubmitting)
|
||||||
const replacement = `@${replyToUsername} `
|
}, [isSubmitting, editor])
|
||||||
memoizedSetComment(replacement + commentText.replace(replacement, ''))
|
|
||||||
|
const submit = () => {
|
||||||
|
submitComment(presetId)
|
||||||
|
editor?.commands?.clearContent()
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!editor) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// submit on Enter key
|
||||||
|
editor.setOptions({
|
||||||
|
editorProps: {
|
||||||
|
handleKeyDown: (view, event) => {
|
||||||
|
if (
|
||||||
|
submitOnEnter &&
|
||||||
|
event.key === 'Enter' &&
|
||||||
|
!event.shiftKey &&
|
||||||
|
(!isMobile || event.ctrlKey || event.metaKey) &&
|
||||||
|
// mention list is closed
|
||||||
|
!(view.state as any).mention$.active
|
||||||
|
) {
|
||||||
|
submit()
|
||||||
|
event.preventDefault()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
// insert at mention and focus
|
||||||
|
if (replyToUser) {
|
||||||
|
editor
|
||||||
|
.chain()
|
||||||
|
.setContent({
|
||||||
|
type: 'mention',
|
||||||
|
attrs: { label: replyToUser.username, id: replyToUser.id },
|
||||||
|
})
|
||||||
|
.insertContent(' ')
|
||||||
|
.focus()
|
||||||
|
.run()
|
||||||
|
}
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [user, replyToUsername, memoizedSetComment])
|
}, [editor])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Row className="gap-1.5 text-gray-700">
|
<div>
|
||||||
<Textarea
|
<TextEditor editor={editor} upload={upload}>
|
||||||
ref={setRef}
|
|
||||||
value={commentText}
|
|
||||||
onChange={(e) => setComment(e.target.value)}
|
|
||||||
className={clsx('textarea textarea-bordered w-full resize-none')}
|
|
||||||
// Make room for floating submit button.
|
|
||||||
style={{ paddingRight: 48 }}
|
|
||||||
placeholder={
|
|
||||||
isReply
|
|
||||||
? 'Write a reply... '
|
|
||||||
: enterToSubmitOnDesktop
|
|
||||||
? 'Send a message'
|
|
||||||
: 'Write a comment...'
|
|
||||||
}
|
|
||||||
autoFocus={false}
|
|
||||||
maxLength={MAX_COMMENT_LENGTH}
|
|
||||||
disabled={isSubmitting}
|
|
||||||
onKeyDown={(e) => {
|
|
||||||
if (
|
|
||||||
(enterToSubmitOnDesktop &&
|
|
||||||
e.key === 'Enter' &&
|
|
||||||
!e.shiftKey &&
|
|
||||||
width &&
|
|
||||||
width > 768) ||
|
|
||||||
(e.key === 'Enter' && (e.ctrlKey || e.metaKey))
|
|
||||||
) {
|
|
||||||
e.preventDefault()
|
|
||||||
submitComment(presetId)
|
|
||||||
e.currentTarget.blur()
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Col className={clsx('relative justify-end')}>
|
|
||||||
{user && !isSubmitting && (
|
{user && !isSubmitting && (
|
||||||
<button
|
<button
|
||||||
className={clsx(
|
className="btn btn-ghost btn-sm px-2 disabled:bg-inherit disabled:text-gray-300"
|
||||||
'btn btn-ghost btn-sm absolute right-2 bottom-2 flex-row pl-2 capitalize',
|
disabled={!editor || editor.isEmpty}
|
||||||
!commentText && 'pointer-events-none text-gray-500'
|
onClick={submit}
|
||||||
)}
|
|
||||||
onClick={() => {
|
|
||||||
submitComment(presetId)
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
<PaperAirplaneIcon
|
<PaperAirplaneIcon className="m-0 h-[25px] min-w-[22px] rotate-90 p-0" />
|
||||||
className={'m-0 min-w-[22px] rotate-90 p-0 '}
|
|
||||||
height={25}
|
|
||||||
/>
|
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{isSubmitting && (
|
{isSubmitting && (
|
||||||
<LoadingIndicator spinnerClassName={'border-gray-500'} />
|
<LoadingIndicator spinnerClassName={'border-gray-500'} />
|
||||||
)}
|
)}
|
||||||
</Col>
|
</TextEditor>
|
||||||
</Row>
|
</div>
|
||||||
<Row>
|
<Row>
|
||||||
{!user && (
|
{!user && (
|
||||||
<button
|
<button
|
||||||
|
@ -567,38 +543,6 @@ export function CommentInputTextArea(props: {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function TruncatedComment(props: {
|
|
||||||
comment: string
|
|
||||||
moreHref: string
|
|
||||||
shouldTruncate?: boolean
|
|
||||||
}) {
|
|
||||||
const { comment, moreHref, shouldTruncate } = props
|
|
||||||
let truncated = comment
|
|
||||||
|
|
||||||
// Keep descriptions to at most 400 characters
|
|
||||||
const MAX_CHARS = 400
|
|
||||||
if (shouldTruncate && truncated.length > MAX_CHARS) {
|
|
||||||
truncated = truncated.slice(0, MAX_CHARS)
|
|
||||||
// Make sure to end on a space
|
|
||||||
const i = truncated.lastIndexOf(' ')
|
|
||||||
truncated = truncated.slice(0, i)
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className="mt-2 whitespace-pre-line break-words text-gray-700"
|
|
||||||
style={{ fontSize: 15 }}
|
|
||||||
>
|
|
||||||
<Linkify text={truncated} />
|
|
||||||
{truncated != comment && (
|
|
||||||
<SiteLink href={moreHref} className="text-indigo-700">
|
|
||||||
... (show more)
|
|
||||||
</SiteLink>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function getBettorsLargestPositionBeforeTime(
|
function getBettorsLargestPositionBeforeTime(
|
||||||
contract: Contract,
|
contract: Contract,
|
||||||
createdTime: number,
|
createdTime: number,
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// From https://tailwindui.com/components/application-ui/lists/feeds
|
// From https://tailwindui.com/components/application-ui/lists/feeds
|
||||||
import React, { useState } from 'react'
|
import React from 'react'
|
||||||
import {
|
import {
|
||||||
BanIcon,
|
BanIcon,
|
||||||
CheckIcon,
|
CheckIcon,
|
||||||
|
@ -22,7 +22,6 @@ import { UserLink } from '../user-page'
|
||||||
import BetRow from '../bet-row'
|
import BetRow from '../bet-row'
|
||||||
import { Avatar } from '../avatar'
|
import { Avatar } from '../avatar'
|
||||||
import { ActivityItem } from './activity-items'
|
import { ActivityItem } from './activity-items'
|
||||||
import { useSaveSeenContract } from 'web/hooks/use-seen-contracts'
|
|
||||||
import { useUser } from 'web/hooks/use-user'
|
import { useUser } from 'web/hooks/use-user'
|
||||||
import { trackClick } from 'web/lib/firebase/tracking'
|
import { trackClick } from 'web/lib/firebase/tracking'
|
||||||
import { DAY_MS } from 'common/util/time'
|
import { DAY_MS } from 'common/util/time'
|
||||||
|
@ -50,11 +49,8 @@ export function FeedItems(props: {
|
||||||
const { contract, items, className, betRowClassName, user } = props
|
const { contract, items, className, betRowClassName, user } = props
|
||||||
const { outcomeType } = contract
|
const { outcomeType } = contract
|
||||||
|
|
||||||
const [elem, setElem] = useState<HTMLElement | null>(null)
|
|
||||||
useSaveSeenContract(elem, contract)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={clsx('flow-root', className)} ref={setElem}>
|
<div className={clsx('flow-root', className)}>
|
||||||
<div className={clsx(tradingAllowed(contract) ? '' : '-mb-6')}>
|
<div className={clsx(tradingAllowed(contract) ? '' : '-mb-6')}>
|
||||||
{items.map((item, activityItemIdx) => (
|
{items.map((item, activityItemIdx) => (
|
||||||
<div key={item.id} className={'relative pb-4'}>
|
<div key={item.id} className={'relative pb-4'}>
|
||||||
|
|
7
web/components/fullscreen-confetti.tsx
Normal file
7
web/components/fullscreen-confetti.tsx
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import Confetti, { Props as ConfettiProps } from 'react-confetti'
|
||||||
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
||||||
|
|
||||||
|
export function FullscreenConfetti(props: ConfettiProps) {
|
||||||
|
const { width, height } = useWindowSize()
|
||||||
|
return <Confetti {...props} width={width} height={height} />
|
||||||
|
}
|
|
@ -5,27 +5,23 @@ import React, { useEffect, memo, useState, useMemo } from 'react'
|
||||||
import { Avatar } from 'web/components/avatar'
|
import { Avatar } from 'web/components/avatar'
|
||||||
import { Group } from 'common/group'
|
import { Group } from 'common/group'
|
||||||
import { Comment, createCommentOnGroup } from 'web/lib/firebase/comments'
|
import { Comment, createCommentOnGroup } from 'web/lib/firebase/comments'
|
||||||
import {
|
import { CommentInputTextArea } from 'web/components/feed/feed-comments'
|
||||||
CommentInputTextArea,
|
|
||||||
TruncatedComment,
|
|
||||||
} from 'web/components/feed/feed-comments'
|
|
||||||
import { track } from 'web/lib/service/analytics'
|
import { track } from 'web/lib/service/analytics'
|
||||||
import { firebaseLogin } from 'web/lib/firebase/users'
|
import { firebaseLogin } from 'web/lib/firebase/users'
|
||||||
|
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import { UserLink } from 'web/components/user-page'
|
import { UserLink } from 'web/components/user-page'
|
||||||
|
|
||||||
import { groupPath } from 'web/lib/firebase/groups'
|
|
||||||
import { CopyLinkDateTimeComponent } from 'web/components/feed/copy-link-date-time'
|
import { CopyLinkDateTimeComponent } from 'web/components/feed/copy-link-date-time'
|
||||||
import { CommentTipMap, CommentTips } from 'web/hooks/use-tip-txns'
|
import { CommentTipMap, CommentTips } from 'web/hooks/use-tip-txns'
|
||||||
import { Tipper } from 'web/components/tipper'
|
import { Tipper } from 'web/components/tipper'
|
||||||
import { sum } from 'lodash'
|
import { sum } from 'lodash'
|
||||||
import { formatMoney } from 'common/util/format'
|
import { formatMoney } from 'common/util/format'
|
||||||
import { useWindowSize } from 'web/hooks/use-window-size'
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
||||||
|
import { Content, useTextEditor } from 'web/components/editor'
|
||||||
import { useUnseenPreferredNotifications } from 'web/hooks/use-notifications'
|
import { useUnseenPreferredNotifications } from 'web/hooks/use-notifications'
|
||||||
import { ChatIcon, ChevronDownIcon } from '@heroicons/react/outline'
|
import { ChevronDownIcon, UsersIcon } from '@heroicons/react/outline'
|
||||||
import { setNotificationsAsSeen } from 'web/pages/notifications'
|
import { setNotificationsAsSeen } from 'web/pages/notifications'
|
||||||
|
import { usePrivateUser } from 'web/hooks/use-user'
|
||||||
|
|
||||||
export function GroupChat(props: {
|
export function GroupChat(props: {
|
||||||
messages: Comment[]
|
messages: Comment[]
|
||||||
|
@ -34,16 +30,21 @@ export function GroupChat(props: {
|
||||||
tips: CommentTipMap
|
tips: CommentTipMap
|
||||||
}) {
|
}) {
|
||||||
const { messages, user, group, tips } = props
|
const { messages, user, group, tips } = props
|
||||||
const [messageText, setMessageText] = useState('')
|
|
||||||
|
const privateUser = usePrivateUser()
|
||||||
|
|
||||||
|
const { editor, upload } = useTextEditor({
|
||||||
|
simple: true,
|
||||||
|
placeholder: 'Send a message',
|
||||||
|
})
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||||
const [scrollToBottomRef, setScrollToBottomRef] =
|
const [scrollToBottomRef, setScrollToBottomRef] =
|
||||||
useState<HTMLDivElement | null>(null)
|
useState<HTMLDivElement | null>(null)
|
||||||
const [scrollToMessageId, setScrollToMessageId] = useState('')
|
const [scrollToMessageId, setScrollToMessageId] = useState('')
|
||||||
const [scrollToMessageRef, setScrollToMessageRef] =
|
const [scrollToMessageRef, setScrollToMessageRef] =
|
||||||
useState<HTMLDivElement | null>(null)
|
useState<HTMLDivElement | null>(null)
|
||||||
const [replyToUsername, setReplyToUsername] = useState('')
|
const [replyToUser, setReplyToUser] = useState<any>()
|
||||||
const [inputRef, setInputRef] = useState<HTMLTextAreaElement | null>(null)
|
|
||||||
const [groupedMessages, setGroupedMessages] = useState<Comment[]>([])
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const isMember = user && group.memberIds.includes(user?.id)
|
const isMember = user && group.memberIds.includes(user?.id)
|
||||||
|
|
||||||
|
@ -54,25 +55,26 @@ export function GroupChat(props: {
|
||||||
const remainingHeight =
|
const remainingHeight =
|
||||||
(height ?? 0) - (containerRef?.offsetTop ?? 0) - bottomBarHeight
|
(height ?? 0) - (containerRef?.offsetTop ?? 0) - bottomBarHeight
|
||||||
|
|
||||||
useMemo(() => {
|
// array of groups, where each group is an array of messages that are displayed as one
|
||||||
|
const groupedMessages = useMemo(() => {
|
||||||
// Group messages with createdTime within 2 minutes of each other.
|
// Group messages with createdTime within 2 minutes of each other.
|
||||||
const tempMessages = []
|
const tempGrouped: Comment[][] = []
|
||||||
for (let i = 0; i < messages.length; i++) {
|
for (let i = 0; i < messages.length; i++) {
|
||||||
const message = messages[i]
|
const message = messages[i]
|
||||||
if (i === 0) tempMessages.push({ ...message })
|
if (i === 0) tempGrouped.push([message])
|
||||||
else {
|
else {
|
||||||
const prevMessage = messages[i - 1]
|
const prevMessage = messages[i - 1]
|
||||||
const diff = message.createdTime - prevMessage.createdTime
|
const diff = message.createdTime - prevMessage.createdTime
|
||||||
const creatorsMatch = message.userId === prevMessage.userId
|
const creatorsMatch = message.userId === prevMessage.userId
|
||||||
if (diff < 2 * 60 * 1000 && creatorsMatch) {
|
if (diff < 2 * 60 * 1000 && creatorsMatch) {
|
||||||
tempMessages[tempMessages.length - 1].text += `\n${message.text}`
|
tempGrouped.at(-1)?.push(message)
|
||||||
} else {
|
} else {
|
||||||
tempMessages.push({ ...message })
|
tempGrouped.push([message])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setGroupedMessages(tempMessages)
|
return tempGrouped
|
||||||
}, [messages])
|
}, [messages])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -94,11 +96,12 @@ export function GroupChat(props: {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// is mobile?
|
// is mobile?
|
||||||
if (inputRef && width && width > 720) inputRef.focus()
|
if (width && width > 720) focusInput()
|
||||||
}, [inputRef, width])
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [width])
|
||||||
|
|
||||||
function onReplyClick(comment: Comment) {
|
function onReplyClick(comment: Comment) {
|
||||||
setReplyToUsername(comment.userUsername)
|
setReplyToUser({ id: comment.userId, username: comment.userUsername })
|
||||||
}
|
}
|
||||||
|
|
||||||
async function submitMessage() {
|
async function submitMessage() {
|
||||||
|
@ -106,13 +109,16 @@ export function GroupChat(props: {
|
||||||
track('sign in to comment')
|
track('sign in to comment')
|
||||||
return await firebaseLogin()
|
return await firebaseLogin()
|
||||||
}
|
}
|
||||||
if (!messageText || isSubmitting) return
|
if (!editor || editor.isEmpty || isSubmitting) return
|
||||||
setIsSubmitting(true)
|
setIsSubmitting(true)
|
||||||
await createCommentOnGroup(group.id, messageText, user)
|
await createCommentOnGroup(group.id, editor.getJSON(), user)
|
||||||
setMessageText('')
|
editor.commands.clearContent()
|
||||||
setIsSubmitting(false)
|
setIsSubmitting(false)
|
||||||
setReplyToUsername('')
|
setReplyToUser(undefined)
|
||||||
inputRef?.focus()
|
focusInput()
|
||||||
|
}
|
||||||
|
function focusInput() {
|
||||||
|
editor?.commands.focus()
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -123,20 +129,20 @@ export function GroupChat(props: {
|
||||||
}
|
}
|
||||||
ref={setScrollToBottomRef}
|
ref={setScrollToBottomRef}
|
||||||
>
|
>
|
||||||
{groupedMessages.map((message) => (
|
{groupedMessages.map((messages) => (
|
||||||
<GroupMessage
|
<GroupMessage
|
||||||
user={user}
|
user={user}
|
||||||
key={message.id}
|
key={`group ${messages[0].id}`}
|
||||||
comment={message}
|
comments={messages}
|
||||||
group={group}
|
group={group}
|
||||||
onReplyClick={onReplyClick}
|
onReplyClick={onReplyClick}
|
||||||
highlight={message.id === scrollToMessageId}
|
highlight={messages[0].id === scrollToMessageId}
|
||||||
setRef={
|
setRef={
|
||||||
scrollToMessageId === message.id
|
scrollToMessageId === messages[0].id
|
||||||
? setScrollToMessageRef
|
? setScrollToMessageRef
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
tips={tips[message.id] ?? {}}
|
tips={tips[messages[0].id] ?? {}}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
{messages.length === 0 && (
|
{messages.length === 0 && (
|
||||||
|
@ -144,7 +150,7 @@ export function GroupChat(props: {
|
||||||
No messages yet. Why not{isMember ? ` ` : ' join and '}
|
No messages yet. Why not{isMember ? ` ` : ' join and '}
|
||||||
<button
|
<button
|
||||||
className={'cursor-pointer font-bold text-gray-700'}
|
className={'cursor-pointer font-bold text-gray-700'}
|
||||||
onClick={() => inputRef?.focus()}
|
onClick={focusInput}
|
||||||
>
|
>
|
||||||
add one?
|
add one?
|
||||||
</button>
|
</button>
|
||||||
|
@ -162,19 +168,26 @@ export function GroupChat(props: {
|
||||||
</div>
|
</div>
|
||||||
<div className={'flex-1'}>
|
<div className={'flex-1'}>
|
||||||
<CommentInputTextArea
|
<CommentInputTextArea
|
||||||
commentText={messageText}
|
editor={editor}
|
||||||
setComment={setMessageText}
|
upload={upload}
|
||||||
isReply={false}
|
|
||||||
user={user}
|
user={user}
|
||||||
replyToUsername={replyToUsername}
|
replyToUser={replyToUser}
|
||||||
submitComment={submitMessage}
|
submitComment={submitMessage}
|
||||||
isSubmitting={isSubmitting}
|
isSubmitting={isSubmitting}
|
||||||
enterToSubmitOnDesktop={true}
|
submitOnEnter
|
||||||
setRef={setInputRef}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{privateUser && (
|
||||||
|
<GroupChatNotificationsIcon
|
||||||
|
group={group}
|
||||||
|
privateUser={privateUser}
|
||||||
|
shouldSetAsSeen={true}
|
||||||
|
hidden={true}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</Col>
|
</Col>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -239,7 +252,7 @@ export function GroupChatInBubble(props: {
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{!shouldShowChat ? (
|
{!shouldShowChat ? (
|
||||||
<ChatIcon className="h-10 w-10" aria-hidden="true" />
|
<UsersIcon className="h-10 w-10" aria-hidden="true" />
|
||||||
) : (
|
) : (
|
||||||
<ChevronDownIcon className={'h-10 w-10'} aria-hidden={'true'} />
|
<ChevronDownIcon className={'h-10 w-10'} aria-hidden={'true'} />
|
||||||
)}
|
)}
|
||||||
|
@ -248,6 +261,7 @@ export function GroupChatInBubble(props: {
|
||||||
group={group}
|
group={group}
|
||||||
privateUser={privateUser}
|
privateUser={privateUser}
|
||||||
shouldSetAsSeen={shouldShowChat}
|
shouldSetAsSeen={shouldShowChat}
|
||||||
|
hidden={false}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</button>
|
</button>
|
||||||
|
@ -259,8 +273,9 @@ function GroupChatNotificationsIcon(props: {
|
||||||
group: Group
|
group: Group
|
||||||
privateUser: PrivateUser
|
privateUser: PrivateUser
|
||||||
shouldSetAsSeen: boolean
|
shouldSetAsSeen: boolean
|
||||||
|
hidden: boolean
|
||||||
}) {
|
}) {
|
||||||
const { privateUser, group, shouldSetAsSeen } = props
|
const { privateUser, group, shouldSetAsSeen, hidden } = props
|
||||||
const preferredNotificationsForThisGroup = useUnseenPreferredNotifications(
|
const preferredNotificationsForThisGroup = useUnseenPreferredNotifications(
|
||||||
privateUser,
|
privateUser,
|
||||||
{
|
{
|
||||||
|
@ -282,7 +297,9 @@ function GroupChatNotificationsIcon(props: {
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={
|
className={
|
||||||
preferredNotificationsForThisGroup.length > 0 && !shouldSetAsSeen
|
!hidden &&
|
||||||
|
preferredNotificationsForThisGroup.length > 0 &&
|
||||||
|
!shouldSetAsSeen
|
||||||
? 'absolute right-4 top-4 h-3 w-3 rounded-full border-2 border-white bg-red-500'
|
? 'absolute right-4 top-4 h-3 w-3 rounded-full border-2 border-white bg-red-500'
|
||||||
: 'hidden'
|
: 'hidden'
|
||||||
}
|
}
|
||||||
|
@ -292,16 +309,18 @@ function GroupChatNotificationsIcon(props: {
|
||||||
|
|
||||||
const GroupMessage = memo(function GroupMessage_(props: {
|
const GroupMessage = memo(function GroupMessage_(props: {
|
||||||
user: User | null | undefined
|
user: User | null | undefined
|
||||||
comment: Comment
|
comments: Comment[]
|
||||||
group: Group
|
group: Group
|
||||||
onReplyClick?: (comment: Comment) => void
|
onReplyClick?: (comment: Comment) => void
|
||||||
setRef?: (ref: HTMLDivElement) => void
|
setRef?: (ref: HTMLDivElement) => void
|
||||||
highlight?: boolean
|
highlight?: boolean
|
||||||
tips: CommentTips
|
tips: CommentTips
|
||||||
}) {
|
}) {
|
||||||
const { comment, onReplyClick, group, setRef, highlight, user, tips } = props
|
const { comments, onReplyClick, group, setRef, highlight, user, tips } = props
|
||||||
const { text, userUsername, userName, userAvatarUrl, createdTime } = comment
|
const first = comments[0]
|
||||||
const isCreatorsComment = user && comment.userId === user.id
|
const { id, userUsername, userName, userAvatarUrl, createdTime } = first
|
||||||
|
|
||||||
|
const isCreatorsComment = user && first.userId === user.id
|
||||||
return (
|
return (
|
||||||
<Col
|
<Col
|
||||||
ref={setRef}
|
ref={setRef}
|
||||||
|
@ -331,23 +350,25 @@ const GroupMessage = memo(function GroupMessage_(props: {
|
||||||
prefix={'group'}
|
prefix={'group'}
|
||||||
slug={group.slug}
|
slug={group.slug}
|
||||||
createdTime={createdTime}
|
createdTime={createdTime}
|
||||||
elementId={comment.id}
|
elementId={id}
|
||||||
/>
|
|
||||||
</Row>
|
|
||||||
<Row className={'text-black'}>
|
|
||||||
<TruncatedComment
|
|
||||||
comment={text}
|
|
||||||
moreHref={groupPath(group.slug)}
|
|
||||||
shouldTruncate={false}
|
|
||||||
/>
|
/>
|
||||||
</Row>
|
</Row>
|
||||||
|
<div className="mt-2 text-base text-black">
|
||||||
|
{comments.map((comment) => (
|
||||||
|
<Content
|
||||||
|
key={comment.id}
|
||||||
|
content={comment.content || comment.text}
|
||||||
|
smallImage
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
<Row>
|
<Row>
|
||||||
{!isCreatorsComment && onReplyClick && (
|
{!isCreatorsComment && onReplyClick && (
|
||||||
<button
|
<button
|
||||||
className={
|
className={
|
||||||
'self-start py-1 text-xs font-bold text-gray-500 hover:underline'
|
'self-start py-1 text-xs font-bold text-gray-500 hover:underline'
|
||||||
}
|
}
|
||||||
onClick={() => onReplyClick(comment)}
|
onClick={() => onReplyClick(first)}
|
||||||
>
|
>
|
||||||
Reply
|
Reply
|
||||||
</button>
|
</button>
|
||||||
|
@ -357,7 +378,7 @@ const GroupMessage = memo(function GroupMessage_(props: {
|
||||||
{formatMoney(sum(Object.values(tips)))}
|
{formatMoney(sum(Object.values(tips)))}
|
||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
{!isCreatorsComment && <Tipper comment={comment} tips={tips} />}
|
{!isCreatorsComment && <Tipper comment={first} tips={tips} />}
|
||||||
</Row>
|
</Row>
|
||||||
</Col>
|
</Col>
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import { InformationCircleIcon } from '@heroicons/react/outline'
|
import { InformationCircleIcon } from '@heroicons/react/outline'
|
||||||
|
import { Tooltip } from './tooltip'
|
||||||
|
|
||||||
export function InfoTooltip(props: { text: string }) {
|
export function InfoTooltip(props: { text: string }) {
|
||||||
const { text } = props
|
const { text } = props
|
||||||
return (
|
return (
|
||||||
<div className="tooltip" data-tip={text}>
|
<Tooltip text={text}>
|
||||||
<InformationCircleIcon className="h-5 w-5 text-gray-500" />
|
<InformationCircleIcon className="h-5 w-5 text-gray-500" />
|
||||||
</div>
|
</Tooltip>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { Contract } from 'common/contract'
|
||||||
|
|
||||||
import { Spacer } from './layout/spacer'
|
import { Spacer } from './layout/spacer'
|
||||||
import { firebaseLogin } from 'web/lib/firebase/users'
|
import { firebaseLogin } from 'web/lib/firebase/users'
|
||||||
import { ContractsGrid } from './contract/contracts-list'
|
import { ContractsGrid } from './contract/contracts-grid'
|
||||||
import { Col } from './layout/col'
|
import { Col } from './layout/col'
|
||||||
import { Row } from './layout/row'
|
import { Row } from './layout/row'
|
||||||
import { withTracking } from 'web/lib/service/analytics'
|
import { withTracking } from 'web/lib/service/analytics'
|
||||||
|
@ -59,11 +59,7 @@ export function LandingPagePanel(props: { hotContracts: Contract[] }) {
|
||||||
<SparklesIcon className="inline h-5 w-5" aria-hidden="true" />
|
<SparklesIcon className="inline h-5 w-5" aria-hidden="true" />
|
||||||
Trending markets
|
Trending markets
|
||||||
</Row>
|
</Row>
|
||||||
<ContractsGrid
|
<ContractsGrid contracts={hotContracts?.slice(0, 10) || []} />
|
||||||
contracts={hotContracts?.slice(0, 10) || []}
|
|
||||||
loadMore={() => {}}
|
|
||||||
hasMore={false}
|
|
||||||
/>
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,6 @@ export function ControlledTabs(props: TabProps & { activeIndex: number }) {
|
||||||
className,
|
className,
|
||||||
currentPageForAnalytics,
|
currentPageForAnalytics,
|
||||||
} = props
|
} = props
|
||||||
const activeTab = tabs[activeIndex] as Tab | undefined // can be undefined in weird case
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<nav
|
<nav
|
||||||
|
@ -64,7 +63,11 @@ export function ControlledTabs(props: TabProps & { activeIndex: number }) {
|
||||||
</a>
|
</a>
|
||||||
))}
|
))}
|
||||||
</nav>
|
</nav>
|
||||||
{activeTab?.content}
|
{tabs.map((tab, i) => (
|
||||||
|
<div key={i} className={i === activeIndex ? 'block' : 'hidden'}>
|
||||||
|
{tab.content}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,6 @@ import { useUserLiquidity } from 'web/hooks/use-liquidity'
|
||||||
import { Tabs } from './layout/tabs'
|
import { Tabs } from './layout/tabs'
|
||||||
import { NoLabel, YesLabel } from './outcome-label'
|
import { NoLabel, YesLabel } from './outcome-label'
|
||||||
import { Col } from './layout/col'
|
import { Col } from './layout/col'
|
||||||
import { InfoTooltip } from './info-tooltip'
|
|
||||||
import { track } from 'web/lib/service/analytics'
|
import { track } from 'web/lib/service/analytics'
|
||||||
|
|
||||||
export function LiquidityPanel(props: { contract: CPMMContract }) {
|
export function LiquidityPanel(props: { contract: CPMMContract }) {
|
||||||
|
@ -103,8 +102,7 @@ function AddLiquidityPanel(props: { contract: CPMMContract }) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="align-center mb-4 text-gray-500">
|
<div className="align-center mb-4 text-gray-500">
|
||||||
Subsidize this market by adding M$ to the liquidity pool.{' '}
|
Subsidize this market by adding M$ to the liquidity pool.
|
||||||
<InfoTooltip text="The greater the M$ subsidy, the greater the incentive for traders to participate, the more accurate the market will be." />
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Row>
|
<Row>
|
||||||
|
@ -114,6 +112,7 @@ function AddLiquidityPanel(props: { contract: CPMMContract }) {
|
||||||
label="M$"
|
label="M$"
|
||||||
error={error}
|
error={error}
|
||||||
disabled={isLoading}
|
disabled={isLoading}
|
||||||
|
inputClassName="w-28"
|
||||||
/>
|
/>
|
||||||
<button
|
<button
|
||||||
className={clsx('btn btn-primary ml-2', isLoading && 'btn-disabled')}
|
className={clsx('btn btn-primary ml-2', isLoading && 'btn-disabled')}
|
||||||
|
|
|
@ -44,7 +44,7 @@ export function BottomNavBar() {
|
||||||
const currentPage = router.pathname
|
const currentPage = router.pathname
|
||||||
|
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
const privateUser = usePrivateUser(user?.id)
|
const privateUser = usePrivateUser()
|
||||||
|
|
||||||
const isIframe = useIsIframe()
|
const isIframe = useIsIframe()
|
||||||
if (isIframe) {
|
if (isIframe) {
|
||||||
|
|
|
@ -29,6 +29,8 @@ import { Spacer } from '../layout/spacer'
|
||||||
import { useUnseenPreferredNotifications } from 'web/hooks/use-notifications'
|
import { useUnseenPreferredNotifications } from 'web/hooks/use-notifications'
|
||||||
import { PrivateUser } from 'common/user'
|
import { PrivateUser } from 'common/user'
|
||||||
import { useWindowSize } from 'web/hooks/use-window-size'
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
||||||
|
import { CHALLENGES_ENABLED } from 'common/challenge'
|
||||||
|
import { buildArray } from 'common/util/array'
|
||||||
|
|
||||||
const logout = async () => {
|
const logout = async () => {
|
||||||
// log out, and then reload the page, in case SSR wants to boot them out
|
// log out, and then reload the page, in case SSR wants to boot them out
|
||||||
|
@ -60,26 +62,40 @@ function getMoreNavigation(user?: User | null) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return [
|
return buildArray(
|
||||||
{ name: 'Charity', href: '/charity' },
|
CHALLENGES_ENABLED && { name: 'Challenges', href: '/challenges' },
|
||||||
{ name: 'Blog', href: 'https://news.manifold.markets' },
|
[
|
||||||
{ name: 'Discord', href: 'https://discord.gg/eHQBNBqXuh' },
|
{ name: 'Charity', href: '/charity' },
|
||||||
{ name: 'Twitter', href: 'https://twitter.com/ManifoldMarkets' },
|
{
|
||||||
]
|
name: 'Salem tournament',
|
||||||
|
href: 'https://salemcenter.manifold.markets/',
|
||||||
|
},
|
||||||
|
{ name: 'Blog', href: 'https://news.manifold.markets' },
|
||||||
|
{ name: 'Discord', href: 'https://discord.gg/eHQBNBqXuh' },
|
||||||
|
{ name: 'Twitter', href: 'https://twitter.com/ManifoldMarkets' },
|
||||||
|
]
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return buildArray(
|
||||||
{ name: 'Referrals', href: '/referrals' },
|
CHALLENGES_ENABLED && { name: 'Challenges', href: '/challenges' },
|
||||||
{ name: 'Charity', href: '/charity' },
|
[
|
||||||
{ name: 'Send M$', href: '/links' },
|
{ name: 'Referrals', href: '/referrals' },
|
||||||
{ name: 'Discord', href: 'https://discord.gg/eHQBNBqXuh' },
|
{ name: 'Charity', href: '/charity' },
|
||||||
{ name: 'About', href: 'https://docs.manifold.markets/$how-to' },
|
{ name: 'Send M$', href: '/links' },
|
||||||
{
|
{
|
||||||
name: 'Sign out',
|
name: 'Salem tournament',
|
||||||
href: '#',
|
href: 'https://salemcenter.manifold.markets/',
|
||||||
onClick: logout,
|
},
|
||||||
},
|
{ name: 'Discord', href: 'https://discord.gg/eHQBNBqXuh' },
|
||||||
]
|
{ name: 'About', href: 'https://docs.manifold.markets/$how-to' },
|
||||||
|
{
|
||||||
|
name: 'Sign out',
|
||||||
|
href: '#',
|
||||||
|
onClick: logout,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const signedOutNavigation = [
|
const signedOutNavigation = [
|
||||||
|
@ -116,21 +132,27 @@ const signedInMobileNavigation = [
|
||||||
]
|
]
|
||||||
|
|
||||||
function getMoreMobileNav() {
|
function getMoreMobileNav() {
|
||||||
return [
|
const signOut = {
|
||||||
...(IS_PRIVATE_MANIFOLD
|
name: 'Sign out',
|
||||||
? []
|
href: '#',
|
||||||
: [
|
onClick: logout,
|
||||||
{ name: 'Referrals', href: '/referrals' },
|
}
|
||||||
{ name: 'Charity', href: '/charity' },
|
if (IS_PRIVATE_MANIFOLD) return [signOut]
|
||||||
{ name: 'Send M$', href: '/links' },
|
|
||||||
{ name: 'Discord', href: 'https://discord.gg/eHQBNBqXuh' },
|
return buildArray<Item>(
|
||||||
]),
|
CHALLENGES_ENABLED && { name: 'Challenges', href: '/challenges' },
|
||||||
{
|
[
|
||||||
name: 'Sign out',
|
{ name: 'Referrals', href: '/referrals' },
|
||||||
href: '#',
|
{
|
||||||
onClick: logout,
|
name: 'Salem tournament',
|
||||||
},
|
href: 'https://salemcenter.manifold.markets/',
|
||||||
]
|
},
|
||||||
|
{ name: 'Charity', href: '/charity' },
|
||||||
|
{ name: 'Send M$', href: '/links' },
|
||||||
|
{ name: 'Discord', href: 'https://discord.gg/eHQBNBqXuh' },
|
||||||
|
],
|
||||||
|
signOut
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Item = {
|
export type Item = {
|
||||||
|
@ -199,7 +221,7 @@ export default function Sidebar(props: { className?: string }) {
|
||||||
const currentPage = router.pathname
|
const currentPage = router.pathname
|
||||||
|
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
const privateUser = usePrivateUser(user?.id)
|
const privateUser = usePrivateUser()
|
||||||
// usePing(user?.id)
|
// usePing(user?.id)
|
||||||
|
|
||||||
const navigationOptions = !user ? signedOutNavigation : getNavigation()
|
const navigationOptions = !user ? signedOutNavigation : getNavigation()
|
||||||
|
@ -295,8 +317,7 @@ function GroupsList(props: {
|
||||||
|
|
||||||
const { height } = useWindowSize()
|
const { height } = useWindowSize()
|
||||||
const [containerRef, setContainerRef] = useState<HTMLDivElement | null>(null)
|
const [containerRef, setContainerRef] = useState<HTMLDivElement | null>(null)
|
||||||
const remainingHeight =
|
const remainingHeight = (height ?? 0) - (containerRef?.offsetTop ?? 0)
|
||||||
(height ?? window.innerHeight) - (containerRef?.offsetTop ?? 0)
|
|
||||||
|
|
||||||
const notifIsForThisItem = useMemo(
|
const notifIsForThisItem = useMemo(
|
||||||
() => (itemHref: string) =>
|
() => (itemHref: string) =>
|
||||||
|
|
|
@ -2,15 +2,14 @@ import { BellIcon } from '@heroicons/react/outline'
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import { Row } from 'web/components/layout/row'
|
import { Row } from 'web/components/layout/row'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { usePrivateUser, useUser } from 'web/hooks/use-user'
|
import { usePrivateUser } from 'web/hooks/use-user'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import { useUnseenPreferredNotificationGroups } from 'web/hooks/use-notifications'
|
import { useUnseenPreferredNotificationGroups } from 'web/hooks/use-notifications'
|
||||||
import { NOTIFICATIONS_PER_PAGE } from 'web/pages/notifications'
|
import { NOTIFICATIONS_PER_PAGE } from 'web/pages/notifications'
|
||||||
import { PrivateUser } from 'common/user'
|
import { PrivateUser } from 'common/user'
|
||||||
|
|
||||||
export default function NotificationsIcon(props: { className?: string }) {
|
export default function NotificationsIcon(props: { className?: string }) {
|
||||||
const user = useUser()
|
const privateUser = usePrivateUser()
|
||||||
const privateUser = usePrivateUser(user?.id)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Row className={clsx('justify-center')}>
|
<Row className={clsx('justify-center')}>
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import { ReactNode } from 'react'
|
|
||||||
import { Answer } from 'common/answer'
|
import { Answer } from 'common/answer'
|
||||||
import { getProbability } from 'common/calculate'
|
import { getProbability } from 'common/calculate'
|
||||||
import { getValueFromBucket } from 'common/calculate-dpm'
|
import { getValueFromBucket } from 'common/calculate-dpm'
|
||||||
|
@ -11,7 +10,7 @@ import {
|
||||||
resolution,
|
resolution,
|
||||||
} from 'common/contract'
|
} from 'common/contract'
|
||||||
import { formatLargeNumber, formatPercent } from 'common/util/format'
|
import { formatLargeNumber, formatPercent } from 'common/util/format'
|
||||||
import { ClientRender } from './client-render'
|
import { Tooltip } from './tooltip'
|
||||||
|
|
||||||
export function OutcomeLabel(props: {
|
export function OutcomeLabel(props: {
|
||||||
contract: Contract
|
contract: Contract
|
||||||
|
@ -91,13 +90,13 @@ export function FreeResponseOutcomeLabel(props: {
|
||||||
const chosen = contract.answers?.find((answer) => answer.id === resolution)
|
const chosen = contract.answers?.find((answer) => answer.id === resolution)
|
||||||
if (!chosen) return <AnswerNumberLabel number={resolution} />
|
if (!chosen) return <AnswerNumberLabel number={resolution} />
|
||||||
return (
|
return (
|
||||||
<FreeResponseAnswerToolTip text={chosen.text}>
|
<Tooltip text={chosen.text}>
|
||||||
<AnswerLabel
|
<AnswerLabel
|
||||||
answer={chosen}
|
answer={chosen}
|
||||||
truncate={truncate}
|
truncate={truncate}
|
||||||
className={answerClassName}
|
className={answerClassName}
|
||||||
/>
|
/>
|
||||||
</FreeResponseAnswerToolTip>
|
</Tooltip>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,23 +173,3 @@ export function AnswerLabel(props: {
|
||||||
</span>
|
</span>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function FreeResponseAnswerToolTip(props: {
|
|
||||||
text: string
|
|
||||||
children?: ReactNode
|
|
||||||
}) {
|
|
||||||
const { text } = props
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<ClientRender>
|
|
||||||
<span
|
|
||||||
className="tooltip hidden cursor-default sm:inline-block"
|
|
||||||
data-tip={text}
|
|
||||||
>
|
|
||||||
{props.children}
|
|
||||||
</span>
|
|
||||||
</ClientRender>
|
|
||||||
<span className="whitespace-nowrap sm:hidden">{props.children}</span>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
|
@ -61,7 +61,8 @@ export const PortfolioValueGraph = memo(function PortfolioValueGraph(props: {
|
||||||
min: Math.min(...points.map((p) => p.y)),
|
min: Math.min(...points.map((p) => p.y)),
|
||||||
}}
|
}}
|
||||||
gridYValues={numYTickValues}
|
gridYValues={numYTickValues}
|
||||||
curve="monotoneX"
|
curve="stepAfter"
|
||||||
|
enablePoints={false}
|
||||||
colors={{ datum: 'color' }}
|
colors={{ datum: 'color' }}
|
||||||
axisBottom={{
|
axisBottom={{
|
||||||
tickValues: numXTickValues,
|
tickValues: numXTickValues,
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user