(queryClient: QueryClient)
| 825 | }); |
| 826 | |
| 827 | export const updateChatWorkspace = (queryClient: QueryClient) => ({ |
| 828 | mutationFn: ({ chatId, workspaceId }: UpdateChatWorkspaceVariables) => |
| 829 | API.experimental.updateChat(chatId, { |
| 830 | workspace_id: |
| 831 | workspaceId ?? |
| 832 | // The API uses the nil UUID to clear the workspace association. |
| 833 | "00000000-0000-0000-0000-000000000000", |
| 834 | }), |
| 835 | onMutate: async ({ chatId, workspaceId }: UpdateChatWorkspaceVariables) => { |
| 836 | await queryClient.cancelQueries({ |
| 837 | queryKey: chatsKey, |
| 838 | predicate: isChatListQuery, |
| 839 | }); |
| 840 | await queryClient.cancelQueries({ |
| 841 | queryKey: chatKey(chatId), |
| 842 | exact: true, |
| 843 | }); |
| 844 | const previousChat = queryClient.getQueryData<TypesGen.Chat>( |
| 845 | chatKey(chatId), |
| 846 | ); |
| 847 | updateInfiniteChatsCache(queryClient, (chats) => |
| 848 | chats.map((chat) => |
| 849 | chat.id === chatId |
| 850 | ? { ...chat, workspace_id: workspaceId ?? undefined } |
| 851 | : chat, |
| 852 | ), |
| 853 | ); |
| 854 | if (previousChat) { |
| 855 | queryClient.setQueryData<TypesGen.Chat>(chatKey(chatId), { |
| 856 | ...previousChat, |
| 857 | workspace_id: workspaceId ?? undefined, |
| 858 | }); |
| 859 | } |
| 860 | return { previousChat }; |
| 861 | }, |
| 862 | onError: ( |
| 863 | _error: unknown, |
| 864 | { chatId }: UpdateChatWorkspaceVariables, |
| 865 | context: |
| 866 | | { |
| 867 | previousChat?: TypesGen.Chat; |
| 868 | } |
| 869 | | undefined, |
| 870 | ) => { |
| 871 | void invalidateChatListQueries(queryClient); |
| 872 | const previousChat = context?.previousChat; |
| 873 | if (previousChat) { |
| 874 | updateInfiniteChatsCache(queryClient, (chats) => |
| 875 | chats.map((chat) => |
| 876 | chat.id === chatId |
| 877 | ? { |
| 878 | ...chat, |
| 879 | workspace_id: previousChat.workspace_id, |
| 880 | } |
| 881 | : chat, |
| 882 | ), |
| 883 | ); |
| 884 | queryClient.setQueryData<TypesGen.Chat>(chatKey(chatId), previousChat); |
no test coverage detected