Skip to content

Commit

Permalink
[CLNP-6072] useConnectionHandler (#1291)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
chrisallo and HoonBaek authored Jan 8, 2025
1 parent 4595031 commit d7fb069
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/hooks/useConnectionState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useState } from 'react';
import { ConnectionState } from '@sendbird/chat';

import ConnectionHandler from '../lib/handlers/ConnectionHandler';
import useSendbirdStateContext from './useSendbirdStateContext';
import uuidv4 from '../utils/uuid';

export const useConnectionState = (): ConnectionState => {
const { stores } = useSendbirdStateContext();
const { sdkStore } = stores;
const { sdk } = sdkStore;

const [connectionState, setConnectionState] = useState(sdk.connectionState);
sdk.addConnectionHandler(uuidv4(), new ConnectionHandler({
onConnected: () => setConnectionState(ConnectionState.OPEN),
onDisconnected: () => setConnectionState(ConnectionState.CLOSED),
onReconnectStarted: () => setConnectionState(ConnectionState.CONNECTING),
onReconnectSucceeded: () => setConnectionState(ConnectionState.OPEN),
onReconnectFailed: () => setConnectionState(ConnectionState.CLOSED),
}));
return connectionState;
};

0 comments on commit d7fb069

Please sign in to comment.