dify/web/app/components/base/zendesk/utils.ts
yyh 66a545fc6d
refactor: centralize deployment edition in system features (#39454)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: GareArc <garethcxy@dify.ai>
2026-07-24 07:15:02 +00:00

56 lines
1.4 KiB
TypeScript

import type { DeploymentEdition } from '@dify/contracts/api/console/system-features/types.gen'
type ConversationField = {
id: string
value: unknown
}
declare global {
// oxlint-disable-next-line typescript/consistent-type-definitions
interface Window {
zE?: (
command: string,
value: string,
payload?: ConversationField[] | string | string[] | (() => unknown),
callback?: () => unknown,
) => void
}
}
export const setZendeskConversationFields = (
fields: ConversationField[],
deploymentEdition: DeploymentEdition,
callback?: () => unknown,
) => {
if (deploymentEdition === 'CLOUD' && window.zE)
window.zE('messenger:set', 'conversationFields', fields, callback)
}
type OpenZendeskWindowOptions = {
interval?: number
retries?: number
}
const openZendeskWindowOnce = (deploymentEdition: DeploymentEdition) => {
if (deploymentEdition !== 'CLOUD' || !window.zE) return false
window.zE('messenger', 'show')
window.zE('messenger', 'open')
return true
}
export const openZendeskWindow = (
deploymentEdition: DeploymentEdition,
{ interval = 100, retries = 20 }: OpenZendeskWindowOptions = {},
) => {
if (deploymentEdition !== 'CLOUD') return
if (openZendeskWindowOnce(deploymentEdition)) return
let attempts = 0
const timer = window.setInterval(() => {
attempts += 1
if (openZendeskWindowOnce(deploymentEdition) || attempts >= retries) window.clearInterval(timer)
}, interval)
}