Files

130 lines
3.8 KiB
JavaScript
Raw Permalink Normal View History

2026-04-29 15:18:30 +08:00
import store from './store.js';
2026-05-25 20:37:36 +08:00
import UIRenderer from './renderers/renderer.js';
import { showNotification, randomMeetingId } from '../shared/utils.js';
import chatMessage from './chat/chatmessage.js';
import { createCallViewController } from './controllers/call-view-controller.js';
2026-05-16 21:26:19 +08:00
import {
bindConnectViewEvents,
initWebSocket,
loadUserSettings
} from './connectview.js';
2026-05-25 20:37:36 +08:00
import { createInviteController } from './controllers/invite-controller.js';
import { createLogger } from '../shared/logger.js';
2026-05-24 14:16:28 +08:00
const logger = createLogger('main');
2026-05-24 12:56:50 +08:00
let connectionId = '';
2026-05-16 21:26:19 +08:00
let currentView = 'connect';
2026-05-24 12:56:50 +08:00
2026-05-24 12:43:16 +08:00
function updateConnectionId(nextConnectionId) {
connectionId = nextConnectionId || '';
2026-05-16 21:26:19 +08:00
2026-05-24 12:43:16 +08:00
if (connectionId) {
localStorage.setItem('connectionId', connectionId);
2026-05-18 21:18:55 +08:00
}
}
2026-05-24 12:56:50 +08:00
async function switchToCallView(targetConnectionId) {
2026-05-16 21:26:19 +08:00
const connectView = document.getElementById('connectView');
const callView = document.getElementById('callView');
2026-05-24 12:56:50 +08:00
if (connectView) {
connectView.classList.add('hidden');
}
if (callView) {
callView.classList.remove('hidden');
}
2026-05-16 21:26:19 +08:00
currentView = 'call';
2026-05-24 12:56:50 +08:00
2026-05-16 21:26:19 +08:00
try {
const renderer = new UIRenderer(store);
2026-05-24 12:56:50 +08:00
await store.joinCall(targetConnectionId);
await store.setUp(targetConnectionId);
2026-05-16 21:26:19 +08:00
renderer.renderHeaderTitle();
2026-05-24 12:56:50 +08:00
callViewController.bindDomEvents();
2026-05-24 14:16:28 +08:00
logger.debug('Video call app initialized successfully');
2026-05-18 23:03:28 +08:00
return true;
2026-05-16 21:26:19 +08:00
} catch (error) {
2026-05-24 14:16:28 +08:00
logger.error('Error initializing app:', error);
2026-05-16 21:26:19 +08:00
showNotification('初始化失败,请刷新页面重试', 'error');
2026-05-18 23:03:28 +08:00
return false;
2026-05-16 21:26:19 +08:00
}
}
2026-05-24 12:43:16 +08:00
const inviteController = createInviteController({
store,
notify: showNotification,
onAcceptConnection: switchToCallView,
getCurrentView: () => currentView,
getConnectionId: () => connectionId,
setConnectionId: updateConnectionId
});
2026-05-24 12:56:50 +08:00
const callViewController = createCallViewController({
store,
chatMessage,
notify: showNotification
});
async function handleJoinCall(targetConnectionId) {
showNotification(`正在加入通话 (${targetConnectionId})`);
updateConnectionId(targetConnectionId);
await switchToCallView(targetConnectionId);
2026-05-16 21:26:19 +08:00
}
async function handleCreateCall() {
showNotification('正在创建通话...');
2026-04-29 15:18:30 +08:00
2026-05-24 12:56:50 +08:00
const nextConnectionId = randomMeetingId();
updateConnectionId(nextConnectionId);
await switchToCallView(nextConnectionId);
2026-04-29 15:18:30 +08:00
}
window.addEventListener('DOMContentLoaded', async () => {
try {
2026-05-16 21:26:19 +08:00
const connectView = document.getElementById('connectView');
const callView = document.getElementById('callView');
2026-05-24 12:56:50 +08:00
if (connectView) {
connectView.classList.remove('hidden');
}
if (callView) {
callView.classList.add('hidden');
}
2026-05-16 21:26:19 +08:00
currentView = 'connect';
loadUserSettings();
await initWebSocket();
2026-05-24 12:43:16 +08:00
inviteController.bindSignalHandlers();
inviteController.bindDialogEvents();
2026-05-24 12:56:50 +08:00
bindConnectViewEvents(handleJoinCall, handleCreateCall);
2026-05-16 21:26:19 +08:00
const savedConnectionId = localStorage.getItem('connectionId');
if (savedConnectionId) {
2026-05-24 12:43:16 +08:00
updateConnectionId(savedConnectionId);
2026-05-16 21:26:19 +08:00
const connectionIdInput = document.getElementById('connectionIdInput');
2026-05-24 12:56:50 +08:00
if (connectionIdInput) {
connectionIdInput.value = savedConnectionId;
}
2026-04-29 15:18:30 +08:00
}
2026-05-24 12:43:16 +08:00
const invitePayload = inviteController.getInvitePayloadFromUrl();
2026-05-18 21:18:55 +08:00
if (invitePayload) {
2026-05-24 12:43:16 +08:00
inviteController.showCallRequestDialog(invitePayload);
2026-05-18 21:18:55 +08:00
}
2026-05-24 14:16:28 +08:00
logger.debug('SPA initialized, showing connect view');
2026-04-29 15:18:30 +08:00
} catch (error) {
2026-05-24 14:16:28 +08:00
logger.error('Error initializing SPA:', error);
2026-04-29 15:18:30 +08:00
showNotification('初始化失败,请刷新页面重试', 'error');
}
});
export { store };