Repository navigation
Expand file tree
/
Copy pathLinearAssignmentTriggerSettings.tsx
More file actions
340 lines (332 loc) · 12.3 KB
/
Copy pathLinearAssignmentTriggerSettings.tsx
File metadata and controls
340 lines (332 loc) · 12.3 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { useAtomValue } from "@effect/atom-react";
import {
AuthOrchestrationOperateScope,
type EnvironmentId,
type LinearAssignmentTrigger,
type ModelSelection,
type ProjectId,
type ProviderInstanceId,
} from "@t3tools/contracts";
import { PencilIcon, PlusIcon, Trash2Icon } from "lucide-react";
import { type ReactNode, useMemo, useState } from "react";
import { useEnvironmentSettings, useUpdateEnvironmentSettings } from "../../hooks/useSettings";
import { randomUUID } from "../../lib/utils";
import { getCustomModelOptionsByInstance } from "../../modelSelection";
import {
applyProviderInstanceSettings,
deriveProviderInstanceEntries,
sortProviderInstanceEntries,
} from "../../providerInstances";
import { useProjects } from "../../state/entities";
import { linearEnvironment } from "../../state/linear";
import { useEnvironmentQuery } from "../../state/query";
import { EMPTY_SERVER_PROVIDERS, serverEnvironment } from "../../state/server";
import { useEnvironmentScope } from "../../state/session";
import { ProviderModelPicker } from "../chat/ProviderModelPicker";
import { Button } from "../ui/button";
import {
Dialog,
DialogClose,
DialogDescription,
DialogFooter,
DialogHeader,
DialogPanel,
DialogPopup,
DialogTitle,
} from "../ui/dialog";
import { Input } from "../ui/input";
import { Label } from "../ui/label";
import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
import { Textarea } from "../ui/textarea";
import { SETTINGS_PICKER_TRIGGER_CLASSNAME, SettingsRow } from "./settingsLayout";
import { scheduledTaskDefaultModel } from "./scheduledTasksSettings.logic";
import { searchableSetting } from "./settingsSearch";
const ANY_TEAM = "__any__";
/**
* Rules that start a thread when a Linear issue is newly assigned to the connected account.
* Stored in environment settings; the server polls Linear and starts the threads.
*/
export function LinearAssignmentTriggerRows({
environmentId,
connected,
}: {
readonly environmentId: EnvironmentId;
/** Rules stay listed while disconnected so they can still be removed; they do nothing then. */
readonly connected: boolean;
}) {
const rules = useEnvironmentSettings(
environmentId,
(settings) => settings.linearAssignmentTriggers,
);
const updateSettings = useUpdateEnvironmentSettings(environmentId);
const canOperate = useEnvironmentScope(environmentId, AuthOrchestrationOperateScope);
const projects = useProjects();
const filterOptions = useEnvironmentQuery(
connected ? linearEnvironment.filterOptions({ environmentId, input: {} }) : null,
).data;
// `null` is closed, `"new"` adds a rule, otherwise the rule being edited.
const [editing, setEditing] = useState<LinearAssignmentTrigger | "new" | null>(null);
const save = (next: ReadonlyArray<LinearAssignmentTrigger>) =>
updateSettings({ linearAssignmentTriggers: next });
return (
<>
<SettingsRow
{...searchableSetting("linear-assignment-triggers")}
title="Start threads from assignments"
description="When an issue is newly assigned to you, start a thread for it. Issues already assigned when you add a rule are skipped. Checks every 2 minutes."
control={
<Button
size="sm"
variant="outline"
disabled={!canOperate || !connected}
onClick={() => setEditing("new")}
>
<PlusIcon />
Add rule
</Button>
}
/>
{rules.map((rule) => {
const project = projects.find(
(entry) => entry.environmentId === environmentId && entry.id === rule.projectId,
);
const team = filterOptions?.teams.find((entry) => entry.id === rule.teamId);
const filters = [
rule.teamId === null ? "Any team" : (team?.name ?? "Unknown team"),
rule.labelName === null ? "any label" : `label “${rule.labelName}”`,
].join(", ");
return (
<SettingsRow
key={rule.id}
title={project?.title ?? "Missing project"}
description={`${filters} · ${rule.modelSelection.model}`}
control={
<div className="flex gap-1">
<Button
size="icon-sm"
variant="ghost"
aria-label="Edit rule"
disabled={!canOperate}
onClick={() => setEditing(rule)}
>
<PencilIcon />
</Button>
<Button
size="icon-sm"
variant="ghost"
aria-label="Remove rule"
disabled={!canOperate}
onClick={() => save(rules.filter((entry) => entry.id !== rule.id))}
>
<Trash2Icon />
</Button>
</div>
}
/>
);
})}
{editing !== null ? (
<AssignmentTriggerDialog
environmentId={environmentId}
rule={editing === "new" ? null : editing}
teams={filterOptions?.teams ?? []}
onClose={() => setEditing(null)}
onSave={(rule) => {
save(
rules.some((entry) => entry.id === rule.id)
? rules.map((entry) => (entry.id === rule.id ? rule : entry))
: [...rules, rule],
);
setEditing(null);
}}
/>
) : null}
</>
);
}
function AssignmentTriggerDialog({
environmentId,
rule,
teams,
onClose,
onSave,
}: {
readonly environmentId: EnvironmentId;
readonly rule: LinearAssignmentTrigger | null;
readonly teams: ReadonlyArray<{ readonly id: string; readonly name: string }>;
readonly onClose: () => void;
readonly onSave: (rule: LinearAssignmentTrigger) => void;
}) {
const settings = useEnvironmentSettings(environmentId);
const providers =
useAtomValue(serverEnvironment.providersValueAtom(environmentId)) ?? EMPTY_SERVER_PROVIDERS;
const allProjects = useProjects();
const projects = useMemo(
() => allProjects.filter((project) => project.environmentId === environmentId),
[allProjects, environmentId],
);
const instanceEntries = useMemo(
() =>
sortProviderInstanceEntries(
applyProviderInstanceSettings(deriveProviderInstanceEntries(providers), settings),
),
[providers, settings],
);
const [projectId, setProjectId] = useState<string>(rule?.projectId ?? projects[0]?.id ?? "");
const [teamId, setTeamId] = useState<string>(rule?.teamId ?? ANY_TEAM);
const [labelName, setLabelName] = useState(rule?.labelName ?? "");
const [prompt, setPrompt] = useState(rule?.prompt ?? "");
const [modelSelection, setModelSelection] = useState<ModelSelection | null>(
rule?.modelSelection ?? null,
);
const project = projects.find((entry) => entry.id === projectId);
const activeSelection =
modelSelection ?? scheduledTaskDefaultModel(settings, project ?? null, instanceEntries);
const activeInstanceId =
activeSelection?.instanceId ?? instanceEntries[0]?.instanceId ?? ("" as ProviderInstanceId);
const activeModel = activeSelection?.model ?? "";
const modelOptionsByInstance = useMemo(
() => getCustomModelOptionsByInstance(settings, providers, activeInstanceId, activeModel),
[settings, providers, activeInstanceId, activeModel],
);
const canSave = project !== undefined && activeSelection !== null;
return (
<Dialog
open
onOpenChange={(open) => {
if (!open) onClose();
}}
>
<DialogPopup className="max-w-xl">
<DialogHeader>
<DialogTitle>
{rule === null ? "New assignment rule" : "Edit assignment rule"}
</DialogTitle>
<DialogDescription>
Each matching issue newly assigned to you starts one thread in a new worktree, with the
issue attached and linked.
</DialogDescription>
</DialogHeader>
<DialogPanel>
<div className="space-y-5">
<Field label="Project" htmlFor="linear-trigger-project">
<Select value={projectId} onValueChange={(value) => setProjectId(value ?? "")}>
<SelectTrigger size="sm" id="linear-trigger-project">
<SelectValue placeholder="Select a project">{project?.title}</SelectValue>
</SelectTrigger>
<SelectPopup>
{projects.map((entry) => (
<SelectItem key={entry.id} value={entry.id}>
{entry.title}
</SelectItem>
))}
</SelectPopup>
</Select>
</Field>
<div className="grid gap-3 sm:grid-cols-2">
<Field label="Linear team" htmlFor="linear-trigger-team">
<Select value={teamId} onValueChange={(value) => setTeamId(value ?? ANY_TEAM)}>
<SelectTrigger size="sm" id="linear-trigger-team">
<SelectValue>
{teamId === ANY_TEAM
? "Any team"
: (teams.find((team) => team.id === teamId)?.name ?? "Unknown team")}
</SelectValue>
</SelectTrigger>
<SelectPopup>
<SelectItem value={ANY_TEAM}>Any team</SelectItem>
{teams.map((team) => (
<SelectItem key={team.id} value={team.id}>
{team.name}
</SelectItem>
))}
</SelectPopup>
</Select>
</Field>
<Field label="Label" hint="Optional" htmlFor="linear-trigger-label">
<Input
id="linear-trigger-label"
size="sm"
placeholder="Any label"
value={labelName}
onChange={(event) => setLabelName(event.target.value)}
/>
</Field>
</div>
<Field label="Prompt" hint="Optional" htmlFor="linear-trigger-prompt">
<Textarea
id="linear-trigger-prompt"
className="max-h-64 overflow-y-auto"
placeholder="This Linear issue was just assigned to me. Work on it."
value={prompt}
onChange={(event) => setPrompt(event.target.value)}
/>
</Field>
<Field label="Model">
<ProviderModelPicker
activeInstanceId={activeInstanceId}
model={activeModel}
lockedProvider={null}
instanceEntries={instanceEntries}
modelOptionsByInstance={modelOptionsByInstance}
isComposerOwned={false}
triggerClassName={SETTINGS_PICKER_TRIGGER_CLASSNAME}
onInstanceModelChange={(instanceId, model) =>
setModelSelection(
rule?.modelSelection.instanceId === instanceId &&
rule.modelSelection.model === model
? rule.modelSelection
: { instanceId, model },
)
}
/>
</Field>
</div>
</DialogPanel>
<DialogFooter>
<DialogClose render={<Button size="sm" variant="ghost" />}>Cancel</DialogClose>
<Button
size="sm"
disabled={!canSave}
onClick={() => {
if (!canSave) return;
onSave({
id: rule?.id ?? randomUUID(),
projectId: project.id as ProjectId,
teamId: teamId === ANY_TEAM ? null : teamId,
labelName: labelName.trim() || null,
prompt: prompt.trim() || null,
modelSelection: activeSelection,
});
}}
>
Save
</Button>
</DialogFooter>
</DialogPopup>
</Dialog>
);
}
function Field({
label,
hint,
htmlFor,
children,
}: {
label: string;
hint?: string;
htmlFor?: string;
children: ReactNode;
}) {
return (
<div className="space-y-1.5">
<Label className="flex items-baseline justify-between" htmlFor={htmlFor}>
<span>{label}</span>
{hint ? (
<span className="font-normal text-2xs text-muted-foreground/80">{hint}</span>
) : null}
</Label>
{children}
</div>
);
}