Skip to content

Commit 6bf8982

Browse files
authored
Introduce delete button in chatbot-list-view.tsx (#1829)
1 parent c31a19b commit 6bf8982

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

packages/patterns/chatbot-list-view.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,40 @@ type Output = {
4747
selectedCharm: Default<{ charm: any }, { charm: undefined }>;
4848
};
4949

50+
const removeChat = handler<
51+
unknown,
52+
{
53+
charmsList: Cell<CharmEntry[]>;
54+
id: string;
55+
selectedCharm: Cell<Default<{ charm: any }, { charm: undefined }>>;
56+
}
57+
>(
58+
(
59+
_,
60+
{ charmsList, id, selectedCharm },
61+
) => {
62+
const list = charmsList.get();
63+
const index = list.findIndex((entry) => entry.local_id === id);
64+
if (index === -1) return;
65+
66+
const removed = list[index];
67+
const next = [...list];
68+
next.splice(index, 1);
69+
charmsList.set(next);
70+
71+
// If we removed the currently selected charm, choose a new selection.
72+
const current = selectedCharm.get();
73+
if (current?.charm === removed.charm) {
74+
const replacement = next[index] ?? next[index - 1];
75+
if (replacement) {
76+
selectedCharm.set({ charm: replacement.charm });
77+
} else {
78+
selectedCharm.set({ charm: undefined as unknown as any });
79+
}
80+
}
81+
},
82+
);
83+
5084
// this will be called whenever charm or selectedCharm changes
5185
// pass isInitialized to make sure we dont call this each time
5286
// we change selectedCharm, otherwise creates a loop
@@ -286,6 +320,23 @@ export default recipe<Input, Output>(
286320
>
287321
<span>{charmEntry.charm[NAME]}</span>
288322
<span slot="meta">{charmEntry.local_id}</span>
323+
<ct-button
324+
slot="actions"
325+
size="sm"
326+
title="Delete Chat"
327+
variant="destructive"
328+
onClick={removeChat({
329+
charmsList: charmsList as unknown as OpaqueRef<
330+
CharmEntry[]
331+
>,
332+
id: charmEntry.local_id,
333+
selectedCharm: selectedCharm as unknown as OpaqueRef<
334+
Default<{ charm: any }, { charm: undefined }>
335+
>,
336+
})}
337+
>
338+
🗑️
339+
</ct-button>
289340
</ct-list-item>
290341
))}
291342
</div>

0 commit comments

Comments
 (0)