Skip to content

Commit

Permalink
fix: Align webhook URL configuration and websocket handling for HiveChat
Browse files Browse the repository at this point in the history
  • Loading branch information
aliraza556 committed Dec 17, 2024
1 parent 8022de2 commit ba408c0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
7 changes: 5 additions & 2 deletions src/people/hiveChat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,11 @@ export const HiveChatView: React.FC = observer(() => {
const sessionId = data.body;
setWebsocketSessionId(sessionId);
console.log(`Websocket Session ID: ${sessionId}`);
} else if (data.type === 'chat_update' && data.chatId === chatId) {
} else if (data.action === 'message' && data.chatMessage) {
chat.addMessage(data.chatMessage);
await refreshChatHistory();
} else if (data.action === 'process' && data.chatMessage) {
chat.updateMessage(data.chatMessage.id, data.chatMessage);
}
} catch (error) {
console.error('Error processing websocket message:', error);
Expand All @@ -259,7 +262,7 @@ export const HiveChatView: React.FC = observer(() => {
socketRef.current.close();
}
};
}, [ui, refreshChatHistory, chatId]);
}, [ui, refreshChatHistory, chatId, chat]);

useEffect(() => {
const loadInitialChat = async () => {
Expand Down
33 changes: 24 additions & 9 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export class ChatService {
const result = await response.json();
console.log('Create chat response:', result);

if (!result.data || !result.data.id) {
throw new Error('Invalid chat response: missing data or id');
if (!result.success || !result.data) {
throw new Error('Invalid chat response');
}

return result.data;
Expand Down Expand Up @@ -57,8 +57,13 @@ export class ChatService {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();
return Array.isArray(data) ? data : [];
const result = await response.json();

if (!result.success || !result.data) {
throw new Error('Invalid chat history response');
}

return result.data;
} catch (e) {
console.error('Error loading chat history:', e);
return undefined;
Expand All @@ -70,7 +75,7 @@ export class ChatService {
if (!uiStore.meInfo) return undefined;
const info = uiStore.meInfo;

const response = await fetch(`${TribesURL}/hivechat/history/${workspace_uuid}`, {
const response = await fetch(`${TribesURL}/hivechat?workspace_id=${workspace_uuid}`, {
method: 'GET',
mode: 'cors',
headers: {
Expand All @@ -83,9 +88,13 @@ export class ChatService {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();
console.log('API Response:', data);
return Array.isArray(data) ? data : [];
const result = await response.json();
console.log('Workspace chats response:', result);
if (!result.success || !result.data) {
throw new Error('Invalid workspace chats response');
}

return result.data;
} catch (e) {
console.error('Error loading workspace chats:', e);
return undefined;
Expand Down Expand Up @@ -123,7 +132,13 @@ export class ChatService {
throw new Error(`HTTP error! status: ${response.status}`);
}

return response.json();
const result = await response.json();

if (!result.success || !result.data) {
throw new Error('Invalid send message response');
}

return result.data;
} catch (e) {
console.error('Error sending message:', e);
return undefined;
Expand Down

0 comments on commit ba408c0

Please sign in to comment.