Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 | 4x 4x 87x 783x 3x 87x 87x 1218x 67x 67x 67x 67x 67x 67x 8x 67x 5x 5x 5x 5x 5x 3x 3x 3x 2x 5x 67x 49x 20x 20x 20x 20x 20x 20x 3x 3x 20x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 20x 14x 21x 21x 10x 14x 7x 10x 10x 21x 479x 479x 479x 479x 2x 2x 1x 1x 1x 2x 479x 4x 8x 1x 3x 3x 4x 4x 2x 1x 253x 253x 253x 253x 253x 253x 100x 8x 3x 8x 8x 253x 8x 8x 8x 7x 7x 1x 8x 253x 8x 8x 253x 3x 3x 3x 1x 1x 2x 2x 1x 1x 2x 1x 253x 9x 9x 3x | import { type ComponentType, useCallback, useEffect, useRef, useState } from "react";
import { api, type Label, type LabelSummary } from "../api";
import { ConfirmDialog } from "./ConfirmDialog";
import {
ArchiveIcon,
CalendarIcon,
DraftIcon,
FilterIcon,
FolderIcon,
InboxIcon,
MailIcon,
PaperclipIcon,
SearchIcon,
SendIcon,
ShieldIcon,
SpamIcon,
StarIcon,
TrashIcon,
XIcon,
} from "./Icons";
import { toast } from "./Toast";
export const LABEL_ICONS: Record<string, ComponentType<{ className?: string }>> = {
inbox: InboxIcon,
send: SendIcon,
draft: DraftIcon,
trash: TrashIcon,
spam: SpamIcon,
archive: ArchiveIcon,
star: StarIcon,
folder: FolderIcon,
search: SearchIcon,
shield: ShieldIcon,
filter: FilterIcon,
calendar: CalendarIcon,
mail: MailIcon,
paperclip: PaperclipIcon,
};
export type LabelIconKey = keyof typeof LABEL_ICONS;
const PRESET_COLORS: { hex: string; name: string }[] = [
{ hex: "#ef4444", name: "Red" },
{ hex: "#f97316", name: "Orange" },
{ hex: "#eab308", name: "Yellow" },
{ hex: "#22c55e", name: "Green" },
{ hex: "#06b6d4", name: "Cyan" },
{ hex: "#3b82f6", name: "Blue" },
{ hex: "#8b5cf6", name: "Violet" },
{ hex: "#ec4899", name: "Pink" },
{ hex: "#6b7280", name: "Gray" },
];
interface ColorPickerProps {
value: string | null;
onChange: (color: string | null) => void;
}
function ColorPicker({ value, onChange }: ColorPickerProps) {
return (
<div className="flex items-center gap-1.5" role="radiogroup" aria-label="Label color">
{PRESET_COLORS.map(({ hex, name }) => (
<button
key={hex}
type="button"
role="radio"
aria-checked={value === hex}
aria-label={name}
onClick={() => onChange(value === hex ? null : hex)}
className={`w-5 h-5 rounded-full border-2 transition-all ${
value === hex ? "border-gray-800 dark:border-white scale-110" : "border-transparent"
}`}
style={{ backgroundColor: hex }}
title={name}
/>
))}
</div>
);
}
interface IconPickerProps {
value: string | null;
onChange: (icon: string | null) => void;
}
function IconPicker({ value, onChange }: IconPickerProps) {
const cls = "w-4 h-4";
return (
<div className="space-y-1">
<p className="text-xs text-gray-400">Icon</p>
<div className="flex items-center gap-1 flex-wrap">
<button
type="button"
onClick={() => onChange(null)}
className={`w-7 h-7 rounded flex items-center justify-center border transition-all ${
value === null
? "border-gray-800 dark:border-white bg-gray-100 dark:bg-gray-800"
: "border-transparent hover:bg-gray-100 dark:hover:bg-gray-800"
}`}
title="Default"
>
<span className="text-[10px] text-gray-400">—</span>
</button>
{Object.entries(LABEL_ICONS).map(([key, IconComponent]) => (
<button
key={key}
type="button"
onClick={() => onChange(value === key ? null : key)}
className={`w-7 h-7 rounded flex items-center justify-center border transition-all text-gray-600 dark:text-gray-300 ${
value === key
? "border-gray-800 dark:border-white bg-gray-100 dark:bg-gray-800"
: "border-transparent hover:bg-gray-100 dark:hover:bg-gray-800"
}`}
title={key.charAt(0).toUpperCase() + key.slice(1)}
>
<IconComponent className={cls} />
</button>
))}
</div>
</div>
);
}
interface CreateLabelFormProps {
onCreated: () => void;
onClose: () => void;
}
function CreateLabelForm({ onCreated, onClose }: CreateLabelFormProps) {
const [name, setName] = useState("");
const [color, setColor] = useState<string | null>(null);
const [icon, setIcon] = useState<string | null>(null);
const [saving, setSaving] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
inputRef.current?.focus();
}, []);
const handleSubmit = useCallback(
async (e: React.FormEvent) => {
e.preventDefault();
Iif (!name.trim()) return;
setSaving(true);
try {
await api.labels.create({
name: name.trim(),
color: color ?? undefined,
icon,
});
toast(`Label "${name.trim()}" created`);
onCreated();
onClose();
} catch (err) {
toast(err instanceof Error ? err.message : "Failed to create label", "error");
} finally {
setSaving(false);
}
},
[name, color, icon, onCreated, onClose],
);
return (
<form onSubmit={handleSubmit} className="p-3 space-y-2">
<div className="flex items-center gap-2">
<input
ref={inputRef}
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Label name"
aria-label="Label name"
className="flex-1 text-sm bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded px-2 py-1 outline-none focus:border-stork-500"
/>
<button
type="button"
onClick={onClose}
className="text-gray-400 hover:text-gray-600 dark:hover:text-gray-200"
title="Cancel"
>
<XIcon className="w-3.5 h-3.5" />
</button>
</div>
<ColorPicker value={color} onChange={setColor} />
<IconPicker value={icon} onChange={setIcon} />
<button
type="submit"
disabled={!name.trim() || saving}
className="w-full text-xs py-1.5 bg-stork-600 hover:bg-stork-700 disabled:opacity-50 text-white rounded font-medium transition-colors"
>
{saving ? "Creating…" : "Create label"}
</button>
</form>
);
}
interface EditLabelFormProps {
label: Label;
onUpdated: () => void;
onClose: () => void;
}
function EditLabelForm({ label, onUpdated, onClose }: EditLabelFormProps) {
const [name, setName] = useState(label.name);
const [color, setColor] = useState<string | null>(label.color);
const [icon, setIcon] = useState<string | null>(label.icon);
const [saving, setSaving] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
inputRef.current?.focus();
inputRef.current?.select();
}, []);
const handleSubmit = useCallback(
async (e: React.FormEvent) => {
e.preventDefault();
Iif (!name.trim()) return;
setSaving(true);
try {
await api.labels.update(label.id, {
name: name.trim(),
color: color ?? undefined,
icon,
});
toast("Label updated");
onUpdated();
onClose();
} catch (err) {
toast(err instanceof Error ? err.message : "Failed to update label", "error");
} finally {
setSaving(false);
}
},
[label.id, name, color, icon, onUpdated, onClose],
);
return (
<form onSubmit={handleSubmit} className="p-3 space-y-2">
<div className="flex items-center gap-2">
<input
ref={inputRef}
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Label name"
aria-label="Label name"
className="flex-1 text-sm bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded px-2 py-1 outline-none focus:border-stork-500"
/>
<button
type="button"
onClick={onClose}
className="text-gray-400 hover:text-gray-600 dark:hover:text-gray-200"
title="Cancel"
>
<XIcon className="w-3.5 h-3.5" />
</button>
</div>
<ColorPicker value={color} onChange={setColor} />
<IconPicker value={icon} onChange={setIcon} />
<button
type="submit"
disabled={!name.trim() || saving}
className="w-full text-xs py-1.5 bg-stork-600 hover:bg-stork-700 disabled:opacity-50 text-white rounded font-medium transition-colors"
>
{saving ? "Saving…" : "Save"}
</button>
</form>
);
}
interface LabelContextMenuProps {
label: Label;
position: { x: number; y: number };
onEdit: () => void;
onDelete: () => void;
onClose: () => void;
}
export function LabelContextMenu({ position, onEdit, onDelete, onClose }: LabelContextMenuProps) {
const menuRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const handler = (e: MouseEvent) => {
if (menuRef.current && !menuRef.current.contains(e.target as Node)) {
onClose();
}
};
document.addEventListener("mousedown", handler);
return () => document.removeEventListener("mousedown", handler);
}, [onClose]);
return (
<div
ref={menuRef}
className="fixed z-50 bg-white dark:bg-gray-900 border border-gray-200 dark:border-gray-700 rounded-lg shadow-lg py-1 min-w-[140px]"
style={{ top: position.y, left: position.x }}
>
<button
type="button"
onClick={onEdit}
className="w-full text-left px-3 py-1.5 text-sm hover:bg-gray-50 dark:hover:bg-gray-800 text-gray-700 dark:text-gray-300"
>
Edit label
</button>
<button
type="button"
onClick={onDelete}
className="w-full text-left px-3 py-1.5 text-sm hover:bg-red-50 dark:hover:bg-red-900/20 text-red-600 dark:text-red-400"
>
Delete label
</button>
</div>
);
}
interface LabelManagerProps {
onLabelsChanged: () => void;
contextMenu: { label: Label; position: { x: number; y: number } } | null;
onContextMenuClose: () => void;
}
export function LabelManager({
onLabelsChanged,
contextMenu,
onContextMenuClose,
}: LabelManagerProps) {
const [showCreate, setShowCreate] = useState(false);
const [editLabel, setEditLabel] = useState<Label | null>(null);
const [deleteLabel, setDeleteLabel] = useState<Label | null>(null);
const handleDeleteConfirmed = useCallback(
async (label: Label) => {
try {
await api.labels.delete(label.id);
toast(`Label "${label.name}" deleted`, "info");
onLabelsChanged();
} catch {
toast("Failed to delete label", "error");
}
setDeleteLabel(null);
},
[onLabelsChanged],
);
return (
<>
{/* Create label section */}
{showCreate ? (
<div className="mt-1 border-t border-gray-200 dark:border-gray-800">
<CreateLabelForm onCreated={onLabelsChanged} onClose={() => setShowCreate(false)} />
</div>
) : (
<button
type="button"
onClick={() => setShowCreate(true)}
className="w-full text-left px-5 py-1.5 mt-1 text-xs text-gray-400 hover:text-stork-600 dark:hover:text-stork-400 hover:bg-gray-100 dark:hover:bg-gray-800 rounded-md transition-colors"
>
+ Create label
</button>
)}
{/* Edit label modal */}
{editLabel && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/30">
<div
className="w-72 bg-white dark:bg-gray-900 rounded-lg shadow-xl border border-gray-200 dark:border-gray-700"
role="dialog"
aria-modal="true"
aria-label="Edit label"
>
<EditLabelForm
label={editLabel}
onUpdated={onLabelsChanged}
onClose={() => setEditLabel(null)}
/>
</div>
</div>
)}
{/* Context menu */}
{contextMenu && (
<LabelContextMenu
label={contextMenu.label}
position={contextMenu.position}
onEdit={() => {
setEditLabel(contextMenu.label);
onContextMenuClose();
}}
onDelete={() => {
setDeleteLabel(contextMenu.label);
onContextMenuClose();
}}
onClose={onContextMenuClose}
/>
)}
{/* Delete confirmation */}
{deleteLabel && (
<ConfirmDialog
title="Delete label"
message={`Delete "${deleteLabel.name}"? Messages with this label won't be deleted, but they'll lose this label.`}
confirmLabel="Delete"
variant="danger"
onConfirm={() => handleDeleteConfirmed(deleteLabel)}
onCancel={() => setDeleteLabel(null)}
/>
)}
</>
);
}
/**
* Label picker for assigning/removing labels on a message.
* Used in MessageDetail.
*/
interface MessageLabelPickerProps {
messageId: number;
onLabelsChanged?: () => void;
}
export function MessageLabelPicker({ messageId, onLabelsChanged }: MessageLabelPickerProps) {
const [open, setOpen] = useState(false);
const [allLabels, setAllLabels] = useState<Label[]>([]);
const [messageLabels, setMessageLabels] = useState<LabelSummary[]>([]);
const [loading, setLoading] = useState(false);
const menuRef = useRef<HTMLDivElement>(null);
// Close on outside click
useEffect(() => {
if (!open) return;
const handler = (e: MouseEvent) => {
Iif (menuRef.current && !menuRef.current.contains(e.target as Node)) {
setOpen(false);
}
};
document.addEventListener("mousedown", handler);
return () => document.removeEventListener("mousedown", handler);
}, [open]);
const loadLabels = useCallback(async () => {
setLoading(true);
try {
const [all, current] = await Promise.all([api.labels.list(), api.messages.labels(messageId)]);
setAllLabels(all);
setMessageLabels(current);
} catch {
toast("Failed to load labels", "error");
} finally {
setLoading(false);
}
}, [messageId]);
const handleOpen = useCallback(() => {
setOpen(true);
loadLabels();
}, [loadLabels]);
const handleToggle = useCallback(
async (labelId: number) => {
const isAssigned = messageLabels.some((l) => l.id === labelId);
try {
if (isAssigned) {
await api.messages.removeLabel(messageId, labelId);
setMessageLabels((prev) => prev.filter((l) => l.id !== labelId));
} else {
await api.messages.addLabels(messageId, [labelId]);
const label = allLabels.find((l) => l.id === labelId);
Eif (label) {
setMessageLabels((prev) => [
...prev,
{
id: label.id,
name: label.name,
color: label.color,
icon: label.icon,
source: label.source,
},
]);
}
}
onLabelsChanged?.();
} catch {
toast("Failed to update label", "error");
}
},
[messageId, messageLabels, allLabels, onLabelsChanged],
);
return (
<div className="relative" ref={menuRef}>
<button
type="button"
onClick={handleOpen}
className="p-1.5 hover:bg-gray-100 dark:hover:bg-gray-800 rounded transition-colors text-gray-400 hover:text-gray-600 dark:hover:text-gray-200"
title="Manage labels"
>
<svg
className="w-4 h-4"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
>
<title>Labels</title>
<path d="M20.59 13.41l-7.17 7.17a2 2 0 01-2.83 0L2 12V2h10l8.59 8.59a2 2 0 010 2.82z" />
<line x1="7" y1="7" x2="7.01" y2="7" />
</svg>
</button>
{open && (
<div className="absolute right-0 top-full mt-1 w-56 bg-white dark:bg-gray-900 border border-gray-200 dark:border-gray-700 rounded-lg shadow-lg py-1 z-10 max-h-64 overflow-y-auto">
<p className="px-3 py-1.5 text-xs font-medium text-gray-400 uppercase tracking-wide">
Labels
</p>
{loading && <p className="px-3 py-2 text-xs text-gray-400">Loading…</p>}
{!loading && allLabels.length === 0 && (
<p className="px-3 py-2 text-xs text-gray-400">No labels available</p>
)}
{!loading &&
allLabels.map((label) => {
const isAssigned = messageLabels.some((l) => l.id === label.id);
return (
<button
key={label.id}
type="button"
onClick={() => handleToggle(label.id)}
className="w-full text-left px-3 py-1.5 text-sm hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors flex items-center gap-2"
>
<span
className={`w-4 h-4 flex-shrink-0 rounded border flex items-center justify-center text-xs ${
isAssigned
? "bg-stork-600 border-stork-600 text-white"
: "border-gray-300 dark:border-gray-600"
}`}
>
{isAssigned && "✓"}
</span>
{label.color && (
<span
className="w-2.5 h-2.5 rounded-full flex-shrink-0"
style={{ backgroundColor: label.color }}
/>
)}
<span className="truncate text-gray-700 dark:text-gray-300">{label.name}</span>
</button>
);
})}
</div>
)}
</div>
);
}
|