File size: 14,380 Bytes
11e3284 b916b29 3927930 11e3284 8a4bb0c 5743e5f 809dc5c 5743e5f 11e3284 5743e5f 744c9d5 11e3284 809dc5c 11e3284 809dc5c 11e3284 7c07000 11e3284 7c07000 809dc5c 11e3284 7c07000 11e3284 809dc5c 11e3284 7c07000 11e3284 7c07000 11e3284 7c07000 11e3284 809dc5c 7c07000 11e3284 809dc5c 11e3284 196c662 809dc5c 7c07000 11e3284 7c07000 11e3284 7c07000 11e3284 7c07000 11e3284 809dc5c 11e3284 196c662 7b68d9e 809dc5c 7b68d9e 809dc5c 7b68d9e 11e3284 f23d414 11e3284 196c662 11e3284 7c07000 11e3284 7c07000 11e3284 7b68d9e 11e3284 7c07000 11e3284 7c07000 7b68d9e 7c07000 11e3284 7c07000 11e3284 196c662 11e3284 809dc5c 11e3284 7c07000 196c662 7c07000 11e3284 7c07000 11e3284 7c07000 11e3284 7c07000 11e3284 809dc5c 11e3284 196c662 809dc5c 11e3284 7c07000 11e3284 7c07000 11e3284 7c07000 11e3284 809dc5c 11e3284 809dc5c 11e3284 809dc5c 11e3284 18a496b 11e3284 7c07000 11e3284 7c07000 11e3284 7c07000 11e3284 7c07000 1ea3843 11e3284 1ea3843 809dc5c 196c662 11e3284 1ea3843 11e3284 7c07000 11e3284 7c07000 196c662 11e3284 196c662 7c07000 11e3284 7c07000 11e3284 7c07000 d2e049e 18a496b 196c662 7d8f5e6 196c662 d2e049e 196c662 d2e049e 196c662 d2e049e 11e3284 b916b29 5743e5f 196c662 5743e5f b916b29 5743e5f b916b29 5743e5f b916b29 5743e5f b916b29 5743e5f b916b29 5743e5f b916b29 5743e5f f850783 5743e5f 196c662 5743e5f b916b29 5743e5f b916b29 5743e5f b916b29 5743e5f 91751af 5743e5f b916b29 5743e5f b916b29 700dff9 11e3284 700dff9 11e3284 700dff9 11e3284 700dff9 11e3284 700dff9 8a4bb0c 11e3284 700dff9 18a496b 11e3284 700dff9 11e3284 700dff9 8a4bb0c 700dff9 3927930 f9e19e4 3927930 744c9d5 5e72d47 744c9d5 3927930 f9e19e4 3927930 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
import { ChatSearchParams } from '@/constants/chat';
import {
IConversation,
IDialog,
IStats,
IToken,
} from '@/interfaces/database/chat';
import {
IAskRequestBody,
IFeedbackRequestBody,
} from '@/interfaces/request/chat';
import i18n from '@/locales/config';
import { IClientConversation } from '@/pages/chat/interface';
import chatService from '@/services/chat-service';
import {
buildMessageListWithUuid,
getConversationId,
isConversationIdExist,
} from '@/utils/chat';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { message } from 'antd';
import dayjs, { Dayjs } from 'dayjs';
import { has, set } from 'lodash';
import { useCallback, useMemo, useState } from 'react';
import { history, useSearchParams } from 'umi';
//#region logic
export const useClickDialogCard = () => {
const [_, setSearchParams] = useSearchParams();
const newQueryParameters: URLSearchParams = useMemo(() => {
return new URLSearchParams();
}, []);
const handleClickDialog = useCallback(
(dialogId: string) => {
newQueryParameters.set(ChatSearchParams.DialogId, dialogId);
// newQueryParameters.set(
// ChatSearchParams.ConversationId,
// EmptyConversationId,
// );
setSearchParams(newQueryParameters);
},
[newQueryParameters, setSearchParams],
);
return { handleClickDialog };
};
export const useClickConversationCard = () => {
const [currentQueryParameters, setSearchParams] = useSearchParams();
const newQueryParameters: URLSearchParams = useMemo(
() => new URLSearchParams(currentQueryParameters.toString()),
[currentQueryParameters],
);
const handleClickConversation = useCallback(
(conversationId: string, isNew: string) => {
newQueryParameters.set(ChatSearchParams.ConversationId, conversationId);
newQueryParameters.set(ChatSearchParams.isNew, isNew);
setSearchParams(newQueryParameters);
},
[setSearchParams, newQueryParameters],
);
return { handleClickConversation };
};
export const useGetChatSearchParams = () => {
const [currentQueryParameters] = useSearchParams();
return {
dialogId: currentQueryParameters.get(ChatSearchParams.DialogId) || '',
conversationId:
currentQueryParameters.get(ChatSearchParams.ConversationId) || '',
isNew: currentQueryParameters.get(ChatSearchParams.isNew) || '',
};
};
//#endregion
//#region dialog
export const useFetchNextDialogList = () => {
const { handleClickDialog } = useClickDialogCard();
const { dialogId } = useGetChatSearchParams();
const {
data,
isFetching: loading,
refetch,
} = useQuery<IDialog[]>({
queryKey: ['fetchDialogList'],
initialData: [],
gcTime: 0,
refetchOnWindowFocus: false,
queryFn: async (...params) => {
console.log('🚀 ~ queryFn: ~ params:', params);
const { data } = await chatService.listDialog();
if (data.code === 0) {
const list: IDialog[] = data.data;
if (list.length > 0) {
if (list.every((x) => x.id !== dialogId)) {
handleClickDialog(data.data[0].id);
}
} else {
history.push('/chat');
}
}
return data?.data ?? [];
},
});
return { data, loading, refetch };
};
export const useSetNextDialog = () => {
const queryClient = useQueryClient();
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['setDialog'],
mutationFn: async (params: IDialog) => {
const { data } = await chatService.setDialog(params);
if (data.code === 0) {
queryClient.invalidateQueries({
exact: false,
queryKey: ['fetchDialogList'],
});
queryClient.invalidateQueries({
queryKey: ['fetchDialog'],
});
message.success(
i18n.t(`message.${params.dialog_id ? 'modified' : 'created'}`),
);
}
return data?.code;
},
});
return { data, loading, setDialog: mutateAsync };
};
export const useFetchNextDialog = () => {
const { dialogId } = useGetChatSearchParams();
const {
data,
isFetching: loading,
refetch,
} = useQuery<IDialog>({
queryKey: ['fetchDialog', dialogId],
gcTime: 0,
initialData: {} as IDialog,
enabled: !!dialogId,
refetchOnWindowFocus: false,
queryFn: async () => {
const { data } = await chatService.getDialog({ dialogId });
return data?.data ?? ({} as IDialog);
},
});
return { data, loading, refetch };
};
export const useFetchManualDialog = () => {
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['fetchManualDialog'],
gcTime: 0,
mutationFn: async (dialogId: string) => {
const { data } = await chatService.getDialog({ dialogId });
return data;
},
});
return { data, loading, fetchDialog: mutateAsync };
};
export const useRemoveNextDialog = () => {
const queryClient = useQueryClient();
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['removeDialog'],
mutationFn: async (dialogIds: string[]) => {
const { data } = await chatService.removeDialog({ dialogIds });
if (data.code === 0) {
queryClient.invalidateQueries({ queryKey: ['fetchDialogList'] });
message.success(i18n.t('message.deleted'));
}
return data.code;
},
});
return { data, loading, removeDialog: mutateAsync };
};
//#endregion
//#region conversation
export const useFetchNextConversationList = () => {
const { dialogId } = useGetChatSearchParams();
const { handleClickConversation } = useClickConversationCard();
const {
data,
isFetching: loading,
refetch,
} = useQuery<IConversation[]>({
queryKey: ['fetchConversationList', dialogId],
initialData: [],
gcTime: 0,
refetchOnWindowFocus: false,
enabled: !!dialogId,
queryFn: async () => {
const { data } = await chatService.listConversation({ dialogId });
if (data.code === 0 && data.data.length > 0) {
handleClickConversation(data.data[0].id, '');
}
return data?.data;
},
});
return { data, loading, refetch };
};
export const useFetchNextConversation = () => {
const { isNew, conversationId } = useGetChatSearchParams();
const {
data,
isFetching: loading,
refetch,
} = useQuery<IClientConversation>({
queryKey: ['fetchConversation', conversationId],
initialData: {} as IClientConversation,
// enabled: isConversationIdExist(conversationId),
gcTime: 0,
refetchOnWindowFocus: false,
queryFn: async () => {
if (isNew !== 'true' && isConversationIdExist(conversationId)) {
const { data } = await chatService.getConversation({ conversationId });
const conversation = data?.data ?? {};
const messageList = buildMessageListWithUuid(conversation?.message);
return { ...conversation, message: messageList };
}
return { message: [] };
},
});
return { data, loading, refetch };
};
export const useFetchManualConversation = () => {
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['fetchManualConversation'],
gcTime: 0,
mutationFn: async (conversationId: string) => {
const { data } = await chatService.getConversation({ conversationId });
return data;
},
});
return { data, loading, fetchConversation: mutateAsync };
};
export const useUpdateNextConversation = () => {
const queryClient = useQueryClient();
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['updateConversation'],
mutationFn: async (params: Record<string, any>) => {
const { data } = await chatService.setConversation({
...params,
conversation_id: params.conversation_id
? params.conversation_id
: getConversationId(),
});
if (data.code === 0) {
queryClient.invalidateQueries({ queryKey: ['fetchConversationList'] });
}
return data;
},
});
return { data, loading, updateConversation: mutateAsync };
};
export const useRemoveNextConversation = () => {
const queryClient = useQueryClient();
const { dialogId } = useGetChatSearchParams();
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['removeConversation'],
mutationFn: async (conversationIds: string[]) => {
const { data } = await chatService.removeConversation({
conversationIds,
dialogId,
});
if (data.code === 0) {
queryClient.invalidateQueries({ queryKey: ['fetchConversationList'] });
}
return data.code;
},
});
return { data, loading, removeConversation: mutateAsync };
};
export const useDeleteMessage = () => {
const { conversationId } = useGetChatSearchParams();
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['deleteMessage'],
mutationFn: async (messageId: string) => {
const { data } = await chatService.deleteMessage({
messageId,
conversationId,
});
if (data.code === 0) {
message.success(i18n.t(`message.deleted`));
}
return data.code;
},
});
return { data, loading, deleteMessage: mutateAsync };
};
export const useFeedback = () => {
const { conversationId } = useGetChatSearchParams();
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['feedback'],
mutationFn: async (params: IFeedbackRequestBody) => {
const { data } = await chatService.thumbup({
...params,
conversationId,
});
if (data.code === 0) {
message.success(i18n.t(`message.operated`));
}
return data.code;
},
});
return { data, loading, feedback: mutateAsync };
};
//#endregion
// #region API provided for external calls
export const useCreateNextToken = () => {
const queryClient = useQueryClient();
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['createToken'],
mutationFn: async (params: Record<string, any>) => {
const { data } = await chatService.createToken(params);
if (data.code === 0) {
queryClient.invalidateQueries({ queryKey: ['fetchTokenList'] });
}
return data?.data ?? [];
},
});
return { data, loading, createToken: mutateAsync };
};
export const useFetchTokenList = (params: Record<string, any>) => {
const {
data,
isFetching: loading,
refetch,
} = useQuery<IToken[]>({
queryKey: ['fetchTokenList', params],
initialData: [],
gcTime: 0,
queryFn: async () => {
const { data } = await chatService.listToken(params);
return data?.data ?? [];
},
});
return { data, loading, refetch };
};
export const useRemoveNextToken = () => {
const queryClient = useQueryClient();
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['removeToken'],
mutationFn: async (params: {
tenantId: string;
dialogId?: string;
tokens: string[];
}) => {
const { data } = await chatService.removeToken(params);
if (data.code === 0) {
queryClient.invalidateQueries({ queryKey: ['fetchTokenList'] });
}
return data?.data ?? [];
},
});
return { data, loading, removeToken: mutateAsync };
};
type RangeValue = [Dayjs | null, Dayjs | null] | null;
const getDay = (date?: Dayjs) => date?.format('YYYY-MM-DD');
export const useFetchNextStats = () => {
const [pickerValue, setPickerValue] = useState<RangeValue>([
dayjs().subtract(7, 'day'),
dayjs(),
]);
const { data, isFetching: loading } = useQuery<IStats>({
queryKey: ['fetchStats', pickerValue],
initialData: {} as IStats,
gcTime: 0,
queryFn: async () => {
if (Array.isArray(pickerValue) && pickerValue[0]) {
const { data } = await chatService.getStats({
fromDate: getDay(pickerValue[0]),
toDate: getDay(pickerValue[1] ?? dayjs()),
});
return data?.data ?? {};
}
return {};
},
});
return { data, loading, pickerValue, setPickerValue };
};
//#endregion
//#region shared chat
export const useCreateNextSharedConversation = () => {
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['createSharedConversation'],
mutationFn: async (userId?: string) => {
const { data } = await chatService.createExternalConversation({ userId });
return data;
},
});
return { data, loading, createSharedConversation: mutateAsync };
};
export const useFetchNextSharedConversation = (conversationId: string) => {
const { data, isPending: loading } = useQuery({
queryKey: ['fetchSharedConversation'],
enabled: !!conversationId,
queryFn: async () => {
const { data } = await chatService.getExternalConversation(
null,
conversationId,
);
const messageList = buildMessageListWithUuid(data?.data?.message);
set(data, 'data.message', messageList);
return data;
},
});
return { data, loading };
};
//#endregion
//#region search page
export const useFetchMindMap = () => {
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['fetchMindMap'],
gcTime: 0,
mutationFn: async (params: IAskRequestBody) => {
try {
const ret = await chatService.getMindMap(params);
return ret?.data?.data ?? {};
} catch (error) {
if (has(error, 'message')) {
message.error(error.message);
}
return [];
}
},
});
return { data, loading, fetchMindMap: mutateAsync };
};
export const useFetchRelatedQuestions = () => {
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['fetchRelatedQuestions'],
gcTime: 0,
mutationFn: async (question: string): Promise<string[]> => {
const { data } = await chatService.getRelatedQuestions({ question });
return data?.data ?? [];
},
});
return { data, loading, fetchRelatedQuestions: mutateAsync };
};
//#endregion
|