Working tree is now the canonical repo. Off-chain play-money + trustless on-chain (FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution, mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
35 lines
1.0 KiB
TypeScript
35 lines
1.0 KiB
TypeScript
import { cleanUsername } from 'common/util/clean-username'
|
|
import { useEffect } from 'react'
|
|
|
|
import { User, writeReferralInfo } from 'web/lib/firebase/users'
|
|
import { useDefinedSearchParams } from 'web/hooks/use-defined-search-params'
|
|
|
|
export const useSaveReferral = (
|
|
user: User | null | undefined,
|
|
options?: {
|
|
defaultReferrerUsername?: string
|
|
contractId?: string
|
|
}
|
|
) => {
|
|
const { searchParams } = useDefinedSearchParams()
|
|
|
|
useEffect(() => {
|
|
const referrer = searchParams.get('r')
|
|
? decodeBase64(searchParams.get('r') as string)
|
|
: (searchParams.get('referrer') as string)
|
|
|
|
const referrerOrDefault = referrer || options?.defaultReferrerUsername
|
|
|
|
if (user === null && referrerOrDefault) {
|
|
writeReferralInfo(referrerOrDefault, {
|
|
contractId: options?.contractId,
|
|
explicitReferrer: referrer,
|
|
})
|
|
}
|
|
}, [user, searchParams, JSON.stringify(options)])
|
|
}
|
|
|
|
const decodeBase64 = (base64: string) => {
|
|
return Buffer.from(cleanUsername(base64), 'base64').toString()
|
|
}
|