Skip to content

Commit 614fb2b

Browse files
committed
fix: merges records with the same values
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.
1 parent 22eb254 commit 614fb2b

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/components/DomainSearchResultsDisplay.tsx

+26-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const DomainSearchResultsDisplay: React.FC<DomainSearchResultProps> = ({
1717
loadMore,
1818
cursor,
1919
}) => {
20+
const mergedRecords = mergeRecordsByValue(Array.from(records.values()));
2021
const { ref: inViewElement, inView } = useInView();
2122

2223
useEffect(() => {
@@ -37,7 +38,7 @@ export const DomainSearchResultsDisplay: React.FC<DomainSearchResultProps> = ({
3738
Search results for <b>{domainQuery}</b>
3839
</p>
3940
<div>
40-
{Array.from(records.values()).map((record) => (
41+
{mergedRecords.map((record) => (
4142
<SelectorResult key={record.id} record={record} />
4243
))}
4344
</div>
@@ -46,3 +47,27 @@ export const DomainSearchResultsDisplay: React.FC<DomainSearchResultProps> = ({
4647
</div>
4748
);
4849
};
50+
51+
function mergeRecordsByValue(records: RecordWithSelector[]): RecordWithSelector[] {
52+
const valueMap = new Map<string, RecordWithSelector>();
53+
54+
records.forEach((record) => {
55+
if (valueMap.has(record.value)) {
56+
const existing = valueMap.get(record.value)!;
57+
58+
existing.firstSeenAt = new Date(Math.min(existing.firstSeenAt.getTime(), record.firstSeenAt.getTime()));
59+
60+
if (existing.lastSeenAt && record.lastSeenAt) {
61+
existing.lastSeenAt = new Date(Math.max(existing.lastSeenAt.getTime(), record.lastSeenAt.getTime()));
62+
} else {
63+
existing.lastSeenAt = existing.lastSeenAt || record.lastSeenAt;
64+
}
65+
66+
valueMap.set(record.value, existing);
67+
} else {
68+
valueMap.set(record.value, record);
69+
}
70+
});
71+
72+
return Array.from(valueMap.values());
73+
}

0 commit comments

Comments
 (0)