Fixed location timeline
This commit is contained in:
1 parent
56d8818581
commit
270652f1a8
3 files changed
+59
-9
No files matched your search
+10
-9
@@ -1,3 +1,4 @@
|
|||||||
|
import { combineLocationOwnerMarkers } from './birdTimeline';
|
||||||
import { BehaviorJournal } from './BehaviorJournal';
|
import { BehaviorJournal } from './BehaviorJournal';
|
||||||
import { useEffect, useMemo, useState, type CSSProperties, type Dispatch, type SetStateAction } from 'react';
|
import { useEffect, useMemo, useState, type CSSProperties, type Dispatch, type SetStateAction } from 'react';
|
||||||
import birdSilhouette from './assets/bird-silhouette.jpg';
|
import birdSilhouette from './assets/bird-silhouette.jpg';
|
||||||
@@ -1187,7 +1188,7 @@ const formatBirdTimelineTitle = (event: BirdTimelineEvent) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (event.eventType === 'owner_changed') {
|
if (event.eventType === 'owner_changed') {
|
||||||
return 'Owner record changed';
|
return 'Adopted';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.eventType === 'manual_note') {
|
if (event.eventType === 'manual_note') {
|
||||||
@@ -1210,7 +1211,7 @@ const formatBirdTimelineDescription = (event: BirdTimelineEvent) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (event.eventType === 'owner_changed') {
|
if (event.eventType === 'owner_changed') {
|
||||||
return event.locationLabel ? `Owner changed at ${event.locationLabel}` : 'Owner changed';
|
return event.locationLabel ? `Adopted at ${event.locationLabel}` : 'Adopted';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.eventType === 'manual_note') {
|
if (event.eventType === 'manual_note') {
|
||||||
@@ -1232,7 +1233,7 @@ const formatBirdTimelineSecondary = (event: BirdTimelineEvent) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (event.eventType === 'owner_changed') {
|
if (event.eventType === 'owner_changed') {
|
||||||
return 'Owner changed without owner name';
|
return 'Adoption recorded without owner names';
|
||||||
}
|
}
|
||||||
|
|
||||||
return '';
|
return '';
|
||||||
@@ -1287,7 +1288,7 @@ const getBirdTimelineGraphLabel = (event: BirdTimelineEvent) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (event.eventType === 'owner_changed') {
|
if (event.eventType === 'owner_changed') {
|
||||||
return 'Owner changed';
|
return event.locationLabel ? `${compactTimelineLocationLabel(event.locationLabel)} — Adopted` : 'Adopted';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.eventType === 'transferred') {
|
if (event.eventType === 'transferred') {
|
||||||
@@ -1387,7 +1388,7 @@ const compactTimelineLocationLabel = (label: string) =>
|
|||||||
.join(', ');
|
.join(', ');
|
||||||
|
|
||||||
const getBirdTimelineGraphItems = (bird: Bird, events: BirdTimelineEvent[]): BirdTimelineGraphItem[] => {
|
const getBirdTimelineGraphItems = (bird: Bird, events: BirdTimelineEvent[]): BirdTimelineGraphItem[] => {
|
||||||
const graphEvents: BirdTimelineGraphItem[] = sortBirdTimelineEvents(events)
|
const graphEvents: BirdTimelineGraphItem[] = sortBirdTimelineEvents(combineLocationOwnerMarkers(events, compactTimelineLocationLabel))
|
||||||
.reverse()
|
.reverse()
|
||||||
.filter((event) => event.eventType !== 'profile_created')
|
.filter((event) => event.eventType !== 'profile_created')
|
||||||
.map((event) => ({
|
.map((event) => ({
|
||||||
@@ -8416,7 +8417,7 @@ function App() {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<option value="location_updated">Location note</option>
|
<option value="location_updated">Location note</option>
|
||||||
<option value="owner_changed">Owner</option>
|
<option value="owner_changed">Adoption</option>
|
||||||
<option value="manual_note">General note</option>
|
<option value="manual_note">General note</option>
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>
|
||||||
@@ -8466,8 +8467,8 @@ function App() {
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<span>
|
<span>
|
||||||
<strong>Owner changed</strong>
|
<strong>Adopted</strong>
|
||||||
<small>Records a change without owner names.</small>
|
<small>Records an adoption without owner names.</small>
|
||||||
</span>
|
</span>
|
||||||
</label>
|
</label>
|
||||||
) : null}
|
) : null}
|
||||||
@@ -8511,7 +8512,7 @@ function App() {
|
|||||||
) : (
|
) : (
|
||||||
<article className="empty-card">
|
<article className="empty-card">
|
||||||
<strong>No timeline events yet</strong>
|
<strong>No timeline events yet</strong>
|
||||||
<small>New location and owner changes for {selectedBird.name} will appear here.</small>
|
<small>New location changes and adoptions for {selectedBird.name} will appear here.</small>
|
||||||
</article>
|
</article>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
type LocationEvent = {
|
||||||
|
eventType: string;
|
||||||
|
eventDate: string;
|
||||||
|
createdAt: string;
|
||||||
|
locationLabel: string | null;
|
||||||
|
toWorkspaceId: number | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Keep the owner marker when it already represents the same dated location.
|
||||||
|
// The original event list remains unchanged so notes and audit history are retained.
|
||||||
|
export const combineLocationOwnerMarkers = <T extends LocationEvent>(
|
||||||
|
events: T[],
|
||||||
|
normalizeLocation: (location: string) => string,
|
||||||
|
): T[] => {
|
||||||
|
const key = (event: T) => JSON.stringify([
|
||||||
|
event.eventDate || event.createdAt.slice(0, 10),
|
||||||
|
normalizeLocation(event.locationLabel || '').trim().toLowerCase(),
|
||||||
|
event.toWorkspaceId,
|
||||||
|
]);
|
||||||
|
const ownerLocations = new Set(events
|
||||||
|
.filter((event) => event.eventType === 'owner_changed' && event.locationLabel?.trim())
|
||||||
|
.map(key));
|
||||||
|
return events.filter((event) =>
|
||||||
|
event.eventType !== 'location_updated' || !event.locationLabel?.trim() || !ownerLocations.has(key(event)),
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import { test } from 'node:test';
|
||||||
|
import { combineLocationOwnerMarkers } from '../src/birdTimeline.ts';
|
||||||
|
|
||||||
|
const location = { id: 'location', eventType: 'location_updated', eventDate: '2023-04-07', createdAt: '2026-10-07T12:00:00Z', locationLabel: 'Warwick, RI, US', toWorkspaceId: 1 };
|
||||||
|
const owner = { ...location, id: 'owner', eventType: 'owner_changed' };
|
||||||
|
const normalize = (value: string) => value.replace('United States', 'US');
|
||||||
|
|
||||||
|
test('combines a same-date location and owner change without modifying saved events', () => {
|
||||||
|
const events = [location, { ...owner, locationLabel: 'Warwick, RI, United States' }];
|
||||||
|
assert.deepEqual(combineLocationOwnerMarkers(events, normalize).map(event => event.id), ['owner']);
|
||||||
|
assert.equal(events.length, 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('preserves different dates, locations, flocks, and standalone location changes', () => {
|
||||||
|
for (const other of [
|
||||||
|
{ ...owner, eventDate: '2023-04-08' },
|
||||||
|
{ ...owner, locationLabel: 'Providence, RI, US' },
|
||||||
|
{ ...owner, toWorkspaceId: 2 },
|
||||||
|
{ ...owner, locationLabel: null },
|
||||||
|
]) assert.equal(combineLocationOwnerMarkers([location, other], normalize).length, 2);
|
||||||
|
assert.deepEqual(combineLocationOwnerMarkers([location], normalize), [location]);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user