import clsx from 'clsx' import { useState } from 'react' import { Col } from '../layout/col' import { Row } from '../layout/row' import { Title } from '../title' import { User } from 'common/user' import { ManalinkCard, ManalinkInfo } from 'web/components/manalink-card' import { createManalink } from 'web/lib/firebase/manalinks' import { Modal } from 'web/components/layout/modal' import Textarea from 'react-expanding-textarea' import dayjs from 'dayjs' import { Button } from '../button' import { getManalinkUrl } from 'web/pages/links' import { DuplicateIcon } from '@heroicons/react/outline' import { QRCode } from '../qr-code' export function CreateLinksButton(props: { user: User highlightedSlug: string setHighlightedSlug: (slug: string) => void }) { const { user, highlightedSlug, setHighlightedSlug } = props const [open, setOpen] = useState(false) return ( <> setOpen(newOpen)}> { const slug = await createManalink({ fromId: user.id, amount: newManalink.amount, expiresTime: newManalink.expiresTime, maxUses: newManalink.maxUses, message: newManalink.message, }) setHighlightedSlug(slug || '') }} /> setOpen(true)} className={clsx('whitespace-nowrap')} > Create a Manalink > ) } function CreateManalinkForm(props: { highlightedSlug: string user: User onCreate: (m: ManalinkInfo) => Promise }) { const { user, onCreate, highlightedSlug } = props const [isCreating, setIsCreating] = useState(false) const [finishedCreating, setFinishedCreating] = useState(false) const [copyPressed, setCopyPressed] = useState(false) setTimeout(() => setCopyPressed(false), 300) const defaultExpire = 'week' const [expiresIn, setExpiresIn] = useState(defaultExpire) const defaultMessage = 'from ' + user.name const [newManalink, setNewManalink] = useState({ expiresTime: dayjs().add(1, defaultExpire).valueOf(), amount: 100, maxUses: 1, uses: 0, message: defaultMessage, }) const EXPIRE_OPTIONS = { day: '1 Day', week: '1 Week', month: '1 Month', never: 'Never', } const expireOptions = Object.entries(EXPIRE_OPTIONS).map(([key, value]) => { return {value} }) function setExpireTime(timeDelta: string) { const expiresTime = timeDelta === 'never' ? null : dayjs().add(1, timeDelta).valueOf() setNewManalink((m) => { return { ...m, expiresTime, } }) } const url = getManalinkUrl(highlightedSlug) return ( <> {!finishedCreating && ( { e.preventDefault() setIsCreating(true) onCreate(newManalink).finally(() => setIsCreating(false)) setFinishedCreating(true) }} > Amount M$ setNewManalink((m) => { return { ...m, amount: parseInt(e.target.value) } }) } /> Uses setNewManalink((m) => { return { ...m, maxUses: parseInt(e.target.value) } }) } > Expires in { setExpiresIn(e.target.value) setExpireTime(e.target.value) }} > {expireOptions} Message setNewManalink((m) => { return { ...m, message: e.target.value } }) } /> Create )} {finishedCreating && ( <> {url} { navigator.clipboard.writeText(url) setCopyPressed(true) }} className="my-auto ml-2 h-5 w-5 cursor-pointer transition hover:opacity-50" /> > )} > ) }