mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-03-25 10:02:27 -04:00
34 lines
1023 B
TypeScript
34 lines
1023 B
TypeScript
import {cleanUsername} from 'common/util/clean-username'
|
|
import {useEffect} from 'react'
|
|
import {useDefinedSearchParams} from 'web/hooks/use-defined-search-params'
|
|
import {User, writeReferralInfo} from 'web/lib/firebase/users'
|
|
|
|
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()
|
|
}
|