From aff6acc8f442396270fb82098febd2a7363cb57b Mon Sep 17 00:00:00 2001 From: James Grugett Date: Thu, 7 Apr 2022 16:15:51 -0500 Subject: [PATCH] Refactor contract components into contract directory --- .../{ => contract}/contract-card.tsx | 20 +-- .../contract/contract-description.tsx | 133 ++++++++++++++++++ .../{ => contract}/contract-info-dialog.tsx | 27 ++-- .../{ => contract}/contract-overview.tsx | 26 ++-- .../{ => contract}/contract-prob-graph.tsx | 10 +- .../{ => contract}/contracts-list.tsx | 15 +- web/components/feed-create.tsx | 2 +- web/components/feed/feed-items.tsx | 128 +---------------- web/components/user-page.tsx | 2 +- web/pages/[username]/[contractSlug].tsx | 4 +- web/pages/embed/[username]/[contractSlug].tsx | 4 +- web/pages/fold/[...slugs]/index.tsx | 2 +- web/pages/landing-page.tsx | 2 +- web/pages/markets.tsx | 5 +- web/pages/tag/[tag].tsx | 2 +- 15 files changed, 199 insertions(+), 183 deletions(-) rename web/components/{ => contract}/contract-card.tsx (95%) create mode 100644 web/components/contract/contract-description.tsx rename web/components/{ => contract}/contract-info-dialog.tsx (79%) rename web/components/{ => contract}/contract-overview.tsx (76%) rename web/components/{ => contract}/contract-prob-graph.tsx (91%) rename web/components/{ => contract}/contracts-list.tsx (97%) diff --git a/web/components/contract-card.tsx b/web/components/contract/contract-card.tsx similarity index 95% rename from web/components/contract-card.tsx rename to web/components/contract/contract-card.tsx index 80e70c41..ecd42d9d 100644 --- a/web/components/contract-card.tsx +++ b/web/components/contract/contract-card.tsx @@ -2,24 +2,24 @@ import clsx from 'clsx' import Link from 'next/link' import { ClockIcon, DatabaseIcon, PencilIcon } from '@heroicons/react/outline' import { TrendingUpIcon } from '@heroicons/react/solid' -import { Row } from '../components/layout/row' -import { formatMoney, formatPercent } from '../../common/util/format' -import { UserLink } from './user-page' +import { Row } from '../layout/row' +import { formatMoney, formatPercent } from '../../../common/util/format' +import { UserLink } from '../user-page' import { Contract, contractMetrics, contractPath, getBinaryProbPercent, updateContract, -} from '../lib/firebase/contracts' -import { Col } from './layout/col' +} from '../../lib/firebase/contracts' +import { Col } from '../layout/col' import dayjs from 'dayjs' -import { DateTimeTooltip } from './datetime-tooltip' -import { fromNow } from '../lib/util/time' -import { Avatar } from './avatar' -import { Spacer } from './layout/spacer' +import { DateTimeTooltip } from '../datetime-tooltip' +import { fromNow } from '../../lib/util/time' +import { Avatar } from '../avatar' +import { Spacer } from '../layout/spacer' import { useState } from 'react' -import { getProbability } from '../../common/calculate' +import { getProbability } from '../../../common/calculate' import { ContractInfoDialog } from './contract-info-dialog' export function ContractCard(props: { diff --git a/web/components/contract/contract-description.tsx b/web/components/contract/contract-description.tsx new file mode 100644 index 00000000..dea08c6a --- /dev/null +++ b/web/components/contract/contract-description.tsx @@ -0,0 +1,133 @@ +import clsx from 'clsx' +import dayjs from 'dayjs' +import { useState } from 'react' +import Textarea from 'react-expanding-textarea' + +import { Contract } from '../../../common/contract' +import { parseTags } from '../../../common/util/parse' +import { useAdmin } from '../../hooks/use-admin' +import { updateContract } from '../../lib/firebase/contracts' +import { Row } from '../layout/row' +import { Linkify } from '../linkify' + +export function ContractDescription(props: { + contract: Contract + isCreator: boolean + className?: string +}) { + const { contract, isCreator, className } = props + const descriptionTimestamp = () => `${dayjs().format('MMM D, h:mma')}: ` + const isAdmin = useAdmin() + + // Append the new description (after a newline) + async function saveDescription(newText: string) { + const newDescription = `${contract.description}\n\n${newText}`.trim() + const tags = parseTags( + `${newDescription} ${contract.tags.map((tag) => `#${tag}`).join(' ')}` + ) + const lowercaseTags = tags.map((tag) => tag.toLowerCase()) + await updateContract(contract.id, { + description: newDescription, + tags, + lowercaseTags, + }) + } + + if (!isCreator && !contract.description.trim()) return null + + return ( +
+ +
+ {isCreator && ( + + )} + {isAdmin && ( + updateContract(contract.id, { question })} + buttonText="ADMIN: Edit question" + /> + )} + {/* {isAdmin && ( + + updateContract(contract.id, { createdTime: Number(time) }) + } + buttonText="ADMIN: Edit createdTime" + /> + )} */} +
+ ) +} + +function EditContract(props: { + text: string + onSave: (newText: string) => void + buttonText: string +}) { + const [text, setText] = useState(props.text) + const [editing, setEditing] = useState(false) + const onSave = (newText: string) => { + setEditing(false) + setText(props.text) // Reset to original text + props.onSave(newText) + } + + return editing ? ( +
+