(queryClient: QueryClient)
| 901 | }); |
| 902 | |
| 903 | export const pinChat = (queryClient: QueryClient) => ({ |
| 904 | mutationFn: (chatId: string) => |
| 905 | API.experimental.updateChat(chatId, { pin_order: 1 }), |
| 906 | onMutate: async (chatId: string) => { |
| 907 | await queryClient.cancelQueries({ |
| 908 | queryKey: chatsKey, |
| 909 | predicate: isChatListQuery, |
| 910 | }); |
| 911 | await queryClient.cancelQueries({ |
| 912 | queryKey: chatKey(chatId), |
| 913 | exact: true, |
| 914 | }); |
| 915 | const previousChat = queryClient.getQueryData<TypesGen.Chat>( |
| 916 | chatKey(chatId), |
| 917 | ); |
| 918 | const optimisticPinOrder = getNextOptimisticPinOrder(queryClient); |
| 919 | updateInfiniteChatsCache(queryClient, (chats) => |
| 920 | chats.map((chat) => |
| 921 | chat.id === chatId ? { ...chat, pin_order: optimisticPinOrder } : chat, |
| 922 | ), |
| 923 | ); |
| 924 | if (previousChat) { |
| 925 | queryClient.setQueryData<TypesGen.Chat>(chatKey(chatId), { |
| 926 | ...previousChat, |
| 927 | pin_order: optimisticPinOrder, |
| 928 | }); |
| 929 | } |
| 930 | return { previousChat }; |
| 931 | }, |
| 932 | onError: ( |
| 933 | _error: unknown, |
| 934 | chatId: string, |
| 935 | context: |
| 936 | | { |
| 937 | previousChat?: TypesGen.Chat; |
| 938 | } |
| 939 | | undefined, |
| 940 | ) => { |
| 941 | // Rollback: invalidate to re-fetch the correct state. |
| 942 | void invalidateChatListQueries(queryClient); |
| 943 | if (context?.previousChat) { |
| 944 | queryClient.setQueryData<TypesGen.Chat>( |
| 945 | chatKey(chatId), |
| 946 | context.previousChat, |
| 947 | ); |
| 948 | } |
| 949 | }, |
| 950 | onSettled: async (_data: unknown, _error: unknown, chatId: string) => { |
| 951 | await invalidateChatListQueries(queryClient); |
| 952 | await queryClient.invalidateQueries({ |
| 953 | queryKey: chatKey(chatId), |
| 954 | exact: true, |
| 955 | }); |
| 956 | }, |
| 957 | }); |
| 958 | |
| 959 | export const unpinChat = (queryClient: QueryClient) => ({ |
| 960 | mutationFn: (chatId: string) => |
no test coverage detected