This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 828
Render poll end events in timeline #10027
Merged
Merged
Changes from 29 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
1fd5654
wip
2c01d2e
remove dupe
4ac6f5f
Merge branch 'develop' into kerry/poll-model
567e59e
Merge branch 'develop' into kerry/poll-model
6adc726
Merge branch 'develop' into kerry/poll-model
563bffb
use poll model relations in all cases
c87bf54
update mpollbody tests to use poll instance
4865dd5
update poll fetching login in pinned messages card
962eb5d
add pinned polls to room polls state
19de598
add spinner while relations are still loading
f97f70c
Merge branch 'develop' into kerry/poll-model
42a31ce
handle no poll in end poll dialog
903a2e0
Merge branch 'kerry/poll-model' of https://github.com/matrix-org/matr…
8512e51
strict errors
7ffd6e9
render a poll body that errors for poll end events
dca5835
add fetching logic to pollend tile
def87ec
extract poll testing utilities
6dbcd43
test mpollend
6d7cff6
Merge branch 'develop' into kerry/poll-model
08d2b3e
strict fix
a76d6a4
Merge branch 'develop' into kerry/poll-model
c345b7c
more strict fix
bc2f596
Merge branch 'kerry/poll-model' into psg-905/poll-end-in-timeline
7d818c1
strict fix for forwardref
282c0cb
Merge branch 'develop' into psg-905/poll-end-in-timeline
6aa4288
Merge branch 'develop' into psg-905/poll-end-in-timeline
a6a6128
update poll test utils
fe17871
Merge branch 'develop' into psg-905/poll-end-in-timeline
a0a0355
implicit anys
ba48cca
Merge branch 'develop' into psg-905/poll-end-in-timeline
e9bd892
tidy and add jsdoc
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
.mx_MPollEndBody_icon { | ||
height: 14px; | ||
margin-right: $spacing-8; | ||
vertical-align: middle; | ||
color: $secondary-content; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import React, { useEffect, useState, useContext } from "react"; | ||
import { MatrixEvent } from "matrix-js-sdk/src/matrix"; | ||
import { M_TEXT } from "matrix-js-sdk/src/@types/extensible_events"; | ||
import { logger } from "matrix-js-sdk/src/logger"; | ||
|
||
import { Icon as PollIcon } from "../../../../res/img/element-icons/room/composer/poll.svg"; | ||
import MatrixClientContext from "../../../contexts/MatrixClientContext"; | ||
import { textForEvent } from "../../../TextForEvent"; | ||
import { IBodyProps } from "./IBodyProps"; | ||
import MPollBody from "./MPollBody"; | ||
|
||
const getRelatedPollStartEventId = (event: MatrixEvent): string | undefined => { | ||
const relation = event.getRelation(); | ||
return relation?.event_id; | ||
}; | ||
|
||
const usePollStartEvent = (event: MatrixEvent): { pollStartEvent?: MatrixEvent; isLoadingPollStartEvent: boolean } => { | ||
const matrixClient = useContext(MatrixClientContext); | ||
const [pollStartEvent, setPollStartEvent] = useState<MatrixEvent>(); | ||
const [isLoadingPollStartEvent, setIsLoadingPollStartEvent] = useState(false); | ||
|
||
const pollStartEventId = getRelatedPollStartEventId(event); | ||
|
||
useEffect(() => { | ||
const room = matrixClient.getRoom(event.getRoomId()); | ||
const fetchPollStartEvent = async (roomId: string, pollStartEventId: string): Promise<void> => { | ||
setIsLoadingPollStartEvent(true); | ||
try { | ||
const startEventJson = await matrixClient.fetchRoomEvent(roomId, pollStartEventId); | ||
const startEvent = new MatrixEvent(startEventJson); | ||
// add the poll to the room polls state | ||
room?.processPollEvents([startEvent, event]); | ||
|
||
if (startEvent.getSender() === event.getSender()) { | ||
setPollStartEvent(startEvent); | ||
} | ||
} catch (error) { | ||
logger.error("Failed to fetch related poll start event", error); | ||
} finally { | ||
setIsLoadingPollStartEvent(false); | ||
} | ||
}; | ||
|
||
if (pollStartEvent || !room || !pollStartEventId) { | ||
return; | ||
} | ||
|
||
const timelineSet = room.getUnfilteredTimelineSet(); | ||
const localEvent = timelineSet | ||
?.getTimelineForEvent(pollStartEventId) | ||
?.getEvents() | ||
.find((e) => e.getId() === pollStartEventId); | ||
|
||
if (localEvent) { | ||
if (localEvent.getSender() === event.getSender()) { | ||
setPollStartEvent(localEvent); | ||
} | ||
} else { | ||
// pollStartEvent is not in the current timeline, | ||
// fetch it | ||
fetchPollStartEvent(room.roomId, pollStartEventId); | ||
} | ||
}, [event, pollStartEventId, pollStartEvent, matrixClient]); | ||
|
||
return { pollStartEvent, isLoadingPollStartEvent }; | ||
}; | ||
|
||
export const MPollEndBody = React.forwardRef<any, IBodyProps>(({ mxEvent, ...props }, ref) => { | ||
const { pollStartEvent, isLoadingPollStartEvent } = usePollStartEvent(mxEvent); | ||
|
||
if (!pollStartEvent) { | ||
const pollEndFallbackMessage = M_TEXT.findIn(mxEvent.getContent()) || textForEvent(mxEvent); | ||
return ( | ||
<div> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that actually required, or could we instead have a document fragment. |
||
<PollIcon className="mx_MPollEndBody_icon" /> | ||
{!isLoadingPollStartEvent && pollEndFallbackMessage} | ||
</div> | ||
); | ||
} | ||
|
||
return <MPollBody mxEvent={pollStartEvent} {...props} />; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add a JSDoc block here explaining what this hook is doing?