fix: immutable optimistic update to trigger re-render

This commit is contained in:
Avraham Sakal
2025-09-21 22:36:30 -04:00
parent 83254fb770
commit a76ec7ae1d
2 changed files with 24 additions and 16 deletions
+11 -11
View File
@@ -316,13 +316,17 @@ export default function ChatPage() {
newConversations: null,
};
}
const newConversations: Array<Conversation> = [
...previousConversations,
{
...conversation,
title,
} as Conversation,
];
/** MUST make a deep, immutable copy in order to trigger re-render of
* oberserving components. */
const newConversations: Array<Conversation> = JSON.parse(
JSON.stringify(previousConversations)
);
const conversationToUpdate = newConversations.find((c) => c.id === id);
if (!conversationToUpdate) {
return { previousConversations, newConversations };
}
conversationToUpdate.title = title;
queryClient.setQueryData<Array<Conversation>>(
trpc.chat.conversations.fetchAll.queryKey(),
newConversations
@@ -508,10 +512,6 @@ export default function ChatPage() {
onKeyUp={(e) => {
if (e.key === "Enter") {
e.preventDefault();
// updateConversationTitle.mutateAsync({
// id: conversationId,
// title: e.currentTarget.value,
// });
e.currentTarget.blur();
}
}}