Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion dist/hooks/actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var __rest = (this && this.__rest) || function (s, e) {
}
return t;
};
import { useMemo, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';
import useAccountsStore from '../../stores/accounts';
import Logger from '@plebbit/plebbit-logger';
const log = Logger('plebbit-react-hooks:actions:hooks');
Expand All @@ -27,6 +27,42 @@ import { useAccount, useAccountId } from '../accounts';
const publishChallengeAnswersNotReady = (challengeAnswers) => __awaiter(void 0, void 0, void 0, function* () {
throw Error(`can't call publishChallengeAnswers() before result.challenge is defined (before the challenge message is received)`);
});
function useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers) {
const challenges = challenge === null || challenge === void 0 ? void 0 : challenge.challenges;
const allIframe = (challenges === null || challenges === void 0 ? void 0 : challenges.length) ? challenges.every((c) => c.type === 'url/iframe') : false;
useEffect(() => {
if (!allIframe || !challenges || !publishChallengeAnswers)
return;
const origins = challenges.map((c) => {
try {
return new URL(c.challenge).origin;
}
catch (e) {
return undefined;
}
});
const answers = new Array(challenges.length).fill(undefined);
let submitted = false;
const handleMessage = (event) => {
var _a;
if (submitted)
return;
if (((_a = event.data) === null || _a === void 0 ? void 0 : _a.type) !== 'challengeanswer' || typeof event.data.challengeAnswer !== 'string')
return;
// find which iframe sent this by matching origin to first unfilled slot
const index = origins.findIndex((origin, i) => answers[i] === undefined && (!origin || origin === event.origin));
if (index === -1)
return;
answers[index] = event.data.challengeAnswer;
if (answers.every((a) => a !== undefined)) {
submitted = true;
publishChallengeAnswers(answers);
}
};
window.addEventListener('message', handleMessage);
return () => window.removeEventListener('message', handleMessage);
}, [allIframe, challenges, publishChallengeAnswers]);
}
export function useSubscribe(options) {
var _a;
assert(!options || typeof options === 'object', `useSubscribe options argument '${options}' not an object`);
Expand Down Expand Up @@ -137,6 +173,7 @@ export function usePublishComment(options) {
const [challenge, setChallenge] = useState();
const [challengeVerification, setChallengeVerification] = useState();
const [publishChallengeAnswers, setPublishChallengeAnswers] = useState();
useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers);
let initialState = 'initializing';
// before the accountId and options is defined, nothing can happen
if (accountId && options) {
Expand Down Expand Up @@ -201,6 +238,7 @@ export function usePublishVote(options) {
const [challenge, setChallenge] = useState();
const [challengeVerification, setChallengeVerification] = useState();
const [publishChallengeAnswers, setPublishChallengeAnswers] = useState();
useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers);
let initialState = 'initializing';
// before the accountId and options is defined, nothing can happen
if (accountId && options) {
Expand Down Expand Up @@ -263,6 +301,7 @@ export function usePublishCommentEdit(options) {
const [challenge, setChallenge] = useState();
const [challengeVerification, setChallengeVerification] = useState();
const [publishChallengeAnswers, setPublishChallengeAnswers] = useState();
useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers);
let initialState = 'initializing';
// before the accountId and options is defined, nothing can happen
if (accountId && options) {
Expand Down Expand Up @@ -325,6 +364,7 @@ export function usePublishCommentModeration(options) {
const [challenge, setChallenge] = useState();
const [challengeVerification, setChallengeVerification] = useState();
const [publishChallengeAnswers, setPublishChallengeAnswers] = useState();
useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers);
let initialState = 'initializing';
// before the accountId and options is defined, nothing can happen
if (accountId && options) {
Expand Down Expand Up @@ -387,6 +427,7 @@ export function usePublishSubplebbitEdit(options) {
const [challenge, setChallenge] = useState();
const [challengeVerification, setChallengeVerification] = useState();
const [publishChallengeAnswers, setPublishChallengeAnswers] = useState();
useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers);
let initialState = 'initializing';
// before the accountId and options is defined, nothing can happen
if (accountId && subplebbitAddress) {
Expand Down
45 changes: 44 additions & 1 deletion src/hooks/actions/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useMemo, useState} from 'react'
import {useEffect, useMemo, useState} from 'react'
import useAccountsStore from '../../stores/accounts'
import Logger from '@plebbit/plebbit-logger'
const log = Logger('plebbit-react-hooks:actions:hooks')
Expand Down Expand Up @@ -36,6 +36,44 @@ const publishChallengeAnswersNotReady: PublishChallengeAnswers = async (challeng
throw Error(`can't call publishChallengeAnswers() before result.challenge is defined (before the challenge message is received)`)
}

function useIframeChallengeAutoAnswer(challenge: Challenge | undefined, publishChallengeAnswers: PublishChallengeAnswers | undefined) {
const challenges: {type: string; challenge: string}[] | undefined = challenge?.challenges
const allIframe = challenges?.length ? challenges.every((c: {type: string; challenge: string}) => c.type === 'url/iframe') : false

useEffect(() => {
if (!allIframe || !challenges || !publishChallengeAnswers) return

const origins = challenges.map((c: {type: string; challenge: string}) => {
try {
return new URL(c.challenge).origin
} catch (e) {
return undefined
}
})

const answers: (string | undefined)[] = new Array(challenges.length).fill(undefined)
let submitted = false

const handleMessage = (event: MessageEvent) => {
if (submitted) return
if (event.data?.type !== 'challengeanswer' || typeof event.data.challengeAnswer !== 'string') return

// find which iframe sent this by matching origin to first unfilled slot
const index = origins.findIndex((origin, i) => answers[i] === undefined && (!origin || origin === event.origin))
if (index === -1) return

answers[index] = event.data.challengeAnswer

if (answers.every((a) => a !== undefined)) {
submitted = true
publishChallengeAnswers(answers as string[])
}
}
window.addEventListener('message', handleMessage)
return () => window.removeEventListener('message', handleMessage)
}, [allIframe, challenges, publishChallengeAnswers])
}

export function useSubscribe(options?: UseSubscribeOptions): UseSubscribeResult {
assert(!options || typeof options === 'object', `useSubscribe options argument '${options}' not an object`)
const {subplebbitAddress, accountName, onError} = options || {}
Expand Down Expand Up @@ -155,6 +193,7 @@ export function usePublishComment(options?: UsePublishCommentOptions): UsePublis
const [challenge, setChallenge] = useState<Challenge>()
const [challengeVerification, setChallengeVerification] = useState<ChallengeVerification>()
const [publishChallengeAnswers, setPublishChallengeAnswers] = useState<PublishChallengeAnswers>()
useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers)

let initialState = 'initializing'
// before the accountId and options is defined, nothing can happen
Expand Down Expand Up @@ -228,6 +267,7 @@ export function usePublishVote(options?: UsePublishVoteOptions): UsePublishVoteR
const [challenge, setChallenge] = useState<Challenge>()
const [challengeVerification, setChallengeVerification] = useState<ChallengeVerification>()
const [publishChallengeAnswers, setPublishChallengeAnswers] = useState<PublishChallengeAnswers>()
useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers)

let initialState = 'initializing'
// before the accountId and options is defined, nothing can happen
Expand Down Expand Up @@ -299,6 +339,7 @@ export function usePublishCommentEdit(options?: UsePublishCommentEditOptions): U
const [challenge, setChallenge] = useState<Challenge>()
const [challengeVerification, setChallengeVerification] = useState<ChallengeVerification>()
const [publishChallengeAnswers, setPublishChallengeAnswers] = useState<PublishChallengeAnswers>()
useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers)

let initialState = 'initializing'
// before the accountId and options is defined, nothing can happen
Expand Down Expand Up @@ -370,6 +411,7 @@ export function usePublishCommentModeration(options?: UsePublishCommentModeratio
const [challenge, setChallenge] = useState<Challenge>()
const [challengeVerification, setChallengeVerification] = useState<ChallengeVerification>()
const [publishChallengeAnswers, setPublishChallengeAnswers] = useState<PublishChallengeAnswers>()
useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers)

let initialState = 'initializing'
// before the accountId and options is defined, nothing can happen
Expand Down Expand Up @@ -441,6 +483,7 @@ export function usePublishSubplebbitEdit(options?: UsePublishSubplebbitEditOptio
const [challenge, setChallenge] = useState<Challenge>()
const [challengeVerification, setChallengeVerification] = useState<ChallengeVerification>()
const [publishChallengeAnswers, setPublishChallengeAnswers] = useState<PublishChallengeAnswers>()
useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers)

let initialState = 'initializing'
// before the accountId and options is defined, nothing can happen
Expand Down
Loading