Skip to content

Commit

Permalink
fix: merges records with the same values
Browse files Browse the repository at this point in the history
fixes zkemail#122, i dont
know if this is exactly what you wanted so you might have to review it,
it merges records with the same values while keeping the earliest first
seen at date with the oldest last seen at date.
  • Loading branch information
hhuntaa committed Oct 29, 2024
1 parent 22eb254 commit 614fb2b
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/components/DomainSearchResultsDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const DomainSearchResultsDisplay: React.FC<DomainSearchResultProps> = ({
loadMore,
cursor,
}) => {
const mergedRecords = mergeRecordsByValue(Array.from(records.values()));
const { ref: inViewElement, inView } = useInView();

useEffect(() => {
Expand All @@ -37,7 +38,7 @@ export const DomainSearchResultsDisplay: React.FC<DomainSearchResultProps> = ({
Search results for <b>{domainQuery}</b>
</p>
<div>
{Array.from(records.values()).map((record) => (
{mergedRecords.map((record) => (
<SelectorResult key={record.id} record={record} />
))}
</div>
Expand All @@ -46,3 +47,27 @@ export const DomainSearchResultsDisplay: React.FC<DomainSearchResultProps> = ({
</div>
);
};

function mergeRecordsByValue(records: RecordWithSelector[]): RecordWithSelector[] {
const valueMap = new Map<string, RecordWithSelector>();

records.forEach((record) => {
if (valueMap.has(record.value)) {
const existing = valueMap.get(record.value)!;

existing.firstSeenAt = new Date(Math.min(existing.firstSeenAt.getTime(), record.firstSeenAt.getTime()));

if (existing.lastSeenAt && record.lastSeenAt) {
existing.lastSeenAt = new Date(Math.max(existing.lastSeenAt.getTime(), record.lastSeenAt.getTime()));
} else {
existing.lastSeenAt = existing.lastSeenAt || record.lastSeenAt;
}

valueMap.set(record.value, existing);
} else {
valueMap.set(record.value, record);
}
});

return Array.from(valueMap.values());
}

0 comments on commit 614fb2b

Please sign in to comment.