Skip to content

Commit

Permalink
Remove generics from examples
Browse files Browse the repository at this point in the history
  • Loading branch information
arnautov-anton committed Feb 5, 2025
1 parent 902ddf4 commit 93b9d95
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 110 deletions.
22 changes: 2 additions & 20 deletions examples/capacitor/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,11 @@ const apiKey = process.env.REACT_APP_STREAM_KEY as string;
const userId = process.env.REACT_APP_USER_ID as string;
const userToken = process.env.REACT_APP_USER_TOKEN as string;

const filters: ChannelFilters = { type: 'messaging', members: {$in: [userId]} };
const filters: ChannelFilters = { type: 'messaging', members: { $in: [userId] } };
const options: ChannelOptions = { state: true, presence: true, limit: 10 };
const sort: ChannelSort = { last_message_at: -1, updated_at: -1 };

type LocalAttachmentType = Record<string, unknown>;
type LocalChannelType = Record<string, unknown>;
type LocalCommandType = string;
type LocalEventType = Record<string, unknown>;
type LocalMessageType = Record<string, unknown>;
type LocalReactionType = Record<string, unknown>;
type LocalUserType = Record<string, unknown>;

type StreamChatGenerics = {
attachmentType: LocalAttachmentType;
channelType: LocalChannelType;
commandType: LocalCommandType;
eventType: LocalEventType;
messageType: LocalMessageType;
reactionType: LocalReactionType;
userType: LocalUserType;
};

const chatClient = StreamChat.getInstance<StreamChatGenerics>(apiKey);
const chatClient = StreamChat.getInstance(apiKey);

if (process.env.REACT_APP_CHAT_SERVER_ENDPOINT) {
chatClient.setBaseURL(process.env.REACT_APP_CHAT_SERVER_ENDPOINT);
Expand Down
30 changes: 3 additions & 27 deletions examples/typescript/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
Window,
} from 'stream-chat-react';

const params = (new Proxy(new URLSearchParams(window.location.search), {
const params = new Proxy(new URLSearchParams(window.location.search), {
get: (searchParams, property) => searchParams.get(property as string),
}) as unknown) as Record<string, string | null>;
}) as unknown as Record<string, string | null>;

const apiKey = process.env.REACT_APP_STREAM_KEY as string;
const userId = params.uid || (process.env.REACT_APP_USER_ID as string);
Expand All @@ -23,31 +23,7 @@ const filters: ChannelFilters = { type: 'messaging', members: { $in: [userId] }
const options: ChannelOptions = { state: true, presence: true, limit: 10 };
const sort: ChannelSort = { last_message_at: -1, updated_at: -1 };

type LocalAttachmentType = Record<string, unknown>;
type LocalChannelType = Record<string, unknown>;
type LocalCommandType = string;
type LocalEventType = Record<string, unknown>;
type LocalMemberType = Record<string, unknown>;
type LocalMessageType = Record<string, unknown>;
type LocalPollOptionType = Record<string, unknown>;
type LocalPollType = Record<string, unknown>;
type LocalReactionType = Record<string, unknown>;
type LocalUserType = Record<string, unknown>;

type StreamChatGenerics = {
attachmentType: LocalAttachmentType;
channelType: LocalChannelType;
commandType: LocalCommandType;
eventType: LocalEventType;
memberType: LocalMemberType;
messageType: LocalMessageType;
pollOptionType: LocalPollOptionType;
pollType: LocalPollType;
reactionType: LocalReactionType;
userType: LocalUserType;
};

const chatClient = StreamChat.getInstance<StreamChatGenerics>(apiKey);
const chatClient = StreamChat.getInstance(apiKey);

if (process.env.REACT_APP_CHAT_SERVER_ENDPOINT) {
chatClient.setBaseURL(process.env.REACT_APP_CHAT_SERVER_ENDPOINT);
Expand Down
30 changes: 3 additions & 27 deletions examples/vite/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import {
ChatView,
} from 'stream-chat-react';

const params = (new Proxy(new URLSearchParams(window.location.search), {
const params = new Proxy(new URLSearchParams(window.location.search), {
get: (searchParams, property) => searchParams.get(property as string),
}) as unknown) as Record<string, string | null>;
}) as unknown as Record<string, string | null>;

const parseUserIdFromToken = (token: string) => {
const [, payload] = token.split('.');
Expand All @@ -39,32 +39,8 @@ const filters: ChannelFilters = {
const options: ChannelOptions = { limit: 5, presence: true, state: true };
const sort: ChannelSort = { pinned_at: 1, last_message_at: -1, updated_at: -1 };

type LocalAttachmentType = Record<string, unknown>;
type LocalChannelType = Record<string, unknown>;
type LocalCommandType = string;
type LocalEventType = Record<string, unknown>;
type LocalMemberType = Record<string, unknown>;
type LocalMessageType = Record<string, unknown>;
type LocalPollOptionType = Record<string, unknown>;
type LocalPollType = Record<string, unknown>;
type LocalReactionType = Record<string, unknown>;
type LocalUserType = Record<string, unknown>;

type StreamChatGenerics = {
attachmentType: LocalAttachmentType;
channelType: LocalChannelType;
commandType: LocalCommandType;
eventType: LocalEventType;
memberType: LocalMemberType;
messageType: LocalMessageType;
pollOptionType: LocalPollOptionType;
pollType: LocalPollType;
reactionType: LocalReactionType;
userType: LocalUserType;
};

const App = () => {
const chatClient = useCreateChatClient<StreamChatGenerics>({
const chatClient = useCreateChatClient({
apiKey,
tokenOrProvider: userToken,
userData: { id: userId },
Expand Down
43 changes: 7 additions & 36 deletions src/stories/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,56 +1,31 @@
import React, { PropsWithChildren, useEffect, useState } from 'react';
import { Chat } from '../';
import {
DefaultGenerics,
ExtendableGenerics,
OwnUserResponse,
StreamChat,
TokenOrProvider,
UserResponse,
} from 'stream-chat';
import { OwnUserResponse, StreamChat, TokenOrProvider, UserResponse } from 'stream-chat';

const appKey = import.meta.env.E2E_APP_KEY;
if (!appKey || typeof appKey !== 'string') {
throw new Error('expected APP_KEY');
}
export const streamAPIKey = appKey;

type LocalAttachmentType = Record<string, unknown>;
type LocalChannelType = Record<string, unknown>;
type LocalCommandType = string;
type LocalEventType = Record<string, unknown>;
type LocalMessageType = Record<string, unknown>;
type LocalReactionType = Record<string, unknown>;
type LocalUserType = Record<string, unknown>;

export type StreamChatGenerics = {
attachmentType: LocalAttachmentType;
channelType: LocalChannelType;
commandType: LocalCommandType;
eventType: LocalEventType;
messageType: LocalMessageType;
reactionType: LocalReactionType;
userType: LocalUserType;
};

export type ConnectedUserProps = PropsWithChildren<{
token: string;
userId: string;
}>;

const useClient = <SCG extends ExtendableGenerics = DefaultGenerics>({
const useClient = ({
apiKey,
tokenOrProvider,
userData,
}: {
apiKey: string;
tokenOrProvider: TokenOrProvider;
userData: OwnUserResponse<SCG> | UserResponse<SCG>;
userData: OwnUserResponse | UserResponse;
}) => {
const [chatClient, setChatClient] = useState<StreamChat<SCG> | null>(null);
const [chatClient, setChatClient] = useState<StreamChat | null>(null);

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

let didUserConnectInterrupt = false;
const connectionPromise = client.connectUser(userData, tokenOrProvider).then(() => {
Expand All @@ -72,12 +47,8 @@ const useClient = <SCG extends ExtendableGenerics = DefaultGenerics>({
return chatClient;
};

export const ConnectedUser = <SCG extends DefaultGenerics = StreamChatGenerics>({
children,
token,
userId,
}: ConnectedUserProps) => {
const client = useClient<SCG>({
export const ConnectedUser = ({ children, token, userId }: ConnectedUserProps) => {
const client = useClient({
apiKey: streamAPIKey,
tokenOrProvider: token,
userData: { id: userId },
Expand Down

0 comments on commit 93b9d95

Please sign in to comment.