Repository navigation
Expand file tree
/
Copy pathConversationAgentFilter.tsx
More file actions
138 lines (136 loc) · 4.17 KB
/
Copy pathConversationAgentFilter.tsx
File metadata and controls
138 lines (136 loc) · 4.17 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
135
136
137
138
import * as uiMessages from "./messages";
import { useEffect, useId, useRef, useState } from "react";
import ProviderIcon from "./ProviderIcon";
import type { Agent, Project, Translate } from "./types";
import { Icon } from "./ui";
export default function ConversationAgentFilter({
agents,
projects,
selected,
onChange,
t,
}: {
agents: Agent[];
projects: Project[];
selected: string[];
onChange: (ids: string[]) => void;
t: Translate;
}) {
const [open, setOpen] = useState(false);
const id = useId();
const root = useRef<HTMLDivElement>(null);
const trigger = useRef<HTMLButtonElement>(null);
const first = useRef<HTMLButtonElement>(null);
const label = t(
...uiMessages.conversationagentfilter_filter_conversations_by_agent_1bed51,
);
const close = () => {
setOpen(false);
trigger.current?.focus();
};
useEffect(() => {
if (!open) return;
first.current?.focus();
const dismiss = (event: PointerEvent) => {
if (!root.current?.contains(event.target as Node)) setOpen(false);
};
document.addEventListener("pointerdown", dismiss);
return () => document.removeEventListener("pointerdown", dismiss);
}, [open]);
return (
<div
className="conversation-agent-filter"
ref={root}
onBlur={(event) => {
if (!event.currentTarget.contains(event.relatedTarget)) setOpen(false);
}}
onKeyDown={(event) => {
if (open && event.key === "Escape") {
event.preventDefault();
event.stopPropagation();
close();
}
}}
>
<button
type="button"
ref={trigger}
className={`icon-button${selected.length ? " active" : ""}`}
aria-label={label}
title={label}
aria-expanded={open}
aria-controls={open ? id : undefined}
onClick={() => setOpen(!open)}
>
<Icon name="agent" />
{selected.length > 0 && <span className="filter-dot" />}
</button>
{open && (
<div
id={id}
className="conversation-agent-options"
role="group"
aria-label={label}
>
<div className="panel-heading">
<strong>
{t(...uiMessages.conversationagentfilter_filter_agents_354ca8)}
</strong>
<button
type="button"
className="icon-button"
aria-label={t(
...uiMessages.conversationagentfilter_close_agent_filter_08200d,
)}
onClick={close}
>
<Icon name="close" size={16} />
</button>
</div>
<button
ref={first}
type="button"
className="conversation-agent-all"
aria-pressed={!selected.length}
onClick={() => onChange([])}
>
{t(...uiMessages.conversationagentfilter_all_agents_60d33a)}
{!selected.length && <span aria-hidden="true">✓</span>}
</button>
<div className="conversation-agent-choices">
{agents.map((agent) => (
<label key={agent.id}>
<input
type="checkbox"
checked={selected.includes(agent.id)}
onChange={(event) =>
onChange(
event.target.checked
? [...selected, agent.id]
: selected.filter((value) => value !== agent.id),
)
}
/>
<ProviderIcon provider={agent.provider} size={20} />
<span>
<strong>{agent.name}</strong>
<small>
{projects.find((p) => p.id === agent.project_id)?.name ??
t(
...uiMessages.conversationagentfilter_everyday_chats_0dc423,
)}
</small>
</span>
</label>
))}
{!agents.length && (
<p className="muted">
{t(...uiMessages.conversationagentfilter_no_agents_696385)}
</p>
)}
</div>
</div>
)}
</div>
);
}