2022-05-13 23:44:16 +02:00
|
|
|
import { useEffect } from 'react';
|
2022-05-26 05:38:40 +02:00
|
|
|
import { atom, selector, useRecoilState, useSetRecoilState } from 'recoil';
|
|
|
|
import { useMachine } from '@xstate/react';
|
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-05-13 23:44:16 +02:00
|
|
|
import { ServerStatus, makeEmptyServerStatus } from '../../interfaces/server-status.model';
|
2022-05-26 05:38:40 +02:00
|
|
|
import appStateModel, {
|
|
|
|
AppStateEvent,
|
|
|
|
AppStateOptions,
|
|
|
|
makeEmptyAppState,
|
|
|
|
} from './application-state';
|
|
|
|
import { setLocalStorage, getLocalStorage } from '../../utils/helpers';
|
2022-05-03 07:13:36 +02:00
|
|
|
import {
|
|
|
|
ConnectedClientInfoEvent,
|
2022-05-03 23:17:05 +02:00
|
|
|
MessageType,
|
2022-05-03 07:13:36 +02:00
|
|
|
ChatEvent,
|
2022-05-12 08:31:31 +02:00
|
|
|
SocketEvent,
|
2022-05-03 07:13:36 +02:00
|
|
|
} from '../../interfaces/socket-events';
|
2022-05-26 05:38:40 +02:00
|
|
|
|
2022-05-03 07:13:36 +02:00
|
|
|
import handleChatMessage from './eventhandlers/handleChatMessage';
|
2022-05-26 05:38:40 +02:00
|
|
|
import handleConnectedClientInfoMessage from './eventhandlers/connected-client-info-handler';
|
2022-05-13 23:44:16 +02:00
|
|
|
import ServerStatusService from '../../services/status-service';
|
|
|
|
|
2022-05-26 05:38:40 +02:00
|
|
|
const SERVER_STATUS_POLL_DURATION = 5000;
|
|
|
|
const ACCESS_TOKEN_KEY = 'accessToken';
|
|
|
|
|
2022-05-13 23:44:16 +02:00
|
|
|
// Server status is what gets updated such as viewer count, durations,
|
|
|
|
// stream title, online/offline state, etc.
|
|
|
|
export const serverStatusState = atom<ServerStatus>({
|
|
|
|
key: 'serverStatusState',
|
|
|
|
default: makeEmptyServerStatus(),
|
|
|
|
});
|
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 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-05-05 01:55:54 +02:00
|
|
|
export const websocketServiceAtom = atom<WebsocketService>({
|
|
|
|
key: 'websocketServiceAtom',
|
|
|
|
default: null,
|
|
|
|
});
|
|
|
|
|
2022-05-26 05:38:40 +02:00
|
|
|
export const appStateAtom = atom<AppStateOptions>({
|
|
|
|
key: 'appState',
|
|
|
|
default: makeEmptyAppState(),
|
|
|
|
});
|
|
|
|
|
|
|
|
export const chatVisibleToggleAtom = atom<boolean>({
|
|
|
|
key: 'chatVisibilityToggleAtom',
|
|
|
|
default: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const isVideoPlayingAtom = atom<boolean>({
|
|
|
|
key: 'isVideoPlayingAtom',
|
|
|
|
default: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Chat is visible if the user wishes it to be visible AND the required
|
|
|
|
// chat state is set.
|
|
|
|
export const isChatVisibleSelector = selector({
|
|
|
|
key: 'isChatVisibleSelector',
|
|
|
|
get: ({ get }) => {
|
|
|
|
const state: AppStateOptions = get(appStateAtom);
|
|
|
|
const userVisibleToggle: boolean = get(chatVisibleToggleAtom);
|
|
|
|
const accessToken: String = get(accessTokenAtom);
|
|
|
|
return accessToken && state.chatAvailable && userVisibleToggle;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-05-26 20:08:37 +02:00
|
|
|
export const isChatAvailableSelector = selector({
|
|
|
|
key: 'isChatAvailableSelector',
|
|
|
|
get: ({ get }) => {
|
|
|
|
const state: AppStateOptions = get(appStateAtom);
|
|
|
|
const accessToken: String = get(accessTokenAtom);
|
|
|
|
return accessToken && state.chatAvailable;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-05-26 05:38:40 +02:00
|
|
|
// We display in an "online/live" state as long as video is actively playing.
|
|
|
|
// Even during the time where technically the server has said it's no longer
|
|
|
|
// live, however the last few seconds of video playback is still taking place.
|
|
|
|
export const isOnlineSelector = selector({
|
|
|
|
key: 'isOnlineSelector',
|
|
|
|
get: ({ get }) => {
|
|
|
|
const state: AppStateOptions = get(appStateAtom);
|
|
|
|
const isVideoPlaying: boolean = get(isVideoPlayingAtom);
|
|
|
|
return state.videoAvailable || isVideoPlaying;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Take a nested object of state metadata and merge it into
|
|
|
|
// a single flattened node.
|
|
|
|
function mergeMeta(meta) {
|
|
|
|
return Object.keys(meta).reduce((acc, key) => {
|
|
|
|
const value = meta[key];
|
|
|
|
Object.assign(acc, value);
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2022-04-30 00:09:53 +02:00
|
|
|
export function ClientConfigStore() {
|
2022-05-26 05:38:40 +02:00
|
|
|
const [appState, appStateSend, appStateService] = useMachine(appStateModel);
|
|
|
|
|
|
|
|
const setChatDisplayName = useSetRecoilState<string>(chatDisplayNameAtom);
|
2022-05-03 02:45:22 +02:00
|
|
|
const setClientConfig = useSetRecoilState<ClientConfig>(clientConfigStateAtom);
|
2022-05-13 23:44:16 +02:00
|
|
|
const setServerStatus = useSetRecoilState<ServerStatus>(serverStatusState);
|
2022-05-03 22:01:50 +02:00
|
|
|
const [chatMessages, setChatMessages] = useRecoilState<ChatMessage[]>(chatMessagesAtom);
|
2022-05-03 07:13:36 +02:00
|
|
|
const [accessToken, setAccessToken] = useRecoilState<string>(accessTokenAtom);
|
2022-05-26 05:38:40 +02:00
|
|
|
const setAppState = useSetRecoilState<AppStateOptions>(appStateAtom);
|
2022-05-03 07:13:36 +02:00
|
|
|
|
2022-05-26 05:38:40 +02:00
|
|
|
const setWebsocketService = useSetRecoilState<WebsocketService>(websocketServiceAtom);
|
2022-05-05 08:06:35 +02:00
|
|
|
let ws: WebsocketService;
|
2022-04-26 08:10:07 +02:00
|
|
|
|
2022-05-26 05:38:40 +02:00
|
|
|
const sendEvent = (event: string) => {
|
|
|
|
// console.log('---- sending event:', event);
|
|
|
|
appStateSend({ type: event });
|
|
|
|
};
|
|
|
|
|
2022-04-26 08:10:07 +02:00
|
|
|
const updateClientConfig = async () => {
|
|
|
|
try {
|
|
|
|
const config = await ClientConfigService.getConfig();
|
|
|
|
setClientConfig(config);
|
2022-05-26 05:38:40 +02:00
|
|
|
sendEvent('LOADED');
|
2022-04-26 08:10:07 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.error(`ClientConfigService -> getConfig() ERROR: \n${error}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-13 23:44:16 +02:00
|
|
|
const updateServerStatus = async () => {
|
|
|
|
try {
|
|
|
|
const status = await ServerStatusService.getStatus();
|
|
|
|
setServerStatus(status);
|
2022-05-26 05:38:40 +02:00
|
|
|
|
2022-05-13 23:44:16 +02:00
|
|
|
if (status.online) {
|
2022-05-26 05:38:40 +02:00
|
|
|
sendEvent(AppStateEvent.Online);
|
|
|
|
} else if (!status.online) {
|
|
|
|
sendEvent(AppStateEvent.Offline);
|
2022-05-13 23:44:16 +02:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2022-05-26 05:38:40 +02:00
|
|
|
sendEvent(AppStateEvent.Fail);
|
2022-05-13 23:44:16 +02:00
|
|
|
console.error(`serverStatusState -> getStatus() ERROR: \n${error}`);
|
|
|
|
}
|
2022-05-26 05:38:40 +02:00
|
|
|
return null;
|
2022-05-13 23:44:16 +02:00
|
|
|
};
|
|
|
|
|
2022-05-03 02:45:22 +02:00
|
|
|
const handleUserRegistration = async (optionalDisplayName?: string) => {
|
2022-05-26 05:38:40 +02:00
|
|
|
const savedAccessToken = getLocalStorage(ACCESS_TOKEN_KEY);
|
|
|
|
if (savedAccessToken) {
|
|
|
|
setAccessToken(savedAccessToken);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-30 00:09:53 +02:00
|
|
|
try {
|
2022-05-26 05:38:40 +02:00
|
|
|
sendEvent(AppStateEvent.NeedsRegister);
|
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-26 05:38:40 +02:00
|
|
|
|
2022-05-03 02:45:22 +02:00
|
|
|
console.log('setting access token', newAccessToken);
|
|
|
|
setAccessToken(newAccessToken);
|
2022-05-26 05:38:40 +02:00
|
|
|
setLocalStorage(ACCESS_TOKEN_KEY, newAccessToken);
|
2022-05-03 02:45:22 +02:00
|
|
|
setChatDisplayName(newDisplayName);
|
2022-04-30 00:09:53 +02:00
|
|
|
} catch (e) {
|
2022-05-26 05:38:40 +02:00
|
|
|
sendEvent(AppStateEvent.Fail);
|
2022-04-30 00:09:53 +02:00
|
|
|
console.error(`ChatService -> registerUser() ERROR: \n${e}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-26 07:51:17 +02:00
|
|
|
const resetAndReAuth = () => {
|
|
|
|
setLocalStorage(ACCESS_TOKEN_KEY, '');
|
|
|
|
setAccessToken('');
|
|
|
|
handleUserRegistration();
|
|
|
|
};
|
|
|
|
|
2022-05-03 07:13:36 +02:00
|
|
|
const handleMessage = (message: SocketEvent) => {
|
|
|
|
switch (message.type) {
|
2022-05-26 07:51:17 +02:00
|
|
|
case MessageType.ERROR_NEEDS_REGISTRATION:
|
|
|
|
resetAndReAuth();
|
|
|
|
break;
|
2022-05-03 23:17:05 +02:00
|
|
|
case MessageType.CONNECTED_USER_INFO:
|
2022-05-26 05:38:40 +02:00
|
|
|
handleConnectedClientInfoMessage(message as ConnectedClientInfoEvent, setChatDisplayName);
|
2022-05-03 07:13:36 +02:00
|
|
|
break;
|
2022-05-03 23:17:05 +02:00
|
|
|
case MessageType.CHAT:
|
2022-05-03 22:01:50 +02:00
|
|
|
handleChatMessage(message as ChatEvent, chatMessages, setChatMessages);
|
2022-05-03 07:13:36 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.error('Unknown socket message type: ', message.type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-30 00:09:53 +02:00
|
|
|
const getChatHistory = async () => {
|
|
|
|
try {
|
|
|
|
const messages = await ChatService.getChatHistory(accessToken);
|
2022-05-03 22:01:50 +02:00
|
|
|
const updatedChatMessages = [...messages, ...chatMessages];
|
|
|
|
setChatMessages(updatedChatMessages);
|
2022-04-30 00:09:53 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.error(`ChatService -> getChatHistory() ERROR: \n${error}`);
|
|
|
|
}
|
2022-05-03 07:13:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const startChat = async () => {
|
2022-05-26 05:38:40 +02:00
|
|
|
sendEvent(AppStateEvent.Loading);
|
2022-05-03 07:13:36 +02:00
|
|
|
try {
|
2022-05-05 08:06:35 +02:00
|
|
|
ws = new WebsocketService(accessToken, '/ws');
|
2022-05-05 01:55:54 +02:00
|
|
|
ws.handleMessage = handleMessage;
|
|
|
|
setWebsocketService(ws);
|
2022-05-26 05:38:40 +02:00
|
|
|
sendEvent(AppStateEvent.Loaded);
|
2022-05-03 07:13:36 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.error(`ChatService -> startChat() ERROR: \n${error}`);
|
|
|
|
}
|
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-05-26 05:38:40 +02:00
|
|
|
updateServerStatus();
|
2022-05-13 23:44:16 +02:00
|
|
|
setInterval(() => {
|
|
|
|
updateServerStatus();
|
2022-05-26 05:38:40 +02:00
|
|
|
}, SERVER_STATUS_POLL_DURATION);
|
|
|
|
}, [appState]);
|
2022-05-13 23:44:16 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-05-03 02:45:22 +02:00
|
|
|
if (!accessToken) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-30 00:09:53 +02:00
|
|
|
getChatHistory();
|
2022-05-03 07:13:36 +02:00
|
|
|
startChat();
|
2022-04-30 00:09:53 +02:00
|
|
|
}, [accessToken]);
|
|
|
|
|
2022-05-26 05:38:40 +02:00
|
|
|
appStateService.onTransition(state => {
|
|
|
|
if (!state.changed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const metadata = mergeMeta(state.meta) as AppStateOptions;
|
|
|
|
|
|
|
|
console.log('--- APP STATE: ', state.value);
|
|
|
|
console.log('--- APP META: ', metadata);
|
|
|
|
|
|
|
|
setAppState(metadata);
|
|
|
|
});
|
2022-05-03 02:45:22 +02:00
|
|
|
|
2022-04-26 08:10:07 +02:00
|
|
|
return null;
|
|
|
|
}
|