fix: immutable optimistic update to trigger re-render
This commit is contained in:
+11
-11
@@ -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();
|
||||
}
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user