2022-05-03 02:45:22 +02:00
|
|
|
import { useEffect, useLayoutEffect } from 'react';
|
|
|
|
import { atom, useRecoilState, useSetRecoilState } from 'recoil';
|
2022-04-28 08:19:20 +02:00
|
|
|
import { makeEmptyClientConfig, ClientConfig } from '../../interfaces/client-config.model';
|
|
|
|
import ClientConfigService from '../../services/client-config-service';
|
2022-04-30 00:09:53 +02:00
|
|
|
import ChatService from '../../services/chat-service';
|
2022-05-03 02:45:22 +02:00
|
|
|
import WebsocketService from '../../services/websocket-service';
|
2022-04-28 23:36:05 +02:00
|
|
|
import { ChatMessage } from '../../interfaces/chat-message.model';
|
2022-04-30 00:09:53 +02:00
|
|
|
import { getLocalStorage, setLocalStorage } from '../../utils/helpers';
|
2022-05-03 02:45:22 +02:00
|
|
|
import {
|
|
|
|
AppState,
|
|
|
|
ChatState,
|
|
|
|
ChatVisibilityState,
|
|
|
|
getChatState,
|
|
|
|
getChatVisibilityState,
|
|
|
|
} from '../../interfaces/application-state';
|
2022-04-26 08:10:07 +02:00
|
|
|
|
2022-04-28 08:19:20 +02:00
|
|
|
// The config that comes from the API.
|
2022-05-03 02:45:22 +02:00
|
|
|
export const clientConfigStateAtom = atom({
|
2022-04-26 08:10:07 +02:00
|
|
|
key: 'clientConfigState',
|
|
|
|
default: makeEmptyClientConfig(),
|
|
|
|
});
|
|
|
|
|
2022-05-03 02:45:22 +02:00
|
|
|
export const appStateAtom = atom<AppState>({
|
|
|
|
key: 'appStateAtom',
|
|
|
|
default: AppState.Loading,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const chatStateAtom = atom<ChatState>({
|
|
|
|
key: 'chatStateAtom',
|
|
|
|
default: ChatState.Offline,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const chatVisibilityAtom = atom<ChatVisibilityState>({
|
2022-04-30 00:09:53 +02:00
|
|
|
key: 'chatVisibility',
|
2022-05-03 02:45:22 +02:00
|
|
|
default: ChatVisibilityState.Visible,
|
2022-04-28 08:19:20 +02:00
|
|
|
});
|
|
|
|
|
2022-05-03 02:45:22 +02:00
|
|
|
export const chatDisplayNameAtom = atom<string>({
|
2022-04-28 08:19:20 +02:00
|
|
|
key: 'chatDisplayName',
|
2022-04-30 00:09:53 +02:00
|
|
|
default: null,
|
|
|
|
});
|
|
|
|
|
2022-05-03 02:45:22 +02:00
|
|
|
export const accessTokenAtom = atom<string>({
|
|
|
|
key: 'accessTokenAtom',
|
2022-04-30 00:09:53 +02:00
|
|
|
default: null,
|
2022-04-28 08:19:20 +02:00
|
|
|
});
|
|
|
|
|
2022-05-03 02:45:22 +02:00
|
|
|
export const chatMessagesAtom = atom<ChatMessage[]>({
|
2022-04-28 23:36:05 +02:00
|
|
|
key: 'chatMessages',
|
|
|
|
default: [] as ChatMessage[],
|
|
|
|
});
|
|
|
|
|
2022-04-30 00:09:53 +02:00
|
|
|
export function ClientConfigStore() {
|
2022-05-03 02:45:22 +02:00
|
|
|
const setClientConfig = useSetRecoilState<ClientConfig>(clientConfigStateAtom);
|
|
|
|
const [appState, setAppState] = useRecoilState<AppState>(appStateAtom);
|
|
|
|
const setChatVisibility = useSetRecoilState<ChatVisibilityState>(chatVisibilityAtom);
|
|
|
|
const [chatState, setChatState] = useRecoilState<ChatState>(chatStateAtom);
|
|
|
|
const setChatMessages = useSetRecoilState<ChatMessage[]>(chatMessagesAtom);
|
2022-04-30 00:09:53 +02:00
|
|
|
const [accessToken, setAccessToken] = useRecoilState<string>(accessTokenAtom);
|
2022-05-03 02:45:22 +02:00
|
|
|
const setChatDisplayName = useSetRecoilState<string>(chatDisplayNameAtom);
|
2022-04-26 08:10:07 +02:00
|
|
|
|
|
|
|
const updateClientConfig = async () => {
|
|
|
|
try {
|
|
|
|
const config = await ClientConfigService.getConfig();
|
2022-05-03 02:45:22 +02:00
|
|
|
// console.log(`ClientConfig: ${JSON.stringify(config)}`);
|
2022-04-26 08:10:07 +02:00
|
|
|
setClientConfig(config);
|
2022-05-03 02:45:22 +02:00
|
|
|
setAppState(AppState.Online);
|
2022-04-26 08:10:07 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.error(`ClientConfigService -> getConfig() ERROR: \n${error}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-03 02:45:22 +02:00
|
|
|
const handleUserRegistration = async (optionalDisplayName?: string) => {
|
2022-04-30 00:09:53 +02:00
|
|
|
try {
|
2022-05-03 02:45:22 +02:00
|
|
|
setAppState(AppState.Registering);
|
2022-04-30 00:09:53 +02:00
|
|
|
const response = await ChatService.registerUser(optionalDisplayName);
|
2022-05-03 02:45:22 +02:00
|
|
|
console.log(`ChatService -> registerUser() response: \n${response}`);
|
|
|
|
const { accessToken: newAccessToken, displayName: newDisplayName } = response;
|
2022-04-30 00:09:53 +02:00
|
|
|
if (!newAccessToken) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-03 02:45:22 +02:00
|
|
|
console.log('setting access token', newAccessToken);
|
|
|
|
setAccessToken(newAccessToken);
|
|
|
|
// setLocalStorage('accessToken', newAccessToken);
|
|
|
|
setChatDisplayName(newDisplayName);
|
|
|
|
setAppState(AppState.Online);
|
2022-04-30 00:09:53 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error(`ChatService -> registerUser() ERROR: \n${e}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getChatHistory = async () => {
|
2022-05-03 02:45:22 +02:00
|
|
|
setChatState(ChatState.Loading);
|
2022-04-30 00:09:53 +02:00
|
|
|
try {
|
|
|
|
const messages = await ChatService.getChatHistory(accessToken);
|
2022-05-03 02:45:22 +02:00
|
|
|
// console.log(`ChatService -> getChatHistory() messages: \n${JSON.stringify(messages)}`);
|
2022-04-30 00:09:53 +02:00
|
|
|
setChatMessages(messages);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`ChatService -> getChatHistory() ERROR: \n${error}`);
|
|
|
|
}
|
2022-05-03 02:45:22 +02:00
|
|
|
setChatState(ChatState.Available);
|
2022-04-30 00:09:53 +02:00
|
|
|
};
|
|
|
|
|
2022-04-26 08:10:07 +02:00
|
|
|
useEffect(() => {
|
|
|
|
updateClientConfig();
|
2022-04-30 00:09:53 +02:00
|
|
|
handleUserRegistration();
|
2022-04-26 08:10:07 +02:00
|
|
|
}, []);
|
|
|
|
|
2022-05-03 02:45:22 +02:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (!accessToken) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('access token changed', accessToken);
|
2022-04-30 00:09:53 +02:00
|
|
|
getChatHistory();
|
|
|
|
}, [accessToken]);
|
|
|
|
|
2022-05-03 02:45:22 +02:00
|
|
|
useEffect(() => {
|
|
|
|
const updatedChatState = getChatState(appState);
|
|
|
|
setChatState(updatedChatState);
|
|
|
|
const updatedChatVisibility = getChatVisibilityState(appState);
|
|
|
|
console.log(
|
|
|
|
'app state: ',
|
|
|
|
AppState[appState],
|
|
|
|
'chat state:',
|
|
|
|
ChatState[updatedChatState],
|
|
|
|
'chat visibility:',
|
|
|
|
ChatVisibilityState[updatedChatVisibility],
|
|
|
|
);
|
|
|
|
setChatVisibility(updatedChatVisibility);
|
|
|
|
}, [appState]);
|
|
|
|
|
2022-04-26 08:10:07 +02:00
|
|
|
return null;
|
|
|
|
}
|