react-optimistic-chat 1.2.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +23 -0
- package/README.md +845 -0
- package/dist/index.d.mts +54 -45
- package/dist/index.d.ts +54 -45
- package/dist/index.js +173 -74
- package/dist/index.mjs +173 -74
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -20,7 +20,7 @@ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
|
20
20
|
|
|
21
21
|
// src/components/indicators/LoadingSpinner.tsx
|
|
22
22
|
import { jsx } from "react/jsx-runtime";
|
|
23
|
-
function LoadingSpinner({ size }) {
|
|
23
|
+
function LoadingSpinner({ size = "md" }) {
|
|
24
24
|
const sizeMap = {
|
|
25
25
|
xs: 24,
|
|
26
26
|
sm: 32,
|
|
@@ -490,20 +490,30 @@ function ChatContainer(props) {
|
|
|
490
490
|
disableVoice,
|
|
491
491
|
placeholder,
|
|
492
492
|
inputClassName,
|
|
493
|
+
fetchNextPage,
|
|
494
|
+
hasNextPage,
|
|
495
|
+
isFetchingNextPage,
|
|
493
496
|
className
|
|
494
497
|
} = props;
|
|
495
498
|
const mappedMessages = typeof props.messageMapper === "function" ? props.messages.map(props.messageMapper) : messages;
|
|
496
499
|
useEffect3(() => {
|
|
497
500
|
const el = scrollRef.current;
|
|
498
501
|
if (!el) return;
|
|
499
|
-
|
|
500
|
-
const handleScroll = () => {
|
|
502
|
+
const handleScroll = async () => {
|
|
501
503
|
const isBottom = el.scrollTop + el.clientHeight >= el.scrollHeight - 10;
|
|
502
504
|
setIsAtBottom(isBottom);
|
|
505
|
+
if (el.scrollTop === 0 && hasNextPage && !isFetchingNextPage && fetchNextPage) {
|
|
506
|
+
const prevScrollHeight = el.scrollHeight;
|
|
507
|
+
await fetchNextPage();
|
|
508
|
+
requestAnimationFrame(() => {
|
|
509
|
+
const newScrollHeight = el.scrollHeight;
|
|
510
|
+
el.scrollTop = newScrollHeight - prevScrollHeight;
|
|
511
|
+
});
|
|
512
|
+
}
|
|
503
513
|
};
|
|
504
514
|
el.addEventListener("scroll", handleScroll);
|
|
505
515
|
return () => el.removeEventListener("scroll", handleScroll);
|
|
506
|
-
}, []);
|
|
516
|
+
}, [fetchNextPage, hasNextPage, isFetchingNextPage]);
|
|
507
517
|
useEffect3(() => {
|
|
508
518
|
const el = scrollRef.current;
|
|
509
519
|
if (!el) return;
|
|
@@ -531,17 +541,20 @@ function ChatContainer(props) {
|
|
|
531
541
|
flex flex-col ${className || ""}
|
|
532
542
|
`,
|
|
533
543
|
children: [
|
|
534
|
-
/* @__PURE__ */
|
|
544
|
+
/* @__PURE__ */ jsxs4(
|
|
535
545
|
"div",
|
|
536
546
|
{
|
|
537
547
|
ref: scrollRef,
|
|
538
548
|
className: `flex-1 overflow-y-auto chatContainer-scroll p-2`,
|
|
539
|
-
children:
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
549
|
+
children: [
|
|
550
|
+
hasNextPage && isFetchingNextPage && /* @__PURE__ */ jsx6("div", { className: "flex justify-center py-2", children: /* @__PURE__ */ jsx6(LoadingSpinner, { size: "sm" }) }),
|
|
551
|
+
/* @__PURE__ */ jsx6(
|
|
552
|
+
ChatList,
|
|
553
|
+
__spreadValues(__spreadValues(__spreadValues({
|
|
554
|
+
messages: mappedMessages
|
|
555
|
+
}, messageRenderer && { messageRenderer }), loadingRenderer && { loadingRenderer }), listClassName && { className: listClassName })
|
|
556
|
+
)
|
|
557
|
+
]
|
|
545
558
|
}
|
|
546
559
|
),
|
|
547
560
|
/* @__PURE__ */ jsxs4("div", { className: "flex-shrink-0 relative", children: [
|
|
@@ -584,12 +597,26 @@ function ChatContainer(props) {
|
|
|
584
597
|
) });
|
|
585
598
|
}
|
|
586
599
|
|
|
587
|
-
// src/hooks/
|
|
588
|
-
import {
|
|
600
|
+
// src/hooks/useChat.ts
|
|
601
|
+
import { useInfiniteQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
|
589
602
|
import { useState as useState4 } from "react";
|
|
590
|
-
function
|
|
603
|
+
function splitRawToMessage(raw, mapped) {
|
|
604
|
+
const custom = {};
|
|
605
|
+
const mappedValues = new Set(Object.values(mapped));
|
|
606
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
607
|
+
if (!mappedValues.has(value)) {
|
|
608
|
+
custom[key] = value;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
return __spreadProps(__spreadValues({}, mapped), {
|
|
612
|
+
custom
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
function useChat({
|
|
591
616
|
queryKey,
|
|
592
617
|
queryFn,
|
|
618
|
+
initialPageParam,
|
|
619
|
+
getNextPageParam,
|
|
593
620
|
mutationFn,
|
|
594
621
|
map,
|
|
595
622
|
onError,
|
|
@@ -599,17 +626,26 @@ function useOptimisticChat({
|
|
|
599
626
|
const [isPending, setIsPending] = useState4(false);
|
|
600
627
|
const queryClient = useQueryClient();
|
|
601
628
|
const {
|
|
602
|
-
data
|
|
603
|
-
isLoading: isInitialLoading
|
|
604
|
-
|
|
629
|
+
data,
|
|
630
|
+
isLoading: isInitialLoading,
|
|
631
|
+
fetchNextPage,
|
|
632
|
+
hasNextPage,
|
|
633
|
+
isFetchingNextPage
|
|
634
|
+
} = useInfiniteQuery({
|
|
605
635
|
queryKey,
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
636
|
+
initialPageParam,
|
|
637
|
+
queryFn: async ({ pageParam }) => {
|
|
638
|
+
const raw = await queryFn(pageParam);
|
|
639
|
+
return raw.map((r) => {
|
|
640
|
+
const mapped = map(r);
|
|
641
|
+
return splitRawToMessage(r, mapped);
|
|
642
|
+
});
|
|
609
643
|
},
|
|
644
|
+
getNextPageParam,
|
|
610
645
|
staleTime,
|
|
611
646
|
gcTime
|
|
612
647
|
});
|
|
648
|
+
const messages = data ? [...data.pages].reverse().flat() : [];
|
|
613
649
|
const mutation = useMutation({
|
|
614
650
|
mutationFn,
|
|
615
651
|
// (content: string) => Promise<TMutationRaw>
|
|
@@ -620,38 +656,47 @@ function useOptimisticChat({
|
|
|
620
656
|
await queryClient.cancelQueries({ queryKey });
|
|
621
657
|
}
|
|
622
658
|
queryClient.setQueryData(queryKey, (old) => {
|
|
623
|
-
|
|
624
|
-
return
|
|
625
|
-
|
|
626
|
-
|
|
659
|
+
var _a;
|
|
660
|
+
if (!old) return old;
|
|
661
|
+
const pages = [...old.pages];
|
|
662
|
+
const firstPage = (_a = pages[0]) != null ? _a : [];
|
|
663
|
+
pages[0] = [
|
|
664
|
+
...firstPage,
|
|
627
665
|
{
|
|
628
666
|
id: crypto.randomUUID(),
|
|
629
667
|
role: "USER",
|
|
630
|
-
content
|
|
668
|
+
content,
|
|
669
|
+
custom: {}
|
|
631
670
|
},
|
|
632
|
-
// AI placeholder 추가
|
|
633
671
|
{
|
|
634
672
|
id: crypto.randomUUID(),
|
|
635
673
|
role: "AI",
|
|
636
674
|
content: "",
|
|
637
|
-
isLoading: true
|
|
675
|
+
isLoading: true,
|
|
676
|
+
custom: {}
|
|
638
677
|
}
|
|
639
678
|
];
|
|
679
|
+
return __spreadProps(__spreadValues({}, old), {
|
|
680
|
+
pages
|
|
681
|
+
});
|
|
640
682
|
});
|
|
641
683
|
return prev ? { prev } : {};
|
|
642
684
|
},
|
|
643
685
|
onSuccess: (rawAiResponse) => {
|
|
644
|
-
const
|
|
686
|
+
const mapped = map(rawAiResponse);
|
|
687
|
+
const aiMessage = splitRawToMessage(rawAiResponse, mapped);
|
|
645
688
|
queryClient.setQueryData(queryKey, (old) => {
|
|
646
|
-
if (!old
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
const
|
|
650
|
-
|
|
651
|
-
next[lastIndex] = __spreadProps(__spreadValues(__spreadValues({}, next[lastIndex]), aiMessage), {
|
|
689
|
+
if (!old) return old;
|
|
690
|
+
const pages = [...old.pages];
|
|
691
|
+
const firstPage = [...pages[0]];
|
|
692
|
+
const lastIndex = firstPage.length - 1;
|
|
693
|
+
firstPage[lastIndex] = __spreadProps(__spreadValues(__spreadValues({}, firstPage[lastIndex]), aiMessage), {
|
|
652
694
|
isLoading: false
|
|
653
695
|
});
|
|
654
|
-
|
|
696
|
+
pages[0] = firstPage;
|
|
697
|
+
return __spreadProps(__spreadValues({}, old), {
|
|
698
|
+
pages
|
|
699
|
+
});
|
|
655
700
|
});
|
|
656
701
|
setIsPending(false);
|
|
657
702
|
},
|
|
@@ -674,17 +719,35 @@ function useOptimisticChat({
|
|
|
674
719
|
// (content: string) => void
|
|
675
720
|
isPending,
|
|
676
721
|
// 사용자가 채팅 전송 후 AI 응답이 올 때까지의 로딩
|
|
677
|
-
isInitialLoading
|
|
722
|
+
isInitialLoading,
|
|
678
723
|
// 초기 로딩 상태
|
|
724
|
+
// infinite query용
|
|
725
|
+
fetchNextPage,
|
|
726
|
+
hasNextPage,
|
|
727
|
+
isFetchingNextPage
|
|
679
728
|
};
|
|
680
729
|
}
|
|
681
730
|
|
|
682
|
-
// src/hooks/
|
|
683
|
-
import {
|
|
731
|
+
// src/hooks/useVoiceChat.ts
|
|
732
|
+
import { useInfiniteQuery as useInfiniteQuery2, useMutation as useMutation2, useQueryClient as useQueryClient2 } from "@tanstack/react-query";
|
|
684
733
|
import { useEffect as useEffect4, useRef as useRef4, useState as useState5 } from "react";
|
|
685
|
-
function
|
|
734
|
+
function splitRawToMessage2(raw, mapped) {
|
|
735
|
+
const custom = {};
|
|
736
|
+
const mappedValues = new Set(Object.values(mapped));
|
|
737
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
738
|
+
if (!mappedValues.has(value)) {
|
|
739
|
+
custom[key] = value;
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
return __spreadProps(__spreadValues({}, mapped), {
|
|
743
|
+
custom
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
function useVoiceChat({
|
|
686
747
|
queryKey,
|
|
687
748
|
queryFn,
|
|
749
|
+
initialPageParam,
|
|
750
|
+
getNextPageParam,
|
|
688
751
|
mutationFn,
|
|
689
752
|
map,
|
|
690
753
|
voice,
|
|
@@ -697,17 +760,26 @@ function useVoiceOptimisticChat({
|
|
|
697
760
|
const currentTextRef = useRef4("");
|
|
698
761
|
const rollbackRef = useRef4(void 0);
|
|
699
762
|
const {
|
|
700
|
-
data
|
|
701
|
-
isLoading: isInitialLoading
|
|
702
|
-
|
|
763
|
+
data,
|
|
764
|
+
isLoading: isInitialLoading,
|
|
765
|
+
fetchNextPage,
|
|
766
|
+
hasNextPage,
|
|
767
|
+
isFetchingNextPage
|
|
768
|
+
} = useInfiniteQuery2({
|
|
703
769
|
queryKey,
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
770
|
+
initialPageParam,
|
|
771
|
+
queryFn: async ({ pageParam }) => {
|
|
772
|
+
const raw = await queryFn(pageParam);
|
|
773
|
+
return raw.map((r) => {
|
|
774
|
+
const mapped = map(r);
|
|
775
|
+
return splitRawToMessage2(r, mapped);
|
|
776
|
+
});
|
|
707
777
|
},
|
|
778
|
+
getNextPageParam,
|
|
708
779
|
staleTime,
|
|
709
780
|
gcTime
|
|
710
781
|
});
|
|
782
|
+
const messages = data ? data.pages.map((page) => [...page]).reverse().flat() : [];
|
|
711
783
|
const mutation = useMutation2({
|
|
712
784
|
mutationFn,
|
|
713
785
|
// (content: string) => Promise<TMutationRaw>
|
|
@@ -718,32 +790,41 @@ function useVoiceOptimisticChat({
|
|
|
718
790
|
await queryClient.cancelQueries({ queryKey });
|
|
719
791
|
}
|
|
720
792
|
queryClient.setQueryData(queryKey, (old) => {
|
|
721
|
-
|
|
722
|
-
return
|
|
723
|
-
|
|
724
|
-
|
|
793
|
+
var _a;
|
|
794
|
+
if (!old) return old;
|
|
795
|
+
const pages = [...old.pages];
|
|
796
|
+
const firstPage = (_a = pages[0]) != null ? _a : [];
|
|
797
|
+
pages[0] = [
|
|
798
|
+
...firstPage,
|
|
725
799
|
{
|
|
726
800
|
id: crypto.randomUUID(),
|
|
727
801
|
role: "AI",
|
|
728
802
|
content: "",
|
|
729
|
-
isLoading: true
|
|
803
|
+
isLoading: true,
|
|
804
|
+
custom: {}
|
|
730
805
|
}
|
|
731
806
|
];
|
|
807
|
+
return __spreadProps(__spreadValues({}, old), {
|
|
808
|
+
pages
|
|
809
|
+
});
|
|
732
810
|
});
|
|
733
811
|
return prev ? { prev } : {};
|
|
734
812
|
},
|
|
735
813
|
onSuccess: (rawAiResponse) => {
|
|
736
|
-
const
|
|
814
|
+
const mapped = map(rawAiResponse);
|
|
815
|
+
const aiMessage = splitRawToMessage2(rawAiResponse, mapped);
|
|
737
816
|
queryClient.setQueryData(queryKey, (old) => {
|
|
738
|
-
if (!old
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
const
|
|
742
|
-
|
|
743
|
-
next[lastIndex] = __spreadProps(__spreadValues(__spreadValues({}, next[lastIndex]), aiMessage), {
|
|
817
|
+
if (!old) return old;
|
|
818
|
+
const pages = [...old.pages];
|
|
819
|
+
const firstPage = [...pages[0]];
|
|
820
|
+
const lastIndex = firstPage.length - 1;
|
|
821
|
+
firstPage[lastIndex] = __spreadProps(__spreadValues(__spreadValues({}, firstPage[lastIndex]), aiMessage), {
|
|
744
822
|
isLoading: false
|
|
745
823
|
});
|
|
746
|
-
|
|
824
|
+
pages[0] = firstPage;
|
|
825
|
+
return __spreadProps(__spreadValues({}, old), {
|
|
826
|
+
pages
|
|
827
|
+
});
|
|
747
828
|
});
|
|
748
829
|
setIsPending(false);
|
|
749
830
|
},
|
|
@@ -762,14 +843,24 @@ function useVoiceOptimisticChat({
|
|
|
762
843
|
if (prev) {
|
|
763
844
|
await queryClient.cancelQueries({ queryKey });
|
|
764
845
|
}
|
|
765
|
-
queryClient.setQueryData(queryKey, (old) =>
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
846
|
+
queryClient.setQueryData(queryKey, (old) => {
|
|
847
|
+
var _a;
|
|
848
|
+
if (!old) return old;
|
|
849
|
+
const pages = [...old.pages];
|
|
850
|
+
const firstPage = (_a = pages[0]) != null ? _a : [];
|
|
851
|
+
pages[0] = [
|
|
852
|
+
...firstPage,
|
|
853
|
+
{
|
|
854
|
+
id: crypto.randomUUID(),
|
|
855
|
+
role: "USER",
|
|
856
|
+
content: "",
|
|
857
|
+
custom: {}
|
|
858
|
+
}
|
|
859
|
+
];
|
|
860
|
+
return __spreadProps(__spreadValues({}, old), {
|
|
861
|
+
pages
|
|
862
|
+
});
|
|
863
|
+
});
|
|
773
864
|
voice.start();
|
|
774
865
|
};
|
|
775
866
|
const onTranscript = (text) => {
|
|
@@ -777,13 +868,17 @@ function useVoiceOptimisticChat({
|
|
|
777
868
|
queryClient.setQueryData(queryKey, (old) => {
|
|
778
869
|
var _a;
|
|
779
870
|
if (!old) return old;
|
|
780
|
-
const
|
|
781
|
-
const
|
|
782
|
-
|
|
783
|
-
|
|
871
|
+
const pages = [...old.pages];
|
|
872
|
+
const firstPage = [...pages[0]];
|
|
873
|
+
const lastIndex = firstPage.length - 1;
|
|
874
|
+
if (((_a = firstPage[lastIndex]) == null ? void 0 : _a.role) !== "USER") return old;
|
|
875
|
+
firstPage[lastIndex] = __spreadProps(__spreadValues({}, firstPage[lastIndex]), {
|
|
784
876
|
content: text
|
|
785
877
|
});
|
|
786
|
-
|
|
878
|
+
pages[0] = firstPage;
|
|
879
|
+
return __spreadProps(__spreadValues({}, old), {
|
|
880
|
+
pages
|
|
881
|
+
});
|
|
787
882
|
});
|
|
788
883
|
};
|
|
789
884
|
useEffect4(() => {
|
|
@@ -809,8 +904,12 @@ function useVoiceOptimisticChat({
|
|
|
809
904
|
// 초기 로딩 상태
|
|
810
905
|
startRecording,
|
|
811
906
|
// 음성 인식 시작 함수
|
|
812
|
-
stopRecording
|
|
907
|
+
stopRecording,
|
|
813
908
|
// 음성 인식 종료 함수
|
|
909
|
+
// infinite query용
|
|
910
|
+
fetchNextPage,
|
|
911
|
+
hasNextPage,
|
|
912
|
+
isFetchingNextPage
|
|
814
913
|
};
|
|
815
914
|
}
|
|
816
915
|
export {
|
|
@@ -821,6 +920,6 @@ export {
|
|
|
821
920
|
LoadingSpinner,
|
|
822
921
|
SendingDots,
|
|
823
922
|
useBrowserSpeechRecognition,
|
|
824
|
-
|
|
825
|
-
|
|
923
|
+
useChat,
|
|
924
|
+
useVoiceChat
|
|
826
925
|
};
|