Skip to content

Commit

Permalink
feat: update title change
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-ae committed Dec 19, 2024
1 parent 8c08f3b commit 6d1c06d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/people/hiveChat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SOCKET_MSG } from 'config/socket';
import styled from 'styled-components';
import { EuiLoadingSpinner } from '@elastic/eui';
import MaterialIcon from '@material/react-material-icon';
import { chatHistoryStore } from 'store/chat.ts';
import { renderMarkdown } from '../utils/RenderMarkdown.tsx';

interface RouteParams {
Expand Down Expand Up @@ -169,6 +170,7 @@ export const HiveChatView: React.FC = observer(() => {
const [title, setTitle] = useState('Talk to Hive - Chat');
const history = useHistory();
const socketRef = useRef<WebSocket | null>(null);
const [isUpdatingTitle, setIsUpdatingTitle] = useState(false);

const handleBackClick = () => {
history.push(`/workspace/${uuid}`);
Expand All @@ -192,6 +194,33 @@ export const HiveChatView: React.FC = observer(() => {
}
}, [chat, chatId, ui]);

const updateChatTitle = async (chatId: string, uuid: string, newTitle: string, setIsUpdatingTitle: (status: boolean) => void): Promise<void> => {
if (!chatId || !uuid || !newTitle.trim()) return;

setIsUpdatingTitle(true);
try {
console.log(newTitle);
chatHistoryStore.updateChatTitle(chatId, newTitle);
ui.setToasts([
{
title: 'Success',
text: 'Chat Title Updated',
},
]);
} catch (error) {
console.error('Error updating chat title:', error);
ui.setToasts([
{
title: 'Error',
color: 'danger',
text: 'Failed to update chat title',
},
]);
} finally {
setIsUpdatingTitle(false);
}
};

useEffect(() => {
const initializeChat = async () => {
setLoading(true);
Expand All @@ -210,6 +239,21 @@ export const HiveChatView: React.FC = observer(() => {
initializeChat();
}, [chatId, chat]);

let debounceUpdateTitle: ReturnType<typeof setTimeout>;
const handleTitleChange = (chatId: string, uuid: string, title: string, setIsUpdatingTitle: (status: boolean) => void) => {
clearTimeout(debounceUpdateTitle);

debounceUpdateTitle = setTimeout(() => {
updateChatTitle(chatId, uuid, title, setIsUpdatingTitle);
}, 500);
};

const onTitleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const newTitle = event.target.value;
setTitle(newTitle);
handleTitleChange(chatId, uuid, newTitle, setIsUpdatingTitle);
};

useEffect(() => {
const socket = createSocketInstance();
socketRef.current = socket;
Expand Down Expand Up @@ -365,8 +409,12 @@ export const HiveChatView: React.FC = observer(() => {
/>
<TitleInput
value={title}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setTitle(e.target.value)}
onChange={onTitleChange}
placeholder="Enter chat title..."
disabled={isUpdatingTitle}
style={{
cursor: isUpdatingTitle ? 'not-allowed' : 'text',
}}
/>
</Header>
<ChatBody>
Expand Down
33 changes: 33 additions & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,39 @@ export class ChatService {
}
}

async updateChatTitle(chat_id: string, title: string): Promise<void> {
try {
if (!uiStore.meInfo) return undefined;
const info = uiStore.meInfo;

const response = await fetch(`${TribesURL}/hivechat/${chat_id}`, {
method: 'PUT',
mode: 'cors',
headers: {
'x-jwt': info.tribe_jwt,
'Content-Type': 'application/json'
},
body: JSON.stringify({ title }),
});

if (!response.ok) {
console.log(response)
throw new Error(`HTTP error! status: ${response.text}`);
}

const result = await response.json();

if (!result.success) {
throw new Error('Invalid chat title update response');
}

return result.data;
} catch (error) {
console.error('Error updating chat title:', error);
throw error;
}
}

async sendMessage(
chat_id: string,
message: string,
Expand Down
10 changes: 10 additions & 0 deletions src/store/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ export class ChatHistoryStore implements ChatStore {
return chat;
}

async updateChatTitle(chat_id: string, newTitle: string): Promise<void> {
try {
await chatService.updateChatTitle(chat_id, newTitle);
this.updateChat(chat_id, { title: newTitle });
} catch (error) {
console.error('Error updating chat title in store:', error);
throw error;
}
}

async loadChatHistory(chat_id: string): Promise<ChatMessage[] | undefined> {
try {
const messages = await chatService.getChatHistory(chat_id);
Expand Down

0 comments on commit 6d1c06d

Please sign in to comment.