diff --git a/src/frontend/src/features/rooms/livekit/components/controls/ChatToggle.tsx b/src/frontend/src/features/rooms/livekit/components/controls/ChatToggle.tsx index 3b56a141..83f249f1 100644 --- a/src/frontend/src/features/rooms/livekit/components/controls/ChatToggle.tsx +++ b/src/frontend/src/features/rooms/livekit/components/controls/ChatToggle.tsx @@ -2,18 +2,13 @@ import { useTranslation } from 'react-i18next' import { RiChat1Line } from '@remixicon/react' import { ToggleButton } from '@/primitives' import { css } from '@/styled-system/css' -import { useLayoutContext } from '@livekit/components-react' -import { useSnapshot } from 'valtio' -import { participantsStore } from '@/stores/participants' +import { useWidgetInteraction } from '../../hooks/useWidgetInteraction' export const ChatToggle = () => { const { t } = useTranslation('rooms') - const { dispatch, state } = useLayoutContext().widget - const tooltipLabel = state?.showChat ? 'open' : 'closed' - - const participantsSnap = useSnapshot(participantsStore) - const showParticipants = participantsSnap.showParticipants + const { isChatOpen, unreadMessages, toggleChat } = useWidgetInteraction() + const tooltipLabel = isChatOpen ? 'open' : 'closed' return (
{ legacyStyle aria-label={t(`controls.chat.${tooltipLabel}`)} tooltip={t(`controls.chat.${tooltipLabel}`)} - isSelected={state?.showChat} - onPress={() => { - if (showParticipants) participantsStore.showParticipants = false - if (dispatch) dispatch({ msg: 'toggle_chat' }) - }} + isSelected={isChatOpen} + onPress={() => toggleChat()} > - {!!state?.unreadMessages && ( + {!!unreadMessages && (
{ const { t } = useTranslation('rooms') - const { dispatch, state } = useLayoutContext().widget - /** * Context could not be used due to inconsistent refresh behavior. * The 'numParticipant' property on the room only updates when the room's metadata changes, @@ -19,10 +16,9 @@ export const ParticipantsToggle = () => { const participants = useParticipants() const numParticipants = participants?.length - const participantsSnap = useSnapshot(participantsStore) - const showParticipants = participantsSnap.showParticipants + const { isParticipantsOpen, toggleParticipants } = useWidgetInteraction() - const tooltipLabel = showParticipants ? 'open' : 'closed' + const tooltipLabel = isParticipantsOpen ? 'open' : 'closed' return (
{ legacyStyle aria-label={t(`controls.participants.${tooltipLabel}`)} tooltip={t(`controls.participants.${tooltipLabel}`)} - isSelected={showParticipants} - onPress={() => { - if (dispatch && state?.showChat) dispatch({ msg: 'toggle_chat' }) - participantsStore.showParticipants = !showParticipants - }} + isSelected={isParticipantsOpen} + onPress={() => toggleParticipants()} > diff --git a/src/frontend/src/features/rooms/livekit/hooks/useWidgetInteraction.ts b/src/frontend/src/features/rooms/livekit/hooks/useWidgetInteraction.ts new file mode 100644 index 00000000..09f92cab --- /dev/null +++ b/src/frontend/src/features/rooms/livekit/hooks/useWidgetInteraction.ts @@ -0,0 +1,34 @@ +import { useLayoutContext } from '@livekit/components-react' +import { useSnapshot } from 'valtio' +import { participantsStore } from '@/stores/participants.ts' + +export const useWidgetInteraction = () => { + const { dispatch, state } = useLayoutContext().widget + + const participantsSnap = useSnapshot(participantsStore) + const isParticipantsOpen = participantsSnap.showParticipants + + const toggleParticipants = () => { + if (dispatch && state?.showChat) { + dispatch({ msg: 'toggle_chat' }) + } + participantsStore.showParticipants = !isParticipantsOpen + } + + const toggleChat = () => { + if (isParticipantsOpen) { + participantsStore.showParticipants = false + } + if (dispatch) { + dispatch({ msg: 'toggle_chat' }) + } + } + + return { + toggleParticipants, + toggleChat, + isChatOpen: state?.showChat, + unreadMessages: state?.unreadMessages, + isParticipantsOpen, + } +}