mediasfu-shared 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +228 -0
  3. package/dist/index.cjs +7707 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.ts +7285 -0
  6. package/dist/index.js +7690 -0
  7. package/dist/index.js.map +1 -0
  8. package/package.json +78 -0
  9. package/src/ProducerClient/producerClientEmits/createDeviceClient.ts +68 -0
  10. package/src/consumers/addVideosGrid.ts +18 -0
  11. package/src/consumers/autoAdjust.ts +100 -0
  12. package/src/consumers/calculateRowsAndColumns.ts +51 -0
  13. package/src/consumers/changeVids.ts +753 -0
  14. package/src/consumers/checkGrid.ts +100 -0
  15. package/src/consumers/checkPermission.ts +89 -0
  16. package/src/consumers/checkScreenShare.ts +114 -0
  17. package/src/consumers/closeAndResize.ts +401 -0
  18. package/src/consumers/compareActiveNames.ts +122 -0
  19. package/src/consumers/compareScreenStates.ts +117 -0
  20. package/src/consumers/connectIps.ts +175 -0
  21. package/src/consumers/connectLocalIps.ts +103 -0
  22. package/src/consumers/connectRecvTransport.ts +158 -0
  23. package/src/consumers/connectSendTransport.ts +150 -0
  24. package/src/consumers/connectSendTransportAudio.ts +161 -0
  25. package/src/consumers/connectSendTransportScreen.ts +169 -0
  26. package/src/consumers/connectSendTransportVideo.ts +149 -0
  27. package/src/consumers/consumerResume.ts +25 -0
  28. package/src/consumers/controlMedia.ts +118 -0
  29. package/src/consumers/createSendTransport.ts +312 -0
  30. package/src/consumers/disconnectSendTransportAudio.ts +170 -0
  31. package/src/consumers/disconnectSendTransportScreen.ts +130 -0
  32. package/src/consumers/disconnectSendTransportVideo.ts +161 -0
  33. package/src/consumers/dispStreams.ts +694 -0
  34. package/src/consumers/generatePageContent.ts +118 -0
  35. package/src/consumers/getEstimate.ts +124 -0
  36. package/src/consumers/getPipedProducersAlt.ts +96 -0
  37. package/src/consumers/getProducersPiped.ts +89 -0
  38. package/src/consumers/getVideos.ts +107 -0
  39. package/src/consumers/mixStreams.ts +97 -0
  40. package/src/consumers/onScreenChanges.ts +106 -0
  41. package/src/consumers/prepopulateUserMedia.ts +18 -0
  42. package/src/consumers/processConsumerTransports.ts +157 -0
  43. package/src/consumers/processConsumerTransportsAudio.ts +121 -0
  44. package/src/consumers/rePort.ts +123 -0
  45. package/src/consumers/reUpdateInter.ts +289 -0
  46. package/src/consumers/readjust.ts +170 -0
  47. package/src/consumers/receiveAllPipedTransports.ts +77 -0
  48. package/src/consumers/receiveRoomMessages.ts +55 -0
  49. package/src/consumers/reorderStreams.ts +246 -0
  50. package/src/consumers/requestScreenShare.ts +103 -0
  51. package/src/consumers/resumePauseAudioStreams.ts +174 -0
  52. package/src/consumers/resumePauseStreams.ts +110 -0
  53. package/src/consumers/resumeSendTransportAudio.ts +143 -0
  54. package/src/consumers/signalNewConsumerTransport.ts +179 -0
  55. package/src/consumers/socketReceiveMethods/joinConsumeRoom.ts +130 -0
  56. package/src/consumers/socketReceiveMethods/newPipeProducer.ts +138 -0
  57. package/src/consumers/socketReceiveMethods/producerClosed.ts +102 -0
  58. package/src/consumers/startShareScreen.ts +124 -0
  59. package/src/consumers/stopShareScreen.ts +241 -0
  60. package/src/consumers/streamSuccessAudio.ts +297 -0
  61. package/src/consumers/streamSuccessAudioSwitch.ts +315 -0
  62. package/src/consumers/streamSuccessScreen.ts +255 -0
  63. package/src/consumers/streamSuccessVideo.ts +373 -0
  64. package/src/consumers/switchUserAudio.ts +140 -0
  65. package/src/consumers/switchUserVideo.ts +201 -0
  66. package/src/consumers/switchUserVideoAlt.ts +331 -0
  67. package/src/consumers/trigger.ts +250 -0
  68. package/src/consumers/updateMiniCardsGrid.ts +150 -0
  69. package/src/consumers/updateParticipantAudioDecibels.ts +56 -0
  70. package/src/index.ts +119 -0
  71. package/src/methods/background/launchBackground.ts +16 -0
  72. package/src/methods/breakoutRooms/breakoutRoomUpdated.ts +161 -0
  73. package/src/methods/breakoutRooms/handleStartBreakout.ts +96 -0
  74. package/src/methods/breakoutRooms/handleStopBreakout.ts +72 -0
  75. package/src/methods/breakoutRooms/index.ts +4 -0
  76. package/src/methods/breakoutRooms/launchBreakoutRooms.ts +31 -0
  77. package/src/methods/coHost/launchCoHost.ts +28 -0
  78. package/src/methods/coHostMethods/index.ts +2 -0
  79. package/src/methods/coHostMethods/modifyCoHostSettings.ts +94 -0
  80. package/src/methods/displaySettings/index.ts +1 -0
  81. package/src/methods/displaySettings/launchDisplaySettings.ts +31 -0
  82. package/src/methods/displaySettings/modifyDisplaySettings.ts +242 -0
  83. package/src/methods/exit/confirmExit.ts +60 -0
  84. package/src/methods/exit/index.ts +2 -0
  85. package/src/methods/exit/launchConfirmExit.ts +29 -0
  86. package/src/methods/index.ts +5 -0
  87. package/src/methods/mediaSettings/launchMediaSettings.ts +61 -0
  88. package/src/methods/menu/launchMenuModal.ts +28 -0
  89. package/src/methods/message/index.ts +1 -0
  90. package/src/methods/message/launchMessages.ts +27 -0
  91. package/src/methods/message/sendMessage.ts +175 -0
  92. package/src/methods/participants/index.ts +3 -0
  93. package/src/methods/participants/launchParticipants.ts +25 -0
  94. package/src/methods/participants/messageParticipants.ts +78 -0
  95. package/src/methods/participants/muteParticipants.ts +79 -0
  96. package/src/methods/participants/removeParticipants.ts +97 -0
  97. package/src/methods/polls/handleCreatePoll.ts +66 -0
  98. package/src/methods/polls/handleEndPoll.ts +64 -0
  99. package/src/methods/polls/handleVotePoll.ts +76 -0
  100. package/src/methods/polls/index.ts +3 -0
  101. package/src/methods/polls/launchPoll.ts +25 -0
  102. package/src/methods/prejoin/handleCreateRoom.ts +441 -0
  103. package/src/methods/prejoin/handleJoinRoom.ts +153 -0
  104. package/src/methods/prejoin/index.ts +14 -0
  105. package/src/methods/recording/checkPauseState.ts +57 -0
  106. package/src/methods/recording/checkResumeState.ts +42 -0
  107. package/src/methods/recording/confirmRecording.ts +241 -0
  108. package/src/methods/recording/launchRecording.ts +114 -0
  109. package/src/methods/recording/recordPauseTimer.ts +52 -0
  110. package/src/methods/recording/recordResumeTimer.ts +92 -0
  111. package/src/methods/recording/recordStartTimer.ts +106 -0
  112. package/src/methods/recording/recordUpdateTimer.ts +49 -0
  113. package/src/methods/recording/startRecording.ts +268 -0
  114. package/src/methods/recording/stopRecording.ts +124 -0
  115. package/src/methods/recording/updateRecording.ts +245 -0
  116. package/src/methods/requests/index.ts +1 -0
  117. package/src/methods/requests/launchRequests.ts +25 -0
  118. package/src/methods/requests/respondToRequests.ts +108 -0
  119. package/src/methods/settings/index.ts +2 -0
  120. package/src/methods/settings/launchSettings.ts +25 -0
  121. package/src/methods/settings/modifySettings.ts +99 -0
  122. package/src/methods/stream/clickVideo.ts +337 -0
  123. package/src/methods/stream/index.ts +3 -0
  124. package/src/methods/stream/switchAudio.ts +73 -0
  125. package/src/methods/stream/switchVideo.ts +148 -0
  126. package/src/methods/stream/switchVideoAlt.ts +152 -0
  127. package/src/methods/utils/checkLimitsAndMakeRequest.ts +103 -0
  128. package/src/methods/utils/validateAlphanumeric.ts +41 -0
  129. package/src/methods/waiting/index.ts +1 -0
  130. package/src/methods/waiting/launchWaiting.ts +26 -0
  131. package/src/methods/waiting/respondToWaiting.ts +82 -0
  132. package/src/methods/welcome/handleWelcomeRequest.ts +189 -0
  133. package/src/methods/welcome/index.ts +5 -0
  134. package/src/methods/whiteboard/handleStartWhiteboard.ts +65 -0
  135. package/src/methods/whiteboard/handleStopWhiteboard.ts +48 -0
  136. package/src/methods/whiteboard/index.ts +4 -0
  137. package/src/methods/whiteboard/launchConfigureWhiteboard.ts +29 -0
  138. package/src/producers/producerEmits/joinConRoom.ts +153 -0
  139. package/src/sockets/SocketManager.ts +232 -0
  140. package/src/types/shared-base-types.ts +752 -0
  141. package/src/types/types.ts +84 -0
@@ -0,0 +1,78 @@
1
+ import type { Participant, CoHostResponsibility, ShowAlert } from "../../types/types";
2
+
3
+ export interface MessageParticipantsOptions {
4
+ coHostResponsibility?: CoHostResponsibility[];
5
+ participant: Participant;
6
+ member: string;
7
+ islevel: string;
8
+ showAlert?: ShowAlert;
9
+ coHost: string;
10
+ updateIsMessagesModalVisible: (isVisible: boolean) => void;
11
+ updateDirectMessageDetails: (participant: Participant | null) => void;
12
+ updateStartDirectMessage: (start: boolean) => void;
13
+ }
14
+
15
+ /**
16
+ * Sends a direct message to a participant if the current member has the necessary permissions.
17
+ *
18
+ * @param {MessageParticipantsOptions} options - The options for messaging participants.
19
+ * @param {CoHostResponsibility[]} options.coHostResponsibility - Array of responsibilities assigned to the co-host.
20
+ * @param {Participant} options.participant - The participant to whom the message is to be sent.
21
+ * @param {string} options.member - The current member attempting to send the message.
22
+ * @param {string} options.islevel - The level of the current member.
23
+ * @param {ShowAlert} [options.showAlert] - Function to show an alert message.
24
+ * @param {string} options.coHost - The co-host member.
25
+ * @param {Function} options.updateIsMessagesModalVisible - Function to update the visibility of the messages modal.
26
+ * @param {Function} options.updateDirectMessageDetails - Function to update the details of the direct message.
27
+ * @param {Function} options.updateStartDirectMessage - Function to start the direct message.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * messageParticipants({
32
+ * coHostResponsibility: [{ name: "chat", value: true }],
33
+ * participant: { name: "John Doe", islevel: "1" },
34
+ * member: "currentMember",
35
+ * islevel: "2",
36
+ * showAlert: (alert) => console.log(alert.message),
37
+ * coHost: "coHostMember",
38
+ * updateIsMessagesModalVisible: (isVisible) => setMessagesModalVisible(isVisible),
39
+ * updateDirectMessageDetails: (participant) => setDirectMessageDetails(participant),
40
+ * updateStartDirectMessage: (start) => setStartDirectMessage(start),
41
+ * });
42
+ * ```
43
+ */
44
+
45
+ export const messageParticipants = ({
46
+ coHostResponsibility = [],
47
+ participant,
48
+ member,
49
+ islevel,
50
+ showAlert,
51
+ coHost,
52
+ updateIsMessagesModalVisible,
53
+ updateDirectMessageDetails,
54
+ updateStartDirectMessage,
55
+ }: MessageParticipantsOptions): void => {
56
+ let chatValue = false;
57
+
58
+ try {
59
+ chatValue = coHostResponsibility.find((item) => item.name === "chat")?.value ?? false;
60
+ } catch (error) {
61
+ console.error(error);
62
+ }
63
+
64
+ if (islevel === "2" || (coHost === member && chatValue === true)) {
65
+ if (participant.islevel !== "2") {
66
+ updateDirectMessageDetails(participant);
67
+ updateStartDirectMessage(true);
68
+ updateIsMessagesModalVisible(true);
69
+ }
70
+ } else {
71
+ showAlert?.({
72
+ message: "You are not allowed to send this message",
73
+ type: "danger",
74
+ duration: 3000,
75
+ });
76
+ return;
77
+ }
78
+ };
@@ -0,0 +1,79 @@
1
+ import { Socket } from "socket.io-client";
2
+ import type { Participant, CoHostResponsibility, ShowAlert } from "../../types/types";
3
+
4
+ export interface MuteParticipantsOptions {
5
+ socket: Socket;
6
+ coHostResponsibility?: CoHostResponsibility[];
7
+ participant: Participant;
8
+ member: string;
9
+ islevel: string;
10
+ showAlert?: ShowAlert;
11
+ coHost: string;
12
+ roomName: string;
13
+ }
14
+
15
+ /**
16
+ * Mutes a participant in a media session if the current member has the necessary permissions.
17
+ *
18
+ * @param {MuteParticipantsOptions} options - The options for muting participants.
19
+ * @param {Socket} options.socket - The socket instance for communication.
20
+ * @param {CoHostResponsibility[]} options.coHostResponsibility - List of co-host responsibilities.
21
+ * @param {Participant} options.participant - The participant to be muted.
22
+ * @param {string} options.member - The current member attempting to mute.
23
+ * @param {string} options.islevel - The level of the current member.
24
+ * @param {ShowAlert} [options.showAlert] - Optional function to show alerts.
25
+ * @param {string} options.coHost - The co-host information.
26
+ * @param {string} options.roomName - The name of the room.
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * muteParticipants({
31
+ * socket,
32
+ * coHostResponsibility: [{ name: "media", value: true }],
33
+ * participant: { id: "123", name: "John Doe", muted: false, islevel: "1" },
34
+ * member: "currentMember",
35
+ * islevel: "2",
36
+ * showAlert: (alert) => console.log(alert.message),
37
+ * coHost: "coHostMember",
38
+ * roomName: "room1",
39
+ * });
40
+ * ```
41
+ */
42
+
43
+ export const muteParticipants = async ({
44
+ socket,
45
+ coHostResponsibility = [],
46
+ participant,
47
+ member,
48
+ islevel,
49
+ showAlert,
50
+ coHost,
51
+ roomName,
52
+ }: MuteParticipantsOptions): Promise<void> => {
53
+ let mediaValue = false;
54
+
55
+ try {
56
+ mediaValue =
57
+ coHostResponsibility.find((item) => item.name === "media")?.value ?? false;
58
+ } catch (error) {
59
+ console.error(error);
60
+ }
61
+
62
+ if (islevel === "2" || (coHost === member && mediaValue)) {
63
+ if (!participant.muted && participant.islevel !== "2") {
64
+ const participantId = participant.id;
65
+ socket.emit("controlMedia", {
66
+ participantId,
67
+ participantName: participant.name,
68
+ type: "all",
69
+ roomName,
70
+ });
71
+ }
72
+ } else {
73
+ showAlert?.({
74
+ message: "You are not allowed to mute other participants",
75
+ type: "danger",
76
+ duration: 3000,
77
+ });
78
+ }
79
+ };
@@ -0,0 +1,97 @@
1
+ import { Socket } from 'socket.io-client';
2
+ import type { CoHostResponsibility, Participant, ShowAlert } from '../../types/types';
3
+
4
+ export interface RemoveParticipantsOptions {
5
+ coHostResponsibility: CoHostResponsibility[];
6
+ participant: Participant;
7
+ member: string;
8
+ islevel: string;
9
+ showAlert?: ShowAlert;
10
+ coHost: string;
11
+ participants: Participant[];
12
+ socket: Socket;
13
+ roomName: string;
14
+ updateParticipants: (participants: Participant[]) => void;
15
+ }
16
+
17
+ /**
18
+ * Removes a participant from the room if the user has the necessary permissions.
19
+ *
20
+ * @param {RemoveParticipantsOptions} options - The options for removing a participant.
21
+ * @param {CoHostResponsibility[]} options.coHostResponsibility - The responsibilities assigned to the co-host.
22
+ * @param {Participant} options.participant - The participant to be removed from the room.
23
+ * @param {string} options.member - The current member attempting to remove the participant.
24
+ * @param {string} options.islevel - The level of the current member.
25
+ * @param {ShowAlert} [options.showAlert] - Optional function to display an alert.
26
+ * @param {string} options.coHost - The co-host identifier.
27
+ * @param {Participant[]} options.participants - List of current participants.
28
+ * @param {Socket} options.socket - The socket instance for communication.
29
+ * @param {string} options.roomName - The name of the room.
30
+ * @param {Function} options.updateParticipants - Function to update the participants list.
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * removeParticipants({
35
+ * coHostResponsibility: [{ name: "participants", value: true }],
36
+ * participant: { id: "123", name: "John Doe", islevel: "1" },
37
+ * member: "currentMember",
38
+ * islevel: "2",
39
+ * showAlert: (alert) => console.log(alert.message),
40
+ * coHost: "coHostMember",
41
+ * participants: [{ id: "123", name: "John Doe", islevel: "1" }],
42
+ * socket,
43
+ * roomName: "room1",
44
+ * updateParticipants: (updatedParticipants) => console.log(updatedParticipants),
45
+ * });
46
+ * ```
47
+ */
48
+
49
+ export const removeParticipants = async ({
50
+ coHostResponsibility,
51
+ participant,
52
+ member,
53
+ islevel,
54
+ showAlert,
55
+ coHost,
56
+ participants,
57
+ socket,
58
+ roomName,
59
+ updateParticipants
60
+ }: RemoveParticipantsOptions): Promise<void> => {
61
+ let participantsValue = false;
62
+
63
+ try {
64
+ participantsValue =
65
+ coHostResponsibility.find((item) => item.name === 'participants')?.value ?? false;
66
+ } catch {
67
+ participantsValue = false;
68
+ }
69
+
70
+ if (islevel === '2' || (coHost === member && participantsValue === true)) {
71
+ if (participant.islevel !== '2') {
72
+ const participantId = participant.id;
73
+
74
+ // Emit a socket event to disconnect the user
75
+ socket.emit('disconnectUserInitiate', {
76
+ member: participant.name,
77
+ roomName,
78
+ id: participantId,
79
+ });
80
+
81
+ // Remove the participant from the local array
82
+ const index = participants.findIndex((obj) => obj.name === participant.name);
83
+ if (index !== -1) {
84
+ participants.splice(index, 1);
85
+ }
86
+
87
+ // Update the participants array
88
+ updateParticipants(participants);
89
+ }
90
+ } else {
91
+ showAlert?.({
92
+ message: 'You are not allowed to remove other participants',
93
+ type: 'danger',
94
+ duration: 3000,
95
+ });
96
+ }
97
+ };
@@ -0,0 +1,66 @@
1
+ import { Socket } from "socket.io-client";
2
+ import type { ShowAlert } from "../../types/types";
3
+
4
+ interface NewPoll {
5
+ question: string;
6
+ type: string;
7
+ options: string[];
8
+ }
9
+
10
+ export interface HandleCreatePollOptions {
11
+ poll: NewPoll;
12
+ socket: Socket;
13
+ roomName: string;
14
+ showAlert?: ShowAlert;
15
+ updateIsPollModalVisible: (visible: boolean) => void;
16
+ }
17
+
18
+ // Export the type definition for the function
19
+ export type HandleCreatePollType = (options: HandleCreatePollOptions) => Promise<void>;
20
+
21
+ /**
22
+ * Handles the creation of a poll by emitting a "createPoll" event with the provided details.
23
+ *
24
+ * @param {HandleCreatePollOptions} options - The options for creating the poll.
25
+ * @param {NewPoll} options.poll - The poll object containing the poll question, type, and options.
26
+ * @param {Socket} options.socket - The socket instance used to communicate with the server.
27
+ * @param {string} options.roomName - The name of the room where the poll is created.
28
+ * @param {Function} [options.showAlert] - Optional function to show alert messages.
29
+ * @param {Function} options.updateIsPollModalVisible - Function to toggle the poll modal visibility.
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * handleCreatePoll({
34
+ * poll: { question: "Favorite color?", type: "singleChoice", options: ["Red", "Blue", "Green"] },
35
+ * socket: socketInstance,
36
+ * roomName: "roomA",
37
+ * showAlert: (message) => console.log(message),
38
+ * updateIsPollModalVisible: (isVisible) => setIsPollModalVisible(isVisible),
39
+ * });
40
+ * ```
41
+ */
42
+
43
+ export const handleCreatePoll = async ({
44
+ poll,
45
+ socket,
46
+ roomName,
47
+ showAlert,
48
+ updateIsPollModalVisible,
49
+ }: HandleCreatePollOptions): Promise<void> => {
50
+ try {
51
+ socket.emit(
52
+ "createPoll",
53
+ { roomName, poll },
54
+ (response: { success: boolean; reason?: string }) => {
55
+ if (response.success) {
56
+ showAlert?.({ message: "Poll created successfully", type: "success" });
57
+ updateIsPollModalVisible(false);
58
+ } else {
59
+ showAlert?.({ message: response.reason || "Failed to create poll", type: "danger" });
60
+ }
61
+ }
62
+ );
63
+ } catch (error) {
64
+ console.error("Error creating poll:", error);
65
+ }
66
+ };
@@ -0,0 +1,64 @@
1
+ import { Socket } from "socket.io-client";
2
+ import type { ShowAlert } from "../../types/types";
3
+
4
+ export interface HandleEndPollOptions {
5
+ pollId: string;
6
+ socket: Socket;
7
+ showAlert?: ShowAlert;
8
+ roomName: string;
9
+ updateIsPollModalVisible: (isVisible: boolean) => void;
10
+ }
11
+
12
+ // Export the type definition for the function
13
+ export type HandleEndPollType = (options: HandleEndPollOptions) => Promise<void>;
14
+
15
+ /**
16
+ * Handles the end of a poll by emitting an "endPoll" event through the provided socket.
17
+ * Displays an alert based on the success or failure of the operation.
18
+ *
19
+ * @param {HandleEndPollOptions} options - The options for ending the poll.
20
+ * @param {string} options.pollId - The ID of the poll to end.
21
+ * @param {Socket} options.socket - The socket instance to emit the event.
22
+ * @param {Function} [options.showAlert] - Optional function to display alerts.
23
+ * @param {string} options.roomName - The name of the room where the poll is being conducted.
24
+ * @param {Function} options.updateIsPollModalVisible - Function to update the poll modal visibility.
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * handleEndPoll({
29
+ * pollId: "poll123",
30
+ * socket: socketInstance,
31
+ * showAlert: (message) => console.log(message),
32
+ * roomName: "roomA",
33
+ * updateIsPollModalVisible: (isVisible) => setIsPollModalVisible(isVisible),
34
+ * });
35
+ * ```
36
+ */
37
+
38
+ export const handleEndPoll = async ({
39
+ pollId,
40
+ socket,
41
+ showAlert,
42
+ roomName,
43
+ updateIsPollModalVisible,
44
+ }: HandleEndPollOptions): Promise<void> => {
45
+ try {
46
+ socket.emit(
47
+ "endPoll",
48
+ { roomName, poll_id: pollId },
49
+ (response: { success: boolean; reason?: string }) => {
50
+ if (response.success) {
51
+ showAlert?.({
52
+ message: "Poll ended successfully",
53
+ type: "success",
54
+ });
55
+ updateIsPollModalVisible(false);
56
+ } else {
57
+ showAlert?.({ message: response.reason || "Failed to end poll", type: "danger" });
58
+ }
59
+ }
60
+ );
61
+ } catch (error) {
62
+ console.error("Error ending poll:", error);
63
+ }
64
+ };
@@ -0,0 +1,76 @@
1
+ import { Socket } from "socket.io-client";
2
+ import type { ShowAlert } from "../../types/types";
3
+
4
+ export interface HandleVotePollOptions {
5
+ pollId: string;
6
+ optionIndex: number;
7
+ socket: Socket;
8
+ showAlert?: ShowAlert;
9
+ member: string;
10
+ roomName: string;
11
+ updateIsPollModalVisible: (isVisible: boolean) => void;
12
+ }
13
+
14
+ // Export the type definition for the function
15
+ export type HandleVotePollType = (options: HandleVotePollOptions) => Promise<void>;
16
+
17
+ /**
18
+ * Handles the voting process for a poll.
19
+ *
20
+ * @param {HandleVotePollOptions} options - The options for handling the vote.
21
+ * @param {string} options.pollId - The ID of the poll.
22
+ * @param {number} options.optionIndex - The index of the selected option.
23
+ * @param {Socket} options.socket - The socket instance for communication.
24
+ * @param {Function} [options.showAlert] - Optional function to show alerts.
25
+ * @param {string} options.member - The member who is voting.
26
+ * @param {string} options.roomName - The name of the room where the poll is conducted.
27
+ * @param {Function} options.updateIsPollModalVisible - Function to update the visibility of the poll modal.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * handleVotePoll({
32
+ * pollId: "poll123",
33
+ * optionIndex: 1,
34
+ * socket: socketInstance,
35
+ * showAlert: (message) => console.log(message),
36
+ * member: "user1",
37
+ * roomName: "roomA",
38
+ * updateIsPollModalVisible: (isVisible) => setIsPollModalVisible(isVisible),
39
+ * });
40
+ * ```
41
+ */
42
+
43
+ export const handleVotePoll = async ({
44
+ pollId,
45
+ optionIndex,
46
+ socket,
47
+ showAlert,
48
+ member,
49
+ roomName,
50
+ updateIsPollModalVisible,
51
+ }: HandleVotePollOptions): Promise<void> => {
52
+ try {
53
+ socket.emit(
54
+ "votePoll",
55
+ {
56
+ roomName,
57
+ poll_id: pollId,
58
+ member,
59
+ choice: optionIndex,
60
+ },
61
+ (response: { success: boolean; reason?: string }) => {
62
+ if (response.success) {
63
+ showAlert?.({
64
+ message: "Vote submitted successfully",
65
+ type: "success",
66
+ });
67
+ updateIsPollModalVisible(false);
68
+ } else {
69
+ showAlert?.({ message: response.reason || "Failed to submit vote", type: "danger" });
70
+ }
71
+ }
72
+ );
73
+ } catch (error) {
74
+ console.error("Error submitting vote:", error);
75
+ }
76
+ };
@@ -0,0 +1,3 @@
1
+ export { handleCreatePoll, type HandleCreatePollType, type HandleCreatePollOptions } from './handleCreatePoll';
2
+ export { handleEndPoll, type HandleEndPollType, type HandleEndPollOptions } from './handleEndPoll';
3
+ export { handleVotePoll, type HandleVotePollType, type HandleVotePollOptions } from './handleVotePoll';
@@ -0,0 +1,25 @@
1
+ export interface LaunchPollOptions {
2
+ updateIsPollModalVisible: (isVisible: boolean) => void
3
+ isPollModalVisible: boolean
4
+ }
5
+
6
+ export type LaunchPollType = (options: LaunchPollOptions) => void
7
+
8
+ /**
9
+ * Toggles the visibility of the poll modal.
10
+ *
11
+ * @param {LaunchPollOptions} options - The options for toggling the poll modal visibility.
12
+ * @param {Function} options.updateIsPollModalVisible - Function to update the visibility state of the poll modal.
13
+ * @param {boolean} options.isPollModalVisible - Current visibility state of the poll modal.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * launchPoll({
18
+ * updateIsPollModalVisible: (visible) => setIsPollModalVisible(visible),
19
+ * isPollModalVisible: false,
20
+ * })
21
+ * ```
22
+ */
23
+ export const launchPoll: LaunchPollType = ({ updateIsPollModalVisible, isPollModalVisible }) => {
24
+ updateIsPollModalVisible(!isPollModalVisible)
25
+ }