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.

1.4 KB
Raw Blame
'use client'
import { useAtomValue } from 'jotai'
import { useCallback, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { toast } from '@/app/notifications'
import { isCurrentWorkspaceManagerAtom } from '@/context/workspace-state'
import { consoleClient } from '@/service/console'
import { billingErrorMessageKey } from '../billing-error'

export function useEducationDiscount() {
  const { t } = useTranslation(['billing'])
  const isCurrentWorkspaceManager = useAtomValue(isCurrentWorkspaceManagerAtom)
  const [isEducationDiscountLoading, setIsEducationDiscountLoading] = useState(false)

  const handleEducationDiscount = useCallback(async () => {
    if (isEducationDiscountLoading) return

    if (!isCurrentWorkspaceManager) {
      toast.error(t(($) => $.buyPermissionDeniedTip, { ns: 'billing' }))
      return
    }

    setIsEducationDiscountLoading(true)
    try {
      const { url } = await consoleClient.billing.subscription.get(
        { query: { plan: 'professional', interval: 'year' } },
        { context: { silent: true } },
      )
      window.location.href = url
    } catch (error) {
      toast.error(t(($) => $[billingErrorMessageKey(error)], { ns: 'billing' }))
    } finally {
      setIsEducationDiscountLoading(false)
    }
  }, [isCurrentWorkspaceManager, isEducationDiscountLoading, t])

  return {
    handleEducationDiscount,
    isEducationDiscountLoading,
  }
}