forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotChatSuggestionView.tsx
More file actions
134 lines (121 loc) · 3.41 KB
/
Copy pathCopilotChatSuggestionView.tsx
File metadata and controls
134 lines (121 loc) · 3.41 KB
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
import React from "react";
import { Suggestion } from "@copilotkit/core";
import { renderSlot, WithSlots } from "../../lib/slots";
import { cn } from "../../lib/utils";
import CopilotChatSuggestionPill, {
CopilotChatSuggestionPillProps,
} from "./CopilotChatSuggestionPill";
const DefaultContainer = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(function DefaultContainer({ className, ...props }, ref) {
return (
<div
ref={ref}
data-copilotkit
data-testid="copilot-suggestions"
className={cn(
"cpk:flex cpk:flex-wrap cpk:items-center cpk:gap-1.5 cpk:sm:gap-2 cpk:pl-0 cpk:pr-4 cpk:sm:px-0 cpk:pointer-events-none",
className,
)}
{...props}
/>
);
});
export type CopilotChatSuggestionViewProps = WithSlots<
{
container: typeof DefaultContainer;
suggestion: typeof CopilotChatSuggestionPill;
},
{
suggestions: Suggestion[];
onSelectSuggestion?: (suggestion: Suggestion, index: number) => void;
loadingIndexes?: ReadonlyArray<number>;
} & React.HTMLAttributes<HTMLDivElement>
>;
export const CopilotChatSuggestionView = React.forwardRef<
HTMLDivElement,
CopilotChatSuggestionViewProps
>(function CopilotChatSuggestionView(
{
suggestions,
onSelectSuggestion,
loadingIndexes,
container,
suggestion: suggestionSlot,
className,
children,
...restProps
},
ref,
) {
const loadingSet = React.useMemo(() => {
if (!loadingIndexes || loadingIndexes.length === 0) {
return new Set<number>();
}
return new Set(loadingIndexes);
}, [loadingIndexes]);
const ContainerElement = renderSlot(container, DefaultContainer, {
ref,
className,
...restProps,
});
const suggestionElements = suggestions.map((suggestion, index) => {
const isLoading = loadingSet.has(index) || suggestion.isLoading === true;
const pill = renderSlot<
typeof CopilotChatSuggestionPill,
CopilotChatSuggestionPillProps
>(suggestionSlot, CopilotChatSuggestionPill, {
children: suggestion.title,
className: suggestion.className,
isLoading,
type: "button",
onClick: () => onSelectSuggestion?.(suggestion, index),
});
return React.cloneElement(pill, {
key: `${suggestion.title}-${index}`,
});
});
const boundContainer = React.cloneElement(
ContainerElement,
undefined,
suggestionElements,
);
if (typeof children === "function") {
const sampleSuggestion = renderSlot<
typeof CopilotChatSuggestionPill,
CopilotChatSuggestionPillProps
>(suggestionSlot, CopilotChatSuggestionPill, {
children: suggestions[0]?.title ?? "",
isLoading:
suggestions.length > 0
? loadingSet.has(0) || suggestions[0]?.isLoading === true
: false,
type: "button",
});
return (
<div data-copilotkit style={{ display: "contents" }}>
{children({
container: boundContainer,
suggestion: sampleSuggestion,
suggestions,
onSelectSuggestion,
loadingIndexes,
className,
...restProps,
})}
</div>
);
}
if (children) {
return (
<div data-copilotkit style={{ display: "contents" }}>
{boundContainer}
{children}
</div>
);
}
return boundContainer;
});
CopilotChatSuggestionView.displayName = "CopilotChatSuggestionView";
export default CopilotChatSuggestionView;