fix: immutable optimistic update to trigger re-render
This commit is contained in:
@@ -68,9 +68,18 @@ function getQueryClient() {
|
|||||||
// suspends during the initial render. This may not be needed if we
|
// suspends during the initial render. This may not be needed if we
|
||||||
// have a suspense boundary BELOW the creation of the query client
|
// have a suspense boundary BELOW the creation of the query client
|
||||||
if (!browserQueryClient) browserQueryClient = makeQueryClient();
|
if (!browserQueryClient) browserQueryClient = makeQueryClient();
|
||||||
|
// This code is for all users
|
||||||
|
window.__TANSTACK_QUERY_CLIENT__ = browserQueryClient;
|
||||||
return browserQueryClient;
|
return browserQueryClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This code is only for TypeScript
|
||||||
|
declare global {
|
||||||
|
interface Window {
|
||||||
|
__TANSTACK_QUERY_CLIENT__: import("@tanstack/query-core").QueryClient;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function SignInWithGoogle() {
|
export function SignInWithGoogle() {
|
||||||
const pageContext = usePageContext();
|
const pageContext = usePageContext();
|
||||||
/** This is populated using the +onCreatePageContext.server.ts hook */
|
/** This is populated using the +onCreatePageContext.server.ts hook */
|
||||||
@@ -232,7 +241,9 @@ function NavLinkChat() {
|
|||||||
const { urlPathname } = pageContext;
|
const { urlPathname } = pageContext;
|
||||||
const trpc = useTRPC();
|
const trpc = useTRPC();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
// const
|
const { data: conversations } = useQuery(
|
||||||
|
trpc.chat.conversations.fetchAll.queryOptions()
|
||||||
|
);
|
||||||
const startConversation = useMutation(
|
const startConversation = useMutation(
|
||||||
trpc.chat.conversations.start.mutationOptions({
|
trpc.chat.conversations.start.mutationOptions({
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
@@ -286,9 +297,6 @@ function NavLinkChat() {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
const { data: conversations } = useQuery(
|
|
||||||
trpc.chat.conversations.fetchAll.queryOptions()
|
|
||||||
);
|
|
||||||
// const selectedConversationId = useStore(
|
// const selectedConversationId = useStore(
|
||||||
// (state) => state.selectedConversationId
|
// (state) => state.selectedConversationId
|
||||||
// );
|
// );
|
||||||
@@ -356,7 +364,7 @@ function NavLinkChat() {
|
|||||||
>
|
>
|
||||||
{conversations?.map((conversation) => (
|
{conversations?.map((conversation) => (
|
||||||
<NavLink
|
<NavLink
|
||||||
key={conversation.id}
|
key={conversation.id + conversation.title}
|
||||||
href={`/chat/${conversation.id}`}
|
href={`/chat/${conversation.id}`}
|
||||||
label={conversation.title}
|
label={conversation.title}
|
||||||
className="hover-container"
|
className="hover-container"
|
||||||
|
|||||||
+11
-11
@@ -316,13 +316,17 @@ export default function ChatPage() {
|
|||||||
newConversations: null,
|
newConversations: null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const newConversations: Array<Conversation> = [
|
|
||||||
...previousConversations,
|
/** MUST make a deep, immutable copy in order to trigger re-render of
|
||||||
{
|
* oberserving components. */
|
||||||
...conversation,
|
const newConversations: Array<Conversation> = JSON.parse(
|
||||||
title,
|
JSON.stringify(previousConversations)
|
||||||
} as Conversation,
|
);
|
||||||
];
|
const conversationToUpdate = newConversations.find((c) => c.id === id);
|
||||||
|
if (!conversationToUpdate) {
|
||||||
|
return { previousConversations, newConversations };
|
||||||
|
}
|
||||||
|
conversationToUpdate.title = title;
|
||||||
queryClient.setQueryData<Array<Conversation>>(
|
queryClient.setQueryData<Array<Conversation>>(
|
||||||
trpc.chat.conversations.fetchAll.queryKey(),
|
trpc.chat.conversations.fetchAll.queryKey(),
|
||||||
newConversations
|
newConversations
|
||||||
@@ -508,10 +512,6 @@ export default function ChatPage() {
|
|||||||
onKeyUp={(e) => {
|
onKeyUp={(e) => {
|
||||||
if (e.key === "Enter") {
|
if (e.key === "Enter") {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
// updateConversationTitle.mutateAsync({
|
|
||||||
// id: conversationId,
|
|
||||||
// title: e.currentTarget.value,
|
|
||||||
// });
|
|
||||||
e.currentTarget.blur();
|
e.currentTarget.blur();
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
|||||||
Reference in New Issue
Block a user