2023-02-03 10:39:23 +13:00
|
|
|
/*
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-13 09:19:45 +13:00
|
|
|
import { Poll, PollEvent } from "matrix-js-sdk/src/matrix";
|
2023-02-03 10:39:23 +13:00
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
|
|
|
|
|
2023-02-13 09:19:45 +13:00
|
|
|
import { useEventEmitterState } from "../../../../hooks/useEventEmitter";
|
|
|
|
|
|
2023-02-03 10:39:23 +13:00
|
|
|
/**
|
2023-02-13 09:19:45 +13:00
|
|
|
* Get poll instances from a room
|
2023-02-03 10:39:23 +13:00
|
|
|
* @param roomId - id of room to retrieve polls for
|
|
|
|
|
* @param matrixClient - client
|
2023-02-13 09:19:45 +13:00
|
|
|
* @returns {Map<string, Poll>} - Map of Poll instances
|
2023-02-03 10:39:23 +13:00
|
|
|
*/
|
2023-02-13 09:19:45 +13:00
|
|
|
export const usePolls = (
|
|
|
|
|
roomId: string,
|
|
|
|
|
matrixClient: MatrixClient,
|
|
|
|
|
): {
|
|
|
|
|
polls: Map<string, Poll>;
|
|
|
|
|
} => {
|
2023-02-03 10:39:23 +13:00
|
|
|
const room = matrixClient.getRoom(roomId);
|
|
|
|
|
|
|
|
|
|
if (!room) {
|
|
|
|
|
throw new Error("Cannot find room");
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 09:19:45 +13:00
|
|
|
const polls = useEventEmitterState(room, PollEvent.New, () => room.polls);
|
2023-02-03 10:39:23 +13:00
|
|
|
|
2023-02-13 09:19:45 +13:00
|
|
|
// @TODO(kerrya) watch polls for end events, trigger refiltering
|
|
|
|
|
|
|
|
|
|
return { polls };
|
2023-02-03 10:39:23 +13:00
|
|
|
};
|