diff --git a/src/components/moderation/FieldComparison.tsx b/src/components/moderation/FieldComparison.tsx index dd5fb883..017cea2d 100644 --- a/src/components/moderation/FieldComparison.tsx +++ b/src/components/moderation/FieldComparison.tsx @@ -203,16 +203,47 @@ interface LocationDiffProps { } export function LocationDiff({ oldLocation, newLocation, compact = false }: LocationDiffProps) { + // Check if we're creating a new location entity + const isCreatingNewLocation = oldLocation?.location_id && !oldLocation?.city && newLocation?.city; + const formatLocation = (loc: any) => { if (!loc) return 'None'; if (typeof loc === 'string') return loc; + + // Handle location_id reference + if (loc.location_id && !loc.city) { + return `Location ID: ${loc.location_id.substring(0, 8)}...`; + } + if (typeof loc === 'object') { - const parts = [loc.city, loc.state_province, loc.country].filter(Boolean); - return parts.join(', ') || 'Unknown'; + const parts = []; + if (loc.city) parts.push(loc.city); + if (loc.state_province) parts.push(loc.state_province); + if (loc.country && loc.country !== loc.state_province) parts.push(loc.country); + if (loc.postal_code) parts.push(loc.postal_code); + + let locationStr = parts.join(', ') || 'Unknown'; + + // Add coordinates if available + if (loc.latitude && loc.longitude) { + const lat = Number(loc.latitude).toFixed(6); + const lng = Number(loc.longitude).toFixed(6); + locationStr += ` (${lat}°, ${lng}°)`; + } + + return locationStr; } return String(loc); }; + // Check if only coordinates changed + const onlyCoordinatesChanged = oldLocation?.city === newLocation?.city && + oldLocation?.state_province === newLocation?.state_province && + oldLocation?.country === newLocation?.country && + oldLocation?.postal_code === newLocation?.postal_code && + (Number(oldLocation?.latitude) !== Number(newLocation?.latitude) || + Number(oldLocation?.longitude) !== Number(newLocation?.longitude)); + if (compact) { const oldLoc = formatLocation(oldLocation); const newLoc = formatLocation(newLocation); @@ -235,14 +266,24 @@ export function LocationDiff({ oldLocation, newLocation, compact = false }: Loca return ( - Location: {oldLoc} → {newLoc} + Location{isCreatingNewLocation && ' (New Entity)'} + {onlyCoordinatesChanged && ' (GPS Refined)'} + : {oldLoc} → {newLoc} ); } return (
-
Location
+
+ Location + {isCreatingNewLocation && ( + New location entity + )} + {onlyCoordinatesChanged && ( + GPS coordinates refined + )} +
{oldLocation && ( diff --git a/src/lib/submissionChangeDetection.ts b/src/lib/submissionChangeDetection.ts index ed2aeccc..e3e1e3dd 100644 --- a/src/lib/submissionChangeDetection.ts +++ b/src/lib/submissionChangeDetection.ts @@ -6,6 +6,9 @@ export interface FieldChange { oldValue: any; newValue: any; changeType: 'added' | 'removed' | 'modified'; + metadata?: { + isCreatingNewLocation?: boolean; + }; } export interface ImageChange { @@ -127,17 +130,28 @@ export async function detectChanges( const newValue = itemData[key]; // Handle location changes specially - compare objects not IDs - if (key === 'location') { + if (key === 'location' || key === 'location_id') { + // Skip location_id if we already have a location object + if (key === 'location_id' && itemData.location) { + return; + } + const oldLoc = originalData.location; const newLoc = itemData.location; + // Check if new location entity is being created (old has location_id, new has location object) + const isCreatingNewLocation = originalData.location_id && newLoc && typeof newLoc === 'object' && !oldLoc; + // Only compare if we have location objects with actual data if (newLoc && typeof newLoc === 'object' && oldLoc && typeof oldLoc === 'object') { - // Compare actual location data (city, state, country) + // Compare all location data including coordinates const locChanged = oldLoc.city !== newLoc.city || oldLoc.state_province !== newLoc.state_province || - oldLoc.country !== newLoc.country; + oldLoc.country !== newLoc.country || + oldLoc.postal_code !== newLoc.postal_code || + Number(oldLoc.latitude) !== Number(newLoc.latitude) || + Number(oldLoc.longitude) !== Number(newLoc.longitude); if (locChanged) { hasLocationChange = true; @@ -148,6 +162,16 @@ export async function detectChanges( changeType: 'modified', }); } + } else if (isCreatingNewLocation) { + // New location entity is being created - mark as location change + hasLocationChange = true; + fieldChanges.push({ + field: 'location', + oldValue: { location_id: originalData.location_id }, + newValue: newLoc, + changeType: 'modified', + metadata: { isCreatingNewLocation: true }, + }); } return; }