const widgetOwner = props.widgetOwner ?? "sking.near"; const src = props.src; const accountId = props.accountId ?? context.accountId; const blockHeight = props.blockHeight ?? "final"; const indexVersion = props.indexVersion ?? "3.2.0"; if (!src) { return "Please provide poll src"; } const poll = Social.get(`${src}`, blockHeight); if (!poll) { return "Loading..."; } poll = JSON.parse(poll); poll.accountId = src.split("/")[0]; let profile = Social.getr(`${poll.accountId}/profile`); let userAnswers = Social.index(`easypoll-${indexVersion}-answer`, `${src}`, { accountId: accountId, }); if (!userAnswers) return "Loading..."; let allAnswers = Social.index(`easypoll-${indexVersion}-answer`, `${src}`); if (!allAnswers) return "Loading..."; const isVerifiedHuman = (account) => { const view = Near.view("registry.i-am-human.near", "sbt_tokens_by_owner", { account: `${account}`, issuer: "fractal.i-am-human.near", }); return view?.[0]?.[1]?.[0]; }; const getValidAnswersOnly = (input) => { const { verifiedHumansOnly, endTimestamp, startTimestamp } = poll; // should be only right poll let filtered = input // should be 1 per user .map((e) => e["accountId"]) .map((e, i, final) => final.indexOf(e) === i && i) .filter((e) => input[e]) .map((e) => input[e]) // .filter(async (v, i) => { // should respect human only if (verifiedHumansOnly && !isVerifiedHuman(v.accountId)) return false; // should respect startTimestamp if (v.value.timestamp < startTimestamp) return false; // should respect endTimestamp if (v.value.timestamp > endTimestamp) return false; return true; }); return filtered; }; const expensiveWork = () => { const filteredAnswers = getValidAnswersOnly(allAnswers); let columns = poll.questions.map((v) => { return { title: v.title, }; }); columns = [{ title: "User" }, ...columns]; let data = filteredAnswers.map((v) => { let items = Object.values(v.value.answers); items = items.map((v) => { if (Array.isArray(v)) { return v.join(" - "); } return v; }); return [v.accountId, ...items]; }); return { filteredAnswers, columns, data, }; }; if (!state) { State.init({ ...expensiveWork() }); } return ( <> <Widget src={`${widgetOwner}/widget/EasyPoll.Data.Table`} props={{ columns: state.columns, data: state.data, title: "Results for: " + poll.title, }} /> </> );