@@ -18,7 +18,7 @@ var __rest = (this && this.__rest) || function (s, e) {
1818 }
1919 return t ;
2020} ;
21- import { useMemo , useState } from 'react' ;
21+ import { useEffect , useMemo , useState } from 'react' ;
2222import useAccountsStore from '../../stores/accounts' ;
2323import Logger from '@plebbit/plebbit-logger' ;
2424const log = Logger ( 'plebbit-react-hooks:actions:hooks' ) ;
@@ -27,6 +27,42 @@ import { useAccount, useAccountId } from '../accounts';
2727const publishChallengeAnswersNotReady = ( challengeAnswers ) => __awaiter ( void 0 , void 0 , void 0 , function * ( ) {
2828 throw Error ( `can't call publishChallengeAnswers() before result.challenge is defined (before the challenge message is received)` ) ;
2929} ) ;
30+ function useIframeChallengeAutoAnswer ( challenge , publishChallengeAnswers ) {
31+ const challenges = challenge === null || challenge === void 0 ? void 0 : challenge . challenges ;
32+ const allIframe = ( challenges === null || challenges === void 0 ? void 0 : challenges . length ) ? challenges . every ( ( c ) => c . type === 'url/iframe' ) : false ;
33+ useEffect ( ( ) => {
34+ if ( ! allIframe || ! challenges || ! publishChallengeAnswers )
35+ return ;
36+ const origins = challenges . map ( ( c ) => {
37+ try {
38+ return new URL ( c . challenge ) . origin ;
39+ }
40+ catch ( e ) {
41+ return undefined ;
42+ }
43+ } ) ;
44+ const answers = new Array ( challenges . length ) . fill ( undefined ) ;
45+ let submitted = false ;
46+ const handleMessage = ( event ) => {
47+ var _a ;
48+ if ( submitted )
49+ return ;
50+ if ( ( ( _a = event . data ) === null || _a === void 0 ? void 0 : _a . type ) !== 'challengeanswer' || typeof event . data . challengeAnswer !== 'string' )
51+ return ;
52+ // find which iframe sent this by matching origin to first unfilled slot
53+ const index = origins . findIndex ( ( origin , i ) => answers [ i ] === undefined && ( ! origin || origin === event . origin ) ) ;
54+ if ( index === - 1 )
55+ return ;
56+ answers [ index ] = event . data . challengeAnswer ;
57+ if ( answers . every ( ( a ) => a !== undefined ) ) {
58+ submitted = true ;
59+ publishChallengeAnswers ( answers ) ;
60+ }
61+ } ;
62+ window . addEventListener ( 'message' , handleMessage ) ;
63+ return ( ) => window . removeEventListener ( 'message' , handleMessage ) ;
64+ } , [ allIframe , challenges , publishChallengeAnswers ] ) ;
65+ }
3066export function useSubscribe ( options ) {
3167 var _a ;
3268 assert ( ! options || typeof options === 'object' , `useSubscribe options argument '${ options } ' not an object` ) ;
@@ -137,6 +173,7 @@ export function usePublishComment(options) {
137173 const [ challenge , setChallenge ] = useState ( ) ;
138174 const [ challengeVerification , setChallengeVerification ] = useState ( ) ;
139175 const [ publishChallengeAnswers , setPublishChallengeAnswers ] = useState ( ) ;
176+ useIframeChallengeAutoAnswer ( challenge , publishChallengeAnswers ) ;
140177 let initialState = 'initializing' ;
141178 // before the accountId and options is defined, nothing can happen
142179 if ( accountId && options ) {
@@ -201,6 +238,7 @@ export function usePublishVote(options) {
201238 const [ challenge , setChallenge ] = useState ( ) ;
202239 const [ challengeVerification , setChallengeVerification ] = useState ( ) ;
203240 const [ publishChallengeAnswers , setPublishChallengeAnswers ] = useState ( ) ;
241+ useIframeChallengeAutoAnswer ( challenge , publishChallengeAnswers ) ;
204242 let initialState = 'initializing' ;
205243 // before the accountId and options is defined, nothing can happen
206244 if ( accountId && options ) {
@@ -263,6 +301,7 @@ export function usePublishCommentEdit(options) {
263301 const [ challenge , setChallenge ] = useState ( ) ;
264302 const [ challengeVerification , setChallengeVerification ] = useState ( ) ;
265303 const [ publishChallengeAnswers , setPublishChallengeAnswers ] = useState ( ) ;
304+ useIframeChallengeAutoAnswer ( challenge , publishChallengeAnswers ) ;
266305 let initialState = 'initializing' ;
267306 // before the accountId and options is defined, nothing can happen
268307 if ( accountId && options ) {
@@ -325,6 +364,7 @@ export function usePublishCommentModeration(options) {
325364 const [ challenge , setChallenge ] = useState ( ) ;
326365 const [ challengeVerification , setChallengeVerification ] = useState ( ) ;
327366 const [ publishChallengeAnswers , setPublishChallengeAnswers ] = useState ( ) ;
367+ useIframeChallengeAutoAnswer ( challenge , publishChallengeAnswers ) ;
328368 let initialState = 'initializing' ;
329369 // before the accountId and options is defined, nothing can happen
330370 if ( accountId && options ) {
@@ -387,6 +427,7 @@ export function usePublishSubplebbitEdit(options) {
387427 const [ challenge , setChallenge ] = useState ( ) ;
388428 const [ challengeVerification , setChallengeVerification ] = useState ( ) ;
389429 const [ publishChallengeAnswers , setPublishChallengeAnswers ] = useState ( ) ;
430+ useIframeChallengeAutoAnswer ( challenge , publishChallengeAnswers ) ;
390431 let initialState = 'initializing' ;
391432 // before the accountId and options is defined, nothing can happen
392433 if ( accountId && subplebbitAddress ) {
0 commit comments