owncast/web/services/chat-service.ts
Michael David Kuckuk b38df2fbe3
Create stories for layout testing (#2722)
* Inject services with useContext

* Extract service for video settings

* Create mock factories for services

* Create test data for chat history

* Add story to visualize different layouts

* Fix renaming mistake

* Add landscape and portrait viewports

* Add landscape stories

---------

Co-authored-by: Gabe Kangas <gabek@real-ity.com>
2023-02-26 16:54:28 -08:00

40 lines
1.2 KiB
TypeScript

import { createContext } from 'react';
import { ChatMessage } from '../interfaces/chat-message.model';
import { getUnauthedData } from '../utils/apis';
const ENDPOINT = `/api/chat`;
const URL_CHAT_REGISTRATION = `/api/chat/register`;
export interface UserRegistrationResponse {
id: string;
accessToken: string;
displayName: string;
displayColor: number;
}
export interface ChatStaticService {
getChatHistory(accessToken: string): Promise<ChatMessage[]>;
registerUser(username: string): Promise<UserRegistrationResponse>;
}
class ChatService {
public static async getChatHistory(accessToken: string): Promise<ChatMessage[]> {
const response = await getUnauthedData(`${ENDPOINT}?accessToken=${accessToken}`);
return response;
}
public static async registerUser(username: string): Promise<UserRegistrationResponse> {
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ displayName: username }),
};
const response = await getUnauthedData(URL_CHAT_REGISTRATION, options);
return response;
}
}
export const ChatServiceContext = createContext<ChatStaticService>(ChatService);