mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-02-15 13:17:32 +00:00
refactor: Extract WebSocket URL construction into a separate function
This commit is contained in:
@@ -17,7 +17,15 @@ export const useWebSocket = () => {
|
|||||||
return context;
|
return context;
|
||||||
};
|
};
|
||||||
|
|
||||||
const useWebSocketProviderState = () => {
|
const buildWebSocketUrl = (token: string | null) => {
|
||||||
|
const isPlatform = import.meta.env.VITE_IS_PLATFORM === 'true';
|
||||||
|
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||||
|
if (isPlatform) return `${protocol}//${window.location.host}/ws`; // Platform mode: Use same domain as the page (goes through proxy)
|
||||||
|
if (!token) return null;
|
||||||
|
return `${protocol}//${window.location.host}/ws?token=${encodeURIComponent(token)}`; // OSS mode: Use same host:port that served the page
|
||||||
|
};
|
||||||
|
|
||||||
|
const useWebSocketProviderState = (): WebSocketContextType => {
|
||||||
const wsRef = useRef<WebSocket | null>(null);
|
const wsRef = useRef<WebSocket | null>(null);
|
||||||
const unmountedRef = useRef(false);
|
const unmountedRef = useRef(false);
|
||||||
const [messages, setMessages] = useState<any[]>([]);
|
const [messages, setMessages] = useState<any[]>([]);
|
||||||
@@ -44,21 +52,9 @@ const useWebSocketProviderState = () => {
|
|||||||
const isPlatform = import.meta.env.VITE_IS_PLATFORM === 'true';
|
const isPlatform = import.meta.env.VITE_IS_PLATFORM === 'true';
|
||||||
|
|
||||||
// Construct WebSocket URL
|
// Construct WebSocket URL
|
||||||
let wsUrl: string;
|
const wsUrl = buildWebSocketUrl(isPlatform ? null : localStorage.getItem('authToken'));
|
||||||
|
|
||||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
if (!wsUrl) return console.warn('No authentication token found for WebSocket connection');
|
||||||
if (isPlatform) {
|
|
||||||
// Platform mode: Use same domain as the page (goes through proxy)
|
|
||||||
wsUrl = `${protocol}//${window.location.host}/ws`;
|
|
||||||
} else {
|
|
||||||
// OSS mode: Connect to same host:port that served the page
|
|
||||||
const token = localStorage.getItem('auth-token');
|
|
||||||
if (!token) {
|
|
||||||
console.warn('No authentication token found for WebSocket connection');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
wsUrl = `${protocol}//${window.location.host}/ws?token=${encodeURIComponent(token)}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const websocket = new WebSocket(wsUrl);
|
const websocket = new WebSocket(wsUrl);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user