main

mattermost/focalboard

Last updated at: 29/12/2023 09:44

websockets.tsx

TLDR

This file provides a custom hook useWebsockets that can be used to subscribe and unsubscribe to websockets for a specific team.

Methods

None

Classes

None

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {useEffect} from 'react'

import wsClient, {WSClient} from '../wsclient'

export const useWebsockets = (teamId: string, fn: (wsClient: WSClient) => () => void, deps: any[] = []): void => {
    useEffect(() => {
        if (!teamId) {
            return () => {}
        }

        wsClient.subscribeToTeam(teamId)
        const teardown = fn(wsClient)

        return () => {
            teardown()
            wsClient.unsubscribeToTeam(teamId)
        }
    }, [teamId, ...deps])
}