State.init({ poll: {}, pollAnswers: [], }); if (!props.blockHeight) { return "Prop block height wasn't provided"; } const widgetOwner = "sking.near"; const indexVersion = props.indexVersion ?? "3.2.0"; const accountId = props.accountId ?? context.accountId; const blockHeight = Number(props.blockHeight); const polls = Social.index("poll_question", `question-v${indexVersion}`); if (!polls) { return "Loading..."; } const poll = polls.find((q) => q.blockHeight == blockHeight); if (JSON.stringify(poll) != JSON.stringify(state.poll)) { State.update({ poll: poll }); } if (!state.poll) { 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 { value: { verifiedHumansOnly, endTimestamp, startTimestamp }, } = state.poll; // should be only right poll input = input.filter((v) => Number(v.value.pollBlockHeight) == blockHeight); 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; }; let allAnswers = Social.index("poll_question", `answer-v${indexVersion}`); if (!allAnswers) return "Loading"; allAnswers = getValidAnswersOnly(allAnswers); if (JSON.stringify(allAnswers) != JSON.stringify(state.pollAnswers)) { State.update({ pollAnswers: allAnswers }); } let columns = state.poll.value.questions.map((v) => { return { title: v.question, }; }); columns = [{ title: "User" }, ...columns]; let data = state.pollAnswers.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 ( <> <Widget src={`${widgetOwner}/widget/EasyPoll.Data.Table`} props={{ columns: columns, data: data, title: "Results for: " + state.poll.value.title, }} /> </> );