2022-04-07 21:15:51 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
import dayjs from 'dayjs'
|
|
|
|
import { useState } from 'react'
|
|
|
|
import Textarea from 'react-expanding-textarea'
|
|
|
|
|
2022-07-18 05:22:44 +00:00
|
|
|
import { Contract, MAX_DESCRIPTION_LENGTH } from 'common/contract'
|
|
|
|
import { exhibitExts, parseTags } from 'common/util/parse'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { useAdmin } from 'web/hooks/use-admin'
|
|
|
|
import { updateContract } from 'web/lib/firebase/contracts'
|
2022-04-07 21:15:51 +00:00
|
|
|
import { Row } from '../layout/row'
|
Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
import { Content } from '../editor'
|
2022-07-18 05:22:44 +00:00
|
|
|
import { TextEditor, useTextEditor } from 'web/components/editor'
|
|
|
|
import { Button } from '../button'
|
|
|
|
import { Spacer } from '../layout/spacer'
|
|
|
|
import { Editor, Content as ContentType } from '@tiptap/react'
|
2022-08-11 21:32:02 +00:00
|
|
|
import { insertContent } from '../editor/utils'
|
2022-04-07 21:15:51 +00:00
|
|
|
|
|
|
|
export function ContractDescription(props: {
|
|
|
|
contract: Contract
|
|
|
|
isCreator: boolean
|
|
|
|
className?: string
|
|
|
|
}) {
|
|
|
|
const { contract, isCreator, className } = props
|
|
|
|
const isAdmin = useAdmin()
|
2022-07-18 05:22:44 +00:00
|
|
|
return (
|
|
|
|
<div className={clsx('mt-2 text-gray-700', className)}>
|
|
|
|
{isCreator || isAdmin ? (
|
2022-07-19 00:17:45 +00:00
|
|
|
<RichEditContract contract={contract} isAdmin={isAdmin && !isCreator} />
|
2022-07-18 05:22:44 +00:00
|
|
|
) : (
|
|
|
|
<Content content={contract.description} />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2022-04-07 21:15:51 +00:00
|
|
|
|
2022-07-18 05:22:44 +00:00
|
|
|
function editTimestamp() {
|
|
|
|
return `${dayjs().format('MMM D, h:mma')}: `
|
|
|
|
}
|
|
|
|
|
2022-07-19 00:17:45 +00:00
|
|
|
function RichEditContract(props: { contract: Contract; isAdmin?: boolean }) {
|
|
|
|
const { contract, isAdmin } = props
|
2022-07-18 05:22:44 +00:00
|
|
|
const [editing, setEditing] = useState(false)
|
|
|
|
const [editingQ, setEditingQ] = useState(false)
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
|
2022-07-18 05:22:44 +00:00
|
|
|
const { editor, upload } = useTextEditor({
|
|
|
|
max: MAX_DESCRIPTION_LENGTH,
|
|
|
|
defaultValue: contract.description,
|
|
|
|
disabled: isSubmitting,
|
|
|
|
})
|
|
|
|
|
|
|
|
async function saveDescription() {
|
|
|
|
if (!editor) return
|
Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
|
2022-04-07 21:15:51 +00:00
|
|
|
const tags = parseTags(
|
Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
`${editor.getText()} ${contract.tags.map((tag) => `#${tag}`).join(' ')}`
|
2022-04-07 21:15:51 +00:00
|
|
|
)
|
|
|
|
const lowercaseTags = tags.map((tag) => tag.toLowerCase())
|
2022-05-12 15:07:10 +00:00
|
|
|
|
2022-04-07 21:15:51 +00:00
|
|
|
await updateContract(contract.id, {
|
Rich content (#620)
* Add TipTap editor and renderer components
* Change market description editor to rich text
* Type description as JSON, fix string-based logic
- Delete make-predictions.tsx
- Delete feed logic that showed descriptions
* wip Fix API validation
* fix type error
* fix extension import (backend)
In firebase, typescript compiles imports into common js imports
like `const StarterKit = require("@tiptap/starter-kit")`
Even though StarterKit is exported from the cjs file, it gets imported
as undefined. But it magically works if we import *
If you're reading this in the future, consider replacing StarterKit with
the entire list of extensions.
* Stop load on fail create market, improve warning
* Refactor editor as hook / fix infinite submit bug
Move state of editor back up to parent
We have to do this later anyways to allow parent to edit
* Add images - display, paste + uploading
* add uploading state of image
* Fix placeholder, misc styling
min height, quote
* Fix appending to description
* code review fixes: rename, refactor, chop carets
* Add hint & upload button on new lines
- bump to Tailwind 3.1 for arbitrary variants
* clean up, run prettier
* rename FileButton to FileUploadButton
* add image extension as functions dependency
2022-07-13 18:58:22 +00:00
|
|
|
description: editor.getJSON(),
|
2022-04-07 21:15:51 +00:00
|
|
|
tags,
|
|
|
|
lowercaseTags,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-18 05:22:44 +00:00
|
|
|
return editing ? (
|
|
|
|
<>
|
|
|
|
<TextEditor editor={editor} upload={upload} />
|
|
|
|
<Spacer h={2} />
|
|
|
|
<Row className="gap-2">
|
|
|
|
<Button
|
|
|
|
onClick={async () => {
|
|
|
|
setIsSubmitting(true)
|
|
|
|
await saveDescription()
|
|
|
|
setEditing(false)
|
|
|
|
setIsSubmitting(false)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Save
|
|
|
|
</Button>
|
|
|
|
<Button color="gray" onClick={() => setEditing(false)}>
|
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
</Row>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<Content content={contract.description} />
|
|
|
|
<Spacer h={2} />
|
2022-07-19 00:17:45 +00:00
|
|
|
<Row className="items-center gap-2">
|
|
|
|
{isAdmin && 'Admin: '}
|
2022-07-18 05:22:44 +00:00
|
|
|
<Button
|
|
|
|
color="gray"
|
2022-07-19 00:17:45 +00:00
|
|
|
size="xs"
|
2022-07-18 05:22:44 +00:00
|
|
|
onClick={() => {
|
|
|
|
setEditing(true)
|
2022-08-11 21:32:02 +00:00
|
|
|
editor?.commands.focus('end')
|
|
|
|
insertContent(editor, `<p>${editTimestamp()}</p>`)
|
2022-07-18 05:22:44 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
Edit description
|
|
|
|
</Button>
|
2022-07-19 00:17:45 +00:00
|
|
|
<Button color="gray" size="xs" onClick={() => setEditingQ(true)}>
|
2022-07-18 05:22:44 +00:00
|
|
|
Edit question
|
|
|
|
</Button>
|
|
|
|
</Row>
|
|
|
|
<EditQuestion
|
|
|
|
contract={contract}
|
|
|
|
editing={editingQ}
|
|
|
|
setEditing={setEditingQ}
|
|
|
|
/>
|
|
|
|
</>
|
2022-05-12 23:28:21 +00:00
|
|
|
)
|
2022-07-18 05:22:44 +00:00
|
|
|
}
|
2022-05-12 15:07:10 +00:00
|
|
|
|
2022-07-18 05:22:44 +00:00
|
|
|
function EditQuestion(props: {
|
|
|
|
contract: Contract
|
|
|
|
editing: boolean
|
|
|
|
setEditing: (editing: boolean) => void
|
|
|
|
}) {
|
|
|
|
const { contract, editing, setEditing } = props
|
|
|
|
const [text, setText] = useState(contract.question)
|
2022-05-12 15:07:10 +00:00
|
|
|
|
2022-07-18 05:22:44 +00:00
|
|
|
function questionChanged(oldQ: string, newQ: string) {
|
|
|
|
return `<p>${editTimestamp()}<s>${oldQ}</s> → ${newQ}</p>`
|
|
|
|
}
|
2022-05-12 15:07:10 +00:00
|
|
|
|
2022-07-18 05:22:44 +00:00
|
|
|
function joinContent(oldContent: ContentType, newContent: string) {
|
|
|
|
const editor = new Editor({ content: oldContent, extensions: exhibitExts })
|
2022-08-29 06:23:40 +00:00
|
|
|
editor.commands.focus('end')
|
2022-08-11 21:32:02 +00:00
|
|
|
insertContent(editor, newContent)
|
2022-07-18 05:22:44 +00:00
|
|
|
return editor.getJSON()
|
|
|
|
}
|
2022-04-07 21:15:51 +00:00
|
|
|
|
2022-07-18 05:22:44 +00:00
|
|
|
const onSave = async (newText: string) => {
|
2022-04-07 21:15:51 +00:00
|
|
|
setEditing(false)
|
2022-07-18 05:22:44 +00:00
|
|
|
await updateContract(contract.id, {
|
|
|
|
question: newText,
|
|
|
|
description: joinContent(
|
|
|
|
contract.description,
|
|
|
|
questionChanged(contract.question, newText)
|
|
|
|
),
|
|
|
|
})
|
2022-04-07 21:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return editing ? (
|
|
|
|
<div className="mt-4">
|
|
|
|
<Textarea
|
|
|
|
className="textarea textarea-bordered mb-1 h-24 w-full resize-none"
|
2022-07-18 05:22:44 +00:00
|
|
|
rows={2}
|
2022-04-07 21:15:51 +00:00
|
|
|
value={text}
|
|
|
|
onChange={(e) => setText(e.target.value || '')}
|
|
|
|
autoFocus
|
|
|
|
onFocus={(e) =>
|
|
|
|
// Focus starts at end of text.
|
|
|
|
e.target.setSelectionRange(text.length, text.length)
|
|
|
|
}
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
|
|
|
|
onSave(text)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Row className="gap-2">
|
2022-07-18 05:22:44 +00:00
|
|
|
<Button onClick={() => onSave(text)}>Save</Button>
|
|
|
|
<Button color="gray" onClick={() => setEditing(false)}>
|
2022-04-07 21:15:51 +00:00
|
|
|
Cancel
|
2022-07-18 05:22:44 +00:00
|
|
|
</Button>
|
2022-04-07 21:15:51 +00:00
|
|
|
</Row>
|
|
|
|
</div>
|
2022-07-18 05:22:44 +00:00
|
|
|
) : null
|
2022-04-07 21:15:51 +00:00
|
|
|
}
|