-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgroup-label-card.tsx
More file actions
278 lines (263 loc) · 9.75 KB
/
Copy pathgroup-label-card.tsx
File metadata and controls
278 lines (263 loc) · 9.75 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
import { Link } from "@tanstack/react-router"
import { FolderPlus, LoaderCircle, MoreVertical, Pencil, PencilLine, Save, Trash2, X } from "lucide-react"
import { useState } from "react"
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogMedia,
AlertDialogTitle,
} from "@/components/ui/alert-dialog"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Card } from "@/components/ui/card"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { Input } from "@/components/ui/input"
import { AddChildLabelDialog } from "@/features/groups-by-label/add-child-label-dialog"
import { cn } from "@/lib/utils"
import { GroupLabelColorPicker } from "./group-label-color-picker"
import { GROUP_LABEL_DESCRIPTION_MAX, getGroupLabelColor } from "./group-labels.constants"
import { formatLabelBreadcrumb, formatLabelSegment } from "./label-tree"
import { RenameLabelDialog } from "./rename-label-dialog"
import type { GroupLabel, GroupLabelEditValues } from "./types"
type GroupLabelCardProps = {
groupLabel: GroupLabel
/** The full label list, used to find this label's descendants when renaming (they get re-pathed too). */
allLabels: GroupLabel[]
/** Rendered before everything else in the row — the tree's expand/collapse chevron, when this node has children. */
leading?: React.ReactNode
/** Off for the two fixed category roots — the whole category tree depends on their exact name. */
allowRename?: boolean
/** Off for flat tags, which never nest. */
allowChildren?: boolean
/** When set, the name badge links here — a distinct target, so the card's own edit and delete controls stay clickable. */
linkTo?: string
onDelete: () => Promise<boolean>
onSave: (values: GroupLabelEditValues) => Promise<boolean>
}
export function GroupLabelCard({
groupLabel,
allLabels,
leading,
allowRename = true,
allowChildren = true,
linkTo,
onDelete,
onSave,
}: GroupLabelCardProps) {
const [color, setColor] = useState(groupLabel.color)
const [description, setDescription] = useState(groupLabel.description ?? "")
const [editing, setEditing] = useState(false)
const [saving, setSaving] = useState(false)
const [deleting, setDeleting] = useState(false)
const [deleteOpen, setDeleteOpen] = useState(false)
const [renameOpen, setRenameOpen] = useState(false)
const [addChildOpen, setAddChildOpen] = useState(false)
const segments = groupLabel.label.split(".")
const segment = segments[segments.length - 1] || groupLabel.label
const displaySegment = formatLabelSegment(segment)
function resetFields() {
setColor(groupLabel.color)
setDescription(groupLabel.description ?? "")
}
function cancelEdit() {
resetFields()
setEditing(false)
}
async function save() {
if (saving) return
setSaving(true)
try {
const saved = await onSave({ color, description: description.trim() })
if (saved) setEditing(false)
} finally {
setSaving(false)
}
}
async function remove() {
setDeleting(true)
try {
if (await onDelete()) setDeleteOpen(false)
} finally {
setDeleting(false)
}
}
const swatch = getGroupLabelColor(editing ? color : groupLabel.color)
const affectedLabels = allLabels.filter(
(label) => label.label === groupLabel.label || label.label.startsWith(`${groupLabel.label}.`)
)
return (
<>
<Card className="flex-row items-center gap-4 px-(--card-spacing) py-3">
{leading}
{editing ? (
<>
<div className="flex shrink-0 items-center gap-2">
<GroupLabelColorPicker value={color} onChange={setColor} />
<Badge
className={cn("h-auto max-w-64 min-w-0 shrink py-1 text-sm", swatch.badgeClassName)}
style={swatch.badgeStyle}
title={groupLabel.label}
>
<span className="truncate">{displaySegment}</span>
</Badge>
</div>
<Input
aria-label="Description"
value={description}
onChange={(event) => setDescription(event.target.value)}
className="min-w-0 flex-1 bg-background"
maxLength={GROUP_LABEL_DESCRIPTION_MAX}
placeholder="What kind of groups does this label apply to?"
/>
</>
) : (
<>
{linkTo ? (
<Link
to={linkTo}
title={`View the groups tagged "${groupLabel.label}"`}
className="flex max-w-[min(50%,24rem)] min-w-0 shrink rounded-md hover:opacity-80 focus-visible:outline-2 focus-visible:outline-ring focus-visible:outline-offset-2"
>
<Badge className={cn("h-auto min-w-0 py-1 text-sm", swatch.badgeClassName)} style={swatch.badgeStyle}>
<span className="truncate">{displaySegment}</span>
</Badge>
</Link>
) : (
<Badge
className={cn("h-auto max-w-[min(50%,24rem)] min-w-0 shrink py-1 text-sm", swatch.badgeClassName)}
style={swatch.badgeStyle}
title={groupLabel.label}
>
<span className="truncate">{displaySegment}</span>
</Badge>
)}
<p className="min-w-0 flex-1 truncate text-sm text-foreground/85">
{groupLabel.description || <span className="text-muted-foreground italic">No description</span>}
</p>
</>
)}
<div className="flex shrink-0 items-center gap-1.5">
{editing ? (
<>
<Button
type="button"
size="icon-sm"
disabled={saving}
onClick={() => void save()}
aria-label={`Save ${displaySegment}`}
>
{saving ? <LoaderCircle className="animate-spin-slow" /> : <Save />}
</Button>
<Button
type="button"
variant="outline"
size="icon-sm"
disabled={saving}
onClick={cancelEdit}
aria-label="Cancel editing"
>
<X />
</Button>
</>
) : (
<>
{(allowRename || allowChildren) && (
<DropdownMenu>
<DropdownMenuTrigger render={<Button type="button" variant="outline" size="icon-sm" />}>
<MoreVertical />
<span className="sr-only">More actions for {displaySegment}</span>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuGroup>
{allowRename && (
<DropdownMenuItem onClick={() => setRenameOpen(true)}>
<PencilLine /> Rename
</DropdownMenuItem>
)}
{allowChildren && (
<DropdownMenuItem onClick={() => setAddChildOpen(true)}>
<FolderPlus /> Add sub-category
</DropdownMenuItem>
)}
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
)}
<Button
type="button"
variant="outline"
size="icon-sm"
aria-label={`Edit color and description for ${displaySegment}`}
onClick={() => {
resetFields()
setEditing(true)
}}
>
<Pencil />
</Button>
<Button
type="button"
variant="destructive"
size="icon-sm"
aria-label={`Delete ${displaySegment}`}
onClick={() => setDeleteOpen(true)}
>
<Trash2 />
</Button>
</>
)}
</div>
</Card>
{allowRename && (
<RenameLabelDialog
path={groupLabel.label}
segment={segment}
affectedLabels={affectedLabels}
open={renameOpen}
onOpenChange={setRenameOpen}
/>
)}
{allowChildren && (
<AddChildLabelDialog
path={groupLabel.label}
open={addChildOpen}
onOpenChange={setAddChildOpen}
navigateOnSuccess={false}
/>
)}
<AlertDialog open={deleteOpen} onOpenChange={(open) => !deleting && setDeleteOpen(open)}>
<AlertDialogContent size="sm">
<AlertDialogHeader>
<AlertDialogMedia className="bg-destructive/10 text-destructive dark:bg-destructive/20">
<Trash2 />
</AlertDialogMedia>
<AlertDialogTitle>Delete label</AlertDialogTitle>
<AlertDialogDescription>
Are you sure you want to delete <strong>{formatLabelBreadcrumb(groupLabel.label)}</strong>? This action
cannot be undone.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={deleting} onClick={() => setDeleteOpen(false)}>
Cancel
</AlertDialogCancel>
<AlertDialogAction variant="destructive" disabled={deleting} onClick={() => void remove()}>
{deleting && <LoaderCircle data-icon="inline-start" className="animate-spin-slow" />} Delete
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</>
)
}