Files
Momento/memento-note/lib/note-history.ts
sepehr 69ea064ca8
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 1m16s
feat: smart note history with manual/auto modes, delete entries, i18n fixes
- Add noteHistoryMode setting (manual default / auto) with DB migration
- Manual mode: commit button in editor toolbar creates snapshots on demand
- Auto mode: smart snapshots with 20-char diff threshold + 5min cooldown,
  structural changes (color, pin, archive, labels) bypass cooldown
- Add delete individual history entries from history modal
- Fix sidebar: Notes nav no longer active on notebook pages
- Fix sidebar icon: replace filled Lightbulb with outlined FileText
- Fix title suggestions: change from amber to sky blue color scheme
- Fix hydration mismatch: add suppressHydrationWarning on locale dates
- Complete i18n: add history, sort, and AI chat translations for all 16 languages
- Translate French AI assistant section (40+ keys) from English to French
- Update README with new features and stack info

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-28 21:05:55 +02:00

185 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
await prisma.$transaction(async (tx) => {
const lastVersionEntry = await (tx as any).noteHistory.findFirst({
where: { noteId },
orderBy: { version: 'desc' },
select: { version: true },
})
const nextVersion = ((lastVersionEntry?.version as number | undefined) ?? 0) + 1
await (tx as any).noteHistory.create({
data: {
noteId: note.id,
userId: note.userId,
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 as any).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
}