(queryClient: QueryClient)
| 1012 | }); |
| 1013 | |
| 1014 | export const reorderPinnedChat = (queryClient: QueryClient) => ({ |
| 1015 | mutationFn: ({ chatId, pinOrder }: { chatId: string; pinOrder: number }) => |
| 1016 | API.experimental.updateChat(chatId, { pin_order: pinOrder }), |
| 1017 | onMutate: async ({ |
| 1018 | chatId, |
| 1019 | pinOrder, |
| 1020 | }: { |
| 1021 | chatId: string; |
| 1022 | pinOrder: number; |
| 1023 | }) => { |
| 1024 | await queryClient.cancelQueries({ |
| 1025 | queryKey: chatsKey, |
| 1026 | predicate: isChatListQuery, |
| 1027 | }); |
| 1028 | await queryClient.cancelQueries({ |
| 1029 | queryKey: chatKey(chatId), |
| 1030 | exact: true, |
| 1031 | }); |
| 1032 | |
| 1033 | // Optimistically reorder pinned chats in the cache so the |
| 1034 | // sidebar reflects the new order immediately without waiting |
| 1035 | // for the server round-trip. |
| 1036 | const allChats = readInfiniteChatsCache(queryClient) ?? []; |
| 1037 | const pinned = allChats |
| 1038 | .filter((c) => c.pin_order > 0) |
| 1039 | .sort((a, b) => a.pin_order - b.pin_order); |
| 1040 | const oldIdx = pinned.findIndex((c) => c.id === chatId); |
| 1041 | if (oldIdx !== -1) { |
| 1042 | const moved = pinned.splice(oldIdx, 1)[0]; |
| 1043 | pinned.splice(pinOrder - 1, 0, moved); |
| 1044 | const newOrders = new Map(pinned.map((c, i) => [c.id, i + 1])); |
| 1045 | updateInfiniteChatsCache(queryClient, (chats) => |
| 1046 | chats.map((c) => { |
| 1047 | const order = newOrders.get(c.id); |
| 1048 | return order !== undefined ? { ...c, pin_order: order } : c; |
| 1049 | }), |
| 1050 | ); |
| 1051 | } |
| 1052 | }, |
| 1053 | onSettled: async ( |
| 1054 | _data: unknown, |
| 1055 | _error: unknown, |
| 1056 | { chatId }: { chatId: string; pinOrder: number }, |
| 1057 | ) => { |
| 1058 | await invalidateChatListQueries(queryClient); |
| 1059 | await queryClient.invalidateQueries({ |
| 1060 | queryKey: chatKey(chatId), |
| 1061 | exact: true, |
| 1062 | }); |
| 1063 | }, |
| 1064 | }); |
| 1065 | |
| 1066 | export const regenerateChatTitle = (queryClient: QueryClient) => ({ |
| 1067 | mutationFn: (chatId: string) => API.experimental.regenerateChatTitle(chatId), |
no test coverage detected