balibabu commited on
Commit
414b2e9
·
1 Parent(s): f666f56

feat: let the messages I send appear immediately in the chat window and remove rewrite configuration from nginx proxy (#86)

Browse files

* feat: remove rewrite configuration from nginx proxy

* feat: let the messages I send appear immediately in the chat window

web/src/pages/chat/chat-container/index.tsx CHANGED
@@ -10,10 +10,8 @@ import reactStringReplace from 'react-string-replace';
10
  import {
11
  useFetchConversationOnMount,
12
  useGetFileIcon,
13
- useScrollToBottom,
14
  useSendMessage,
15
  } from '../hooks';
16
- import { IClientConversation } from '../interface';
17
 
18
  import Image from '@/components/image';
19
  import NewDocumentLink from '@/components/new-document-link';
@@ -187,17 +185,24 @@ const MessageItem = ({
187
 
188
  const ChatContainer = () => {
189
  const [value, setValue] = useState('');
190
- const conversation: IClientConversation = useFetchConversationOnMount();
 
 
 
 
191
  const { sendMessage } = useSendMessage();
 
192
  const loading = useOneNamespaceEffectsLoading('chatModel', [
193
  'completeConversation',
194
  ]);
195
- const ref = useScrollToBottom();
196
  useGetFileIcon();
197
 
198
  const handlePressEnter = () => {
199
- setValue('');
200
- sendMessage(value);
 
 
 
201
  };
202
 
203
  const handleInputChange: ChangeEventHandler<HTMLInputElement> = (e) => {
 
10
  import {
11
  useFetchConversationOnMount,
12
  useGetFileIcon,
 
13
  useSendMessage,
14
  } from '../hooks';
 
15
 
16
  import Image from '@/components/image';
17
  import NewDocumentLink from '@/components/new-document-link';
 
185
 
186
  const ChatContainer = () => {
187
  const [value, setValue] = useState('');
188
+ const {
189
+ ref,
190
+ currentConversation: conversation,
191
+ addNewestConversation,
192
+ } = useFetchConversationOnMount();
193
  const { sendMessage } = useSendMessage();
194
+
195
  const loading = useOneNamespaceEffectsLoading('chatModel', [
196
  'completeConversation',
197
  ]);
 
198
  useGetFileIcon();
199
 
200
  const handlePressEnter = () => {
201
+ if (!loading) {
202
+ setValue('');
203
+ addNewestConversation(value);
204
+ sendMessage(value);
205
+ }
206
  };
207
 
208
  const handleInputChange: ChangeEventHandler<HTMLInputElement> = (e) => {
web/src/pages/chat/hooks.ts CHANGED
@@ -374,32 +374,64 @@ export const useSetConversation = () => {
374
  const dispatch = useDispatch();
375
  const { dialogId } = useGetChatSearchParams();
376
 
377
- const setConversation = (message: string) => {
378
- return dispatch<any>({
379
- type: 'chatModel/setConversation',
380
- payload: {
381
- // conversation_id: '',
382
- dialog_id: dialogId,
383
- name: message,
384
- message: [
385
- {
386
- role: MessageType.Assistant,
387
- content: message,
388
- },
389
- ],
390
- },
391
- });
392
- };
 
 
 
393
 
394
  return { setConversation };
395
  };
396
 
397
  export const useSelectCurrentConversation = () => {
 
 
 
398
  const conversation: IClientConversation = useSelector(
399
  (state: any) => state.chatModel.currentConversation,
400
  );
401
 
402
- return conversation;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
403
  };
404
 
405
  export const useFetchConversation = () => {
@@ -421,11 +453,30 @@ export const useFetchConversation = () => {
421
  return fetchConversation;
422
  };
423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
424
  export const useFetchConversationOnMount = () => {
425
  const { conversationId } = useGetChatSearchParams();
426
- const conversation = useSelectCurrentConversation();
427
  const setCurrentConversation = useSetCurrentConversation();
428
  const fetchConversation = useFetchConversation();
 
 
 
429
 
430
  const fetchConversationOnMount = useCallback(() => {
431
  if (isConversationIdExist(conversationId)) {
@@ -439,68 +490,59 @@ export const useFetchConversationOnMount = () => {
439
  fetchConversationOnMount();
440
  }, [fetchConversationOnMount]);
441
 
442
- return conversation;
443
  };
444
 
445
  export const useSendMessage = () => {
446
  const dispatch = useDispatch();
447
  const { setConversation } = useSetConversation();
448
  const { conversationId } = useGetChatSearchParams();
449
- const conversation = useSelector(
450
  (state: any) => state.chatModel.currentConversation,
451
  );
 
452
  const { handleClickConversation } = useClickConversationCard();
453
 
454
- const sendMessage = (message: string, id?: string) => {
455
- dispatch({
456
- type: 'chatModel/completeConversation',
457
- payload: {
458
- conversation_id: id ?? conversationId,
459
- messages: [
460
- ...(conversation?.message ?? []).map((x: IMessage) => omit(x, 'id')),
461
- {
462
- role: MessageType.User,
463
- content: message,
464
- },
465
- ],
466
- },
467
- });
468
- };
 
 
 
 
 
469
 
470
- const handleSendMessage = async (message: string) => {
471
- if (conversationId !== '') {
472
- sendMessage(message);
473
- } else {
474
- const data = await setConversation(message);
475
- if (data.retcode === 0) {
476
- const id = data.data.id;
477
- handleClickConversation(id);
478
- sendMessage(message, id);
 
 
479
  }
480
- }
481
- };
 
482
 
483
  return { sendMessage: handleSendMessage };
484
  };
485
 
486
- export const useScrollToBottom = () => {
487
- const ref = useRef<HTMLDivElement>(null);
488
- let chatModel: ChatModelState = useSelector((state: any) => state.chatModel);
489
- const { currentConversation } = chatModel;
490
-
491
- const scrollToBottom = useCallback(() => {
492
- if (currentConversation.id) {
493
- ref.current?.scrollIntoView({ behavior: 'instant' });
494
- }
495
- }, [currentConversation]);
496
-
497
- useEffect(() => {
498
- scrollToBottom();
499
- }, [scrollToBottom]);
500
-
501
- return ref;
502
- };
503
-
504
  export const useGetFileIcon = () => {
505
  // const req = require.context('@/assets/svg/file-icon');
506
  // const ret = req.keys().map(req);
 
374
  const dispatch = useDispatch();
375
  const { dialogId } = useGetChatSearchParams();
376
 
377
+ const setConversation = useCallback(
378
+ (message: string) => {
379
+ return dispatch<any>({
380
+ type: 'chatModel/setConversation',
381
+ payload: {
382
+ // conversation_id: '',
383
+ dialog_id: dialogId,
384
+ name: message,
385
+ message: [
386
+ {
387
+ role: MessageType.Assistant,
388
+ content: message,
389
+ },
390
+ ],
391
+ },
392
+ });
393
+ },
394
+ [dispatch, dialogId],
395
+ );
396
 
397
  return { setConversation };
398
  };
399
 
400
  export const useSelectCurrentConversation = () => {
401
+ const [currentConversation, setCurrentConversation] =
402
+ useState<IClientConversation>({} as IClientConversation);
403
+
404
  const conversation: IClientConversation = useSelector(
405
  (state: any) => state.chatModel.currentConversation,
406
  );
407
 
408
+ const addNewestConversation = useCallback((message: string) => {
409
+ setCurrentConversation((pre) => {
410
+ return {
411
+ ...pre,
412
+ message: [
413
+ ...pre.message,
414
+ {
415
+ role: MessageType.User,
416
+ content: message,
417
+ id: uuid(),
418
+ } as IMessage,
419
+ ],
420
+ };
421
+ });
422
+ }, []);
423
+
424
+ useEffect(() => {
425
+ console.info('useSelectCurrentConversation: 1', currentConversation);
426
+ }, [currentConversation]);
427
+
428
+ useEffect(() => {
429
+ console.info('useSelectCurrentConversation: 2', conversation);
430
+
431
+ setCurrentConversation(conversation);
432
+ }, [conversation]);
433
+
434
+ return { currentConversation, addNewestConversation };
435
  };
436
 
437
  export const useFetchConversation = () => {
 
453
  return fetchConversation;
454
  };
455
 
456
+ export const useScrollToBottom = (currentConversation: IClientConversation) => {
457
+ const ref = useRef<HTMLDivElement>(null);
458
+
459
+ const scrollToBottom = useCallback(() => {
460
+ console.info('useScrollToBottom');
461
+ if (currentConversation.id) {
462
+ ref.current?.scrollIntoView({ behavior: 'instant' });
463
+ }
464
+ }, [currentConversation]);
465
+
466
+ useEffect(() => {
467
+ scrollToBottom();
468
+ }, [scrollToBottom]);
469
+
470
+ return ref;
471
+ };
472
+
473
  export const useFetchConversationOnMount = () => {
474
  const { conversationId } = useGetChatSearchParams();
 
475
  const setCurrentConversation = useSetCurrentConversation();
476
  const fetchConversation = useFetchConversation();
477
+ const { currentConversation, addNewestConversation } =
478
+ useSelectCurrentConversation();
479
+ const ref = useScrollToBottom(currentConversation);
480
 
481
  const fetchConversationOnMount = useCallback(() => {
482
  if (isConversationIdExist(conversationId)) {
 
490
  fetchConversationOnMount();
491
  }, [fetchConversationOnMount]);
492
 
493
+ return { currentConversation, addNewestConversation, ref };
494
  };
495
 
496
  export const useSendMessage = () => {
497
  const dispatch = useDispatch();
498
  const { setConversation } = useSetConversation();
499
  const { conversationId } = useGetChatSearchParams();
500
+ const conversation: IClientConversation = useSelector(
501
  (state: any) => state.chatModel.currentConversation,
502
  );
503
+
504
  const { handleClickConversation } = useClickConversationCard();
505
 
506
+ const sendMessage = useCallback(
507
+ (message: string, id?: string) => {
508
+ dispatch({
509
+ type: 'chatModel/completeConversation',
510
+ payload: {
511
+ conversation_id: id ?? conversationId,
512
+ messages: [
513
+ ...(conversation?.message ?? []).map((x: IMessage) =>
514
+ omit(x, 'id'),
515
+ ),
516
+ {
517
+ role: MessageType.User,
518
+ content: message,
519
+ },
520
+ ],
521
+ },
522
+ });
523
+ },
524
+ [dispatch, conversation?.message, conversationId],
525
+ );
526
 
527
+ const handleSendMessage = useCallback(
528
+ async (message: string) => {
529
+ if (conversationId !== '') {
530
+ sendMessage(message);
531
+ } else {
532
+ const data = await setConversation(message);
533
+ if (data.retcode === 0) {
534
+ const id = data.data.id;
535
+ handleClickConversation(id);
536
+ sendMessage(message, id);
537
+ }
538
  }
539
+ },
540
+ [conversationId, handleClickConversation, setConversation, sendMessage],
541
+ );
542
 
543
  return { sendMessage: handleSendMessage };
544
  };
545
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
546
  export const useGetFileIcon = () => {
547
  // const req = require.context('@/assets/svg/file-icon');
548
  // const ret = req.keys().map(req);