Skip to content

Commit

Permalink
iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
eatski committed Aug 12, 2024
1 parent e7bb876 commit 8f01fb2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/app/verification/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default async function Verification() {}
4 changes: 4 additions & 0 deletions src/components/play/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Script from "next/script";
import { useCallback, useState } from "react";
import { z } from "zod";
import { useConfirmModal } from "../confirmModal";
import { useVerificationIframe } from "../verification/VerificationIframe";
import { AnswerForm } from "./components/answerForm";
import { AnswerResult } from "./components/answerResult";
import { Feed } from "./components/feed";
Expand Down Expand Up @@ -153,14 +154,17 @@ export function Play({
queryFn: () => fetchCanPlay(),
});
const { confirm, view } = useConfirmModal();
const { iframe } = useVerificationIframe();
if (canPlayResult && !canPlayResult.canPlay) {
switch (canPlayResult.reason) {
case "desktop_only":
return <MobileLimitation />;
}
}

return (
<>
{iframe}
<Script
strategy="lazyOnload"
src={`https://www.google.com/recaptcha/api.js?render=${CLIENT_KEY}`}
Expand Down
26 changes: 26 additions & 0 deletions src/components/verification/VerificationIframe.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useEffect, useState } from "react";
import { VERIFICATION_PARAMETER_NAME } from "./constants";

const getNow = () => new Date().getTime().toString();

export const useVerificationIframe = () => {
const [verificationId, setVerificationId] = useState(getNow);
useEffect(() => {
const id = setInterval(
() => {
const now = getNow();
setVerificationId(now);
},
1000 * 60 * 5,
);
}, []);

return {
iframe: (
<iframe
style={{ display: "none" }}
src={`/verification?${VERIFICATION_PARAMETER_NAME}=${verificationId}`}
/>
),
};
};
20 changes: 20 additions & 0 deletions src/components/verification/VerificationIframeContent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use client";

import { useEffect } from "react";
import {
VERIFICATION_PARAMETER_NAME,
createVerificationDonePostMessage,
} from "./constants";

export const VerificationIframeContent = () => {
useEffect(() => {
const verificationId = new URLSearchParams(window.location.search).get(
VERIFICATION_PARAMETER_NAME,
);
verificationId &&
window.top?.postMessage(
createVerificationDonePostMessage(verificationId),
"*",
);
}, []);
};
4 changes: 4 additions & 0 deletions src/components/verification/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const VERIFICATION_PARAMETER_NAME = "verificationId";
export const createVerificationDonePostMessage = (messageId: string) => {
return `VerificationDone-${messageId}`;
};

0 comments on commit 8f01fb2

Please sign in to comment.