Skip to content

Commit

Permalink
some improvements to merged index feed
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotBraem committed Feb 2, 2024
1 parent ce06520 commit 1c6764d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 25 deletions.
82 changes: 57 additions & 25 deletions apps/bos-blocks/widget/PR/MergedIndexFeed.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,60 @@ const computeFetchFrom = (items, limit, desc) => {
return desc ? blockHeight - 1 : blockHeight + 1;
};

const mergeItems = (iIndex, oldItems, newItems, desc) => {
const index = indices[iIndex];
const items = [
...new Set(
[
...newItems.map((item) => ({
...item,
action: index.action,
key: index.key,
index: iIndex,
})),
...oldItems,
].map((i) => JSON.stringify(i))
),
].map((i) => JSON.parse(i));
items.sort((a, b) => a.blockHeight - b.blockHeight);
if (desc) {
items.reverse();
}
return items;
};
function mergeItems(iIndex, oldItems, newItems, desc) {
const itemMap = new Map();

const generateKey = (item) => ({
accountId: item.accountId,
blockHeight: item.blockHeight,
});

// Add old items to the map
oldItems.forEach((item) => {
const key = generateKey(item);
itemMap.set(key, item);
});

newItems.forEach((item) => {
const key = generateKey(item);
if (!itemMap.has(key)) {
itemMap.set(key, {
...item,
index: iIndex,
});
}
});

// Convert the Map values to an array
let mergedItems = Array.from(itemMap.values());

// Sort items by blockHeight, ascending or descending based on the `desc` flag
mergedItems.sort((a, b) =>
desc ? b.blockHeight - a.blockHeight : a.blockHeight - b.blockHeight
);

return mergedItems;
}

// const mergeItems = (iIndex, newItems, desc) => {
// const uniqueItems = new Map();

// newItems.forEach((item) => {
// const key = { blockHeight: item.blockHeight, accountId: item.accountId };
// if (!uniqueItems.has(key)) {
// uniqueItems.set(key, {
// ...item,
// index: iIndex,
// });
// }
// });

// const sortedItems = Array.from(uniqueItems.values()).sort((a, b) =>
// desc ? b.blockHeight - a.blockHeight : a.blockHeight - b.blockHeight
// );

// return sortedItems;
// };

const jIndices = JSON.stringify(indices);
if (jIndices !== state.jIndices) {
Expand Down Expand Up @@ -145,12 +178,11 @@ if (requiredIndices.length > 0) {
}
}
// Compute the intersection of uniqueIdentifiers across all required indices
commonUniqueIdentifiers = itemsByRequiredIndex.reduce((a, b) =>
a.filter((c) => b.includes(c))
);
commonUniqueIdentifiers =
itemsByRequiredIndex.length &&
itemsByRequiredIndex.reduce((a, b) => a.filter((c) => b.includes(c)));
}


// Construct merged feed and compute usage per feed.

const filteredItems = [];
Expand Down
9 changes: 9 additions & 0 deletions apps/builddao/widget/config/feed.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ const feedLink = "https://nearbuilders.org/feed";
return {
type: "app", // every.near/type/app
routes: {
all: {
path: "buildhub.near/widget/Feed",
blockHeight: "final",
init: {
name: "All", // maybe these should be moved to navbar specific
icon: "bi-list",
requiredHashtags: ["build"]
},
},
resolutions: {
path: "buildhub.near/widget/Feed",
blockHeight: "final",
Expand Down

0 comments on commit 1c6764d

Please sign in to comment.