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 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { useCallback, useState } from 'react';
  2. import { useDispatch } from 'react-redux';
  3. import { handleLobbyChatInitialized } from '../chat/actions.any';
  4. import { approveKnockingParticipant, rejectKnockingParticipant } from '../lobby/actions.web';
  5. interface IDrawerParticipant {
  6. displayName?: string;
  7. participantID: string;
  8. }
  9. /**
  10. * Hook used to create admit/reject lobby actions.
  11. *
  12. * @param {Object} participant - The participant for which the actions are created.
  13. * @param {Function} closeDrawer - Callback for closing the drawer.
  14. * @returns {Array<Function>}
  15. */
  16. export function useLobbyActions(participant?: IDrawerParticipant | null, closeDrawer?: Function) {
  17. const dispatch = useDispatch();
  18. return [
  19. useCallback(e => {
  20. e.stopPropagation();
  21. dispatch(approveKnockingParticipant(participant?.participantID ?? ''));
  22. closeDrawer?.();
  23. }, [ dispatch, closeDrawer ]),
  24. useCallback(() => {
  25. dispatch(rejectKnockingParticipant(participant?.participantID ?? ''));
  26. closeDrawer?.();
  27. }, [ dispatch, closeDrawer ]),
  28. useCallback(() => {
  29. dispatch(handleLobbyChatInitialized(participant?.participantID ?? ''));
  30. }, [ dispatch ])
  31. ];
  32. }
  33. /**
  34. * Hook used to create actions & state for opening a drawer.
  35. *
  36. * @returns {Array<any>}
  37. */
  38. export function useParticipantDrawer(): [
  39. IDrawerParticipant | null,
  40. () => void,
  41. (p: IDrawerParticipant | null) => void ] {
  42. const [ drawerParticipant, openDrawerForParticipant ] = useState<IDrawerParticipant | null>(null);
  43. const closeDrawer = useCallback(() => {
  44. openDrawerForParticipant(null);
  45. }, []);
  46. return [
  47. drawerParticipant,
  48. closeDrawer,
  49. openDrawerForParticipant
  50. ];
  51. }