Files
thoughtsync/frontend/src/stores/notes.ts
T
bvandeusenandClaude Opus 4.8 e4c898cd1b
CI & Build / Python lint (push) Successful in 3s
CI & Build / TypeScript typecheck (push) Successful in 5s
CI & Build / Python tests (push) Successful in 10s
CI & Build / Build & push image (push) Successful in 34s
M2 attachments: image upload + owner-scoped media serving
- Migration 0007: note_attachments (path/mime/size). Upload POST
  /api/notes/<id>/attachments (multipart, png/jpeg/gif/webp, 12MB cap via
  MAX_CONTENT_LENGTH) stored under Config.media_root() (first use of DATA_DIR);
  owner/ACL-scoped GET serves the file (nosniff); DELETE removes row + file.
  Note responses include attachments[].
- Frontend: notes store uploadAttachment (FormData)/deleteAttachment; editor
  image button + paste-to-upload + thumbnail grid with remove; card shows the
  first image as a cover.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FRgehjoz7Yv8LkUfADxACm
2026-07-19 22:21:26 -04:00

179 lines
5.2 KiB
TypeScript

import { defineStore } from "pinia";
import { ref } from "vue";
import { api } from "../api/client";
import type { NoteColor } from "../notes/colors";
export type NoteView = "active" | "archived" | "trash";
export type NoteKind = "text" | "list";
export interface NoteLabel {
id: string;
name: string;
}
export interface ChecklistItem {
id: string;
text: string;
checked: boolean;
position: number;
}
export interface Attachment {
id: string;
url: string;
mime: string;
}
export interface Note {
id: string;
title: string | null;
body: string;
color: NoteColor;
kind: NoteKind;
pinned: boolean;
archived: boolean;
trashed: boolean;
labels: NoteLabel[];
items: ChecklistItem[];
attachments: Attachment[];
created_at: string | null;
updated_at: string | null;
}
export const useNotesStore = defineStore("notes", () => {
const items = ref<Note[]>([]);
const loading = ref(false);
const view = ref<NoteView>("active");
const activeLabel = ref<string | null>(null);
function sortItems(): void {
items.value.sort((a, b) => {
if (a.pinned !== b.pinned) return a.pinned ? -1 : 1;
return (b.updated_at ?? "").localeCompare(a.updated_at ?? "");
});
}
function belongsHere(n: Note): boolean {
const v = view.value;
const inView =
v === "trash" ? n.trashed : v === "archived" ? !n.trashed && n.archived : !n.trashed && !n.archived;
if (!inView) return false;
if (activeLabel.value) return n.labels.some((lb) => lb.id === activeLabel.value);
return true;
}
function reconcile(note: Note): void {
const idx = items.value.findIndex((n) => n.id === note.id);
if (belongsHere(note)) {
if (idx >= 0) items.value[idx] = note;
else items.value.push(note);
sortItems();
} else if (idx >= 0) {
items.value.splice(idx, 1);
}
}
async function load(v: NoteView, labelId: string | null = null): Promise<void> {
view.value = v;
activeLabel.value = labelId;
loading.value = true;
try {
const query = labelId ? `/api/notes?filter=${v}&label=${labelId}` : `/api/notes?filter=${v}`;
const res = await api.get<{ notes: Note[] }>(query);
items.value = res.notes;
sortItems();
} finally {
loading.value = false;
}
}
async function create(input: { title: string; body: string; color: NoteColor }): Promise<void> {
reconcile(await api.post<Note>("/api/notes", input));
}
async function mutate(
id: string,
changes: Partial<Pick<Note, "title" | "body" | "color" | "kind" | "pinned" | "archived">>,
): Promise<void> {
reconcile(await api.patch<Note>(`/api/notes/${id}`, changes));
}
const setPinned = (id: string, pinned: boolean) => mutate(id, { pinned });
const setArchived = (id: string, archived: boolean) => mutate(id, { archived });
const setColor = (id: string, color: NoteColor) => mutate(id, { color });
const setKind = (id: string, kind: NoteKind) => mutate(id, { kind });
const saveEdit = (id: string, changes: { title: string; body: string; color: NoteColor }) => mutate(id, changes);
async function setLabels(id: string, labelIds: string[]): Promise<void> {
reconcile(await api.put<Note>(`/api/notes/${id}/labels`, { label_ids: labelIds }));
}
async function addItem(id: string, text: string): Promise<void> {
reconcile(await api.post<Note>(`/api/notes/${id}/items`, { text }));
}
async function updateItem(id: string, itemId: string, changes: { text?: string; checked?: boolean }): Promise<void> {
reconcile(await api.patch<Note>(`/api/notes/${id}/items/${itemId}`, changes));
}
async function deleteItem(id: string, itemId: string): Promise<void> {
reconcile(await api.del<Note>(`/api/notes/${id}/items/${itemId}`));
}
async function uploadAttachment(id: string, file: File): Promise<void> {
const form = new FormData();
form.append("file", file);
const resp = await fetch(`/api/notes/${id}/attachments`, { method: "POST", credentials: "include", body: form });
const data: unknown = await resp.json().catch(() => ({}));
if (!resp.ok) {
const message =
typeof data === "object" && data !== null && "error" in data
? String((data as { error: unknown }).error)
: "Upload failed.";
throw { error: message, status: resp.status };
}
reconcile(data as Note);
}
async function deleteAttachment(id: string, attId: string): Promise<void> {
reconcile(await api.del<Note>(`/api/notes/${id}/attachments/${attId}`));
}
async function trash(id: string): Promise<void> {
reconcile(await api.post<Note>(`/api/notes/${id}/trash`));
}
async function restore(id: string): Promise<void> {
reconcile(await api.post<Note>(`/api/notes/${id}/restore`));
}
async function deleteForever(id: string): Promise<void> {
await api.del(`/api/notes/${id}`);
const idx = items.value.findIndex((n) => n.id === id);
if (idx >= 0) items.value.splice(idx, 1);
}
return {
items,
loading,
view,
activeLabel,
load,
create,
setPinned,
setArchived,
setColor,
setKind,
saveEdit,
setLabels,
addItem,
updateItem,
deleteItem,
uploadAttachment,
deleteAttachment,
trash,
restore,
deleteForever,
};
});