Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

8.5 KB
Raw Blame
import type { SettingFieldProps } from '@platform/ui/settings/contracts';

import { Badge, Field, HStack, Input, Stack } from '@chakra-ui/react';
import { resolveSettingsText } from '@platform/ui/settings/contracts';
import { SettingLayerIndicator } from '@platform/ui/settings/SettingLayerIndicator';
import { getImageMapClusterEps, MAX_CLUSTER_EPS, MIN_CLUSTER_EPS } from '@workbench/image-map/imageMapSettings';
import { imageMapStore } from '@workbench/image-map/imageMapStore';
import { useLayeredSetting } from '@workbench/settings/useLayeredSetting';
import { useCallback, useEffect, useRef, useState, type ChangeEvent } from 'react';
import { useTranslation } from 'react-i18next';

/** One recluster per held spinner or typed number, not per keystroke: reclustering costs seconds of server CPU. */
const COMMIT_DEBOUNCE_MS = 500;

const DIALOG_DIRECTION = { base: 'column', md: 'row' } as const;
const DIALOG_ALIGNMENT = { base: 'stretch', md: 'center' } as const;

/** Three significant digits, not decimals: the endpoint uses values down to 1e-6, which `toFixed(3)` zeroes. */
const forDisplay = (eps: number): string => String(Number(eps.toPrecision(3)));

/** The DBSCAN eps; not a shared setting because blank means "derive it again", which that control cannot hold. */
export const ClusterStrengthField = ({ field, surface, target }: SettingFieldProps) => {
  const { t } = useTranslation();
  const {
    disabled,
    layer,
    set,
    value: chosen,
  } = useLayeredSetting('image-map', 'clusterEps', target, getImageMapClusterEps, null, field);
  // What the server actually clustered with, which is the heuristic's answer whenever nothing has been chosen.
  const resolved = imageMapStore.useSelector((snapshot) => snapshot.data?.clusterEps ?? null);
  const shown = chosen ?? resolved;
  const [invalid, setInvalid] = useState<string | null>(null);
  const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
  const pending = useRef(false);
  // Read by the unmount cleanup, which cannot see the render's closure.
  const flush = useRef<(() => void) | null>(null);
  const resolvedRef = useRef<number | null>(null);
  // Uncontrolled on purpose. A controlled `value` driven from the element would erase exactly the input this field has
  // to tolerate: `type="number"` reports "" for a half-typed "0.", so React would write that "" straight back and wipe
  // the keystrokes out from under the cursor — taking `badInput` with them, which is the only signal distinguishing a
  // partial number from a deliberately cleared box.
  const input = useRef<HTMLInputElement | null>(null);

  /** Selects the box when the caret is inside, or the next keystroke appends to a number the user did not type. */
  const show = useCallback((eps: number | null) => {
    const field = input.current;

    if (!field) {
      return;
    }

    field.value = eps === null ? '' : forDisplay(eps);

    if (document.activeElement === field) {
      field.select();
    }
  }, []);

  const commit = useCallback(
    (eps: number | null) => {
      pending.current = false;
      set(eps);

      if (eps === null) {
        // Refilled from here rather than left to the sync effect below: clearing a field that was already on the
        // heuristic changes no state at all, so that effect would never run and the box would sit empty. A committed
        // number needs no write — the box already holds exactly what was typed, to more digits than forDisplay keeps.
        show(resolvedRef.current);
      }
    },
    [set, show]
  );

  useEffect(() => {
    resolvedRef.current = resolved;
  }, [resolved]);

  // Anything arriving from outside — a recluster's resolved value, another surface changing the setting — must not move
  // the number under the cursor, so it is skipped while an edit is in flight. Skipped, not queued: an update landing
  // mid-edit is dropped, and the box catches up on the next keystroke or on blur, both of which rewrite it from
  // `shown`. Skipped too when the box already holds this number, since rewriting it through forDisplay would truncate a
  // value typed to more digits than it keeps.
  useEffect(() => {
    if (pending.current || !input.current || Number(input.current.value) === shown) {
      return;
    }

    setInvalid(null);
    show(shown);
  }, [show, shown]);

  // Flushed rather than dropped: the settings dialog can close without the field ever blurring, and an edit made and
  // then dismissed should still take effect.
  useEffect(
    () => () => {
      if (timer.current !== null) {
        clearTimeout(timer.current);
        flush.current?.();
      }
    },
    []
  );

  /** Deferred behind the debounce so "0.15" does not flash a range error on its way past "0". */
  const resolve = useCallback(
    (text: string, badInput: boolean) => {
      timer.current = null;

      // `type="number"` reports an empty value for anything it cannot parse yet — ".", "-", "1e" — so an empty box
      // alone cannot be read as "use the heuristic". `badInput` is what separates a half-typed number from a
      // deliberately cleared one; without it, a pause mid-keystroke throws away the strength the map was tuned to.
      if (badInput) {
        setInvalid(t('settingsDialog.fields.clusterStrengthIncomplete'));

        return;
      }

      if (text.trim() === '') {
        commit(null);

        return;
      }

      const eps = Number(text);

      if (!Number.isFinite(eps) || eps < MIN_CLUSTER_EPS || eps > MAX_CLUSTER_EPS) {
        // Refused rather than clamped: the endpoint rejects anything outside this range, and quietly substituting a
        // different number would make the control disagree with the map.
        setInvalid(t('settingsDialog.fields.clusterStrengthRange', { max: MAX_CLUSTER_EPS, min: MIN_CLUSTER_EPS }));

        return;
      }

      commit(eps);
    },
    [commit, t]
  );

  const onChange = useCallback(
    (event: ChangeEvent<HTMLInputElement>) => {
      const { validity, value } = event.currentTarget;

      pending.current = true;
      setInvalid(null);

      if (timer.current !== null) {
        clearTimeout(timer.current);
      }

      flush.current = () => resolve(value, validity.badInput);
      timer.current = setTimeout(() => resolve(value, validity.badInput), COMMIT_DEBOUNCE_MS);
    },
    [resolve]
  );

  // Leaving the field ends the edit: flush a pending commit so a value typed and then dismissed still takes effect, and
  // otherwise release the guard so a refused edit does not read as mid-edit forever.
  const onBlur = useCallback(() => {
    if (timer.current !== null) {
      clearTimeout(timer.current);
      const field = input.current;
      resolve(field?.value ?? '', field?.validity.badInput === true);

      return;
    }

    pending.current = false;
    setInvalid(null);
    show(shown);
  }, [resolve, show, shown]);

  const label = resolveSettingsText(field.label, t);
  const description = field.description ? resolveSettingsText(field.description, t) : undefined;

  return (
    <Field.Root
      data-setting-control=""
      alignItems={surface === 'quick' ? 'stretch' : DIALOG_ALIGNMENT}
      disabled={disabled}
      display="flex"
      flexDirection={surface === 'quick' ? 'column' : DIALOG_DIRECTION}
      gap="3"
      invalid={invalid !== null}
      justifyContent="space-between"
    >
      <Stack flex="1" gap="1">
        <HStack gap="2">
          <Field.Label fontSize={surface === 'quick' ? 'md' : 'lg'} fontWeight="500">
            {label}
          </Field.Label>
          {layer ? <SettingLayerIndicator label={label} layer={layer} /> : null}
        </HStack>
        {description ? <Field.HelperText color="fg.muted">{description}</Field.HelperText> : null}
        {invalid ? <Field.ErrorText fontSize="md">{invalid}</Field.ErrorText> : null}
      </Stack>
      <HStack flexShrink={0} gap="2">
        <Input
          aria-label={label}
          defaultValue={shown === null ? '' : forDisplay(shown)}
          disabled={disabled}
          max={MAX_CLUSTER_EPS}
          min={MIN_CLUSTER_EPS}
          ref={input}
          size="lg"
          step="0.01"
          type="number"
          w="24"
          onBlur={onBlur}
          onChange={onChange}
        />
        {/* Says whose number this is: without it a derived value and a chosen
            one are the same digits in the same box. */}
        <Badge
          data-testid="cluster-strength-auto"
          size="lg"
          variant="subtle"
          visibility={chosen === null ? 'visible' : 'hidden'}
        >
          {t('settingsDialog.fields.clusterStrengthAuto')}
        </Badge>
      </HStack>
    </Field.Root>
  );
};