Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

13.7 KB
Raw Blame
'use client'

import type { AgentAppDetailWithSite } from '@dify/contracts/api/console/agent/types.gen'
import type { AppSiteUpdatePayload } from '@dify/contracts/api/console/apps/types.gen'
import type { SettingsAppInfo } from '@/app/components/app/overview/settings'
import type { AppIconType } from '@/types/app'
import { Button } from '@langgenius/dify-ui/button'
import { Dialog, DialogTrigger } from '@langgenius/dify-ui/dialog'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import dynamic from 'next/dynamic'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import {
  WebAppAccessControlEntry,
  WebAppAccessControlEntrySkeleton,
} from '@/app/components/app/access-point/shared/web-app-access-control'
import { CustomizeDialog } from '@/app/components/app/overview/customize'
import { EmbeddedDialogContent } from '@/app/components/app/overview/embedded'
import { SettingsDialog } from '@/app/components/app/overview/settings'
import { AccessPointCard } from '@/app/components/base/access-point/card'
import { AccessPointUrl } from '@/app/components/base/access-point/url'
import AppIcon from '@/app/components/base/app-icon'
import { toast } from '@/app/notifications'
import { getAgentACLCapabilities } from '@/features/agent-v2/acl'
import { consoleQuery } from '@/service/console'
import { getErrorMessage } from '@/utils/error-message'
import { getAgentWebAppUrl } from '../../web-app-access'
import { useWebAppAccessControl } from './use-web-app-access-control'

const AccessControl = dynamic(() => import('@/app/components/app/app-access-control'), {
  ssr: false,
})

export function WebAppAccessCard({
  agent,
  agentId,
  isLoading,
}: {
  agent?: AgentAppDetailWithSite
  agentId: string
  isLoading: boolean
}) {
  const { t } = useTranslation(['agentV2'])
  const { t: tCommon } = useTranslation(['common'])
  const { t: tApp } = useTranslation(['app'])
  const queryClient = useQueryClient()
  const appId = agent?.app_id
  const apiBaseUrl = agent?.api_base_url
  const site = agent?.site
  const accessToken = site?.access_token ?? site?.code
  const appBaseUrl =
    site?.app_base_url || (typeof window === 'undefined' ? '' : window.location.origin)
  const webAppUrl = getAgentWebAppUrl(agent)
  const accessReady = Boolean(agent?.access_ready)
  const { canManageAccessPoint } = getAgentACLCapabilities(agent?.permission_keys)
  const canManageWebApp = canManageAccessPoint && Boolean(appId && accessReady)
  const embeddedConfig =
    appId && accessToken
      ? {
          accessToken,
          appBaseUrl,
          siteInfo: {
            title: site?.title ?? agent?.name ?? '',
            chat_color_theme: site?.chat_color_theme ?? null,
            chat_color_theme_inverted: site?.chat_color_theme_inverted ?? false,
          },
        }
      : null
  const settingsAppInfo = agent ? createSettingsAppInfo(agent) : null
  const customizeConfig =
    appId && apiBaseUrl
      ? {
          apiBaseUrl,
          appId,
        }
      : null
  const [showAccessControl, setShowAccessControl] = useState(false)
  const accessControl = useWebAppAccessControl(agent, isLoading)
  const agentDetailQueryKey = consoleQuery.agent.byAgentId.get.queryKey({
    input: { params: { agent_id: agentId } },
  })
  const toggleSiteMutation = useMutation(
    consoleQuery.apps.byAppId.siteEnable.post.mutationOptions({
      context: { silent: true },
      scope: {
        id: `agent-web-app-toggle:${agentId}`,
      },
      onSuccess: (updatedApp) => {
        queryClient.setQueryData<AgentAppDetailWithSite | undefined>(
          agentDetailQueryKey,
          (agentDetail) =>
            agentDetail
              ? {
                  ...agentDetail,
                  enable_site: updatedApp.enable_site,
                  updated_at: updatedApp.updated_at,
                  updated_by: updatedApp.updated_by,
                }
              : agentDetail,
        )
      },
      onError: (error) => {
        toast.error(getErrorMessage(error) ?? tCommon(($) => $['actionMsg.modifiedUnsuccessfully']))
      },
    }),
  )
  const resetAccessTokenMutation = useMutation(
    consoleQuery.apps.byAppId.site.accessTokenReset.post.mutationOptions({
      context: { silent: true },
      onSuccess: (site) => {
        queryClient.setQueryData<AgentAppDetailWithSite | undefined>(
          agentDetailQueryKey,
          (agentDetail) => {
            if (!agentDetail || !agentDetail.site) return agentDetail

            return {
              ...agentDetail,
              site: {
                ...agentDetail.site,
                code: site.code ?? agentDetail.site.code,
                access_token: site.code ?? agentDetail.site.access_token,
              },
            }
          },
        )
      },
      onError: (error) => {
        toast.error(
          getErrorMessage(error) ?? tCommon(($) => $['actionMsg.generatedUnsuccessfully']),
        )
      },
    }),
  )
  const updateSiteMutation = useMutation(
    consoleQuery.apps.byAppId.site.post.mutationOptions({ context: { silent: true } }),
  )
  const pendingEnabled = toggleSiteMutation.variables?.body.enable_site
  const optimisticEnabled =
    toggleSiteMutation.isPending && pendingEnabled !== undefined
      ? pendingEnabled
      : Boolean(agent?.enable_site)
  const canUseIntegrationActions =
    canManageWebApp && optimisticEnabled && !toggleSiteMutation.isPending
  const status = isLoading ? 'loading' : optimisticEnabled ? 'inService' : 'disabled'
  const statusLabel = isLoading
    ? tCommon(($) => $.loading)
    : t(
        ($) =>
          $[
            optimisticEnabled
              ? 'agentDetail.access.status.inService'
              : 'agentDetail.access.status.outOfService'
          ],
      )
  const icon = agent ? getSettingsIcon(agent) : null
  const notAvailableLabel = t(($) => $['agentDetail.access.workflow.notAvailable'])
  const openUrl =
    accessReady &&
    webAppUrl &&
    agent?.enable_site &&
    !toggleSiteMutation.isPending &&
    !accessControl.noAccessPermission
      ? webAppUrl
      : undefined
  const publishRequiredMessage = t(($) => $['agentDetail.access.publishRequired'])
  const showPublishRequiredMessage = !isLoading && !accessReady

  function handleEnabledChange(enabled: boolean) {
    if (!appId || !canManageWebApp) return

    toggleSiteMutation.mutate({
      params: {
        app_id: appId,
      },
      body: {
        enable_site: enabled,
      },
    })
  }

  function handleRefreshUrl() {
    if (!appId || !canManageWebApp) return

    resetAccessTokenMutation.mutate({
      params: {
        app_id: appId,
      },
    })
  }

  async function handleSaveSettings(params: AppSiteUpdatePayload): Promise<boolean> {
    if (!appId || !canManageWebApp) return false

    const sitePayload = params satisfies AppSiteUpdatePayload

    try {
      await updateSiteMutation.mutateAsync({
        params: {
          app_id: appId,
        },
        body: sitePayload,
      })
      void queryClient.invalidateQueries({ queryKey: agentDetailQueryKey })
      toast.success(tCommon(($) => $['actionMsg.modifiedSuccessfully']))
      return true
    } catch (error) {
      toast.error(getErrorMessage(error) ?? tCommon(($) => $['actionMsg.modifiedUnsuccessfully']))
      return false
    }
  }

  return (
    <>
      <AccessPointCard
        className="min-h-55.5"
        headingLevel={3}
        title={t(($) => $['agentDetail.access.webApp.title'])}
        description={t(($) => $['agentDetail.access.webApp.description'])}
        icon={icon ? <AppIcon size="large" icon={icon} /> : 'i-ri-window-line'}
        status={status}
        statusLabel={statusLabel}
        switchDisabled={isLoading || !canManageWebApp}
        switchDisabledReason={showPublishRequiredMessage ? publishRequiredMessage : undefined}
        switchLabel={t(($) => $['agentDetail.access.toggleSurface'], {
          name: t(($) => $['agentDetail.access.webApp.title']),
        })}
        onEnabledChange={handleEnabledChange}
        actions={
          <>
            <Dialog>
              <DialogTrigger
                disabled={!canUseIntegrationActions || !embeddedConfig}
                render={<Button variant="secondary" className="px-3" />}
              >
                <span aria-hidden className="i-ri-window-line size-4" />
                {t(($) => $['agentDetail.access.webApp.actions.embedIntoSite'])}
              </DialogTrigger>
              {embeddedConfig && (
                <EmbeddedDialogContent
                  appBaseUrl={embeddedConfig.appBaseUrl}
                  accessToken={embeddedConfig.accessToken}
                  siteInfo={embeddedConfig.siteInfo}
                  webAppRoute="agent"
                />
              )}
            </Dialog>
            {canManageWebApp && customizeConfig ? (
              <CustomizeDialog
                appId={customizeConfig.appId}
                api_base_url={customizeConfig.apiBaseUrl}
                sourceCodeRepository="webapp-conversation"
                disabled={!canUseIntegrationActions}
                triggerLabel={t(($) => $['agentDetail.access.webApp.actions.customFrontend'])}
              />
            ) : (
              <Button variant="secondary" disabled className="flex items-center gap-1 px-3">
                <span aria-hidden className="i-custom-vender-deploy-code-block size-4" />
                {t(($) => $['agentDetail.access.webApp.actions.customFrontend'])}
              </Button>
            )}
            <SettingsDialog
              key={appId}
              isChat
              appInfo={canManageWebApp && settingsAppInfo ? settingsAppInfo : undefined}
              onSave={handleSaveSettings}
              triggerLabel={t(($) => $['agentDetail.access.webApp.actions.settings'])}
            />
          </>
        }
      >
        <AccessPointUrl
          label={t(($) => $['agentDetail.access.webApp.accessUrl'])}
          value={webAppUrl || notAvailableLabel}
          enabled={optimisticEnabled}
          copyDisabled={!webAppUrl}
          loading={isLoading}
          unavailableLabel={notAvailableLabel}
          showOpen
          showQrCode
          showRegenerate
          openDisabledReason={
            showPublishRequiredMessage
              ? publishRequiredMessage
              : accessControl.noAccessPermission
                ? tApp(($) => $.noAccessPermission)
                : undefined
          }
          openLabel={t(($) => $['agentDetail.access.webApp.actions.open'])}
          openUrl={openUrl}
          qrCodeLabel={t(($) => $['agentDetail.access.webApp.showQrCode'])}
          qrCodeScanLabel={t(($) => $['agentDetail.access.webApp.qrCode.scanToShare'])}
          qrCodeDownloadLabel={t(($) => $['agentDetail.access.webApp.qrCode.download'])}
          regenerateLabel={t(($) => $['agentDetail.access.webApp.refreshUrl'])}
          regenerateDisabled={!canManageWebApp}
          regenerating={resetAccessTokenMutation.isPending}
          onRegenerate={handleRefreshUrl}
          copyLabel={t(($) => $['agentDetail.access.copyAccessUrl'])}
          copiedLabel={tCommon(($) => $['operation.copied'])}
          onCopyError={() => {
            toast.error(t(($) => $['agentDetail.access.copyFailed']))
          }}
        />
        {accessControl.state === 'loading' && <WebAppAccessControlEntrySkeleton loading />}
        {accessControl.state === 'ready' && (
          <WebAppAccessControlEntry
            {...accessControl.entryProps}
            onClick={() => setShowAccessControl(true)}
          />
        )}
      </AccessPointCard>

      {canManageAccessPoint && showAccessControl && accessControl.state === 'ready' && (
        <AccessControl
          app={accessControl.app}
          onClose={() => setShowAccessControl(false)}
          onConfirm={async () => {
            await accessControl.refetchUserCanAccessApp()
            setShowAccessControl(false)
          }}
        />
      )}
    </>
  )
}

function createSettingsAppInfo(agent: AgentAppDetailWithSite): SettingsAppInfo | null {
  const site = agent.site
  const appId = agent.app_id
  if (!site || !appId) return null
  const icon = getSettingsIcon(agent)

  return {
    id: appId,
    mode: 'chat' as const,
    site: {
      title: site.title ?? agent.name,
      description: site.description ?? '',
      default_language: (site.default_language ??
        'en-US') as SettingsAppInfo['site']['default_language'],
      chat_color_theme: site.chat_color_theme ?? '',
      chat_color_theme_inverted: site.chat_color_theme_inverted ?? false,
      copyright: site.copyright ?? '',
      privacy_policy: site.privacy_policy ?? '',
      custom_disclaimer: site.custom_disclaimer ?? '',
      input_placeholder: site.input_placeholder ?? '',
      icon_type: icon.icon_type,
      icon: icon.icon,
      icon_background: icon.icon_background,
      icon_url: icon.icon_url,
      show_workflow_steps: site.show_workflow_steps ?? false,
      use_icon_as_answer_icon: site.use_icon_as_answer_icon ?? false,
    },
  }
}

function isAppIconType(iconType: unknown): iconType is AppIconType {
  return iconType === 'image' || iconType === 'emoji' || iconType === 'link'
}

function getSettingsIcon(agent: AgentAppDetailWithSite) {
  const site = agent.site
  if (site && isAppIconType(site.icon_type) && site.icon) {
    return {
      icon_type: site.icon_type,
      icon: site.icon,
      icon_background: site.icon_background ?? null,
      icon_url: site.icon_url ?? null,
    }
  }

  if (isAppIconType(agent.icon_type) && agent.icon) {
    return {
      icon_type: agent.icon_type,
      icon: agent.icon,
      icon_background: agent.icon_background ?? null,
      icon_url: agent.icon_type === 'image' || agent.icon_type === 'link' ? agent.icon_url : null,
    }
  }

  return {
    icon_type: 'emoji' as const,
    icon: '',
    icon_background: null,
    icon_url: null,
  }
}