Skip to content

Commit d7fb069

Browse files
chrisalloHoonBaek
andauthored
[CLNP-6072] useConnectionHandler (#1291)
https://sendbird.atlassian.net/browse/SBISSUE-18182 ### Checklist Put an `x` in the boxes that apply. You can also fill these out after creating the PR. If unsure, ask the members. This is a reminder of what we look for before merging your code. - [x] **All tests pass locally with my changes** - [ ] **I have added tests that prove my fix is effective or that my feature works** - [ ] **Public components / utils / props are appropriately exported** - [ ] I have added necessary documentation (if appropriate) ## External Contributions This project is not yet set up to accept pull requests from external contributors. If you have a pull request that you believe should be accepted, please contact the Developer Relations team <[email protected]> with details and we'll evaluate if we can set up a [CLA](https://en.wikipedia.org/wiki/Contributor_License_Agreement) to allow for the contribution. --------- Co-authored-by: HoonBaek <[email protected]>
1 parent 4595031 commit d7fb069

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/hooks/useConnectionState.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useState } from 'react';
2+
import { ConnectionState } from '@sendbird/chat';
3+
4+
import ConnectionHandler from '../lib/handlers/ConnectionHandler';
5+
import useSendbirdStateContext from './useSendbirdStateContext';
6+
import uuidv4 from '../utils/uuid';
7+
8+
export const useConnectionState = (): ConnectionState => {
9+
const { stores } = useSendbirdStateContext();
10+
const { sdkStore } = stores;
11+
const { sdk } = sdkStore;
12+
13+
const [connectionState, setConnectionState] = useState(sdk.connectionState);
14+
sdk.addConnectionHandler(uuidv4(), new ConnectionHandler({
15+
onConnected: () => setConnectionState(ConnectionState.OPEN),
16+
onDisconnected: () => setConnectionState(ConnectionState.CLOSED),
17+
onReconnectStarted: () => setConnectionState(ConnectionState.CONNECTING),
18+
onReconnectSucceeded: () => setConnectionState(ConnectionState.OPEN),
19+
onReconnectFailed: () => setConnectionState(ConnectionState.CLOSED),
20+
}));
21+
return connectionState;
22+
};

0 commit comments

Comments
 (0)