(queryClient: QueryClient)
| 709 | }); |
| 710 | |
| 711 | export const unarchiveChat = (queryClient: QueryClient) => ({ |
| 712 | mutationFn: (chatId: string) => |
| 713 | API.experimental.updateChat(chatId, { archived: false }), |
| 714 | onMutate: async (chatId: string) => { |
| 715 | await queryClient.cancelQueries({ |
| 716 | queryKey: chatsKey, |
| 717 | predicate: isChatListQuery, |
| 718 | }); |
| 719 | await queryClient.cancelQueries({ |
| 720 | queryKey: chatKey(chatId), |
| 721 | exact: true, |
| 722 | }); |
| 723 | const previousChat = queryClient.getQueryData<TypesGen.Chat>( |
| 724 | chatKey(chatId), |
| 725 | ); |
| 726 | updateInfiniteChatsCache(queryClient, (chats) => |
| 727 | chats.map((chat) => |
| 728 | chat.id === chatId ? { ...chat, archived: false } : chat, |
| 729 | ), |
| 730 | ); |
| 731 | if (previousChat) { |
| 732 | queryClient.setQueryData<TypesGen.Chat>(chatKey(chatId), { |
| 733 | ...previousChat, |
| 734 | archived: false, |
| 735 | }); |
| 736 | } |
| 737 | return { previousChat }; |
| 738 | }, |
| 739 | onError: ( |
| 740 | _error: unknown, |
| 741 | chatId: string, |
| 742 | context: |
| 743 | | { |
| 744 | previousChat?: TypesGen.Chat; |
| 745 | } |
| 746 | | undefined, |
| 747 | ) => { |
| 748 | // Rollback: invalidate to re-fetch the correct state. |
| 749 | void invalidateChatListQueries(queryClient); |
| 750 | if (context?.previousChat) { |
| 751 | queryClient.setQueryData<TypesGen.Chat>( |
| 752 | chatKey(chatId), |
| 753 | context.previousChat, |
| 754 | ); |
| 755 | } |
| 756 | }, |
| 757 | onSettled: async (_data: unknown, _error: unknown, chatId: string) => { |
| 758 | await invalidateChatListQueries(queryClient); |
| 759 | await queryClient.invalidateQueries({ |
| 760 | queryKey: chatKey(chatId), |
| 761 | exact: true, |
| 762 | }); |
| 763 | await queryClient.invalidateQueries({ |
| 764 | queryKey: chatsByWorkspaceKeyPrefix, |
| 765 | }); |
| 766 | }, |
| 767 | }); |
| 768 |
no test coverage detected