Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from DevClad-Inc/messages
Messages
- Loading branch information
Showing
9 changed files
with
415 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import React, { createContext } from 'react'; | ||
import { StreamChat } from 'stream-chat'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import toast from 'react-hot-toast'; | ||
import { useAuth } from '@/services/useAuth.services'; | ||
import { initialProfileState, Profile } from '@/lib/InterfacesStates.lib'; | ||
import { profileQuery } from '@/lib/queriesAndLoaders'; | ||
import { Error } from '@/components/Feedback'; | ||
|
||
interface StreamProviderProps { | ||
children?: React.ReactNode; | ||
} | ||
|
||
interface StreamContextInterface { | ||
client: StreamChat; | ||
connected: boolean; | ||
toggleConnection: (connected: boolean) => void; | ||
} | ||
|
||
export const StreamContext = createContext({} as StreamContextInterface); | ||
|
||
export function StreamProvider({ children }: StreamProviderProps) { | ||
const client = StreamChat.getInstance(import.meta.env.VITE_STREAM_API_KEY as string); | ||
const [connected, setConnected] = React.useState(false); | ||
const { loggedInUser, streamToken } = useAuth(); | ||
let profileData: Profile = { ...initialProfileState }; | ||
const { data: profileQueryData, isSuccess: profileQuerySuccess } = useQuery({ | ||
...profileQuery(), | ||
}); | ||
if (profileQuerySuccess && profileQueryData !== null) { | ||
profileData = profileQueryData.data; | ||
} | ||
|
||
if (loggedInUser && streamToken && !connected) { | ||
const connect = async () => { | ||
setConnected(true); | ||
await client | ||
.connectUser( | ||
{ | ||
id: streamToken?.uid as string, | ||
first_name: loggedInUser.first_name as string, | ||
last_name: loggedInUser.last_name as string, | ||
username: loggedInUser.username, | ||
email: loggedInUser.email, | ||
image: profileData.avatar as string, | ||
}, | ||
streamToken?.token as string | ||
) | ||
.then(() => { | ||
// console.log('connected to stream'); | ||
}) | ||
.catch(() => { | ||
// console.log(err); | ||
setConnected(false); | ||
toast.custom(<Error error="Cannot connect to Stream. Try refreshing the page." />, { | ||
id: 'stream-connect-error', | ||
}); | ||
}); | ||
}; | ||
if (connected === false) { | ||
connect(); | ||
} | ||
} | ||
|
||
const toggleConnection = (conn: boolean) => { | ||
setConnected(conn); | ||
}; | ||
const value = React.useMemo(() => ({ client, connected, toggleConnection }), [client, connected]); | ||
return <StreamContext.Provider value={value}>{children}</StreamContext.Provider>; | ||
} | ||
|
||
StreamProvider.defaultProps = { | ||
children: null, | ||
}; | ||
|
||
export const useStreamContext = () => { | ||
const context = React.useContext(StreamContext); | ||
return context; | ||
}; | ||
|
||
export default StreamProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
feea59e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
devclad-web – ./apps/web
devclad-client-landing.vercel.app
devclad-web-git-main-devclad.vercel.app
devclad-web-devclad.vercel.app