2023-12-15 16:48:49 +01:00
|
|
|
import { ChangeEvent, Dispatch, SetStateAction, useCallback, useEffect, useRef, useState } from 'react'
|
2023-11-21 16:56:26 +01:00
|
|
|
import { FormattedMessage, useIntl } from 'react-intl'
|
2023-12-29 04:37:04 +01:00
|
|
|
import {
|
|
|
|
FaEnvelope,
|
|
|
|
FaFaceLaughBeam,
|
|
|
|
FaGlobe,
|
|
|
|
FaListCheck,
|
|
|
|
FaLock,
|
|
|
|
FaLockOpen,
|
|
|
|
FaPaperPlane,
|
|
|
|
FaPaperclip,
|
|
|
|
FaPencil,
|
|
|
|
FaXmark
|
|
|
|
} from 'react-icons/fa6'
|
2023-11-23 06:07:48 +01:00
|
|
|
import { Entity, MegalodonInterface } from 'megalodon'
|
|
|
|
import { useToast } from '@/utils/toast'
|
2023-12-10 15:55:41 +01:00
|
|
|
import Picker from '@emoji-mart/react'
|
|
|
|
import { data } from '@/utils/emojiData'
|
2023-12-29 04:37:04 +01:00
|
|
|
import EditMedia from './EditMedia'
|
2024-01-09 13:30:05 +01:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Checkbox,
|
|
|
|
IconButton,
|
|
|
|
Input,
|
|
|
|
List,
|
|
|
|
ListItem,
|
|
|
|
Option,
|
|
|
|
Popover,
|
|
|
|
PopoverContent,
|
|
|
|
PopoverHandler,
|
|
|
|
Radio,
|
|
|
|
Select,
|
|
|
|
Switch,
|
|
|
|
Textarea
|
|
|
|
} from '@material-tailwind/react'
|
2023-11-21 16:56:26 +01:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
client: MegalodonInterface
|
2023-11-28 16:37:21 +01:00
|
|
|
in_reply_to?: Entity.Status
|
2023-11-23 06:07:48 +01:00
|
|
|
}
|
2023-11-21 16:56:26 +01:00
|
|
|
|
2023-11-30 16:38:55 +01:00
|
|
|
type Poll = {
|
|
|
|
options: Array<string>
|
|
|
|
expires_in: number
|
|
|
|
multiple: boolean
|
|
|
|
}
|
|
|
|
|
2023-11-21 16:56:26 +01:00
|
|
|
export default function Compose(props: Props) {
|
|
|
|
const [body, setBody] = useState('')
|
|
|
|
const [visibility, setVisibility] = useState<'public' | 'unlisted' | 'private' | 'direct'>('public')
|
|
|
|
const [cw, setCW] = useState(false)
|
|
|
|
const [spoiler, setSpoiler] = useState('')
|
2023-11-23 06:07:48 +01:00
|
|
|
const [attachments, setAttachments] = useState<Array<Entity.Attachment | Entity.AsyncAttachment>>([])
|
2023-11-30 16:38:55 +01:00
|
|
|
const [poll, setPoll] = useState<Poll | null>(null)
|
2023-11-23 06:07:48 +01:00
|
|
|
const [loading, setLoading] = useState(false)
|
2023-12-29 04:37:04 +01:00
|
|
|
const [editMedia, setEditMedia] = useState<Entity.Attachment>()
|
2024-01-02 04:22:40 +01:00
|
|
|
const [maxCharacters, setMaxCharacters] = useState<number | null>(null)
|
|
|
|
const [remaining, setRemaining] = useState<number | null>(null)
|
2024-01-09 13:30:05 +01:00
|
|
|
const [popoverVisibility, setPopoverVisibility] = useState(false)
|
|
|
|
const [popoverEmoji, setPopoverEmoji] = useState(false)
|
2023-11-23 06:07:48 +01:00
|
|
|
|
2023-11-21 16:56:26 +01:00
|
|
|
const { formatMessage } = useIntl()
|
2023-11-23 06:07:48 +01:00
|
|
|
const uploaderRef = useRef(null)
|
|
|
|
const showToast = useToast()
|
2024-01-09 13:30:05 +01:00
|
|
|
const textareaRef = useRef<HTMLDivElement>(null)
|
2023-11-21 16:56:26 +01:00
|
|
|
|
2024-01-02 04:22:40 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (!props.client) return
|
|
|
|
const f = async () => {
|
|
|
|
const instance = await props.client.getInstance()
|
|
|
|
if (instance.data.configuration.statuses.max_characters) {
|
|
|
|
setMaxCharacters(instance.data.configuration.statuses.max_characters)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f()
|
|
|
|
}, [props.client])
|
|
|
|
|
2023-11-21 16:56:26 +01:00
|
|
|
useEffect(() => {
|
2023-11-23 06:07:48 +01:00
|
|
|
if (!cw) {
|
2023-11-21 16:56:26 +01:00
|
|
|
setSpoiler('')
|
|
|
|
}
|
|
|
|
}, [cw])
|
|
|
|
|
2023-11-28 16:37:21 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (props.in_reply_to) {
|
|
|
|
const f = async () => {
|
|
|
|
const myself = await props.client.verifyAccountCredentials()
|
|
|
|
const mentionAccounts = [props.in_reply_to.account.acct, ...props.in_reply_to.mentions.map(a => a.acct)]
|
|
|
|
.filter((a, i, self) => self.indexOf(a) === i)
|
|
|
|
.filter(a => a !== myself.data.username)
|
|
|
|
setBody(`${mentionAccounts.map(m => `@${m}`).join(' ')} `)
|
|
|
|
setVisibility(props.in_reply_to.visibility)
|
|
|
|
}
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
}, [props.in_reply_to])
|
|
|
|
|
2024-01-02 04:22:40 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (maxCharacters) {
|
|
|
|
setRemaining(maxCharacters - body.length - spoiler.length)
|
|
|
|
}
|
|
|
|
}, [maxCharacters, body, spoiler])
|
|
|
|
|
2023-11-21 16:56:26 +01:00
|
|
|
const post = async () => {
|
|
|
|
if (body.length === 0) return
|
|
|
|
let options = { visibility: visibility }
|
|
|
|
if (cw) {
|
|
|
|
options = Object.assign({}, options, {
|
|
|
|
spoiler_text: spoiler
|
|
|
|
})
|
|
|
|
}
|
2023-11-23 06:07:48 +01:00
|
|
|
if (attachments.length > 0) {
|
|
|
|
options = Object.assign({}, options, {
|
|
|
|
media_ids: attachments.map(m => m.id)
|
|
|
|
})
|
|
|
|
}
|
2023-11-23 07:06:46 +01:00
|
|
|
const sensitive = document.getElementById('sensitive') as HTMLInputElement
|
2023-11-25 06:50:46 +01:00
|
|
|
if (sensitive && sensitive.checked) {
|
2023-11-23 07:06:46 +01:00
|
|
|
options = Object.assign({}, options, {
|
|
|
|
sensitive: sensitive.checked
|
|
|
|
})
|
|
|
|
}
|
2023-11-30 16:38:55 +01:00
|
|
|
if (poll && poll.options.length > 0) {
|
|
|
|
options = Object.assign({}, options, {
|
|
|
|
poll: poll
|
|
|
|
})
|
|
|
|
}
|
2023-11-23 06:07:48 +01:00
|
|
|
setLoading(true)
|
|
|
|
try {
|
|
|
|
await props.client.postStatus(body, options)
|
|
|
|
reset()
|
2024-01-02 04:22:40 +01:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
showToast({ text: formatMessage({ id: 'alert.compose.post_failed' }), type: 'failure' })
|
2023-11-23 06:07:48 +01:00
|
|
|
} finally {
|
|
|
|
setLoading(false)
|
|
|
|
}
|
2023-11-21 16:56:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const reset = () => {
|
|
|
|
setBody('')
|
|
|
|
setSpoiler('')
|
|
|
|
setCW(false)
|
2023-11-23 06:07:48 +01:00
|
|
|
setAttachments([])
|
2023-11-30 16:38:55 +01:00
|
|
|
setPoll(null)
|
2024-01-02 04:22:40 +01:00
|
|
|
setMaxCharacters(null)
|
2023-11-23 06:07:48 +01:00
|
|
|
}
|
|
|
|
|
2023-12-15 16:48:49 +01:00
|
|
|
const handleKeyPress = useCallback(
|
|
|
|
(event: KeyboardEvent) => {
|
|
|
|
if (event.ctrlKey === true && event.key === 'Enter') {
|
|
|
|
post()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[post]
|
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
textareaRef.current?.addEventListener('keydown', handleKeyPress)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
textareaRef.current?.removeEventListener('keydown', handleKeyPress)
|
|
|
|
}
|
|
|
|
}, [handleKeyPress])
|
|
|
|
|
2023-11-23 06:07:48 +01:00
|
|
|
const selectFile = () => {
|
|
|
|
if (uploaderRef.current) {
|
|
|
|
uploaderRef.current.click()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fileChanged = async (event: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
const file = event.target.files?.item(0)
|
|
|
|
if (file === null || file === undefined) {
|
|
|
|
return
|
|
|
|
}
|
2024-01-02 04:22:40 +01:00
|
|
|
if (attachments.length >= 4) {
|
|
|
|
showToast({ text: formatMessage({ id: 'alert.validation.attachment_length' }, { limit: 4 }), type: 'failure' })
|
|
|
|
return
|
|
|
|
}
|
2023-11-23 06:07:48 +01:00
|
|
|
if (!file.type.includes('image') && !file.type.includes('video')) {
|
|
|
|
showToast({ text: formatMessage({ id: 'alert.validation.attachment_type' }), type: 'failure' })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setLoading(true)
|
|
|
|
try {
|
|
|
|
const res = await props.client.uploadMedia(file)
|
|
|
|
setAttachments(current => [...current, res.data])
|
|
|
|
} catch {
|
|
|
|
showToast({ text: formatMessage({ id: 'alert.upload_error' }), type: 'failure' })
|
|
|
|
} finally {
|
|
|
|
setLoading(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const removeFile = (index: number) => {
|
|
|
|
setAttachments(current => current.filter((_, i) => i !== index))
|
2023-11-21 16:56:26 +01:00
|
|
|
}
|
|
|
|
|
2023-12-29 04:37:04 +01:00
|
|
|
const openDescription = (index: number) => {
|
|
|
|
setEditMedia(attachments[index])
|
|
|
|
}
|
|
|
|
|
|
|
|
const closeDescription = () => {
|
|
|
|
setEditMedia(undefined)
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:38:55 +01:00
|
|
|
const togglePoll = () => {
|
|
|
|
if (poll) {
|
|
|
|
setPoll(null)
|
|
|
|
} else {
|
|
|
|
setPoll(defaultPoll())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-10 15:55:41 +01:00
|
|
|
const onEmojiSelect = emoji => {
|
|
|
|
const textarea = document.getElementById('body') as HTMLTextAreaElement
|
|
|
|
const cursor = textarea.selectionStart
|
|
|
|
if (emoji.native) {
|
|
|
|
setBody(current => `${current.slice(0, cursor)}${emoji.native} ${current.slice(cursor)}`)
|
|
|
|
} else if (emoji.shortcodes) {
|
|
|
|
setBody(current => `${current.slice(0, cursor)}${emoji.shortcodes} ${current.slice(cursor)}`)
|
|
|
|
}
|
2024-01-09 13:30:05 +01:00
|
|
|
setPopoverEmoji(false)
|
2023-12-10 15:55:41 +01:00
|
|
|
}
|
|
|
|
|
2023-11-21 16:56:26 +01:00
|
|
|
return (
|
2023-11-23 06:07:48 +01:00
|
|
|
<div className="px-4 pb-4">
|
|
|
|
<form id="form">
|
|
|
|
{cw && (
|
2024-01-09 13:30:05 +01:00
|
|
|
<Input
|
2023-11-23 06:07:48 +01:00
|
|
|
id="spoiler"
|
|
|
|
type="text"
|
2024-01-09 13:30:05 +01:00
|
|
|
color="blue"
|
|
|
|
containerProps={{ className: 'mb-2' }}
|
2023-11-23 06:07:48 +01:00
|
|
|
value={spoiler}
|
|
|
|
onChange={ev => setSpoiler(ev.target.value)}
|
|
|
|
placeholder={formatMessage({ id: 'compose.spoiler.placeholder' })}
|
2023-11-21 16:56:26 +01:00
|
|
|
/>
|
2023-11-23 06:07:48 +01:00
|
|
|
)}
|
2023-12-10 15:55:41 +01:00
|
|
|
<div className="relative">
|
|
|
|
<Textarea
|
|
|
|
id="body"
|
2024-01-09 13:30:05 +01:00
|
|
|
color="blue"
|
2023-12-10 15:55:41 +01:00
|
|
|
className="resize-none focus:ring-0"
|
|
|
|
placeholder={formatMessage({ id: 'compose.placeholder' })}
|
|
|
|
rows={3}
|
|
|
|
value={body}
|
|
|
|
onChange={ev => setBody(ev.target.value)}
|
2023-12-15 16:48:49 +01:00
|
|
|
ref={textareaRef}
|
2023-12-10 15:55:41 +01:00
|
|
|
/>
|
2024-01-09 13:30:05 +01:00
|
|
|
<Popover open={popoverEmoji} handler={setPopoverEmoji}>
|
|
|
|
<PopoverHandler>
|
|
|
|
<span className="absolute top-1 right-1 text-gray-600 cursor-pointer">
|
|
|
|
<FaFaceLaughBeam />
|
|
|
|
</span>
|
|
|
|
</PopoverHandler>
|
|
|
|
<PopoverContent>
|
2023-12-11 16:00:44 +01:00
|
|
|
<Picker data={data} onEmojiSelect={onEmojiSelect} previewPosition="none" set="native" perLine="7" theme="light" />
|
2024-01-09 13:30:05 +01:00
|
|
|
</PopoverContent>
|
|
|
|
</Popover>
|
2023-12-10 15:55:41 +01:00
|
|
|
</div>
|
2023-11-23 06:07:48 +01:00
|
|
|
</form>
|
2023-11-30 16:38:55 +01:00
|
|
|
{poll && <PollForm poll={poll} setPoll={setPoll} />}
|
2023-11-23 06:07:48 +01:00
|
|
|
<div className="attachments flex gap-2">
|
|
|
|
{attachments.map((f, index) => (
|
|
|
|
<div className="py-2 relative" key={index}>
|
|
|
|
<button className="absolute bg-gray-600 rounded" onClick={() => removeFile(index)}>
|
|
|
|
<FaXmark className="text-gray-200" />
|
|
|
|
</button>
|
2023-12-29 04:37:04 +01:00
|
|
|
<button className="absolute right-0 bg-gray-600 rounded" onClick={() => openDescription(index)}>
|
|
|
|
<FaPencil className="text-gray-200" />
|
|
|
|
</button>
|
2023-11-23 06:07:48 +01:00
|
|
|
<img src={f.preview_url} style={{ width: '80px', height: '80px' }} className="rounded-md" />
|
2023-11-21 16:56:26 +01:00
|
|
|
</div>
|
2023-11-23 06:07:48 +01:00
|
|
|
))}
|
|
|
|
</div>
|
2023-11-23 07:06:46 +01:00
|
|
|
|
|
|
|
{attachments.length > 0 && (
|
|
|
|
<div>
|
2024-01-09 13:30:05 +01:00
|
|
|
<Checkbox id="sensitive" label={formatMessage({ id: 'compose.nsfw' })} />
|
2023-11-23 07:06:46 +01:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2023-11-23 06:07:48 +01:00
|
|
|
<div className="w-full flex justify-between mt-1 items-center h-5">
|
|
|
|
<div className="ml-1 flex gap-3">
|
|
|
|
<input type="file" id="file" className="hidden" ref={uploaderRef} onChange={fileChanged} />
|
2024-01-09 13:30:05 +01:00
|
|
|
<IconButton variant="text" size="sm" onClick={selectFile} className="text-gray-400 hover:text-gray-600 text-base">
|
|
|
|
<FaPaperclip />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton variant="text" size="sm" onClick={togglePoll} className="text-gray-400 hover:text-gray-600 text-base">
|
|
|
|
<FaListCheck />
|
|
|
|
</IconButton>
|
|
|
|
<Popover open={popoverVisibility} handler={setPopoverVisibility}>
|
|
|
|
<PopoverHandler>{visibilityIcon(visibility)}</PopoverHandler>
|
|
|
|
<PopoverContent>
|
|
|
|
<List>
|
|
|
|
<ListItem
|
|
|
|
onClick={() => {
|
|
|
|
setVisibility('public')
|
|
|
|
setPopoverVisibility(false)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<FormattedMessage id="compose.visibility.public" />
|
|
|
|
</ListItem>
|
|
|
|
<ListItem
|
|
|
|
onClick={() => {
|
|
|
|
setVisibility('unlisted')
|
|
|
|
setPopoverVisibility(false)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<FormattedMessage id="compose.visibility.unlisted" />
|
|
|
|
</ListItem>
|
|
|
|
<ListItem
|
|
|
|
onClick={() => {
|
|
|
|
setVisibility('private')
|
|
|
|
setPopoverVisibility(false)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<FormattedMessage id="compose.visibility.private" />
|
|
|
|
</ListItem>
|
|
|
|
<ListItem
|
|
|
|
onClick={() => {
|
|
|
|
setVisibility('direct')
|
|
|
|
setPopoverVisibility(false)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<FormattedMessage id="compose.visibility.direct" />
|
|
|
|
</ListItem>
|
|
|
|
</List>
|
|
|
|
</PopoverContent>
|
|
|
|
</Popover>
|
2023-11-23 06:07:48 +01:00
|
|
|
|
|
|
|
{cw ? (
|
2024-01-09 13:30:05 +01:00
|
|
|
<IconButton
|
|
|
|
variant="text"
|
|
|
|
size="sm"
|
|
|
|
className="text-blue-400 hover:text-blue-600 leading-4 text-base"
|
|
|
|
onClick={() => setCW(false)}
|
|
|
|
>
|
2023-11-23 06:07:48 +01:00
|
|
|
CW
|
2024-01-09 13:30:05 +01:00
|
|
|
</IconButton>
|
2023-11-23 06:07:48 +01:00
|
|
|
) : (
|
2024-01-09 13:30:05 +01:00
|
|
|
<IconButton
|
|
|
|
variant="text"
|
|
|
|
size="sm"
|
|
|
|
className="text-gray-400 hover:text-gray-600 leading-4 text-base"
|
|
|
|
onClick={() => setCW(true)}
|
|
|
|
>
|
2023-11-23 06:07:48 +01:00
|
|
|
CW
|
2024-01-09 13:30:05 +01:00
|
|
|
</IconButton>
|
2023-11-23 06:07:48 +01:00
|
|
|
)}
|
|
|
|
</div>
|
2024-01-09 13:30:05 +01:00
|
|
|
<div className="mr-1 flex items-center gap-2">
|
2024-01-02 04:22:40 +01:00
|
|
|
<span className="text-gray-400">{remaining}</span>
|
2024-01-09 13:30:05 +01:00
|
|
|
<IconButton disabled={loading} onClick={post} variant="text" size="sm">
|
|
|
|
<FaPaperPlane className="text-base text-gray-600" />
|
|
|
|
</IconButton>
|
2023-11-21 16:56:26 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-12-29 04:37:04 +01:00
|
|
|
<EditMedia media={editMedia} close={closeDescription} client={props.client} />
|
2023-11-21 16:56:26 +01:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const visibilityIcon = (visibility: 'public' | 'unlisted' | 'private' | 'direct') => {
|
|
|
|
switch (visibility) {
|
|
|
|
case 'public':
|
|
|
|
return (
|
2024-01-09 13:30:05 +01:00
|
|
|
<IconButton variant="text" size="sm" className="text-gray-400 hover:text-gray-600 text-base">
|
|
|
|
<FaGlobe />
|
|
|
|
</IconButton>
|
2023-11-21 16:56:26 +01:00
|
|
|
)
|
|
|
|
case 'unlisted':
|
|
|
|
return (
|
2024-01-09 13:30:05 +01:00
|
|
|
<IconButton variant="text" size="sm" className="text-gray-400 hover:text-gray-600 text-base">
|
|
|
|
<FaLockOpen />
|
|
|
|
</IconButton>
|
2023-11-21 16:56:26 +01:00
|
|
|
)
|
|
|
|
case 'private':
|
|
|
|
return (
|
2024-01-09 13:30:05 +01:00
|
|
|
<IconButton variant="text" size="sm" className="text-gray-400 hover:text-gray-600 text-base">
|
|
|
|
<FaLock />
|
|
|
|
</IconButton>
|
2023-11-21 16:56:26 +01:00
|
|
|
)
|
|
|
|
case 'direct':
|
|
|
|
return (
|
2024-01-09 13:30:05 +01:00
|
|
|
<IconButton variant="text" size="sm" className="text-gray-400 hover:text-gray-600 text-base">
|
|
|
|
<FaEnvelope />
|
|
|
|
</IconButton>
|
2023-11-21 16:56:26 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-11-30 16:38:55 +01:00
|
|
|
|
|
|
|
const defaultPoll = () => ({
|
|
|
|
options: ['', ''],
|
|
|
|
expires_in: 86400,
|
|
|
|
multiple: false
|
|
|
|
})
|
|
|
|
|
|
|
|
type PollProps = {
|
|
|
|
poll: Poll
|
|
|
|
setPoll: Dispatch<SetStateAction<Poll>>
|
|
|
|
}
|
|
|
|
|
|
|
|
const PollForm = (props: PollProps) => {
|
|
|
|
const { formatMessage } = useIntl()
|
|
|
|
|
|
|
|
const expiresList = [
|
|
|
|
{ label: formatMessage({ id: 'compose.poll.5min' }), value: 300 },
|
|
|
|
{ label: formatMessage({ id: 'compose.poll.30min' }), value: 1800 },
|
|
|
|
{ label: formatMessage({ id: 'compose.poll.1h' }), value: 3600 },
|
|
|
|
{ label: formatMessage({ id: 'compose.poll.6h' }), value: 21600 },
|
|
|
|
{ label: formatMessage({ id: 'compose.poll.1d' }), value: 86400 },
|
|
|
|
{ label: formatMessage({ id: 'compose.poll.3d' }), value: 259200 },
|
|
|
|
{ label: formatMessage({ id: 'compose.poll.7d' }), value: 604800 }
|
|
|
|
]
|
|
|
|
|
|
|
|
const addOption = () => {
|
|
|
|
props.setPoll(current =>
|
|
|
|
Object.assign({}, current, {
|
|
|
|
options: [...current.options, '']
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const removeOption = (index: number) => {
|
|
|
|
props.setPoll(current =>
|
|
|
|
Object.assign({}, current, {
|
|
|
|
options: current.options.filter((_, i) => i !== index)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateOption = (index: number, value: string) => {
|
|
|
|
props.setPoll(current =>
|
|
|
|
Object.assign({}, current, {
|
|
|
|
options: current.options.map((original, i) => (i === index ? value : original))
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-09 13:30:05 +01:00
|
|
|
const changeMultiple = (ev: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
console.log(ev)
|
2023-11-30 16:38:55 +01:00
|
|
|
props.setPoll(current =>
|
|
|
|
Object.assign({}, current, {
|
2024-01-09 13:30:05 +01:00
|
|
|
multiple: ev.target.checked
|
2023-11-30 16:38:55 +01:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const changeExpire = (value: number) => {
|
|
|
|
props.setPoll(current =>
|
|
|
|
Object.assign({}, current, {
|
|
|
|
expires_in: value
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="pt-1">
|
|
|
|
{props.poll.options.map((option, index) => (
|
|
|
|
<div className="flex items-center gap-3 py-1" key={index}>
|
2024-01-09 13:30:05 +01:00
|
|
|
{props.poll.multiple ? (
|
|
|
|
<Checkbox disabled containerProps={{ className: 'p-1' }} />
|
|
|
|
) : (
|
|
|
|
<Radio disabled containerProps={{ className: 'p-1' }} />
|
|
|
|
)}
|
|
|
|
<Input
|
|
|
|
type="text"
|
|
|
|
color="blue"
|
|
|
|
value={option}
|
|
|
|
onChange={ev => updateOption(index, ev.target.value)}
|
|
|
|
containerProps={{ className: 'h-8' }}
|
|
|
|
/>
|
2023-11-30 16:38:55 +01:00
|
|
|
<FaXmark className="text-gray-400 cursor-pointer" onClick={() => removeOption(index)} />
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
<div className="flex gap-3 pt-2">
|
2024-01-09 13:30:05 +01:00
|
|
|
<Button onClick={addOption} size="sm" color="indigo" variant="outlined">
|
2023-11-30 16:38:55 +01:00
|
|
|
<FormattedMessage id="compose.poll.add" />
|
|
|
|
</Button>
|
2024-01-09 13:30:05 +01:00
|
|
|
<Select
|
|
|
|
id="expires"
|
|
|
|
color="blue"
|
|
|
|
value={`${props.poll.expires_in}`}
|
|
|
|
onChange={e => changeExpire(parseInt(e))}
|
|
|
|
containerProps={{ className: 'h-8' }}
|
|
|
|
>
|
2023-11-30 16:38:55 +01:00
|
|
|
{expiresList.map((expire, index) => (
|
2024-01-09 13:30:05 +01:00
|
|
|
<Option value={`${expire.value}`} key={index}>
|
2023-11-30 16:38:55 +01:00
|
|
|
{expire.label}
|
2024-01-09 13:30:05 +01:00
|
|
|
</Option>
|
2023-11-30 16:38:55 +01:00
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
</div>
|
2024-01-09 13:30:05 +01:00
|
|
|
<div className="mt-2">
|
|
|
|
<Switch checked={props.poll.multiple} onChange={v => changeMultiple(v)} label={formatMessage({ id: 'compose.poll.multiple' })} />
|
|
|
|
</div>
|
2023-11-30 16:38:55 +01:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|