diff --git a/web/apps/cast/src/pages/index.tsx b/web/apps/cast/src/pages/index.tsx index 98dabd97e..5cf3f8f3a 100644 --- a/web/apps/cast/src/pages/index.tsx +++ b/web/apps/cast/src/pages/index.tsx @@ -11,6 +11,9 @@ export default function Index() { const [publicKeyB64, setPublicKeyB64] = useState(); const [privateKeyB64, setPrivateKeyB64] = useState(); const [pairingCode, setPairingCode] = useState(); + // TODO: This needs to change, since there is an interim period when the + // code becomes invalid. + const [haveInitializedCast, setHaveInitializedCast] = useState(false); const router = useRouter(); @@ -27,10 +30,11 @@ export default function Index() { }; useEffect(() => { - if (pairingCode) { - castReceiverLoadingIfNeeded().then((cast) => - advertiseCode(cast, () => pairingCode), - ); + if (pairingCode && !haveInitializedCast) { + castReceiverLoadingIfNeeded().then((cast) => { + setHaveInitializedCast(true); + advertiseCode(cast, () => pairingCode); + }); } }, [pairingCode]); @@ -54,10 +58,11 @@ export default function Index() { storeCastData(data); await router.push("/slideshow"); } catch (e) { + // Code has become invalid log.error("Failed to get cast data", e); // Start again from the beginning. - // setPairingCode(undefined); - // init(); + setPairingCode(undefined); + init(); } };