Skip to content

armgit / meet

Public Imported from GitHub
0

Open source video conferencing app powered by LiveKit. Built with Django and React.

♻️(frontend) extract duplicated logic into reusable hook

lebaudantoinee21858febeb1
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 (
<div
@@ -27,15 +22,12 @@ export const ChatToggle = () => {
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()}
>
<RiChat1Line />
</ToggleButton>
- {!!state?.unreadMessages && (
+ {!!unreadMessages && (
<div
className={css({
position: 'absolute',
diff --git a/src/frontend/src/features/rooms/livekit/components/controls/Participants/ParticipantsToggle.tsx b/src/frontend/src/features/rooms/livekit/components/controls/Participants/ParticipantsToggle.tsx
index 286c669d..9257d5f4 100644
--- a/src/frontend/src/features/rooms/livekit/components/controls/Participants/ParticipantsToggle.tsx
+++ b/src/frontend/src/features/rooms/livekit/components/controls/Participants/ParticipantsToggle.tsx
@@ -2,15 +2,12 @@ import { useTranslation } from 'react-i18next'
import { RiGroupLine, RiInfinityLine } from '@remixicon/react'
import { ToggleButton } from '@/primitives'
import { css } from '@/styled-system/css'
-import { useLayoutContext, useParticipants } from '@livekit/components-react'
-import { useSnapshot } from 'valtio'
-import { participantsStore } from '@/stores/participants'
+import { useParticipants } from '@livekit/components-react'
+import { useWidgetInteraction } from '../../../hooks/useWidgetInteraction'
export const ParticipantsToggle = () => {
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 (
<div
@@ -36,11 +32,8 @@ export const ParticipantsToggle = () => {
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()}
>
<RiGroupLine />
</ToggleButton>
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,
+ }
+}