import { LOCAL_STORAGE_KEYS } from 'common' import { useCallback, useEffect, useState } from 'react' import { VERCEL_MARKETPLACE_SURVEY_START } from './OnboardingSurvey.constants' import { getFollowUpRemainingMs, parseFollowUpState, type OnboardingSurveyFollowUpStatus, } from './OnboardingSurveyFollowUp.utils' import { useOrganizationQuery } from '@/data/organizations/organization-query' import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage' import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization' import { MANAGED_BY } from '@/lib/constants/infrastructure' import { useProfile } from '@/lib/profile' import type { Organization } from '@/types' const UNKNOWN_STORAGE_KEY = 'supabase-onboarding-survey-follow-up-unknown' /** * Whether the selected org's creator should be asked the onboarding survey * again: they left it blank on the new-org form in this browser, haven't * answered or skipped since, and the org is less than five minutes old. * `hasExpired` turns true once those five minutes pass with no answer or skip. * * Vercel Marketplace orgs never pass through the new-org form, so their owners * are asked here instead: once per browser, with no time limit, for orgs * created since VERCEL_MARKETPLACE_SURVEY_START. */ export function useOnboardingSurveyFollowUp() { const { profile } = useProfile() const { data: organization } = useSelectedOrganizationQuery() const profileId = profile?.gotrue_id const orgSlug = organization?.slug const storageKey = profileId && orgSlug ? LOCAL_STORAGE_KEYS.ONBOARDING_SURVEY_FOLLOW_UP(profileId, orgSlug) : UNKNOWN_STORAGE_KEY const [storedState, setStoredState, { isSuccess: isStoredStateLoaded }] = useLocalStorageQuery(storageKey, null) const followUpState = parseFollowUpState(storedState) // An answer or skip also counts in memory: storage can refuse the write (blocked or full) const [resolvedOrgSlug, setResolvedOrgSlug] = useState() const isUnasked = isStoredStateLoaded && followUpState === null && orgSlug !== resolvedOrgSlug const newVercelOrgCreatedAt = useNewVercelOrgCreatedAt(organization, { enabled: !!profileId && isUnasked, }) const pendingOrgCreatedAt = followUpState?.status === 'pending' ? followUpState.orgCreatedAt : undefined // The window is time-based: it's checked against the clock on render, and a // timer marks it expired (and re-renders) when it closes. const [expiredOrgCreatedAt, setExpiredOrgCreatedAt] = useState() const hasExpired = !!pendingOrgCreatedAt && (expiredOrgCreatedAt === pendingOrgCreatedAt || getFollowUpRemainingMs(pendingOrgCreatedAt, Date.now()) <= 0) const isEligible = (!!pendingOrgCreatedAt && !hasExpired) || !!newVercelOrgCreatedAt useEffect(() => { if (!pendingOrgCreatedAt) return const timeout = setTimeout( () => setExpiredOrgCreatedAt(pendingOrgCreatedAt), getFollowUpRemainingMs(pendingOrgCreatedAt, Date.now()) ) return () => clearTimeout(timeout) }, [pendingOrgCreatedAt]) const resolveFollowUp = useCallback( (status: Exclude) => { setResolvedOrgSlug(orgSlug) setStoredState((previous: unknown) => { const previousState = parseFollowUpState(previous) if (previousState) return { ...previousState, status } return newVercelOrgCreatedAt ? { status, orgCreatedAt: newVercelOrgCreatedAt } : previous }) }, [newVercelOrgCreatedAt, orgSlug, setStoredState] ) // Undoes a dismissal whose save failed, keeping the original org details and // creation time, so the next page load retries it const reopenFollowUp = useCallback(() => { setStoredState((previous: unknown) => { const previousState = parseFollowUpState(previous) return previousState?.status === 'dismissed' ? { ...previousState, status: 'pending' } : previous }) }, [setStoredState]) return { isEligible, hasExpired, orgSlug, orgKind: followUpState?.kind, orgSize: followUpState?.size, resolveFollowUp, reopenFollowUp, } } function useNewVercelOrgCreatedAt( organization: Organization | undefined, { enabled }: { enabled: boolean } ) { const isOwnedVercelOrg = organization?.managed_by === MANAGED_BY.VERCEL_MARKETPLACE && organization.is_owner const shouldCheck = enabled && isOwnedVercelOrg const { data: createdAt } = useOrganizationQuery( { slug: organization?.slug }, { enabled: shouldCheck, select: (org) => org.created_at } ) // A disabled query still returns cached data if (!shouldCheck || !createdAt) return undefined const isSinceSurveyStart = Date.parse(createdAt) >= Date.parse(VERCEL_MARKETPLACE_SURVEY_START) return isSinceSurveyStart ? createdAt : undefined }