Skip to content

Commit

Permalink
updates feed
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotBraem committed Jan 4, 2024
1 parent d9be9ed commit f1e04c3
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 41 deletions.
3 changes: 2 additions & 1 deletion replacements.dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"REPL_DEVHUB_CONTRACT": "bodevhub.testnet",
"REPL_NEAR": "discom.testnet",
"REPL_MOB": "eugenethedream",
"REPL_EFIZ": "efiz.testnet"
"REPL_EFIZ": "efiz.testnet",
"REPL_DEVS": "nearbuilders.testnet"
}
3 changes: 2 additions & 1 deletion replacements.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"REPL_DEVHUB_CONTRACT": "devgovgigs.near",
"REPL_NEAR": "near",
"REPL_MOB": "mob.near",
"REPL_EFIZ": "efiz.near"
"REPL_EFIZ": "efiz.near",
"REPL_DEVS": "devs.near"
}
3 changes: 2 additions & 1 deletion replacements.testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"REPL_DEVHUB_CONTRACT": "thomaselliot.testnet",
"REPL_NEAR": "discom.testnet",
"REPL_MOB": "eugenethedream",
"REPL_EFIZ": "efiz.testnet"
"REPL_EFIZ": "efiz.testnet",
"REPL_DEVS": "nearbuilders.testnet"
}
116 changes: 116 additions & 0 deletions src/devhub/components/organism/Feed.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
const { Feed } = VM.require("${REPL_DEVS}/widget/Module.Feed");
Feed = Feed || (() => <></>);

const GRAPHQL_ENDPOINT =
props.GRAPHQL_ENDPOINT ?? "https://near-queryapi.api.pagoda.co";

let lastPostSocialApi = Social.index("post", "main", {
limit: 1,
order: "desc",
});

if (lastPostSocialApi == null) {
return "Loading...";
}

State.init({
// If QueryAPI Feed is lagging behind Social API, fallback to old widget.
shouldFallback: false,
});

function fetchGraphQL(operationsDoc, operationName, variables) {
return asyncFetch(`${GRAPHQL_ENDPOINT}/v1/graphql`, {
method: "POST",
headers: { "x-hasura-role": "dataplatform_near" },
body: JSON.stringify({
query: operationsDoc,
variables: variables,
operationName: operationName,
}),
});
}

const lastPostQuery = `
query IndexerQuery {
dataplatform_near_social_feed_posts( limit: 1, order_by: { block_height: desc }) {
block_height
}
}
`;

fetchGraphQL(lastPostQuery, "IndexerQuery", {})
.then((feedIndexerResponse) => {
if (
feedIndexerResponse &&
feedIndexerResponse.body.data.dataplatform_near_social_feed_posts.length >
0
) {
const nearSocialBlockHeight = lastPostSocialApi[0].blockHeight;
const feedIndexerBlockHeight =
feedIndexerResponse.body.data.dataplatform_near_social_feed_posts[0]
.block_height;

const lag = nearSocialBlockHeight - feedIndexerBlockHeight;
let shouldFallback = lag > 2 || !feedIndexerBlockHeight;
if (shouldFallback === true) {
console.log(
"Falling back to Social index feed. Block difference is: ",
nearSocialBlockHeight - feedIndexerBlockHeight
);
State.update({ shouldFallback });
}
} else {
console.log(
"Falling back to Social index feed. No QueryApi data received."
);
State.update({ shouldFallback: true });
}
})
.catch((error) => {
console.log(
"Error while fetching QueryApi feed (falling back to index feed): ",
error
);
State.update({ shouldFallback: true });
});

return (
<>
{state.shouldFallback ? (
<Feed
index={[
{
action: "post",
key: "main",
options: {
limit: 10,
order: "desc",
accountId: [`${handle}.community.${REPL_DEVHUB_CONTRACT}`],
},
cacheOptions: {
ignoreCache: true,
},
},
]}
Item={(item) => (
<Widget
src="${REPL_NEAR}/widget/v1.Posts.Post"
props={{
accountId: item.accountId,
blockHeight: item.blockHeight,
}}
/>
)}
/>
) : (
<Widget
src={`${REPL_DEVHUB}/widget/devhub.components.organism.Feed.NearQueryApi`}
props={{
GRAPHQL_ENDPOINT,
showFlagAccountFeature: true,
...props,
}}
/>
)}
</>
);
Loading

0 comments on commit f1e04c3

Please sign in to comment.