Files
MineGeo/shared/map-refresh.ts

18 lines
705 B
TypeScript

export type MapCapture = { id: string; name: string; created_at: string };
// A contribution can be newer than the original capture, even when both have
// the same name/date in the picker. Keep deliberate choices; otherwise use the
// newest eligible capture, including when accepting a contribution adds one.
export function refreshCapture(sources: MapCapture[], chosen?: string) {
if (chosen && sources.some((source) => source.id === chosen)) return chosen;
return (
sources.reduce<MapCapture | undefined>(
(latest, source) =>
!latest || Date.parse(source.created_at) > Date.parse(latest.created_at)
? source
: latest,
undefined,
)?.id ?? ""
);
}