// import useAudio from "@/audio"; import UIState from "@/uiState"; import { config } from "@/config"; import useWebsockets from "./composables/useWebsockets"; import { backgroundMitt, ipcEmit } from "@/composables/useEmitter"; import { Notification } from 'electron'; import { getWindowFocus, getWindowOpen } from './store'; import { initAudio, terminateAudio, playback } from "./audio"; export const CONNECTION_STATUS = { connected: 'Connected', nominal: 'Nominal', reconnecting: 'Reconnecting', lost: 'Connection Lost', }; const uiState = new UIState(); const onMessageCallback = (payload: string) => { if (payload === "CLOSE_AUTH_FAIL") { backgroundMitt.emit("close-auth-fail"); return; } const message = JSON.parse(payload) as ClientProtocol; if (message.header === "init") { uiState.set(message.body as Init); } else { const body = message.body as Update; uiState.update(body); if (body.name === "add") { const data = body.data as Message; // Show a notification if new content and window is not focused. if (!getWindowFocus()) { new Notification({ title: 'New Message', subtitle: data.context.text as string, body: data.content[0].text, }) .show(); } // Playback audio if message has audio. // TODO: Should we worry about playback and record at the same time? // if (data.content.audio) // { // playback(data.content.audio); // } } } } const connectionStatusCallback = (status: string) => { // backgroundMitt.emit("update-icon-status", status); ipcEmit('set-connection-status', status); } const statusOptions = { openMessage: CONNECTION_STATUS.connected, pongMessage: CONNECTION_STATUS.nominal, closeMessage: CONNECTION_STATUS.reconnecting, pingErrorMessage: CONNECTION_STATUS.lost, } const { connect, send, close } = useWebsockets( onMessageCallback, connectionStatusCallback, statusOptions ); export const updateAppUI = (): void => { uiState.emit(); } // Listen for audio recordings and send them to backend. backgroundMitt.on("audio", (audio: Int16Array) => { sendMessage({ text: false, audio: audio } as Raw); }); export function sendMessage(message: Raw | Request): void { send(message); } /* launch a new session (the main process for authenticated users) */ export function launchSession(platformKey: string) { /* connect to the platform */ connect(config.PLATFORM_URL, platformKey); initAudio(); } export function endSession() { terminateAudio(); close(1000, 'session-logout'); uiState.reset(); }