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.

5.7 KB
Raw Blame
'use client'
import type { MeterTone } from '@langgenius/dify-ui/meter'
import type { FC, ReactNode } from 'react'
import type { BillingQuota } from '../quota'
import { cn } from '@langgenius/dify-ui/cn'
import { Infotip, InfotipContent, InfotipTrigger } from '@langgenius/dify-ui/infotip'
import { Meter, MeterIndicator, MeterTrack } from '@langgenius/dify-ui/meter'
import * as React from 'react'
import { useTranslation } from 'react-i18next'

type Props = Readonly<{
  className?: string
  iconClassName: string
  name: string
  tooltip?: string
  quota: BillingQuota
  unit?: string
  unitPosition?: 'inline' | 'suffix'
  resetHint?: string
  hideIcon?: boolean
  // Props for the 50MB threshold display logic
  storageMode?: boolean
  storageThreshold?: number
  storageTooltip?: string
  isSandboxPlan?: boolean
  usageUnknown?: boolean
}>

const UsageInfo: FC<Props> = ({
  className,
  iconClassName,
  name,
  tooltip,
  quota,
  unit,
  unitPosition = 'suffix',
  resetHint,
  hideIcon = false,
  storageMode = false,
  storageThreshold = 50,
  storageTooltip,
  isSandboxPlan = false,
  usageUnknown = false,
}) => {
  const labelId = React.useId()

  const { t } = useTranslation(['billing'])
  const { usage, limit, resetInDays } = quota

  const isBelowThreshold = !usageUnknown && storageMode && usage < storageThreshold
  const isSandboxFull = !usageUnknown && storageMode && isSandboxPlan && usage >= storageThreshold
  const storageHelp = !usageUnknown && storageMode ? storageTooltip : undefined

  // Zero count quotas have no remaining capacity; storage keeps its separate limit convention.
  const isZeroQuota = !storageMode && limit === 0
  const rawPercent = limit !== 'unlimited' && limit > 0 ? (usage / limit) * 100 : 0
  const effectivePercent = isSandboxFull || isZeroQuota ? 100 : Math.min(rawPercent, 100)
  const tone: MeterTone =
    effectivePercent >= 100 ? 'error' : effectivePercent >= 80 ? 'warning' : 'neutral'

  const isUnlimited = limit === 'unlimited'
  let totalDisplay: string | number = isUnlimited
    ? t(($) => $['plansCommon.unlimited'], { ns: 'billing' })
    : limit
  if (!isUnlimited && unit && unitPosition === 'inline') totalDisplay = `${limit}${unit}`
  const showUnit = !!unit && !isUnlimited && unitPosition === 'suffix'
  const resetText =
    resetHint ??
    (typeof resetInDays === 'number'
      ? t(($) => $['usagePage.resetsIn'], { ns: 'billing', count: resetInDays })
      : undefined)

  const rightInfo: ReactNode = resetText ? (
    <div className="ml-auto flex-1 text-right system-xs-regular text-text-tertiary">
      {resetText}
    </div>
  ) : showUnit ? (
    <div className="ml-auto system-xs-medium text-text-tertiary">{unit}</div>
  ) : null

  const usageDisplay: ReactNode = (() => {
    if (usageUnknown) return <span>--</span>

    if (storageMode) {
      if (isSandboxFull) {
        return (
          <div className="flex items-center gap-1">
            <span>{storageThreshold}</span>
            <span className="system-md-regular text-text-quaternary">/</span>
            <span>
              {storageThreshold} {unit}
            </span>
          </div>
        )
      }
      if (isBelowThreshold) {
        return (
          <div className="flex items-center gap-1">
            <span>&lt; {storageThreshold}</span>
            {!isSandboxPlan && (
              <>
                <span className="system-md-regular text-text-quaternary">/</span>
                <span>{totalDisplay}</span>
              </>
            )}
            {isSandboxPlan && <span>{unit}</span>}
          </div>
        )
      }
      return (
        <div className="flex items-center gap-1">
          <span>{usage}</span>
          <span className="system-md-regular text-text-quaternary">/</span>
          <span>{totalDisplay}</span>
        </div>
      )
    }

    return (
      <div className="flex items-center gap-1">
        <span>{usage}</span>
        <span className="system-md-regular text-text-quaternary">/</span>
        <span>{totalDisplay}</span>
      </div>
    )
  })()

  const bar: ReactNode = isBelowThreshold ? (
    // Decorative "< N MB" placeholder — not a meter, not a progressbar.
    <div aria-hidden="true" className="overflow-hidden rounded-md bg-components-progress-bar-bg">
      <div
        className={cn(
          'h-1 rounded-md bg-progress-bar-indeterminate-stripe',
          isSandboxPlan ? 'w-full' : 'w-7.5',
        )}
      />
    </div>
  ) : (
    <Meter value={effectivePercent} max={100} aria-label={name}>
      <MeterTrack>
        <MeterIndicator tone={tone} />
      </MeterTrack>
    </Meter>
  )

  return (
    <div
      role="group"
      aria-label={name}
      className={cn('flex flex-col gap-2 rounded-xl bg-components-panel-bg p-4', className)}
    >
      {!hideIcon && iconClassName && (
        <span aria-hidden className={cn(iconClassName, 'size-4 text-text-tertiary')} />
      )}
      <dl className="flex flex-col gap-2">
        <dt className="flex items-center gap-1 system-xs-medium text-text-tertiary">
          <span id={labelId}>{name}</span>
          {(tooltip || storageHelp) && (
            <Infotip>
              <InfotipTrigger aria-labelledby={labelId} />
              <InfotipContent aria-labelledby={labelId} className="w-45">
                {tooltip && <p>{tooltip}</p>}
                {storageHelp && <p>{storageHelp}</p>}
              </InfotipContent>
            </Infotip>
          )}
        </dt>
        <dd
          data-testid="billing-quota-value"
          className="flex items-center gap-1 system-md-semibold text-text-primary"
        >
          {usageDisplay}
          {rightInfo}
        </dd>
      </dl>
      {!usageUnknown && bar}
    </div>
  )
}
export default React.memo(UsageInfo)