Skip to content
This repository was archived by the owner on Mar 26, 2025. It is now read-only.

Commit 1e43db2

Browse files
committed
Add UI components for handling API responses
1 parent 9e1a943 commit 1e43db2

File tree

1 file changed

+147
-1
lines changed

1 file changed

+147
-1
lines changed

docs/widget/build-your-own-ui.mdx

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,156 @@ export default OpencopilotCustomWidget;
7070
5. So we will ge to the [dashboard](https://cloud.opencopilot.so/) to setup some actions and then we will be able to execute them from the chat interface.
7171
Click on the Copilot you just created and then click on the `Actions` tab.
7272

73-
use any API you want to use, for example we will use dummy data returned by jsonplaceholder.
73+
use any API you want to use, for example we will use dummy data returned by jsonplaceholder.
7474

7575
<Note>
7676
In this demo we are testing the UI handling, so we will not go into the
7777
details of creating actions.
7878
</Note>
7979

80+
6. The trick behind UI response is that you know excatly what the shape of the data will be returned by the copilot,
81+
so we are going to set key which is the action name and a value which is the UI component that will handle the response.
82+
83+
```tsx
84+
import type { ComponentType } from "@openchatai/copilot-widget";
85+
const components: ComponentType[] = [];
86+
```
87+
88+
let's add this to the options object.
89+
90+
```tsx
91+
const options: Options = {
92+
token: "YOUR_TOKEN",
93+
apiUrl: "https://api.opencopilot.so/backend",
94+
socketUrl: "https://api.opencopilot.so",
95+
initialMessage: "Hello, I need help!",
96+
triggerSelector: "",
97+
defaultOpen: true,
98+
components,
99+
};
100+
```
101+
102+
7. Let's review our action api response and see what we can do with it.
103+
- [GET todos api](https://jsonplaceholder.typicode.com/todos)
104+
```json
105+
[{
106+
"userId": 1,
107+
"id": 1,
108+
"title": "delectus aut autem",
109+
"completed": false
110+
}]
111+
```
112+
113+
- [GET todo by id](https://jsonplaceholder.typicode.com/todos/{{todo_id}})
114+
```json
115+
{
116+
"userId": 1,
117+
"id": 1,
118+
"title": "delectus aut autem",
119+
"completed": false
120+
}
121+
```
122+
123+
8. Let's start with the first action, we will create a component that will handle the response of the first action.
124+
125+
```tsx
126+
import { ComponentType,ComponentProps } from "@openchatai/copilot-widget";
127+
128+
const components: ComponentType[] = [
129+
{
130+
key: "getAllTodos",
131+
component: (
132+
props: ComponentProps<
133+
{
134+
userId: number;
135+
id: number;
136+
title: string;
137+
completed: boolean;
138+
}[]
139+
>
140+
) => {
141+
return (
142+
<div className="w-full p-4">
143+
<Card className="">
144+
<CardHeader>
145+
<CardTitle>Todos</CardTitle>
146+
<CardDescription>Found {props.data.length} Todos</CardDescription>
147+
</CardHeader>
148+
<CardContent>
149+
<Table>
150+
<TableHeader>
151+
<TableRow>
152+
<TableHead>UserId</TableHead>
153+
<TableHead>Id</TableHead>
154+
<TableHead>Title</TableHead>
155+
<TableHead>Completed</TableHead>
156+
</TableRow>
157+
</TableHeader>
158+
<TableBody>
159+
{props.data.map((todo) => (
160+
<TableRow key={todo.id}>
161+
<TableCell>{todo.userId}</TableCell>
162+
<TableCell>{todo.id}</TableCell>
163+
<TableCell>{todo.title}</TableCell>
164+
<TableCell>{todo.completed ? "Yes" : "No"}</TableCell>
165+
</TableRow>
166+
))}
167+
</TableBody>
168+
</Table>
169+
</CardContent>
170+
</Card>
171+
</div>
172+
);
173+
},
174+
},
175+
];
176+
177+
```
178+
179+
9. Let's complete the second action, which is to get a todo by id.
180+
just add this to components object
181+
182+
```tsx
183+
{
184+
key: "getTodoById",
185+
component: (
186+
props: ComponentProps<{
187+
userId: number;
188+
id: number;
189+
title: string;
190+
completed: boolean;
191+
}>
192+
) => {
193+
return (
194+
<div className="w-full p-4">
195+
<Card className="">
196+
<CardHeader>
197+
<CardTitle>Todo</CardTitle>
198+
<CardDescription>Found a Todo</CardDescription>
199+
</CardHeader>
200+
<CardContent>
201+
<Table>
202+
<TableHeader>
203+
<TableRow>
204+
<TableHead>UserId</TableHead>
205+
<TableHead>Id</TableHead>
206+
<TableHead>Title</TableHead>
207+
<TableHead>Completed</TableHead>
208+
</TableRow>
209+
</TableHeader>
210+
<TableBody>
211+
<TableRow>
212+
<TableCell>{props.data.userId}</TableCell>
213+
<TableCell>{props.data.id}</TableCell>
214+
<TableCell>{props.data.title}</TableCell>
215+
<TableCell>{props.data.completed ? "Yes" : "No"}</TableCell>
216+
</TableRow>
217+
</TableBody>
218+
</Table>
219+
</CardContent>
220+
</Card>
221+
</div>
222+
);
223+
},
224+
},
225+
```

0 commit comments

Comments
 (0)