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: handle switching the active account #2613

Merged
merged 19 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 16 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
39 changes: 34 additions & 5 deletions packages/mgt-chat/src/components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React, { useEffect, useState } from 'react';
import { ErrorBar, FluentThemeProvider, MessageThread, SendBox } from '@azure/communication-react';
import { Person, PersonCardInteraction, Spinner } from '@microsoft/mgt-react';
import { FluentTheme } from '@fluentui/react';
import { FluentTheme, MessageBarType } from '@fluentui/react';
import { FluentProvider, makeStyles, shorthands, teamsLightTheme } from '@fluentui/react-components';
import { useGraphChatClient } from '../../statefulClient/useGraphChatClient';
import ChatHeader from '../ChatHeader/ChatHeader';
import ChatMessageBar from '../ChatMessageBar/ChatMessageBar';
import { registerAppIcons } from '../styles/registerIcons';
import { ManageChatMembers } from '../ManageChatMembers/ManageChatMembers';
import { StatefulGraphChatClient } from 'src/statefulClient/StatefulGraphChatClient';

registerAppIcons();

Expand Down Expand Up @@ -34,24 +36,35 @@ const useStyles = makeStyles({
},
fullHeight: {
height: '100%'
},
spinner: {
justifyContent: 'center',
display: 'flex',
alignItems: 'center',
height: '100%'
}
});

export const Chat = ({ chatId }: IMgtChatProps) => {
const styles = useStyles();
const chatClient = useGraphChatClient(chatId);
const chatClient: StatefulGraphChatClient = useGraphChatClient(chatId);
const [chatState, setChatState] = useState(chatClient.getState());
useEffect(() => {
chatClient.onStateChange(setChatState);
return () => {
chatClient.offStateChange(setChatState);
};
}, [chatClient]);

const isLoading = ['creating server connections', 'subscribing to notifications', 'loading messages'].includes(
chatState.status
);

return (
<FluentThemeProvider fluentTheme={FluentTheme}>
<FluentProvider theme={teamsLightTheme} className={styles.fullHeight}>
<div className={styles.chat}>
{chatState.userId && chatState.messages.length > 0 ? (
{chatState.userId && chatId && chatState.messages.length > 0 ? (
<>
<ChatHeader
chat={chatState.chat}
Expand Down Expand Up @@ -96,8 +109,24 @@ export const Chat = ({ chatId }: IMgtChatProps) => {
</>
) : (
<>
{chatState.status}
<Spinner />
{isLoading && (
<div className={styles.spinner}>
<Spinner /> <br />
{chatState.status}
</div>
)}
{chatState.status === 'initial' && (
musale marked this conversation as resolved.
Show resolved Hide resolved
<ChatMessageBar messageBarType={MessageBarType.info} message={'Select a chat to display messages.'} />
musale marked this conversation as resolved.
Show resolved Hide resolved
)}
{chatState.status === 'no messages' && (
<ChatMessageBar
messageBarType={MessageBarType.error}
message={`No messages were found for the id ${chatId}.`}
/>
)}
{chatState.status === 'no chat id' && (
<ChatMessageBar messageBarType={MessageBarType.error} message={'A valid chat id is required.'} />
)}
</>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';

import { MessageBar, MessageBarType } from '@fluentui/react';

interface ChatMessageBarProps {
messageBarType: MessageBarType;
message: string;
}

const ChatMessageBar = ({ messageBarType, message }: ChatMessageBarProps) => {
return <MessageBar messageBarType={messageBarType}> {message}</MessageBar>;
};

export default ChatMessageBar;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { buttonIconStyles } from './common.styles';

const registerAppIcons = () => {
const icons = Object.assign(DEFAULT_COMPONENT_ICONS, {
// TODO: Register the info and errorbadge icons
'add-friend': (
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,25 @@ export class GraphNotificationClient {
}
}

public async closeSignalRConnection() {
if (this.connection && this.connection?.state === signalR.HubConnectionState.Connected) {
await this.connection?.stop();
}
}

public async reConnectSignalR() {
if (!this.connection) await this.createSignalConnection();
if (this.connection?.state === signalR.HubConnectionState.Disconnected) {
await this.connection?.start();
}
}

private async subscribeToResource(
resourcePath: string,
eventEmitter: ThreadEventEmitter,
changeTypes: ChangeTypes[] = ['created', 'updated', 'deleted']
) {
if (!this.connection) throw new Error('SignalR connection not initialized');
if (!this.connection) throw new Error('subscribeToResource: SignalR connection is not initialized');

const token = await this.getToken();

Expand Down
Loading
Loading