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.

3.8 KB
Raw Blame
'use client'
import type { FC } from 'react'
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'
import { Infotip, InfotipContent, InfotipTrigger } from '@langgenius/dify-ui/infotip'
import {
  NumberField,
  NumberFieldControls,
  NumberFieldDecrement,
  NumberFieldGroup,
  NumberFieldIncrement,
  NumberFieldInput,
} from '@langgenius/dify-ui/number-field'
import {
  Slider,
  SliderControl,
  SliderIndicator,
  SliderLabel,
  SliderThumb,
  SliderTrack,
} from '@langgenius/dify-ui/slider'
import { Switch } from '@langgenius/dify-ui/switch'
import { useId } from 'react'

type Props = Readonly<{
  className?: string
  id: string
  name: string
  noTooltip?: boolean
  tip?: string
  value: number
  enable: boolean
  step?: number
  min?: number
  max: number
  onChange: (key: string, value: number) => void
  disabled?: boolean
  hasSwitch?: boolean
  onSwitchChange?: (key: string, enable: boolean) => void
}>

const ParamItem: FC<Props> = ({
  className,
  id,
  name,
  noTooltip,
  tip,
  step = 0.1,
  min = 0,
  max,
  value,
  enable,
  onChange,
  disabled = false,
  hasSwitch,
  onSwitchChange,
}) => {
  const labelId = useId()
  return (
    <Fieldset className={className}>
      <FieldsetLegend className="sr-only">{name}</FieldsetLegend>
      <div className="flex items-center justify-between">
        <div className="flex min-h-6 flex-wrap items-center gap-y-1">
          {hasSwitch && (
            <Switch
              aria-labelledby={labelId}
              size="md"
              className="mr-2"
              checked={enable}
              disabled={disabled}
              onCheckedChange={async (val) => {
                onSwitchChange?.(id, val)
              }}
            />
          )}
          <span id={labelId} className="mr-1 system-sm-semibold text-text-secondary">
            {name}
          </span>
          {!noTooltip && tip && (
            <Infotip>
              <InfotipTrigger aria-labelledby={labelId} />
              <InfotipContent aria-labelledby={labelId} className="w-50">
                {tip}
              </InfotipContent>
            </Infotip>
          )}
        </div>
      </div>
      <div className="mt-1 flex flex-wrap items-center gap-3">
        <div className="flex shrink-0 items-center">
          <NumberField
            disabled={disabled || !enable}
            min={min}
            max={max}
            step={step}
            value={value}
            onValueChange={(nextValue) => {
              if (nextValue !== null) onChange(id, nextValue)
            }}
          >
            <NumberFieldGroup>
              <NumberFieldInput aria-label={name} className="w-18" />
              <NumberFieldControls>
                <NumberFieldIncrement />
                <NumberFieldDecrement />
              </NumberFieldControls>
            </NumberFieldGroup>
          </NumberField>
        </div>
        <div className="flex min-w-25 grow items-center">
          <Slider
            className="w-full"
            disabled={disabled || !enable}
            value={max < 5 ? value * 100 : value}
            min={min < 1 ? min * 100 : min}
            max={max < 5 ? max * 100 : max}
            onValueChange={(value) => onChange(id, value / (max < 5 ? 100 : 1))}
          >
            <SliderLabel className="sr-only">{name}</SliderLabel>
            <SliderControl>
              <SliderTrack>
                <SliderIndicator />
                <SliderThumb
                  getAriaValueText={
                    max < 5
                      ? (_formattedValue, sliderValue) => (sliderValue / 100).toString()
                      : undefined
                  }
                />
              </SliderTrack>
            </SliderControl>
          </Slider>
        </div>
      </div>
    </Fieldset>
  )
}
export default ParamItem