- I1: trashCount 가 upsertNote 안에서 항상 trashNotes.length 로 덮어써져 server 값 (refreshMeta) 손상. showTrash=true (trashNotes cache-loaded) 일 때만 local recompute. - I2: restoreNote 의 "fallback for missed event" 주석 부정확 — main 은 trash/restore 시 pushNoteUpdated 안 보냄. 자가 갱신이 primary mechanism. 주석 정정. - M5: restoreNote 테스트가 IPC 호출만 검증, 노트 이동 미검증. trashNotes → notes 라우팅 + deletedAt=null 어설션 추가. + I1 회귀 가드 테스트 신규. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
141 lines
4.7 KiB
TypeScript
141 lines
4.7 KiB
TypeScript
import { create } from 'zustand';
|
|
import type { Note, WeeklyContinuity } from '@shared/types';
|
|
import { inboxApi } from './api.js';
|
|
|
|
export { selectFilteredNotes } from './selectFilteredNotes.js';
|
|
|
|
interface InboxState {
|
|
notes: Note[];
|
|
trashNotes: Note[];
|
|
trashCount: number;
|
|
showTrash: boolean;
|
|
continuity: WeeklyContinuity;
|
|
pendingCount: number;
|
|
ollamaStatus: { ok: boolean; reason?: string };
|
|
todayCount: number;
|
|
loading: boolean;
|
|
tagFilter: string | null;
|
|
loadInitial: () => Promise<void>;
|
|
refreshMeta: () => Promise<void>;
|
|
upsertNote: (note: Note) => void;
|
|
removeNote: (id: string) => void;
|
|
setTagFilter: (tag: string | null) => void;
|
|
toggleShowTrash: () => Promise<void>;
|
|
loadTrash: () => Promise<void>;
|
|
restoreNote: (id: string) => Promise<void>;
|
|
permanentDeleteNote: (id: string) => Promise<void>;
|
|
emptyTrash: () => Promise<void>;
|
|
}
|
|
|
|
const emptyContinuity: WeeklyContinuity = {
|
|
weekStart: '', weekCount: 0, weekTarget: 7,
|
|
consecutiveCompleteWeeks: 0, showRecoveryToast: false, lastNoteAt: null
|
|
};
|
|
|
|
export const useInbox = create<InboxState>((set, get) => ({
|
|
notes: [],
|
|
trashNotes: [],
|
|
trashCount: 0,
|
|
showTrash: false,
|
|
continuity: emptyContinuity,
|
|
pendingCount: 0,
|
|
ollamaStatus: { ok: true },
|
|
todayCount: 0,
|
|
loading: false,
|
|
tagFilter: null,
|
|
async loadInitial() {
|
|
set({ loading: true });
|
|
const [notes, continuity, pendingCount, ollamaStatus, todayCount, trashCount] = await Promise.all([
|
|
inboxApi.listNotes({ limit: 50 }),
|
|
inboxApi.getContinuity(),
|
|
inboxApi.getPendingCount(),
|
|
inboxApi.getOllamaStatus(),
|
|
inboxApi.getTodayCount(),
|
|
inboxApi.getTrashCount()
|
|
]);
|
|
set({ notes, continuity, pendingCount, ollamaStatus, todayCount, trashCount, loading: false });
|
|
},
|
|
async refreshMeta() {
|
|
const [continuity, pendingCount, ollamaStatus, todayCount, trashCount] = await Promise.all([
|
|
inboxApi.getContinuity(),
|
|
inboxApi.getPendingCount(),
|
|
inboxApi.getOllamaStatus(),
|
|
inboxApi.getTodayCount(),
|
|
inboxApi.getTrashCount()
|
|
]);
|
|
set({ continuity, pendingCount, ollamaStatus, todayCount, trashCount });
|
|
},
|
|
upsertNote(note) {
|
|
// trashCount 는 server-authoritative. trashNotes 가 cache-loaded (showTrash=true) 일
|
|
// 때만 trashCount 를 local recompute. 그 외엔 server 값 (refreshMeta) 보존.
|
|
const showTrash = get().showTrash;
|
|
if (note.deletedAt !== null) {
|
|
// trash 노트: notes 에서 제거 + trashNotes 에 upsert
|
|
const cleanNotes = get().notes.filter((n) => n.id !== note.id);
|
|
const ti = get().trashNotes.findIndex((n) => n.id === note.id);
|
|
const nextTrash = get().trashNotes.slice();
|
|
if (ti >= 0) nextTrash[ti] = note;
|
|
else nextTrash.unshift(note);
|
|
set({
|
|
notes: cleanNotes,
|
|
trashNotes: nextTrash,
|
|
...(showTrash ? { trashCount: nextTrash.length } : {})
|
|
});
|
|
} else {
|
|
// active 노트: trashNotes 에서 제거 + notes 에 upsert (restore 케이스 포함)
|
|
const cleanTrash = get().trashNotes.filter((n) => n.id !== note.id);
|
|
const i = get().notes.findIndex((n) => n.id === note.id);
|
|
const nextNotes = get().notes.slice();
|
|
if (i >= 0) nextNotes[i] = note;
|
|
else nextNotes.unshift(note);
|
|
set({
|
|
notes: nextNotes,
|
|
trashNotes: cleanTrash,
|
|
...(showTrash ? { trashCount: cleanTrash.length } : {})
|
|
});
|
|
}
|
|
},
|
|
removeNote(id) {
|
|
const cleanNotes = get().notes.filter((n) => n.id !== id);
|
|
const cleanTrash = get().trashNotes.filter((n) => n.id !== id);
|
|
const showTrash = get().showTrash;
|
|
set({
|
|
notes: cleanNotes,
|
|
trashNotes: cleanTrash,
|
|
...(showTrash ? { trashCount: cleanTrash.length } : {})
|
|
});
|
|
},
|
|
setTagFilter(tag) {
|
|
set({ tagFilter: tag });
|
|
},
|
|
async toggleShowTrash() {
|
|
const next = !get().showTrash;
|
|
set({ showTrash: next });
|
|
if (next) await get().loadTrash();
|
|
},
|
|
async loadTrash() {
|
|
const trashNotes = await inboxApi.listTrash({ limit: 200 });
|
|
set({ trashNotes, trashCount: trashNotes.length });
|
|
},
|
|
async restoreNote(id) {
|
|
await inboxApi.restoreNote(id);
|
|
// 낙관적 갱신: main 은 trash/restore 시 pushNoteUpdated 를 보내지 않음
|
|
// (현재 AiWorker.onUpdate 만 push). 자가 반영이 primary 메커니즘.
|
|
// 전제: 호출 시점에 trashNotes 에 노트가 존재 (T14 trash view 한정 호출).
|
|
const note = get().trashNotes.find((n) => n.id === id);
|
|
if (note) {
|
|
get().upsertNote({ ...note, deletedAt: null });
|
|
}
|
|
},
|
|
async permanentDeleteNote(id) {
|
|
const r = await inboxApi.permanentDeleteNote(id);
|
|
if (r.confirmed) get().removeNote(id);
|
|
},
|
|
async emptyTrash() {
|
|
const r = await inboxApi.emptyTrash();
|
|
if (r.confirmed) {
|
|
set({ trashNotes: [], trashCount: 0 });
|
|
}
|
|
}
|
|
}));
|