2022-07-05 23:26:58 +00:00
|
|
|
import { DuplicateIcon } from '@heroicons/react/outline'
|
|
|
|
import clsx from 'clsx'
|
|
|
|
import { Contract } from 'common/contract'
|
|
|
|
import { getMappedValue } from 'common/pseudo-numeric'
|
|
|
|
import { trackCallback } from 'web/lib/service/analytics'
|
2022-10-05 22:51:10 +00:00
|
|
|
import { buttonClass } from './button'
|
2022-07-05 23:26:58 +00:00
|
|
|
|
2022-10-05 22:51:10 +00:00
|
|
|
export function DuplicateContractButton(props: { contract: Contract }) {
|
|
|
|
const { contract } = props
|
2022-07-05 23:26:58 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<a
|
2022-10-05 22:51:10 +00:00
|
|
|
className={clsx(
|
|
|
|
buttonClass('2xs', 'override'),
|
|
|
|
'gap-1 border-2 border-violet-400 text-violet-400 hover:bg-violet-400 hover:text-white'
|
|
|
|
)}
|
2022-07-05 23:26:58 +00:00
|
|
|
href={duplicateContractHref(contract)}
|
|
|
|
onClick={trackCallback('duplicate market')}
|
|
|
|
target="_blank"
|
|
|
|
>
|
2022-10-05 22:51:10 +00:00
|
|
|
<DuplicateIcon className="h-4 w-4" aria-hidden="true" />
|
2022-07-05 23:26:58 +00:00
|
|
|
<div>Duplicate</div>
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass along the Uri to create a new contract
|
|
|
|
function duplicateContractHref(contract: Contract) {
|
2022-08-22 17:49:54 +00:00
|
|
|
const descriptionString = JSON.stringify(contract.description)
|
|
|
|
// Don't set a closeTime that's in the past
|
|
|
|
const closeTime =
|
|
|
|
(contract?.closeTime ?? 0) <= Date.now() ? 0 : contract.closeTime
|
2022-07-05 23:26:58 +00:00
|
|
|
const params = {
|
|
|
|
q: contract.question,
|
2022-08-22 17:49:54 +00:00
|
|
|
closeTime,
|
|
|
|
description: descriptionString,
|
2022-07-05 23:26:58 +00:00
|
|
|
outcomeType: contract.outcomeType,
|
2022-10-05 14:38:19 +00:00
|
|
|
visibility: contract.visibility,
|
2022-07-05 23:26:58 +00:00
|
|
|
} as Record<string, any>
|
|
|
|
|
|
|
|
if (contract.outcomeType === 'PSEUDO_NUMERIC') {
|
|
|
|
params.min = contract.min
|
|
|
|
params.max = contract.max
|
2022-08-22 17:49:54 +00:00
|
|
|
if (contract.isLogScale) {
|
|
|
|
// Conditional, because `?isLogScale=false` evaluates to `true`
|
|
|
|
params.isLogScale = true
|
|
|
|
}
|
2022-07-05 23:26:58 +00:00
|
|
|
params.initValue = getMappedValue(contract)(contract.initialProbability)
|
|
|
|
}
|
|
|
|
|
2022-08-22 17:49:54 +00:00
|
|
|
// TODO: Support multiple choice markets?
|
|
|
|
|
2022-07-28 02:47:25 +00:00
|
|
|
if (contract.groupLinks && contract.groupLinks.length > 0) {
|
|
|
|
params.groupId = contract.groupLinks[0].groupId
|
|
|
|
}
|
|
|
|
|
2022-07-05 23:26:58 +00:00
|
|
|
return (
|
|
|
|
`/create?` +
|
|
|
|
Object.entries(params)
|
|
|
|
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
|
|
|
|
.join('&')
|
|
|
|
)
|
|
|
|
}
|