Fix founded year/date handling

This commit is contained in:
gpt-engineer-app[bot]
2025-10-13 17:48:52 +00:00
parent 5feee9f4bc
commit b3ffb60ffb
4 changed files with 79 additions and 47 deletions

View File

@@ -8,6 +8,9 @@ export interface FieldChange {
changeType: 'added' | 'removed' | 'modified';
metadata?: {
isCreatingNewLocation?: boolean;
precision?: 'day' | 'month' | 'year';
oldPrecision?: 'day' | 'month' | 'year';
newPrecision?: 'day' | 'month' | 'year';
};
}
@@ -234,28 +237,32 @@ export async function detectChanges(
// Check for changes
if (!isEqual(oldValue, newValue)) {
if (oldEmpty && !newEmpty) {
fieldChanges.push({
field: key,
oldValue,
newValue,
changeType: 'added',
});
} else if (newEmpty && !oldEmpty) {
fieldChanges.push({
field: key,
oldValue,
newValue,
changeType: 'removed',
});
} else {
fieldChanges.push({
field: key,
oldValue,
newValue,
changeType: 'modified',
});
const fieldChange: FieldChange = {
field: key,
oldValue,
newValue,
changeType: oldEmpty && !newEmpty ? 'added' :
newEmpty && !oldEmpty ? 'removed' :
'modified',
};
// Add precision metadata for date fields
if (key.endsWith('_date') && !key.endsWith('_precision')) {
const precisionKey = `${key}_precision`;
const newPrecision = itemData[precisionKey];
const oldPrecision = originalData[precisionKey];
if (newPrecision || oldPrecision) {
fieldChange.metadata = {
...fieldChange.metadata,
precision: newPrecision || oldPrecision,
oldPrecision,
newPrecision,
};
}
}
fieldChanges.push(fieldChange);
}
});
@@ -490,14 +497,23 @@ function formatEntityType(entityType: string): string {
/**
* Format field value for display
*/
export function formatFieldValue(value: any): string {
export function formatFieldValue(value: any, precision?: 'day' | 'month' | 'year'): string {
if (value === null || value === undefined) return 'None';
if (typeof value === 'boolean') return value ? 'Yes' : 'No';
// Handle dates
// Handle dates with precision support
if (value instanceof Date || (typeof value === 'string' && /^\d{4}-\d{2}-\d{2}/.test(value))) {
try {
const date = new Date(value);
// Apply precision if provided
if (precision === 'year') {
return date.getFullYear().toString();
} else if (precision === 'month') {
return date.toLocaleDateString('en-US', { year: 'numeric', month: 'long' });
}
// Default: full date
return date.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
} catch {
return String(value);
@@ -525,6 +541,15 @@ export function formatFieldValue(value: any): string {
return entries.map(([k, v]) => `${k}: ${v}`).join(', ');
}
// Handle year-like numbers (prevent comma formatting for founded_year)
if (typeof value === 'number') {
const currentYear = new Date().getFullYear();
if (value >= 1800 && value <= currentYear + 10) {
return value.toString(); // Don't add commas for year values
}
return value.toLocaleString(); // Add commas for other numbers
}
// Handle URLs
if (typeof value === 'string' && value.startsWith('http')) {
try {