Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

4.4 KB
Raw Blame
import { InputControl } from '@/containers/dynamicControllerSetting/InputControl'
import { CheckboxControl } from '@/containers/dynamicControllerSetting/CheckboxControl'
import { DropdownControl } from '@/containers/dynamicControllerSetting/DropdownControl'
import { TextareaControl } from '@/containers/dynamicControllerSetting/TextareaControl'
import { SliderControl } from '@/containers/dynamicControllerSetting/SliderControl'

// Dynamic controller component that renders the appropriate control based on controller_type
type DynamicControllerProps = {
  key?: string
  title?: string
  className?: string
  description?: string
  readonly?: boolean
  /** Human reason the control is disabled (e.g. "Controlled by Mirostat"). */
  disabledReason?: string
  controllerType:
    | 'input'
    | 'checkbox'
    | 'dropdown'
    | 'textarea'
    | 'slider'
    | string
  controllerProps: {
    value?: string | boolean | number
    placeholder?: string
    type?: string
    options?: Array<{ value: number | string; name: string }>
    input_actions?: string[]
    rows?: number
    min?: number
    max?: number
    step?: number
    warnAbove?: number
    warnBelow?: number
    recommended?: string
  }
  onChange: (value: string | boolean | number) => void
}

export function DynamicControllerSetting({
  className,
  controllerType,
  controllerProps,
  disabledReason,
  onChange,
}: DynamicControllerProps) {
  const control = renderControl(controllerType, controllerProps, className, onChange)
  if (!disabledReason) return control
  return (
    <div
      className="opacity-50 pointer-events-none select-none"
      title={disabledReason}
      aria-disabled
    >
      {control}
    </div>
  )
}

function renderControl(
  controllerType: DynamicControllerProps['controllerType'],
  controllerProps: DynamicControllerProps['controllerProps'],
  className: string | undefined,
  onChange: DynamicControllerProps['onChange']
) {
  if (controllerType === 'input') {
    const isNumeric =
      controllerProps.type === 'number' ||
      typeof controllerProps.value === 'number' ||
      controllerProps.min !== undefined ||
      controllerProps.max !== undefined ||
      controllerProps.step !== undefined
    return (
      <InputControl
        type={controllerProps.type}
        placeholder={controllerProps.placeholder}
        value={
          typeof controllerProps.value === 'number'
            ? controllerProps.value
            : (controllerProps.value as string) || ''
        }
        inputActions={controllerProps.input_actions}
        className={className}
        min={controllerProps.min}
        max={controllerProps.max}
        step={controllerProps.step}
        onChange={(newValue) => {
          if (!isNumeric || typeof newValue !== 'string') return onChange(newValue)
          const trimmed = newValue.trim()
          if (trimmed === '' || trimmed === '-') return onChange(trimmed)
          const n = Number(trimmed)
          onChange(Number.isNaN(n) ? newValue : n)
        }}
      />
    )
  } else if (controllerType === 'checkbox') {
    return (
      <CheckboxControl
        checked={controllerProps.value as boolean}
        onChange={(newValue) => onChange(newValue)}
      />
    )
  } else if (controllerType === 'dropdown') {
    return (
      <DropdownControl
        value={controllerProps.value as string}
        options={controllerProps.options}
        recommended={controllerProps.recommended}
        onChange={(newValue) => onChange(newValue)}
      />
    )
  } else if (controllerType === 'textarea') {
    return (
      <TextareaControl
        placeholder={controllerProps.placeholder}
        value={(controllerProps.value as string) || ''}
        inputActions={controllerProps.input_actions}
        rows={controllerProps.rows}
        onChange={(newValue) => onChange(newValue)}
      />
    )
  } else if (controllerType === 'slider') {
    return (
      <SliderControl
        value={[controllerProps.value as number]}
        min={controllerProps.min}
        max={controllerProps.max}
        step={controllerProps.step}
        warnAbove={controllerProps.warnAbove}
        warnBelow={controllerProps.warnBelow}
        onChange={(newValue) => newValue && onChange(newValue[0])}
      />
    )
  }

  // Default to checkbox if controller type is not recognized
  return (
    <CheckboxControl
      checked={!!controllerProps.value}
      onChange={(newValue) => onChange(newValue)}
    />
  )
}