(queryClient: QueryClient)
| 647 | }); |
| 648 | |
| 649 | export const archiveChat = (queryClient: QueryClient) => ({ |
| 650 | mutationFn: (chatId: string) => |
| 651 | API.experimental.updateChat(chatId, { archived: true }), |
| 652 | onMutate: async (chatId: string) => { |
| 653 | await queryClient.cancelQueries({ |
| 654 | queryKey: chatsKey, |
| 655 | predicate: isChatListQuery, |
| 656 | }); |
| 657 | await queryClient.cancelQueries({ |
| 658 | queryKey: chatKey(chatId), |
| 659 | exact: true, |
| 660 | }); |
| 661 | const previousChat = queryClient.getQueryData<TypesGen.Chat>( |
| 662 | chatKey(chatId), |
| 663 | ); |
| 664 | // Flip archived flag in the flat root list; strip the |
| 665 | // chat from any parent's embedded children (individual |
| 666 | // child archive). |
| 667 | updateInfiniteChatsCache(queryClient, (chats) => |
| 668 | chats.map((chat) => |
| 669 | chat.id === chatId ? { ...chat, archived: true } : chat, |
| 670 | ), |
| 671 | ); |
| 672 | removeChildFromParentInCache(queryClient, chatId); |
| 673 | if (previousChat) { |
| 674 | queryClient.setQueryData<TypesGen.Chat>(chatKey(chatId), { |
| 675 | ...previousChat, |
| 676 | archived: true, |
| 677 | }); |
| 678 | } |
| 679 | return { previousChat }; |
| 680 | }, |
| 681 | onError: ( |
| 682 | _error: unknown, |
| 683 | chatId: string, |
| 684 | context: |
| 685 | | { |
| 686 | previousChat?: TypesGen.Chat; |
| 687 | } |
| 688 | | undefined, |
| 689 | ) => { |
| 690 | // Rollback: invalidate to re-fetch the correct state. |
| 691 | void invalidateChatListQueries(queryClient); |
| 692 | if (context?.previousChat) { |
| 693 | queryClient.setQueryData<TypesGen.Chat>( |
| 694 | chatKey(chatId), |
| 695 | context.previousChat, |
| 696 | ); |
| 697 | } |
| 698 | }, |
| 699 | onSettled: async (_data: unknown, _error: unknown, chatId: string) => { |
| 700 | await invalidateChatListQueries(queryClient); |
| 701 | await queryClient.invalidateQueries({ |
| 702 | queryKey: chatKey(chatId), |
| 703 | exact: true, |
| 704 | }); |
| 705 | await queryClient.invalidateQueries({ |
| 706 | queryKey: chatsByWorkspaceKeyPrefix, |
no test coverage detected