From 2f610b35654fc61a31706eb92bd5850222cb673f Mon Sep 17 00:00:00 2001 From: Shea Date: Sun, 31 May 2026 04:56:38 +0300 Subject: [PATCH] add poll creation modal Signed-off-by: Shea --- src/app/components/message/PollEvent.tsx | 7 +- src/app/features/room/RoomInput.tsx | 66 ++++- src/app/features/room/add-poll/PollDialog.tsx | 259 ++++++++++++++++++ src/app/features/room/add-poll/index.ts | 1 + 4 files changed, 321 insertions(+), 12 deletions(-) create mode 100644 src/app/features/room/add-poll/PollDialog.tsx create mode 100644 src/app/features/room/add-poll/index.ts diff --git a/src/app/components/message/PollEvent.tsx b/src/app/components/message/PollEvent.tsx index ed086f778..f5ead6f84 100644 --- a/src/app/components/message/PollEvent.tsx +++ b/src/app/components/message/PollEvent.tsx @@ -29,7 +29,7 @@ type PollEventProps = { mEvent: MatrixEvent; }; -type PollAnswerItem = { +export type PollAnswerItem = { id: string; 'org.matrix.msc1767.text': string; }; @@ -67,7 +67,8 @@ export function PollEvent({ content, mEvent }: PollEventProps) { const answers = (poll as { answers: PollAnswerItem[] })?.answers; const maxSelections = (poll as { max_selections: number })?.max_selections; const isDisclosed = (poll as { kind: string })?.kind === M_POLL_KIND_DISCLOSED.name; - const canEnd = (mx.getUserId() === mEvent.sender?.userId || permissions.action('redact', mx.getUserId() ?? '')) + const canEnd = + mx.getUserId() === mEvent.sender?.userId || permissions.action('redact', mx.getUserId() ?? ''); let votes: PollVotes = {}; answers.forEach((item) => (votes[item.id] = 0)); @@ -76,6 +77,8 @@ export function PollEvent({ content, mEvent }: PollEventProps) { ?.getUnfilteredTimelineSet() .relations.getAllChildEventsForEvent(eventId ?? '') .filter((event) => event.getRelation()?.rel_type === REFERENCE_RELATION.name); + + // manual sorting because the timeline is sometimes sent stupidly <3 let sortedChildEvents = childEvents ? childEvents.toSorted((a: MatrixEvent, b: MatrixEvent) => a.event.origin_server_ts && b.event.origin_server_ts diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index adaae6490..72d4603ec 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -159,6 +159,7 @@ import type { } from './AudioMessageRecorder'; import { AudioMessageRecorder } from './AudioMessageRecorder'; import * as prefix from '$unstable/prefixes'; +import { PollDialog } from './add-poll'; // Returns the event ID of the most recent non-reaction/non-edit event in a thread, // falling back to the thread root if no replies exist yet. @@ -381,6 +382,8 @@ export const RoomInput = forwardRef( const [editingScheduledDelayId, setEditingScheduledDelayId] = useAtom( roomIdToEditingScheduledDelayIdAtomFamily(roomId) ); + const [AddMenuAnchor, setAddMenuAnchor] = useState(); + const [showPollPicker, setShowPollPicker] = useState(false); const [scheduleMenuAnchor, setScheduleMenuAnchor] = useState(); const [showSchedulePicker, setShowSchedulePicker] = useState(false); const [silentReply, setSilentReply] = useState(!mentionInReplies); @@ -1519,16 +1522,58 @@ export const RoomInput = forwardRef( } before={ - pickFile('*')} - variant="SurfaceVariant" - size="300" - radii="300" - title="Upload File" - aria-label="Upload and attach a File" - > - - + <> + setAddMenuAnchor(undefined), + clickOutsideDeactivates: true, + escapeDeactivates: stopPropagation, + }} + > + + + { + setAddMenuAnchor(undefined); + setShowPollPicker(true); + }} + before={} + > + Create Poll + + pickFile('*')} + before={} + > + Add File + + + + + } + /> + setAddMenuAnchor(evt.currentTarget.getBoundingClientRect())} + variant="SurfaceVariant" + size="300" + radii="300" + title="Upload File" + aria-label="Upload and attach a File" + > + + + } after={ <> @@ -1771,6 +1816,7 @@ export const RoomInput = forwardRef( }} /> )} + {showPollPicker && setShowPollPicker(false)} mx={mx} roomId={roomId} />} ); } diff --git a/src/app/features/room/add-poll/PollDialog.tsx b/src/app/features/room/add-poll/PollDialog.tsx new file mode 100644 index 000000000..7f74ca152 --- /dev/null +++ b/src/app/features/room/add-poll/PollDialog.tsx @@ -0,0 +1,259 @@ +import FocusTrap from 'focus-trap-react'; +import { + Dialog, + Overlay, + OverlayCenter, + OverlayBackdrop, + Header, + config, + Box, + Text, + IconButton, + Icon, + Icons, + Button, + Input, + Chip, + Switch, + toRem, + color, +} from 'folds'; +import { stopPropagation } from '$utils/keyboard'; +import type { ChangeEventHandler } from 'react'; +import { useCallback, useRef, useState } from 'react'; +import type { PollAnswerItem } from '$components/message/PollEvent'; +import { randomStr } from '$utils/common'; +import { SettingTile } from '$components/setting-tile'; +import { SequenceCard } from '$components/sequence-card'; +import { SequenceCardStyle } from '$features/settings/styles.css'; +import type { IContent, MatrixClient } from 'matrix-js-sdk'; +import { M_POLL_KIND_DISCLOSED, M_POLL_KIND_UNDISCLOSED, M_POLL_START } from 'matrix-js-sdk'; + +type PollDialogProps = { + onCancel: () => void; + mx: MatrixClient; + roomId: string; +}; + +export function PollDialog({ onCancel, mx, roomId }: PollDialogProps) { + const [isDisclosed, setIsDisclosed] = useState(true); + const [maxSelections, setMaxSelections] = useState(1); + const title = useRef(''); + const [answers, setAnswers] = useState([ + { + id: randomStr(), + 'org.matrix.msc1767.text': '', + }, + { + id: randomStr(), + 'org.matrix.msc1767.text': '', + }, + ]); + const addOption = useCallback(() => { + if(maxSelections === answers.length) + setMaxSelections(maxSelections+1) + setAnswers([ + ...answers, + { + id: randomStr(), + 'org.matrix.msc1767.text': '', + }, + ]); + }, [answers, setAnswers, maxSelections, setMaxSelections]); + + const handleSubmit = () => { + // its an IContent instead of the proper object because the proper object doesnt work w other clients :> + const pollContent: IContent = { + [M_POLL_START.name]: { + "question": { + "org.matrix.msc1767.text": title.current, + "body": title.current, + "msgtype": "m.text" + }, + "kind": isDisclosed ? M_POLL_KIND_DISCLOSED.name : M_POLL_KIND_UNDISCLOSED.name, + "max_selections": maxSelections, + "answers": answers, + }, + "org.matrix.msc1767.text": `New poll\n Question: ${title.current}\nAnswers:\n ${answers.map((item) => item['org.matrix.msc1767.text']).join('\n')}` + } + + /* mx.sendEvent( + roomId, + M_POLL_START.name as keyof TimelineEvents, + pollContent as TimelineEvents[keyof TimelineEvents] + ); + */ + // oxlint-disable-next-line no-console + console.log('submit', title, answers, isDisclosed, maxSelections, pollContent); + // onCancel(); + }; + + const handleMaxOptions: ChangeEventHandler = (evt) => { + const val = evt.target.value; + const parsed = Number.parseInt(val, 10); + if (!Number.isNaN(parsed)) setMaxSelections(parsed); + }; + return ( + }> + + + +
+ + New Poll + + + + +
+ + + Title + (title.current = evt.currentTarget.value.trim())} + placeholder={'What should we have for dinner?'} + /> + + + + Options ({answers.length}) + + Add Option + + + {answers.map((item, index) => ( + + + setAnswers([ + ...answers.slice(0, index), + { + id: answers[index]?.id ?? randomStr(), + 'org.matrix.msc1767.text': evt.currentTarget.value.trim() ?? '', + }, + ...answers.slice(index + 1), + ]) + } + placeholder={`Type Option ${index+1}`} + after={ + { + if (answers.length > 2) + setAnswers(answers.filter((answer) => answer.id !== item.id)); + }} + > + + + } + /> + + ))} + + + + + } + /> + + + + } + /> + setMaxSelections(Number.parseInt(e.target.value, 10))} + style={{ + width: '100%', + cursor: 'pointer', + appearance: 'none', + height: toRem(6), + borderRadius: config.radii.Pill, + backgroundColor: color.Background.ContainerLine, + accentColor: color.Primary.Main, + }} + /> + + + + +
+
+
+
+ ); +} diff --git a/src/app/features/room/add-poll/index.ts b/src/app/features/room/add-poll/index.ts new file mode 100644 index 000000000..726b6a44f --- /dev/null +++ b/src/app/features/room/add-poll/index.ts @@ -0,0 +1 @@ +export { PollDialog } from './PollDialog';