Fixed two critical bugs:
- Highlights not persisting after refresh - Data wasn't being saved to database
- Clear All button not clearing database - Migration logic was re-importing deleted data
The API route /api/history (POST and GET) was missing the highlights and entities fields:
- GET route: Not selecting these fields from database
- POST route: Not saving these fields to database during upsert
- User creates entry → Highlights show correctly ✅
- User refreshes page → Highlights disappear ❌
- Entities (people, places, events) also lost after refresh
// File: app/api/history/route.ts (lines 36-47)
select: {
id: true,
userId: true,
entryText: true,
reflectionText: true,
summary: true,
topic: true,
mood: true,
entities: true, // ← ADDED
highlights: true, // ← ADDED
audioData: includeAudio,
createdAt: true,
updatedAt: true,
}// File: app/api/history/route.ts (lines 136-147)
create: {
id: entryData.id,
userId: userId,
entryText: entryData.entry_text,
reflectionText: entryData.reflection_text,
summary: entryData.summary || null,
topic: entryData.topic || null,
mood: entryData.mood ?? null,
entities: entryData.entities || null, // ← ADDED
highlights: entryData.highlights || null, // ← ADDED
audioData: entryData.audio_data || null,
createdAt: entryData.created_at ? new Date(entryData.created_at) : new Date(),
}// File: app/api/history/route.ts (lines 149-159)
update: {
entryText: entryData.entry_text,
reflectionText: entryData.reflection_text,
summary: entryData.summary || null,
topic: entryData.topic || null,
mood: entryData.mood ?? null,
entities: entryData.entities || null, // ← ADDED
highlights: entryData.highlights || null, // ← ADDED
// audioData conditional update...
}✅ Highlights now persist across page refreshes
✅ Entities (names, places, events) also persist
✅ Data correctly saved to journal_entries.highlights JSON column
✅ Data correctly loaded from database on page load
The app has migration logic that automatically imports localStorage data into the database when:
- Database is empty (0 entries)
- localStorage has cached data
Bug scenario:
- User creates entries → Saved to DB ✅
- localStorage still has old cached data (from migration or pre-login)
- User clicks "Clear All" → DB cleared ✅
- Page refreshes → Migration logic triggers ❌
- Migration sees: "DB empty but localStorage has data"
- Old data from localStorage re-imported into DB ❌
- User sees "deleted" entries back again! ❌
// File: app/components/JournalApp.tsx (lines 331-338)
// Migrate localStorage data to Supabase if exists
const legacyHistory = localStorage.getItem(LEGACY_HISTORY_KEY);
const legacyUserHistory = localStorage.getItem(`serenity_journal_history_${currentUserId}`);
const historyToMigrate = legacyUserHistory || legacyHistory;
if (historyToMigrate && loadedHistory.length === 0) {
// AUTO-MIGRATE: This was causing re-import after clear!
const parsed = JSON.parse(historyToMigrate);
// ... save to database
}Modified clearAllHistory function to clear both database AND localStorage:
// File: app/components/JournalApp.tsx (lines 1795-1807)
} else if (userId) {
// Authenticated: clear from Supabase
try {
await historyService.deleteAllEntries();
// IMPORTANT: Also clear localStorage to prevent migration re-import
localStorage.removeItem(LEGACY_HISTORY_KEY);
localStorage.removeItem(`serenity_journal_history_${userId}`);
localStorage.removeItem(currentHistoryKey);
console.log('[JournalApp] Cleared database and localStorage');
} catch (error) {
// ... error handling
}
}- User clicks "Clear All"
- Database entries deleted via API ✅
- localStorage keys also cleared ✅
- Page refreshes
- Migration logic checks localStorage → empty ✅
- No re-import occurs ✅
- History stays cleared! ✅
LEGACY_HISTORY_KEY- Old unified history keyserenity_journal_history_${userId}- User-specific historycurrentHistoryKey- Current active key (same as user-specific)
✅ Clear All deletes from database ✅ Clear All also clears localStorage cache ✅ Page refresh doesn't re-import deleted data ✅ Migration logic no longer interferes ✅ Success toast shows correctly
Changes:
- Added
entitiesandhighlightsto GET route select statement - Added
entitiesandhighlightsto POST route create operation - Added
entitiesandhighlightsto POST route update operation
Lines changed: 36-47, 136-159
Changes:
- Modified
clearAllHistoryfunction to clear localStorage after database deletion - Added console log for debugging
Lines changed: 1795-1807
- Create journal entry
- Verify highlights show in reflection
- Refresh page (Ctrl+R / F5)
- Verify highlights still show
- Check browser dev tools → Network → Verify highlights in API response
- Check database → Verify
highlightscolumn has JSON data
- Create multiple journal entries (authenticated user)
- Verify entries saved to database
- Click "Clear All" button
- Verify success toast appears
- Verify UI shows empty history
- Refresh page (Ctrl+R / F5)
- Verify history is still empty (not re-imported)
- Check localStorage → Verify keys are cleared
- Check database → Verify entries are deleted
- Clear All in demo mode (localStorage only)
- Clear All in authenticated mode (database + localStorage)
- API failure handling (shows error toast, reloads history)
- Multiple users don't see each other's data
Both bugs stemmed from incomplete data persistence:
- Bug 1: Fields missing from API routes
- Bug 2: Cache not cleared alongside database
model JournalEntry {
// ... other fields
entities Json? @map("entities") // Extracted entities
highlights Json? @map("highlights") // AI-detected highlights
// ... other fields
}// Entities
{
"people": ["Sarah", "Miller"],
"places": ["office", "gym"],
"events": [{ "name": "team meeting", "date": "2025-01-15" }],
"organizations": ["Acme Corp"]
}
// Highlights
[
{ "text": "jaw is locking up", "type": "somatic_marker" },
{ "text": "pushed through 18 miles", "type": "identity_anchor" },
{ "text": "Sarah's email", "type": "external_stressor" },
{ "text": "incredible relief", "type": "emotional_shift" }
]Database migration already applied:
npx prisma migrate dev --name add_highlights_field
npx prisma generateMigration file: prisma/migrations/20260102070453_add_highlights_field/migration.sql
While fixing these bugs, also ensured:
- ✅ TypeScript types updated with
highlightsfield - ✅ History service handles highlights in conversion functions
- ✅ UI components receive and display highlights correctly
- ✅ No breaking changes to existing functionality
- ✅ All TypeScript errors resolved
- ✅ No linter errors introduced
Both bugs are now fully resolved:
- ✅ Highlights persist across page refreshes
- ✅ Clear All properly deletes from database without re-import
- ✅ Migration logic no longer interferes with clear operation
- ✅ Data consistency maintained between database and localStorage
The app now provides a reliable, high-quality highlighting experience that persists across sessions and devices.