import { useQueryClient } from '@tanstack/react-query' import { getAccessToken, useFlag } from 'common' import { useTheme } from 'next-themes' import Link from 'next/link' import { useRouter } from 'next/router' import { PropsWithChildren, useEffect, useState } from 'react' import { tweets } from 'shared-data' import { DocsButton } from '@/components/ui/DocsButton' import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled' import { BASE_PATH, DOCS_URL } from '@/lib/constants' import { auth, buildPathWithParams, getReturnToPath } from '@/lib/gotrue' type SignInLayoutProps = { heading: string subheading: string showDisclaimer?: boolean logoLinkToMarketingSite?: boolean } const SignInLayout = ({ heading, subheading, showDisclaimer = true, logoLinkToMarketingSite = false, children, }: PropsWithChildren) => { const router = useRouter() const queryClient = useQueryClient() const { resolvedTheme } = useTheme() const ongoingIncident = useFlag('ongoingIncident') const { dashboardAuthShowTestimonial: showTestimonial, brandingLargeLogo: largeLogo, dashboardAuthShowTos: showTos, } = useIsFeatureEnabled([ 'dashboard_auth:show_testimonial', 'branding:large_logo', 'dashboard_auth:show_tos', ]) // This useEffect redirects the user to MFA if they're already halfway signed in useEffect(() => { auth .initialize() .then(async ({ error }) => { if (error) { // if there was a problem signing in via the url, don't redirect return } const token = await getAccessToken() if (token) { const { data, error } = await auth.mfa.getAuthenticatorAssuranceLevel() if (error) { // if there was a problem signing in via the url, don't redirect return } if (data) { // we're already where we need to be if (router.pathname === '/sign-in-mfa') { return } if (data.currentLevel !== data.nextLevel) { const redirectTo = buildPathWithParams('/sign-in-mfa') router.replace(redirectTo) return } } await queryClient.resetQueries() router.push(getReturnToPath()) } }) .catch(() => {}) // catch all errors thrown by auth methods }, []) const [quote, setQuote] = useState<{ text: string url: string handle: string img_url: string } | null>(null) useEffect(() => { // Weighted random selection // Calculate total weight (default weight is fallbackWeight for tweets without weight specified) const fallbackWeight = 1 const totalWeight = tweets.reduce((sum, tweet) => sum + (tweet.weight ?? fallbackWeight), 0) // Generate random number between 0 and totalWeight const random = Math.random() * totalWeight // Find the selected tweet based on cumulative weights let accumulatedWeight = 0 for (const tweet of tweets) { const weight = tweet.weight ?? fallbackWeight accumulatedWeight += weight if (random <= accumulatedWeight) { setQuote(tweet) break } } }, []) return ( <>

{heading}

{subheading}

{children}
{showDisclaimer && showTos && (

By continuing, you agree to Briven’s{' '} Terms of Service {' '} and{' '} Privacy Policy , and to receive periodic emails with updates.

)}
) } export default SignInLayout