Feat integrate partner stack (#28353)

Co-authored-by: Joel <iamjoel007@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
This commit is contained in:
hj24
2025-11-20 15:58:05 +08:00
committed by GitHub
parent 1e4e963d8c
commit 2431ddfde6
10 changed files with 665 additions and 3 deletions

View File

@@ -0,0 +1,70 @@
import { PARTNER_STACK_CONFIG } from '@/config'
import { useBindPartnerStackInfo } from '@/service/use-billing'
import { useBoolean } from 'ahooks'
import Cookies from 'js-cookie'
import { useSearchParams } from 'next/navigation'
import { useCallback } from 'react'
const usePSInfo = () => {
const searchParams = useSearchParams()
const psInfoInCookie = (() => {
try {
return JSON.parse(Cookies.get(PARTNER_STACK_CONFIG.cookieName) || '{}')
}
catch (e) {
console.error('Failed to parse partner stack info from cookie:', e)
return {}
}
})()
const psPartnerKey = searchParams.get('ps_partner_key') || psInfoInCookie?.partnerKey
const psClickId = searchParams.get('ps_xid') || psInfoInCookie?.clickId
const isPSChanged = psInfoInCookie?.partnerKey !== psPartnerKey || psInfoInCookie?.clickId !== psClickId
const [hasBind, {
setTrue: setBind,
}] = useBoolean(false)
const { mutateAsync } = useBindPartnerStackInfo()
// Save to top domain. cloud.dify.ai => .dify.ai
const domain = globalThis.location.hostname.replace('cloud', '')
const saveOrUpdate = useCallback(() => {
if(!psPartnerKey || !psClickId)
return
if(!isPSChanged)
return
Cookies.set(PARTNER_STACK_CONFIG.cookieName, JSON.stringify({
partnerKey: psPartnerKey,
clickId: psClickId,
}), {
expires: PARTNER_STACK_CONFIG.saveCookieDays,
path: '/',
domain,
})
}, [psPartnerKey, psClickId, isPSChanged])
const bind = useCallback(async () => {
if (psPartnerKey && psClickId && !hasBind) {
let shouldRemoveCookie = false
try {
await mutateAsync({
partnerKey: psPartnerKey,
clickId: psClickId,
})
shouldRemoveCookie = true
}
catch (error: unknown) {
if((error as { status: number })?.status === 400)
shouldRemoveCookie = true
}
if (shouldRemoveCookie)
Cookies.remove(PARTNER_STACK_CONFIG.cookieName, { path: '/', domain })
setBind()
}
}, [psPartnerKey, psClickId, mutateAsync, hasBind, setBind])
return {
psPartnerKey,
psClickId,
saveOrUpdate,
bind,
}
}
export default usePSInfo