Extract a typed repository interface (adapters/repo.ts) from the scattered store/view -> api.* calls, backed by adapters/rest.ts (verbatim HTTP mapping) and selected through adapters/index.ts. Every store and the notes-facing views now depend on `repo`, never the HTTP client directly -- the seam the offline local source (M10.5, over Tauri invoke) plugs into next. Behavior-preserving for web: rest.ts maps each semantic method to the exact endpoint the code called before; query-string and multipart building moved out of the stores/views into rest.ts (the one place that knows the URL shape). Client-side logic (reconcile/sort/optimistic reorder/toasts) stays in the stores. GraphView + admin SettingsView keep direct api calls -- out of the offline-core scope (M10.5 is board/editor/capture/search/filter/labels/ checklists/reminders). Task 1992. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FRgehjoz7Yv8LkUfADxACm
66 lines
2.2 KiB
TypeScript
66 lines
2.2 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));
|
|
}
|
|
|
|
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> {
|
|
const updated = await repo.labels.rename(id, name);
|
|
const idx = items.value.findIndex((lb) => lb.id === id);
|
|
// The single-label PATCH doesn't recompute the count — keep the one we have.
|
|
if (idx >= 0) items.value[idx] = { ...updated, count: items.value[idx].count };
|
|
sort();
|
|
}
|
|
|
|
async function setColor(id: string, color: string): Promise<void> {
|
|
const updated = await repo.labels.setColor(id, color);
|
|
const idx = items.value.findIndex((lb) => lb.id === id);
|
|
if (idx >= 0) items.value[idx] = { ...updated, count: items.value[idx].count };
|
|
}
|
|
|
|
async function remove(id: string): Promise<void> {
|
|
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 };
|
|
});
|