Files
inkwell/frontend/src/stores/labels.ts
T
bvandeusenandClaude Opus 5.5 ef22910154 DRY pass #3: the labels store swaps a returned label in one way (#5373)
rename and setColor each found the label and replaced it, keeping the count
the single-label PATCH doesn't recompute; keepingCount(updated) is that.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 15:39:25 -04:00

110 lines
4.3 KiB
TypeScript

import { defineStore } from "pinia";
import { ref } from "vue";
import { repo } from "../adapters";
export interface Label {
id: string;
name: string;
color: string;
// Number of notes carrying this label (from GET /api/labels; used in label management).
count?: number;
}
export const useLabelsStore = defineStore("labels", () => {
const items = ref<Label[]>([]);
const loaded = ref(false);
function sort() {
items.value.sort((a, b) => a.name.localeCompare(b.name));
}
// Put a label the server sent back in place of ours. A single-label PATCH doesn't
// recompute the count, so the one we have is kept.
function keepingCount(updated: Label): void {
const idx = items.value.findIndex((lb) => lb.id === updated.id);
if (idx >= 0) items.value[idx] = { ...updated, count: items.value[idx].count };
}
async function load(): Promise<void> {
items.value = await repo.labels.list();
loaded.value = true;
}
async function create(name: string): Promise<Label> {
const label = await repo.labels.create(name);
if (!items.value.some((lb) => lb.id === label.id)) {
items.value.push(label);
sort();
}
return label;
}
async function rename(id: string, name: string): Promise<void> {
// Renaming onto a name another tag already holds MERGES the two — server-side
// and in the local store, identically. Detected from the LIST rather than from
// the response: the survivor is whichever row is older, so it may well be the
// one we asked to rename, and an id that still matches proves nothing happened.
const absorbing = items.value.find(
(lb) => lb.id !== id && lb.name.toLowerCase() === name.toLowerCase(),
);
if (absorbing) {
// A merge cannot be undone by repeating it, and here it is reachable by a
// typo in a text field — so it asks, the way deleting one does. The counts
// are named because "40 notes" is the part that makes the consequence real.
const mine = items.value.find((lb) => lb.id === id);
const confirmed = window.confirm(
`A tag called "${absorbing.name}" already exists.\n\n` +
`Renaming will MERGE these two into one tag named "${name}", carrying ` +
`every note from both (${mine?.count ?? 0} + ${absorbing.count ?? 0}). ` +
"The notes are kept; one of the two tags stops existing, and that cannot " +
"be undone.",
);
if (!confirmed) return;
}
const updated = await repo.labels.rename(id, name);
if (absorbing) {
// One row is gone and the survivor's count grew, and this response carries no
// count — reload rather than guess which of the two we are now holding.
await load();
return;
}
keepingCount(updated);
sort();
}
async function setColor(id: string, color: string): Promise<void> {
keepingCount(await repo.labels.setColor(id, color));
}
async function remove(id: string): Promise<void> {
// Labels have no trash of their own — a label is organization, not content, so
// the reversible middle step notes get would be ceremony. But deleting one is
// still irreversible and now reaches every synced device, so it asks first. The
// notes themselves survive; only the membership goes, which is the part people
// most need reassuring about.
const label = items.value.find((lb) => lb.id === id);
const subject = label ? `the tag "${label.name}"` : "this tag";
const confirmed = window.confirm(
`Delete ${subject}?\n\n` +
"It will be removed from every note that uses it, on every device you sync " +
"with. The notes themselves are kept.",
);
if (!confirmed) return;
await repo.labels.remove(id);
items.value = items.value.filter((lb) => lb.id !== id);
}
// Merge `sourceId` into `targetId`: the server moves the source's notes onto the
// target and deletes the source; the response carries the target's new count.
async function mergeInto(sourceId: string, targetId: string): Promise<void> {
const target = await repo.labels.merge(sourceId, targetId);
items.value = items.value.filter((lb) => lb.id !== sourceId);
const idx = items.value.findIndex((lb) => lb.id === targetId);
if (idx >= 0) items.value[idx] = target;
sort();
}
return { items, loaded, load, create, rename, setColor, remove, mergeInto };
});