Skip to content

Commit c90bd3d

Browse files
committed
Add location and custom date changes to recent edits system
- Location changes now create version records and appear in recent edits - Custom date changes now create version records and appear in recent edits - Added special handling in DiffPreview for location/date change types - Version records include changeType metadata and descriptive previews - Both changes update lastDiff with appropriate preview messages - Maintains fallback behavior if version creation fails
1 parent 7d7992b commit c90bd3d

File tree

3 files changed

+152
-14
lines changed

3 files changed

+152
-14
lines changed

app/api/pages/[id]/custom-date/route.js

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,73 @@ export async function PATCH(request, { params }) {
9797
);
9898
}
9999

100-
// Update the page with new custom date
101-
const updateData = {
102-
customDate: customDate || null, // Allow clearing the custom date
103-
lastModified: new Date().toISOString()
104-
};
100+
// Create a version record for this custom date change
101+
const now = new Date().toISOString();
105102

106-
await pageRef.update(updateData);
103+
try {
104+
// Get current user data for version record
105+
const userRef = db.collection(getCollectionName('users')).doc(userId);
106+
const userDoc = await userRef.get();
107+
const userData = userDoc.exists ? userDoc.data() : null;
108+
const username = userData?.username || 'Anonymous';
109+
110+
// Create version data for custom date change
111+
const versionData = {
112+
content: pageData.content || '', // Keep existing content
113+
title: pageData.title || 'Untitled',
114+
createdAt: now,
115+
userId: userId,
116+
username: username,
117+
previousVersionId: pageData.currentVersion || null,
118+
groupId: pageData.groupId || null,
119+
120+
// Special metadata for custom date changes
121+
changeType: 'customDate',
122+
customDateChange: {
123+
from: pageData.customDate || null,
124+
to: customDate
125+
},
126+
127+
// No content diff for custom date changes
128+
diff: {
129+
added: 0,
130+
removed: 0,
131+
hasChanges: true // Always true for custom date changes
132+
}
133+
};
134+
135+
// Create the version document
136+
const versionRef = await pageRef.collection('versions').add(versionData);
137+
console.log(`API: Created version record for custom date change: ${versionRef.id}`);
138+
139+
// Update the page with new custom date and version info
140+
const updateData = {
141+
customDate: customDate || null, // Allow clearing the custom date
142+
lastModified: now,
143+
currentVersion: versionRef.id,
144+
// Add lastDiff for recent edits display
145+
lastDiff: {
146+
added: 0,
147+
removed: 0,
148+
hasChanges: true,
149+
preview: customDate ? `Set date to ${customDate}` : 'Removed custom date'
150+
}
151+
};
107152

108-
console.log(`API: Successfully updated custom date for page ${id}`);
153+
await pageRef.update(updateData);
154+
console.log(`API: Successfully updated custom date and created version for page ${id}`);
155+
156+
} catch (versionError) {
157+
console.error('API: Error creating version record for custom date (non-fatal):', versionError);
158+
159+
// Fallback: just update the custom date without version record
160+
const updateData = {
161+
customDate: customDate || null,
162+
lastModified: now
163+
};
164+
await pageRef.update(updateData);
165+
console.log(`API: Updated custom date without version record (fallback) for page ${id}`);
166+
}
109167

110168
// Trigger cache invalidation to refresh daily notes and other components
111169
try {

app/api/pages/[id]/location/route.ts

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,73 @@ export async function PATCH(
118118
}
119119
}
120120

121-
// Update the page with the new location
122-
const updateData: any = {
123-
location: location,
124-
lastModified: new Date().toISOString()
125-
};
121+
// Create a version record for this location change
122+
const now = new Date().toISOString();
126123

127-
await pageRef.update(updateData);
124+
try {
125+
// Get current user data for version record
126+
const userRef = db.collection(getCollectionName('users')).doc(currentUserId);
127+
const userDoc = await userRef.get();
128+
const userData = userDoc.exists ? userDoc.data() : null;
129+
const username = userData?.username || 'Anonymous';
128130

129-
console.log('🗺️ Location API: Successfully updated location for page:', pageId);
131+
// Create version data for location change
132+
const versionData = {
133+
content: pageData.content || '', // Keep existing content
134+
title: pageData.title || 'Untitled',
135+
createdAt: now,
136+
userId: currentUserId,
137+
username: username,
138+
previousVersionId: pageData.currentVersion || null,
139+
groupId: pageData.groupId || null,
140+
141+
// Special metadata for location changes
142+
changeType: 'location',
143+
locationChange: {
144+
from: pageData.location || null,
145+
to: location
146+
},
147+
148+
// No content diff for location changes
149+
diff: {
150+
added: 0,
151+
removed: 0,
152+
hasChanges: true // Always true for location changes
153+
}
154+
};
155+
156+
// Create the version document
157+
const versionRef = await pageRef.collection('versions').add(versionData);
158+
console.log('🗺️ Location API: Created version record:', versionRef.id);
159+
160+
// Update the page with the new location and version info
161+
const updateData: any = {
162+
location: location,
163+
lastModified: now,
164+
currentVersion: versionRef.id,
165+
// Add lastDiff for recent edits display
166+
lastDiff: {
167+
added: 0,
168+
removed: 0,
169+
hasChanges: true,
170+
preview: location ? 'Added location' : 'Removed location'
171+
}
172+
};
173+
174+
await pageRef.update(updateData);
175+
console.log('🗺️ Location API: Successfully updated location and created version for page:', pageId);
176+
177+
} catch (versionError) {
178+
console.error('🗺️ Location API: Error creating version record (non-fatal):', versionError);
179+
180+
// Fallback: just update the location without version record
181+
const updateData: any = {
182+
location: location,
183+
lastModified: now
184+
};
185+
await pageRef.update(updateData);
186+
console.log('🗺️ Location API: Updated location without version record (fallback)');
187+
}
130188

131189
return NextResponse.json({
132190
success: true,

app/components/activity/DiffPreview.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ export default function DiffPreview({
9292
);
9393
}
9494

95+
// Handle special change types (location, custom date)
96+
if (typeof preview === 'string') {
97+
// Check if it's a special change type preview
98+
if (preview.includes('location') || preview.includes('date')) {
99+
return (
100+
<div className={`text-xs h-full flex items-center ${className}`}>
101+
<span className="text-blue-600 dark:text-blue-400 bg-blue-50 dark:bg-blue-900/40 px-1.5 py-0.5 rounded text-xs font-medium">
102+
{preview}
103+
</span>
104+
</div>
105+
);
106+
}
107+
}
108+
95109
// If no diff data, show fallback message (but avoid redundancy for new pages)
96110
if (!preview) {
97111
return (
@@ -183,6 +197,14 @@ export function DiffStats({
183197
const TooltipTrigger = showTooltips ? require('../ui/tooltip').TooltipTrigger : null;
184198

185199
const renderStats = () => {
200+
// For special change types (location, custom date), show a different indicator
201+
if (added === 0 && removed === 0 && !isNewPage) {
202+
// This might be a special change type (location, custom date)
203+
return (
204+
<span className="text-blue-600 dark:text-blue-400"></span>
205+
);
206+
}
207+
186208
if (added > 0 && removed > 0) {
187209
// Show both additions and deletions in git-style format
188210
return (

0 commit comments

Comments
 (0)