Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce useCreateChatClient hook #1916

Merged
merged 3 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/components/Chat/hooks/useCreateChatClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useEffect, useState } from 'react';

import {
DefaultGenerics,
ExtendableGenerics,
OwnUserResponse,
StreamChat,
TokenOrProvider,
UserResponse,
} from 'stream-chat';

/**
* React hook to create, connect and return `StreamChat` client.
*/
export const useCreateChatClient = <SCG extends ExtendableGenerics = DefaultGenerics>({
apiKey,
tokenOrProvider,
userData,

Check warning on line 18 in src/components/Chat/hooks/useCreateChatClient.ts

View check run for this annotation

Codecov / codecov/patch

src/components/Chat/hooks/useCreateChatClient.ts#L16-L18

Added lines #L16 - L18 were not covered by tests
}: {
apiKey: string;
tokenOrProvider: TokenOrProvider;
userData: OwnUserResponse<SCG> | UserResponse<SCG>;
}) => {
const [chatClient, setChatClient] = useState<StreamChat<SCG> | null>(null);
const [cachedUserData, setCachedUserData] = useState(userData);

Check warning on line 25 in src/components/Chat/hooks/useCreateChatClient.ts

View check run for this annotation

Codecov / codecov/patch

src/components/Chat/hooks/useCreateChatClient.ts#L24-L25

Added lines #L24 - L25 were not covered by tests

if (userData.id !== cachedUserData.id) {
setCachedUserData(userData);

Check warning on line 28 in src/components/Chat/hooks/useCreateChatClient.ts

View check run for this annotation

Codecov / codecov/patch

src/components/Chat/hooks/useCreateChatClient.ts#L28

Added line #L28 was not covered by tests
}

useEffect(() => {
const client = new StreamChat<SCG>(apiKey);
let didUserConnectInterrupt = false;

Check warning on line 33 in src/components/Chat/hooks/useCreateChatClient.ts

View check run for this annotation

Codecov / codecov/patch

src/components/Chat/hooks/useCreateChatClient.ts#L31-L33

Added lines #L31 - L33 were not covered by tests

const connectionPromise = client.connectUser(cachedUserData, tokenOrProvider).then(() => {

Check warning on line 35 in src/components/Chat/hooks/useCreateChatClient.ts

View check run for this annotation

Codecov / codecov/patch

src/components/Chat/hooks/useCreateChatClient.ts#L35

Added line #L35 was not covered by tests
if (!didUserConnectInterrupt) setChatClient(client);
});

return () => {
didUserConnectInterrupt = true;
setChatClient(null);
connectionPromise
.then(() => client.disconnectUser())

Check warning on line 43 in src/components/Chat/hooks/useCreateChatClient.ts

View check run for this annotation

Codecov / codecov/patch

src/components/Chat/hooks/useCreateChatClient.ts#L39-L43

Added lines #L39 - L43 were not covered by tests
.then(() => {
console.log(`Connection for user "${cachedUserData.id}" has been closed`);

Check warning on line 45 in src/components/Chat/hooks/useCreateChatClient.ts

View check run for this annotation

Codecov / codecov/patch

src/components/Chat/hooks/useCreateChatClient.ts#L45

Added line #L45 was not covered by tests
});
};
}, [apiKey, cachedUserData, tokenOrProvider]);

return chatClient;

Check warning on line 50 in src/components/Chat/hooks/useCreateChatClient.ts

View check run for this annotation

Codecov / codecov/patch

src/components/Chat/hooks/useCreateChatClient.ts#L50

Added line #L50 was not covered by tests
};
1 change: 1 addition & 0 deletions src/components/Chat/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './Chat';
export * from './hooks/useChat';
export * from './hooks/useCustomStyles';
export * from './hooks/useCreateChatClient';
Loading