Files
Momento/memento-note/lib/note-history.ts
sepehr 3818eb8237
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 2m14s
feat: note history modal with restore, diff comparison, and dynamic UI updates
- Complete rewrite of note-history-modal: version list with inline restore/delete,
  split diff view with synced scrolling, rich preview for markdown/richtext/checklist
- Fix restore not updating editor: use key={id-updatedAt} on NoteInlineEditor to
  force remount on restore, and add sync useEffect to reset local state on prop changes
- Add sync useEffect in NoteEditor for external note updates
- Add historyEnabled to NOTE_LIST_SELECT (no more 'Activer' on every modal open)
- Replace browser confirm() with shadcn AlertDialog for delete confirmation
- Add i18n keys: currentVersion, compareVersions, diffTitle, diffSelectHint, deleteVersionDesc
- Install diff + @types/diff for visual line diffing
- Fix MasonryGrid render-phase sync to preserve local sizes
- Update notes-tabs-view merge logic for restored note propagation
- Remove debug console.log from restoreNoteVersion
2026-05-02 16:51:12 +02:00

187 lines
5.0 KiB
TypeScript

import prisma from '@/lib/prisma'
import { asArray } from '@/lib/utils'
import type { NoteHistoryEntry } from '@/lib/types'
const COOLDOWN_MS = 5 * 60 * 1000 // 5 minutes
const CONTENT_DIFF_THRESHOLD = 20 // characters
const HISTORY_TRACKED_FIELDS = [
'title',
'content',
'color',
'isPinned',
'isArchived',
'type',
'checkItems',
'labels',
'images',
'links',
'isMarkdown',
'size',
'notebookId',
] as const
export function shouldCaptureHistorySnapshot(data: Record<string, unknown>): boolean {
return HISTORY_TRACKED_FIELDS.some((field) => field in data)
}
export async function isNoteHistoryEnabledForUser(userId: string): Promise<boolean> {
const settings = await prisma.userAISettings.findUnique({
where: { userId },
select: { noteHistory: true },
})
return settings?.noteHistory === true
}
export async function createNoteHistorySnapshot({
noteId,
userId,
reason,
}: {
noteId: string
userId: string
reason?: string
}): Promise<void> {
const note = await prisma.note.findFirst({
where: { id: noteId, userId },
select: {
id: true,
userId: true,
title: true,
content: true,
color: true,
isPinned: true,
isArchived: true,
type: true,
checkItems: true,
labels: true,
images: true,
links: true,
isMarkdown: true,
size: true,
notebookId: true,
},
})
if (!note || !note.userId) return
const ownerId = note.userId
await prisma.$transaction(async (tx) => {
const lastVersionEntry = await tx.noteHistory.findFirst({
where: { noteId },
orderBy: { version: 'desc' },
select: { version: true },
})
const nextVersion = ((lastVersionEntry?.version as number | undefined) ?? 0) + 1
await tx.noteHistory.create({
data: {
noteId: note.id,
userId: ownerId,
version: nextVersion,
reason: reason ?? null,
title: note.title,
content: note.content,
color: note.color,
isPinned: note.isPinned,
isArchived: note.isArchived,
type: note.type,
checkItems: note.checkItems,
labels: note.labels,
images: note.images,
links: note.links,
isMarkdown: note.isMarkdown,
size: note.size,
notebookId: note.notebookId,
},
})
})
}
export function parseNoteHistoryEntry(entry: any): NoteHistoryEntry {
return {
id: entry.id,
noteId: entry.noteId,
userId: entry.userId,
version: entry.version,
reason: entry.reason ?? null,
title: entry.title ?? null,
content: entry.content,
color: entry.color,
isPinned: entry.isPinned,
isArchived: entry.isArchived,
type: entry.type,
checkItems: asArray(entry.checkItems, null as any) ?? null,
labels: asArray(entry.labels) || null,
images: asArray(entry.images) || null,
links: asArray(entry.links) || null,
isMarkdown: entry.isMarkdown,
size: entry.size,
notebookId: entry.notebookId ?? null,
createdAt: entry.createdAt,
}
}
export async function getNoteHistoryMode(userId: string): Promise<'manual' | 'auto'> {
const settings = await prisma.userAISettings.findUnique({
where: { userId },
select: { noteHistoryMode: true },
})
const mode = settings?.noteHistoryMode
return mode === 'auto' ? 'auto' : 'manual'
}
const STRUCTURAL_FIELDS = ['color', 'isPinned', 'isArchived', 'labels', 'notebookId'] as const
export async function shouldCreateAutoSnapshot(params: {
noteId: string
userId: string
updateData: Record<string, unknown>
existingContent: string
existingTitle: string | null
}): Promise<boolean> {
const { noteId, userId, updateData, existingContent, existingTitle } = params
// Structural changes (color, pin, archive, labels, notebook) always create a snapshot
const hasStructuralChange = STRUCTURAL_FIELDS.some((f) => f in updateData)
if (hasStructuralChange) return true
// Content changes: check diff threshold
const newContent = typeof updateData.content === 'string' ? updateData.content : null
const newTitle = typeof updateData.title === 'string' ? updateData.title : null
const contentChanged = newContent !== null
const titleChanged = newTitle !== null
if (!contentChanged && !titleChanged) return false
// Check cooldown: find the most recent snapshot for this note
const lastSnapshot = await prisma.noteHistory.findFirst({
where: { noteId },
orderBy: { createdAt: 'desc' },
select: { createdAt: true },
})
if (lastSnapshot) {
const elapsed = Date.now() - new Date(lastSnapshot.createdAt).getTime()
if (elapsed < COOLDOWN_MS) {
// Within cooldown — only skip if the diff is trivial
const contentDiff = contentChanged
? Math.abs((newContent as string).length - existingContent.length)
: 0
const titleDiff = titleChanged
? Math.abs((newTitle ?? '').length - (existingTitle ?? '').length)
: 0
if (contentDiff < CONTENT_DIFF_THRESHOLD && titleDiff === 0) {
return false
}
}
}
return true
}