predikt/oracle/web/components/new-contract/contextual-editor-panel.tsx
caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
Working tree is now the canonical repo. Off-chain play-money + trustless on-chain
(FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution,
mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 23:29:50 +03:00

198 lines
6.5 KiB
TypeScript

import { JSONContent } from '@tiptap/core'
import clsx from 'clsx'
import { CreateableOutcomeType, PollType } from 'common/contract'
import { getAnte } from 'common/economy'
import { Group } from 'common/group'
import { Col } from 'web/components/layout/col'
import { Row } from 'web/components/layout/row'
import { ChoicesToggleGroup } from '../widgets/choices-toggle-group'
import { Input } from '../widgets/input'
import { BoostSection } from './boost-section'
import { CostSection } from './cost-section'
import { getAnteAnswerCount } from './utils/get-ante-answer-count'
const POLL_TYPE_OPTIONS: { value: PollType; label: string }[] = [
{ value: 'single', label: 'Single Vote' },
{ value: 'multi-select', label: 'Multi-Select' },
{ value: 'ranked-choice', label: 'Ranked Choice' },
]
export type FormState = {
question: string
outcomeType: CreateableOutcomeType | null
description?: JSONContent
// Plain-text resolution criteria surfaced as a first-class field. The backend
// has no dedicated column, so on submit this is folded into the description.
resolutionCriteria?: string
answers: string[]
closeDate?: string
closeHoursMinutes?: string
neverCloses: boolean
selectedGroups: Group[]
visibility: 'public' | 'unlisted'
liquidityTier: number
shouldAnswersSumToOne?: boolean
addAnswersMode?: 'DISABLED' | 'ONLY_CREATOR' | 'ANYONE'
probability?: number
min?: number
max?: number
minString?: string
maxString?: string
unit?: string
midpoints?: number[]
isAnnouncement?: boolean
isChangeLog?: boolean
includeSeeResults?: boolean // For POLLs - adds "See results" option
// Poll-specific options
pollType?: PollType // 'single' | 'multi-select' | 'ranked-choice'
maxSelections?: number // For multi-select polls
boostMarket?: boolean // Whether to also boost the market on creation
}
export type ValidationErrors = {
[key: string]: string
}
export function ContextualEditorPanel(props: {
formState: FormState
onUpdate: (field: string, value: any) => void
validationErrors: ValidationErrors
onGenerateAnswers?: () => void
isGeneratingAnswers?: boolean
balance?: number
submitState?: 'EDITING' | 'LOADING' | 'DONE'
// Mirrors the backend FREE_MARKET_USER_ID waiver; suppresses the
// CostSection "Insufficient balance" banner for eligible free markets.
isFreeMarket?: boolean
}) {
const { formState, onUpdate, balance, validationErrors, isFreeMarket } = props
const { outcomeType, answers, liquidityTier, addAnswersMode } = formState
const isPoll = outcomeType === 'POLL'
const hasBalanceError = !!validationErrors.balance
const numAnswersForCost = getAnteAnswerCount(
answers,
addAnswersMode,
formState.shouldAnswersSumToOne
)
const marketCost = outcomeType
? getAnte(
outcomeType as any,
numAnswersForCost > 0 ? numAnswersForCost : undefined,
liquidityTier
)
: 0
return (
<Col
className={clsx(
'gap-4 overflow-y-auto rounded-lg border-0 p-4 shadow-none outline-none ring-offset-0 focus-within:outline-none focus:outline-none focus:ring-offset-0 focus-visible:outline-none',
hasBalanceError
? 'ring-2 ring-red-500 focus-within:ring-2 focus-within:ring-red-500 dark:ring-red-600 dark:focus-within:ring-red-600'
: 'ring-1 ring-transparent'
)}
>
{isPoll ? (
<PollOptionsSection formState={formState} onUpdate={onUpdate} />
) : (
<>
{/* Liquidity & Cost */}
{balance !== undefined && (
<Col className="gap-3">
<span className="text-ink-700 text-sm font-semibold">
Liquidity
</span>
<CostSection
balance={balance}
outcomeType={outcomeType as any}
liquidityTier={liquidityTier}
setLiquidityTier={(tier) => onUpdate('liquidityTier', tier)}
numAnswers={
numAnswersForCost > 0 ? numAnswersForCost : undefined
}
isFreeMarket={isFreeMarket}
/>
</Col>
)}
</>
)}
<BoostSection
enabled={!!formState.boostMarket}
setEnabled={(value) => onUpdate('boostMarket', value)}
visibility={formState.visibility}
marketCost={marketCost}
balance={balance ?? 0}
/>
</Col>
)
}
function PollOptionsSection(props: {
formState: FormState
onUpdate: (field: string, value: any) => void
}) {
const { formState, onUpdate } = props
const { pollType = 'single', maxSelections, answers } = formState
return (
<Col className="gap-4">
<Col className="gap-2">
<span className="text-ink-700 text-sm font-semibold">About polls</span>
<p className="text-ink-600 text-sm">
{pollType === 'single' &&
'People can vote for one option, but they cannot bet.'}
{pollType === 'multi-select' &&
'People can vote for multiple options, but they cannot bet.'}
{pollType === 'ranked-choice' &&
'People rank the options in order of preference, but they cannot bet.'}
</p>
</Col>
<Col className="gap-2">
<span className="text-ink-700 text-sm font-semibold">Poll type</span>
<ChoicesToggleGroup
className="w-fit"
currentChoice={pollType}
choicesMap={Object.fromEntries(
POLL_TYPE_OPTIONS.map((o) => [o.label, o.value])
)}
setChoice={(value) => {
onUpdate('pollType', value)
// Disable "See results" for multi-select and ranked-choice
if (value !== 'single') {
onUpdate('includeSeeResults', false)
}
}}
/>
</Col>
{pollType === 'multi-select' && (
<Col className="gap-2">
<Row className="items-center gap-2">
<span className="text-ink-700 text-sm font-semibold">
Max votes
</span>
<span className="text-ink-500 text-xs">(optional)</span>
</Row>
<Input
type="number"
min={1}
max={answers.length || 100}
placeholder={`${answers.length || 'all'}`}
value={maxSelections ?? ''}
onChange={(e) => {
const val = e.target.value
onUpdate(
'maxSelections',
val === '' ? undefined : parseInt(val, 10)
)
}}
className=" w-32"
/>
</Col>
)}
</Col>
)
}