import { FeatureFlagContext } from 'common'
import { useRouter } from 'next/router'
import { ReactNode, useContext, useEffect, useRef, useState } from 'react'
import { Button, Card, CardContent } from 'ui'
import { Admonition, ShimmeringLoader } from 'ui-patterns'
import { OrganizationSelector } from '../Connect/OrganizationSelector'
import { CreditCodeRedemption } from '@/components/interfaces/Organization/BillingSettings/CreditCodeRedemption'
import {
InterstitialAccountRow,
InterstitialLayout,
BrivenLogo,
} from '@/components/layouts/InterstitialLayout'
import { useOrganizationsQuery } from '@/data/organizations/organizations-query'
import { useProfile } from '@/lib/profile'
import { EMPTY_ARR } from '@/lib/void'
const RETURN_TO_SELECTED_ORG_PARAM = 'selected_org'
const RedeemCreditsInterstitial = ({
title,
description,
children,
}: {
title: ReactNode
description?: ReactNode
children: ReactNode
}) => (
} title={title} description={description}>
{children}
)
export const RedeemCreditsScreen = () => {
const router = useRouter()
const { profile, isLoading: isLoadingProfile } = useProfile()
const { hasLoaded } = useContext(FeatureFlagContext)
const [selectedOrgSlug, setSelectedOrgSlug] = useState(null)
const [redemptionModalOrgSlug, setRedemptionModalOrgSlug] = useState(null)
const appliedReturnSelectedOrgRef = useRef(null)
const {
data: organizationOptions = EMPTY_ARR,
error: organizationsError,
isLoading: isLoadingOrganizations,
isError: isOrganizationsError,
} = useOrganizationsQuery()
const returnSelectedOrgSlug =
router.isReady && typeof router.query[RETURN_TO_SELECTED_ORG_PARAM] === 'string'
? router.query[RETURN_TO_SELECTED_ORG_PARAM]
: null
const displayName = profile?.primary_email ?? profile?.username
const isLoading = isLoadingProfile || isLoadingOrganizations || !hasLoaded
useEffect(() => {
if (!returnSelectedOrgSlug) return
if (appliedReturnSelectedOrgRef.current === returnSelectedOrgSlug) return
const hasReturnedOrganization = (organizationOptions ?? []).some(
(organization) => organization.slug === returnSelectedOrgSlug
)
if (hasReturnedOrganization) {
setSelectedOrgSlug(returnSelectedOrgSlug)
appliedReturnSelectedOrgRef.current = returnSelectedOrgSlug
}
}, [organizationOptions, returnSelectedOrgSlug])
if (isLoading) {
return (
}
description={}
>
)
}
if (isOrganizationsError) {
return (
We could not load your organizations.
{organizationsError && (
Error: {organizationsError.message}
)}
>
}
/>
)
}
const createOrganizationParams = {
returnTo: router.asPath || '/redeem',
returnToOrgParam: RETURN_TO_SELECTED_ORG_PARAM,
}
const openRedemption = () => {
if (!selectedOrgSlug) return
setRedemptionModalOrgSlug(selectedOrgSlug)
}
return (
<>
`${organization.plan.name} Plan`}
createLabel={
organizationOptions.length === 0
? 'Create your first organization'
: 'Create new organization'
}
createHrefParams={createOrganizationParams}
/>
{organizationOptions.length === 0 && (
)}
Credits apply to one organization and are used toward future invoices before your
payment method is charged.
{redemptionModalOrgSlug && (
setRedemptionModalOrgSlug(null)}
/>
)}
>
)
}
const ConnectLoadingCards = () => (
{Array.from({ length: 3 }).map((_, index) => (
))}
)