|
| 1 | +import { useState } from "react"; |
| 2 | +import { useTeams } from "../../features/agents2/react"; |
| 3 | +import type { Agents2, Team } from "../../features/agents2/service"; |
| 4 | +import { |
| 5 | + channelTemplates, |
| 6 | + useChannelTemplates, |
| 7 | + type ChannelTemplate, |
| 8 | +} from "../../features/channel-templates/local"; |
| 9 | +import { CopyIcon, PlusIcon } from "../../shared/design-system/icons"; |
| 10 | +import { Button } from "../../shared/design-system/ui/Button"; |
| 11 | +import { Checkbox } from "../../shared/design-system/ui/Checkbox"; |
| 12 | +import { Dialog } from "../../shared/design-system/ui/Dialog"; |
| 13 | +import { EmptyState } from "../../shared/design-system/ui/EmptyState"; |
| 14 | +import { Field } from "../../shared/design-system/ui/Field"; |
| 15 | +import { Header } from "../../shared/design-system/ui/Header"; |
| 16 | +import { Input } from "../../shared/design-system/ui/Input"; |
| 17 | +import { PreferenceRow } from "../../shared/design-system/ui/PreferenceRow"; |
| 18 | +import { SettingsGroup } from "../../shared/design-system/ui/SettingsGroup"; |
| 19 | +import { Textarea } from "../../shared/design-system/ui/Textarea"; |
| 20 | +import styles from "./ChannelTemplates.module.css"; |
| 21 | + |
| 22 | +/** Templates a new channel can start from: its teams and its Canvas. */ |
| 23 | +export function ChannelTemplateSettings({ |
| 24 | + agents2, |
| 25 | + active, |
| 26 | +}: { |
| 27 | + agents2?: Agents2 | undefined; |
| 28 | + active(): boolean; |
| 29 | +}) { |
| 30 | + const templates = useChannelTemplates(); |
| 31 | + const teams = useTeams(agents2); |
| 32 | + // A template's id while it is edited, or "new" for one being made. |
| 33 | + const [editing, setEditing] = useState<string>(); |
| 34 | + const template = templates.find((item) => item.id === editing); |
| 35 | + const newTemplate = () => { |
| 36 | + if (active()) setEditing("new"); |
| 37 | + }; |
| 38 | + return ( |
| 39 | + <section aria-label="Channel templates settings"> |
| 40 | + <Header title="Channel templates" /> |
| 41 | + {templates.length ? ( |
| 42 | + <div className={styles.stack}> |
| 43 | + <SettingsGroup> |
| 44 | + {templates.map((item) => ( |
| 45 | + <PreferenceRow |
| 46 | + key={item.id} |
| 47 | + title={item.name} |
| 48 | + subtitle={summary(item, teams)} |
| 49 | + trailing={ |
| 50 | + <Button |
| 51 | + aria-label={`Edit ${item.name}`} |
| 52 | + onClick={() => { |
| 53 | + if (active()) setEditing(item.id); |
| 54 | + }} |
| 55 | + > |
| 56 | + Edit |
| 57 | + </Button> |
| 58 | + } |
| 59 | + /> |
| 60 | + ))} |
| 61 | + </SettingsGroup> |
| 62 | + <div className={styles.actions}> |
| 63 | + <Button onClick={newTemplate}> |
| 64 | + <PlusIcon size={14} aria-hidden="true" /> New template |
| 65 | + </Button> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + ) : ( |
| 69 | + <EmptyState |
| 70 | + icon={<CopyIcon />} |
| 71 | + title="No channel templates yet" |
| 72 | + description="Start new channels with your teams and a Canvas already in place." |
| 73 | + action={<Button onClick={newTemplate}>New template</Button>} |
| 74 | + /> |
| 75 | + )} |
| 76 | + {(editing === "new" || template) && ( |
| 77 | + <TemplateDialog |
| 78 | + key={editing} |
| 79 | + teams={teams} |
| 80 | + {...(template ? { template } : {})} |
| 81 | + onClose={() => setEditing(undefined)} |
| 82 | + /> |
| 83 | + )} |
| 84 | + </section> |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +function summary(template: ChannelTemplate, teams: readonly Team[]) { |
| 89 | + const names = template.teams.flatMap( |
| 90 | + (id) => teams.find((team) => team.id === id)?.name ?? [], |
| 91 | + ); |
| 92 | + return ( |
| 93 | + [names.join(", "), template.canvas.trim() ? "Canvas" : ""] |
| 94 | + .filter(Boolean) |
| 95 | + .join(" · ") || "Empty" |
| 96 | + ); |
| 97 | +} |
| 98 | + |
| 99 | +function TemplateDialog({ |
| 100 | + teams, |
| 101 | + template, |
| 102 | + onClose, |
| 103 | +}: { |
| 104 | + teams: readonly Team[]; |
| 105 | + template?: ChannelTemplate; |
| 106 | + onClose(): void; |
| 107 | +}) { |
| 108 | + const [name, setName] = useState(template?.name ?? ""); |
| 109 | + const [chosen, setChosen] = useState<readonly string[]>( |
| 110 | + template?.teams ?? [], |
| 111 | + ); |
| 112 | + const [canvas, setCanvas] = useState(template?.canvas ?? ""); |
| 113 | + const [error, setError] = useState(""); |
| 114 | + const act = (work: () => void) => { |
| 115 | + setError(""); |
| 116 | + try { |
| 117 | + work(); |
| 118 | + onClose(); |
| 119 | + } catch (reason) { |
| 120 | + setError(reason instanceof Error ? reason.message : String(reason)); |
| 121 | + } |
| 122 | + }; |
| 123 | + return ( |
| 124 | + <Dialog |
| 125 | + open |
| 126 | + onOpenChange={(open) => { |
| 127 | + if (!open) onClose(); |
| 128 | + }} |
| 129 | + title={template ? "Edit template" : "New template"} |
| 130 | + closeLabel="Close template" |
| 131 | + actions={ |
| 132 | + <> |
| 133 | + {template && ( |
| 134 | + <Button |
| 135 | + variant="destructive" |
| 136 | + onClick={() => act(() => channelTemplates.remove(template.id))} |
| 137 | + > |
| 138 | + Delete |
| 139 | + </Button> |
| 140 | + )} |
| 141 | + <Button |
| 142 | + variant="prominent" |
| 143 | + type="submit" |
| 144 | + form="channel-template-form" |
| 145 | + disabled={!name.trim()} |
| 146 | + > |
| 147 | + Save template |
| 148 | + </Button> |
| 149 | + </> |
| 150 | + } |
| 151 | + > |
| 152 | + <form |
| 153 | + id="channel-template-form" |
| 154 | + className={styles.stack} |
| 155 | + onSubmit={(event) => { |
| 156 | + event.preventDefault(); |
| 157 | + act(() => |
| 158 | + channelTemplates.save({ |
| 159 | + ...(template ? { id: template.id } : {}), |
| 160 | + name, |
| 161 | + teams: chosen, |
| 162 | + canvas, |
| 163 | + }), |
| 164 | + ); |
| 165 | + }} |
| 166 | + > |
| 167 | + <Field label="Name"> |
| 168 | + <Input |
| 169 | + autoFocus |
| 170 | + value={name} |
| 171 | + onChange={(event) => setName(event.target.value)} |
| 172 | + /> |
| 173 | + </Field> |
| 174 | + <fieldset className={styles.teams}> |
| 175 | + <legend className="text-label-sm">Teams</legend> |
| 176 | + {teams.length ? ( |
| 177 | + teams.map((team) => ( |
| 178 | + <Checkbox |
| 179 | + key={team.id} |
| 180 | + label={team.name} |
| 181 | + checked={chosen.includes(team.id)} |
| 182 | + onCheckedChange={(checked) => |
| 183 | + setChosen((last) => |
| 184 | + checked |
| 185 | + ? [...last, team.id] |
| 186 | + : last.filter((id) => id !== team.id), |
| 187 | + ) |
| 188 | + } |
| 189 | + /> |
| 190 | + )) |
| 191 | + ) : ( |
| 192 | + <p className="m-0 text-body-sm text-secondary"> |
| 193 | + Make teams on the Agents page. |
| 194 | + </p> |
| 195 | + )} |
| 196 | + </fieldset> |
| 197 | + <Field |
| 198 | + label="Starting Canvas" |
| 199 | + description="Markdown, copied into each new channel made from this template." |
| 200 | + > |
| 201 | + <Textarea |
| 202 | + rows={8} |
| 203 | + value={canvas} |
| 204 | + onChange={(event) => setCanvas(event.target.value)} |
| 205 | + placeholder={"# Goal\n\n# Scope\n\n# Success criteria"} |
| 206 | + /> |
| 207 | + </Field> |
| 208 | + {error && ( |
| 209 | + <p role="alert" className={styles.error}> |
| 210 | + {error} |
| 211 | + </p> |
| 212 | + )} |
| 213 | + </form> |
| 214 | + </Dialog> |
| 215 | + ); |
| 216 | +} |
0 commit comments