(queryClient: QueryClient)
| 767 | }); |
| 768 | |
| 769 | export const updateChatPlanMode = (queryClient: QueryClient) => ({ |
| 770 | mutationFn: ({ chatId, planMode }: UpdateChatPlanModeVariables) => |
| 771 | API.experimental.updateChat(chatId, { |
| 772 | plan_mode: toChatPlanModePayload(planMode), |
| 773 | }), |
| 774 | onMutate: async ({ chatId, planMode }: UpdateChatPlanModeVariables) => { |
| 775 | await queryClient.cancelQueries({ |
| 776 | queryKey: chatsKey, |
| 777 | predicate: isChatListQuery, |
| 778 | }); |
| 779 | await queryClient.cancelQueries({ |
| 780 | queryKey: chatKey(chatId), |
| 781 | exact: true, |
| 782 | }); |
| 783 | const previousChat = queryClient.getQueryData<TypesGen.Chat>( |
| 784 | chatKey(chatId), |
| 785 | ); |
| 786 | updateInfiniteChatsCache(queryClient, (chats) => |
| 787 | chats.map((chat) => |
| 788 | chat.id === chatId ? { ...chat, plan_mode: planMode } : chat, |
| 789 | ), |
| 790 | ); |
| 791 | if (previousChat) { |
| 792 | queryClient.setQueryData<TypesGen.Chat>(chatKey(chatId), { |
| 793 | ...previousChat, |
| 794 | plan_mode: planMode, |
| 795 | }); |
| 796 | } |
| 797 | return { previousChat }; |
| 798 | }, |
| 799 | onError: ( |
| 800 | _error: unknown, |
| 801 | { chatId }: UpdateChatPlanModeVariables, |
| 802 | context: |
| 803 | | { |
| 804 | previousChat?: TypesGen.Chat; |
| 805 | } |
| 806 | | undefined, |
| 807 | ) => { |
| 808 | void invalidateChatListQueries(queryClient); |
| 809 | const previousChat = context?.previousChat; |
| 810 | if (!previousChat) { |
| 811 | return; |
| 812 | } |
| 813 | updateInfiniteChatsCache(queryClient, (chats) => |
| 814 | chats.map((chat) => |
| 815 | chat.id === chatId |
| 816 | ? { |
| 817 | ...chat, |
| 818 | plan_mode: previousChat.plan_mode, |
| 819 | } |
| 820 | : chat, |
| 821 | ), |
| 822 | ); |
| 823 | queryClient.setQueryData<TypesGen.Chat>(chatKey(chatId), previousChat); |
| 824 | }, |
| 825 | }); |
| 826 |
no test coverage detected