2022-08-15 05:03:05 +00:00
|
|
|
import React from 'react'
|
2022-03-21 20:23:21 +00:00
|
|
|
import { CodeIcon } from '@heroicons/react/outline'
|
2022-08-15 05:03:05 +00:00
|
|
|
import toast from 'react-hot-toast'
|
2022-06-15 21:34:34 +00:00
|
|
|
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Contract } from 'common/contract'
|
|
|
|
import { contractPath } from 'web/lib/firebase/contracts'
|
|
|
|
import { DOMAIN } from 'common/envs/constants'
|
|
|
|
import { copyToClipboard } from 'web/lib/util/copy'
|
2022-06-15 21:34:34 +00:00
|
|
|
import { track } from 'web/lib/service/analytics'
|
2022-10-05 22:51:10 +00:00
|
|
|
import { Button } from './button'
|
2022-03-21 20:23:21 +00:00
|
|
|
|
2022-09-01 16:47:45 +00:00
|
|
|
export function embedContractCode(contract: Contract) {
|
2022-04-07 20:52:54 +00:00
|
|
|
const title = contract.question
|
|
|
|
const src = `https://${DOMAIN}/embed${contractPath(contract)}`
|
2022-09-01 16:47:45 +00:00
|
|
|
return `<iframe src="${src}" title="${title}" frameborder="0"></iframe>`
|
|
|
|
}
|
2022-03-21 20:23:21 +00:00
|
|
|
|
2022-10-05 22:51:10 +00:00
|
|
|
// TODO: move this function elsewhere
|
2022-09-01 16:47:45 +00:00
|
|
|
export function embedContractGridCode(contracts: Contract[]) {
|
|
|
|
const height = (contracts.length - (contracts.length % 2)) * 100 + 'px'
|
2022-09-01 16:54:46 +00:00
|
|
|
const src = `https://${DOMAIN}/embed/grid/${contracts
|
2022-09-01 16:47:45 +00:00
|
|
|
.map((c) => c.slug)
|
|
|
|
.join('/')}`
|
|
|
|
return `<iframe height="${height}" src="${src}" title="Grid of contracts" frameborder="0"></iframe>`
|
2022-04-07 20:52:54 +00:00
|
|
|
}
|
2022-03-21 20:23:21 +00:00
|
|
|
|
2022-08-15 05:03:05 +00:00
|
|
|
export function ShareEmbedButton(props: { contract: Contract }) {
|
|
|
|
const { contract } = props
|
|
|
|
|
2022-10-05 22:51:10 +00:00
|
|
|
const codeIcon = <CodeIcon className="h-4 w-4" aria-hidden />
|
2022-03-21 20:23:21 +00:00
|
|
|
|
|
|
|
return (
|
2022-10-05 22:51:10 +00:00
|
|
|
<Button
|
|
|
|
size="2xs"
|
|
|
|
color="gray-outline"
|
|
|
|
className="gap-1"
|
|
|
|
onClick={() => {
|
2022-09-01 16:47:45 +00:00
|
|
|
copyToClipboard(embedContractCode(contract))
|
2022-10-05 22:51:10 +00:00
|
|
|
toast.success('Embed code copied!', { icon: codeIcon })
|
2022-06-15 21:34:34 +00:00
|
|
|
track('copy embed code')
|
|
|
|
}}
|
2022-03-21 21:44:11 +00:00
|
|
|
>
|
2022-10-05 22:51:10 +00:00
|
|
|
{codeIcon}
|
|
|
|
Embed
|
|
|
|
</Button>
|
2022-03-21 20:23:21 +00:00
|
|
|
)
|
|
|
|
}
|