blob: 1408953cdd602d2fb5ac9f6bbd348b3d8dca9289 [file] [log] [blame]
Issam E. Maghni09a3a1f2022-11-02 04:56:21 +00001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
idillona4b96ab2023-02-01 15:30:12 -050018import { useEffect, useMemo, useRef } from 'react';
Issam E. Maghni09a3a1f2022-11-02 04:56:21 +000019
idillona4b96ab2023-02-01 15:30:12 -050020import { createOptionalContext } from '../hooks/createOptionalContext';
idillondfb9f1f2023-01-23 10:32:51 -050021import { WebSocketClient } from '../services/WebSocketClient';
Issam E. Maghni09a3a1f2022-11-02 04:56:21 +000022import { apiUrl } from '../utils/constants';
23import { WithChildren } from '../utils/utils';
24import { useAuthContext } from './AuthProvider';
25
Issam E. Maghni0432cb72022-11-12 06:09:26 +000026export interface IWebSocketContext {
idillondfb9f1f2023-01-23 10:32:51 -050027 bind: WebSocketClient['bind'];
28 unbind: WebSocketClient['unbind'];
29 send: WebSocketClient['send'];
Issam E. Maghni09a3a1f2022-11-02 04:56:21 +000030}
31
idillona4b96ab2023-02-01 15:30:12 -050032const optionalWebSocketContext = createOptionalContext<IWebSocketContext>('WebSocketContext');
33export const useWebSocketContext = optionalWebSocketContext.useOptionalContext;
Issam E. Maghni09a3a1f2022-11-02 04:56:21 +000034
35export default ({ children }: WithChildren) => {
idillondfb9f1f2023-01-23 10:32:51 -050036 const webSocketClientRef = useRef<WebSocketClient>(new WebSocketClient());
Issam E. Maghni09a3a1f2022-11-02 04:56:21 +000037
38 const { token: accessToken } = useAuthContext();
39
idillondfb9f1f2023-01-23 10:32:51 -050040 useEffect(() => {
41 webSocketClientRef.current.connect(apiUrl, accessToken);
Issam E. Maghni0432cb72022-11-12 06:09:26 +000042 }, [accessToken]);
Issam E. Maghni09a3a1f2022-11-02 04:56:21 +000043
idillona4b96ab2023-02-01 15:30:12 -050044 const value = useMemo(
simon90efa042022-11-29 17:17:23 -050045 () => ({
idillondfb9f1f2023-01-23 10:32:51 -050046 bind: webSocketClientRef.current.bind.bind(webSocketClientRef.current),
47 unbind: webSocketClientRef.current.unbind.bind(webSocketClientRef.current),
48 send: webSocketClientRef.current.send.bind(webSocketClientRef.current),
simon90efa042022-11-29 17:17:23 -050049 }),
idillondfb9f1f2023-01-23 10:32:51 -050050 []
simon90efa042022-11-29 17:17:23 -050051 );
simonf929a362022-11-18 16:53:45 -050052
idillona4b96ab2023-02-01 15:30:12 -050053 return (
54 <optionalWebSocketContext.Context.Provider value={value}>{children}</optionalWebSocketContext.Context.Provider>
55 );
Issam E. Maghni09a3a1f2022-11-02 04:56:21 +000056};