|
1 | | -import {useMemo, useState} from 'react' |
| 1 | +import {useEffect, useMemo, useState} from 'react' |
2 | 2 | import useAccountsStore from '../../stores/accounts' |
3 | 3 | import Logger from '@plebbit/plebbit-logger' |
4 | 4 | const log = Logger('plebbit-react-hooks:actions:hooks') |
@@ -36,6 +36,28 @@ const publishChallengeAnswersNotReady: PublishChallengeAnswers = async (challeng |
36 | 36 | throw Error(`can't call publishChallengeAnswers() before result.challenge is defined (before the challenge message is received)`) |
37 | 37 | } |
38 | 38 |
|
| 39 | +function useIframeChallengeAutoAnswer(challenge: Challenge | undefined, publishChallengeAnswers: PublishChallengeAnswers | undefined) { |
| 40 | + const iframeChallengeUrl = challenge?.challenges?.find((c) => c.type === 'url/iframe')?.challenge |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + if (!iframeChallengeUrl || !publishChallengeAnswers) return |
| 44 | + |
| 45 | + let iframeOrigin: string | undefined |
| 46 | + try { |
| 47 | + iframeOrigin = new URL(iframeChallengeUrl).origin |
| 48 | + } catch (e) {} |
| 49 | + |
| 50 | + const handleMessage = (event: MessageEvent) => { |
| 51 | + if (iframeOrigin && event.origin !== iframeOrigin) return |
| 52 | + if (event.data?.type === 'challengeAnswer' && Array.isArray(event.data.challengeAnswers)) { |
| 53 | + publishChallengeAnswers(event.data.challengeAnswers) |
| 54 | + } |
| 55 | + } |
| 56 | + window.addEventListener('message', handleMessage) |
| 57 | + return () => window.removeEventListener('message', handleMessage) |
| 58 | + }, [iframeChallengeUrl, publishChallengeAnswers]) |
| 59 | +} |
| 60 | + |
39 | 61 | export function useSubscribe(options?: UseSubscribeOptions): UseSubscribeResult { |
40 | 62 | assert(!options || typeof options === 'object', `useSubscribe options argument '${options}' not an object`) |
41 | 63 | const {subplebbitAddress, accountName, onError} = options || {} |
@@ -155,6 +177,7 @@ export function usePublishComment(options?: UsePublishCommentOptions): UsePublis |
155 | 177 | const [challenge, setChallenge] = useState<Challenge>() |
156 | 178 | const [challengeVerification, setChallengeVerification] = useState<ChallengeVerification>() |
157 | 179 | const [publishChallengeAnswers, setPublishChallengeAnswers] = useState<PublishChallengeAnswers>() |
| 180 | + useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers) |
158 | 181 |
|
159 | 182 | let initialState = 'initializing' |
160 | 183 | // before the accountId and options is defined, nothing can happen |
@@ -228,6 +251,7 @@ export function usePublishVote(options?: UsePublishVoteOptions): UsePublishVoteR |
228 | 251 | const [challenge, setChallenge] = useState<Challenge>() |
229 | 252 | const [challengeVerification, setChallengeVerification] = useState<ChallengeVerification>() |
230 | 253 | const [publishChallengeAnswers, setPublishChallengeAnswers] = useState<PublishChallengeAnswers>() |
| 254 | + useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers) |
231 | 255 |
|
232 | 256 | let initialState = 'initializing' |
233 | 257 | // before the accountId and options is defined, nothing can happen |
@@ -299,6 +323,7 @@ export function usePublishCommentEdit(options?: UsePublishCommentEditOptions): U |
299 | 323 | const [challenge, setChallenge] = useState<Challenge>() |
300 | 324 | const [challengeVerification, setChallengeVerification] = useState<ChallengeVerification>() |
301 | 325 | const [publishChallengeAnswers, setPublishChallengeAnswers] = useState<PublishChallengeAnswers>() |
| 326 | + useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers) |
302 | 327 |
|
303 | 328 | let initialState = 'initializing' |
304 | 329 | // before the accountId and options is defined, nothing can happen |
@@ -370,6 +395,7 @@ export function usePublishCommentModeration(options?: UsePublishCommentModeratio |
370 | 395 | const [challenge, setChallenge] = useState<Challenge>() |
371 | 396 | const [challengeVerification, setChallengeVerification] = useState<ChallengeVerification>() |
372 | 397 | const [publishChallengeAnswers, setPublishChallengeAnswers] = useState<PublishChallengeAnswers>() |
| 398 | + useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers) |
373 | 399 |
|
374 | 400 | let initialState = 'initializing' |
375 | 401 | // before the accountId and options is defined, nothing can happen |
@@ -441,6 +467,7 @@ export function usePublishSubplebbitEdit(options?: UsePublishSubplebbitEditOptio |
441 | 467 | const [challenge, setChallenge] = useState<Challenge>() |
442 | 468 | const [challengeVerification, setChallengeVerification] = useState<ChallengeVerification>() |
443 | 469 | const [publishChallengeAnswers, setPublishChallengeAnswers] = useState<PublishChallengeAnswers>() |
| 470 | + useIframeChallengeAutoAnswer(challenge, publishChallengeAnswers) |
444 | 471 |
|
445 | 472 | let initialState = 'initializing' |
446 | 473 | // before the accountId and options is defined, nothing can happen |
|
0 commit comments