49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
import type { SupportMessage } from '../types';
|
|
|
|
export type SupportWsClientMessage =
|
|
| { type: 'ping' }
|
|
| { type: 'subscribe'; conversationId: string }
|
|
| { type: 'unsubscribe'; conversationId: string }
|
|
| { type: 'subscribe_inbox' };
|
|
|
|
export type SupportWsServerMessage =
|
|
| { type: 'connected'; authenticated: boolean; role: string | null; path: string }
|
|
| { type: 'pong'; ts: number }
|
|
| { type: 'subscribed'; conversationId: string }
|
|
| { type: 'subscribed_inbox' }
|
|
| {
|
|
type: 'message.new';
|
|
conversationId: string;
|
|
messageId: string;
|
|
message?: SupportMessage;
|
|
ts?: number;
|
|
}
|
|
| { type: 'message.read'; conversationId: string; ts?: number }
|
|
| {
|
|
type: 'message.updated' | 'message.deleted';
|
|
conversationId: string;
|
|
messageId: string;
|
|
message?: SupportMessage;
|
|
ts?: number;
|
|
}
|
|
| { type: 'conversation.updated'; conversationId: string; ts?: number }
|
|
| { type: 'typing'; conversationId: string; party: 'user' | 'staff'; active: boolean; ts?: number }
|
|
| { type: 'error'; code: string; message: string };
|
|
|
|
export function resolveSupportWsBaseUrl(): string {
|
|
const explicit = import.meta.env.VITE_WS_SUPPORT_URL as string | undefined;
|
|
if (explicit) return explicit.replace(/\/$/, '');
|
|
|
|
const api = import.meta.env.VITE_API_BASE_URL || 'http://localhost:4000';
|
|
const url = new URL(api);
|
|
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
url.port = import.meta.env.VITE_WS_PORT || '4001';
|
|
url.pathname = '/ws/v1/support';
|
|
url.search = '';
|
|
return url.toString().replace(/\/$/, '');
|
|
}
|
|
|
|
export function isSupportWsEnabled(): boolean {
|
|
return import.meta.env.VITE_WS_ENABLED === 'true';
|
|
}
|