You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hooks.web.ts 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { useCallback, useState } from 'react';
  2. import { useDispatch, useSelector } from 'react-redux';
  3. import { handleLobbyChatInitialized } from '../chat/actions.web';
  4. import { approveKnockingParticipant, rejectKnockingParticipant } from '../lobby/actions.web';
  5. import ParticipantsPaneButton from './components/web/ParticipantsPaneButton';
  6. import { isParticipantsPaneEnabled } from './functions';
  7. interface IDrawerParticipant {
  8. displayName?: string;
  9. participantID: string;
  10. }
  11. const participants = {
  12. key: 'participants-pane',
  13. Content: ParticipantsPaneButton,
  14. group: 2
  15. };
  16. /**
  17. * Hook used to create admit/reject lobby actions.
  18. *
  19. * @param {Object} participant - The participant for which the actions are created.
  20. * @param {Function} closeDrawer - Callback for closing the drawer.
  21. * @returns {Array<Function>}
  22. */
  23. export function useLobbyActions(participant?: IDrawerParticipant | null, closeDrawer?: Function) {
  24. const dispatch = useDispatch();
  25. return [
  26. useCallback(e => {
  27. e.stopPropagation();
  28. dispatch(approveKnockingParticipant(participant?.participantID ?? ''));
  29. closeDrawer?.();
  30. }, [ dispatch, closeDrawer, participant?.participantID ]),
  31. useCallback(() => {
  32. dispatch(rejectKnockingParticipant(participant?.participantID ?? ''));
  33. closeDrawer?.();
  34. }, [ dispatch, closeDrawer, participant?.participantID ]),
  35. useCallback(() => {
  36. dispatch(handleLobbyChatInitialized(participant?.participantID ?? ''));
  37. }, [ dispatch, participant?.participantID ])
  38. ];
  39. }
  40. /**
  41. * Hook used to create actions & state for opening a drawer.
  42. *
  43. * @returns {Array<any>}
  44. */
  45. export function useParticipantDrawer(): [
  46. IDrawerParticipant | null,
  47. () => void,
  48. (p: IDrawerParticipant | null) => void ] {
  49. const [ drawerParticipant, openDrawerForParticipant ] = useState<IDrawerParticipant | null>(null);
  50. const closeDrawer = useCallback(() => {
  51. openDrawerForParticipant(null);
  52. }, []);
  53. return [
  54. drawerParticipant,
  55. closeDrawer,
  56. openDrawerForParticipant
  57. ];
  58. }
  59. /**
  60. * A hook that returns the participants pane button if it is enabled and undefined otherwise.
  61. *
  62. * @returns {Object | undefined}
  63. */
  64. export function useParticipantPaneButton() {
  65. const participantsPaneEnabled = useSelector(isParticipantsPaneEnabled);
  66. if (participantsPaneEnabled) {
  67. return participants;
  68. }
  69. }