feat: add category management page (create/edit/delete)

The backend already had full category CRUD but nothing in the UI exposed
it — categories could only be viewed inside charts. Adds a Categories nav
item and page with icon/color pickers, grouped by expense/income, backed
by new create/update/delete mutations. Also expands icons.tsx's ICON_MAP,
which was missing 14 icon keys already in use by real category data and
silently falling back to a wallet glyph everywhere they render.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ZProger
2026-08-24 23:43:06 +03:00
parent 6cc532378b
commit 9f5ea00227
13 changed files with 512 additions and 5 deletions

View File

@@ -0,0 +1,54 @@
import { useTranslation } from "@/lib/i18n";
import { cn } from "@/lib/utils";
interface CategoryColorPickerProps {
value: string;
onChange: (color: string) => void;
}
// The same 8-slot colorblind-safe categorical palette the seeded default
// categories and every chart in the app already use — see
// backend/app/db/seed.py and index.css's --series-* tokens.
const PRESET_COLORS = [
"#2a78d6",
"#eb6834",
"#1baf7a",
"#eda100",
"#e87ba4",
"#008300",
"#4a3aa7",
"#e34948",
"#898781",
];
export function CategoryColorPicker({ value, onChange }: CategoryColorPickerProps) {
const { t } = useTranslation();
const normalized = value.toLowerCase();
return (
<div className="flex flex-wrap items-center gap-2">
{PRESET_COLORS.map((color) => (
<button
key={color}
type="button"
aria-label={color}
aria-pressed={normalized === color}
onClick={() => onChange(color)}
className={cn(
"h-7 w-7 rounded-full border-2 transition-transform",
normalized === color ? "scale-110 border-text-primary" : "border-transparent hover:scale-105"
)}
style={{ backgroundColor: color }}
/>
))}
<input
type="color"
aria-label={t("category.form.customColorLabel")}
title={t("category.form.customColorLabel")}
value={/^#[0-9a-f]{6}$/i.test(value) ? value : "#898781"}
onChange={(event) => onChange(event.target.value)}
className="h-7 w-7 cursor-pointer rounded-md border border-border bg-transparent p-0"
/>
</div>
);
}

View File

@@ -0,0 +1,118 @@
import { useEffect, useState } from "react";
import { Dialog } from "@/components/ui/Dialog";
import { Button } from "@/components/ui/Button";
import { Input, Label, Select } from "@/components/ui/Input";
import { CategoryColorPicker } from "@/components/categories/CategoryColorPicker";
import { CategoryIconPicker } from "@/components/categories/CategoryIconPicker";
import { useCreateCategory, useUpdateCategory } from "@/hooks/useCategories";
import { useTranslation, type TranslationKey } from "@/lib/i18n";
import type { Category, CategoryKind } from "@/types";
interface CategoryFormModalProps {
open: boolean;
onClose: () => void;
category?: Category | null;
// Preselects kind for a new category — irrelevant once editing, since kind
// can't change after creation (see backend CategoryUpdate schema).
defaultKind: CategoryKind;
}
const KINDS: CategoryKind[] = ["expense", "income"];
function emptyForm(kind: CategoryKind) {
return { name: "", kind, icon: "wallet", color: "#2a78d6" };
}
export function CategoryFormModal({ open, onClose, category, defaultKind }: CategoryFormModalProps) {
const { t } = useTranslation();
const createCategory = useCreateCategory();
const updateCategory = useUpdateCategory();
const [form, setForm] = useState(emptyForm(defaultKind));
const [error, setError] = useState<string | null>(null);
useEffect(() => {
if (!open) return;
setForm(
category
? { name: category.name, kind: category.kind, icon: category.icon ?? "wallet", color: category.color }
: emptyForm(defaultKind)
);
setError(null);
}, [open, category, defaultKind]);
const isSaving = createCategory.isPending || updateCategory.isPending;
async function handleSubmit(event: React.FormEvent) {
event.preventDefault();
setError(null);
try {
if (category) {
await updateCategory.mutateAsync({
id: category.id,
input: { name: form.name, icon: form.icon, color: form.color },
});
} else {
await createCategory.mutateAsync(form);
}
onClose();
} catch {
setError(t("category.form.saveError"));
}
}
return (
<Dialog open={open} onClose={onClose} title={category ? t("category.form.editTitle") : t("category.form.newTitle")}>
<form onSubmit={handleSubmit} className="space-y-3">
<div>
<Label htmlFor="category-name">{t("category.form.nameLabel")}</Label>
<Input
id="category-name"
required
placeholder={t("category.form.namePlaceholder")}
value={form.name}
onChange={(event) => setForm((prev) => ({ ...prev, name: event.target.value }))}
/>
</div>
<div>
<Label htmlFor="category-kind">{t("category.form.kindLabel")}</Label>
<Select
id="category-kind"
value={form.kind}
disabled={Boolean(category)}
onChange={(event) => setForm((prev) => ({ ...prev, kind: event.target.value as CategoryKind }))}
>
{KINDS.map((kind) => (
<option key={kind} value={kind}>
{t(`category.kind.${kind}` as TranslationKey)}
</option>
))}
</Select>
</div>
<div>
<Label>{t("category.form.iconLabel")}</Label>
<CategoryIconPicker value={form.icon} onChange={(icon) => setForm((prev) => ({ ...prev, icon }))} />
</div>
<div>
<Label>{t("category.form.colorLabel")}</Label>
<CategoryColorPicker value={form.color} onChange={(color) => setForm((prev) => ({ ...prev, color }))} />
</div>
{error && <p className="text-sm text-danger">{error}</p>}
<div className="flex justify-end gap-2 pt-2">
<Button type="button" variant="ghost" onClick={onClose}>
{t("common.cancel")}
</Button>
<Button type="submit" disabled={isSaving}>
{isSaving ? t("common.saving") : t("common.save")}
</Button>
</div>
</form>
</Dialog>
);
}

View File

@@ -0,0 +1,36 @@
import { CATEGORY_ICON_OPTIONS, getCategoryIcon } from "@/lib/icons";
import { cn } from "@/lib/utils";
interface CategoryIconPickerProps {
value: string;
onChange: (icon: string) => void;
}
export function CategoryIconPicker({ value, onChange }: CategoryIconPickerProps) {
return (
<div className="grid grid-cols-6 gap-1.5 sm:grid-cols-10">
{CATEGORY_ICON_OPTIONS.map((icon) => {
const Icon = getCategoryIcon(icon);
const selected = icon === value;
return (
<button
key={icon}
type="button"
aria-label={icon}
aria-pressed={selected}
onClick={() => onChange(icon)}
className={cn(
"flex h-9 w-9 items-center justify-center rounded-lg border transition-colors",
selected
? "border-series-1 bg-series-1/10 text-series-1"
: "border-border text-text-secondary hover:bg-surface-2"
)}
>
<Icon size={16} />
</button>
);
})}
</div>
);
}

View File

@@ -0,0 +1,65 @@
import { Pencil, Trash2 } from "lucide-react";
import { getCategoryIcon } from "@/lib/icons";
import { useTranslation } from "@/lib/i18n";
import type { Category } from "@/types";
interface CategoryListProps {
items: Category[];
onEdit: (category: Category) => void;
onDelete: (category: Category) => void;
}
export function CategoryList({ items, onEdit, onDelete }: CategoryListProps) {
const { t } = useTranslation();
if (items.length === 0) {
return <p className="py-6 text-center text-sm text-text-muted">{t("category.empty")}</p>;
}
return (
<ul className="divide-y divide-gridline">
{items.map((category) => {
const Icon = getCategoryIcon(category.icon);
return (
<li key={category.id} className="flex items-center gap-3 py-2.5">
<span
className="flex h-8 w-8 shrink-0 items-center justify-center rounded-full"
style={{ backgroundColor: `${category.color}1a`, color: category.color }}
>
<Icon size={15} />
</span>
<span className="flex min-w-0 flex-1 items-center gap-1.5 truncate text-sm font-medium text-text-primary">
<span className="truncate">{category.name}</span>
{category.is_default && (
<span className="shrink-0 rounded bg-surface-2 px-1 py-0.5 text-[10px] leading-none text-text-muted">
{t("category.defaultBadge")}
</span>
)}
</span>
<span className="flex shrink-0 gap-1">
<button
type="button"
aria-label={t("common.edit")}
onClick={() => onEdit(category)}
className="rounded-md p-1.5 text-text-muted hover:bg-surface-2 hover:text-text-primary"
>
<Pencil size={15} />
</button>
<button
type="button"
aria-label={t("common.delete")}
title={category.is_default ? t("category.defaultCannotDelete") : undefined}
disabled={category.is_default}
onClick={() => onDelete(category)}
className="rounded-md p-1.5 text-text-muted hover:bg-surface-2 hover:text-danger disabled:cursor-not-allowed disabled:opacity-30 disabled:hover:bg-transparent disabled:hover:text-text-muted"
>
<Trash2 size={15} />
</button>
</span>
</li>
);
})}
</ul>
);
}