Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

2.7 KB
Raw Blame
import {
  getSearchFieldInputPaddingRightClass,
  shouldShowSearchFieldClearButton,
  shouldShowSearchFieldShortcutHint,
  type SearchFieldFocusEvent,
  type SearchFieldKeyboardEvent,
  type SearchFieldProps,
} from './searchFieldModel';

type SearchFieldStateOptions = Pick<
  SearchFieldProps,
  | 'clearOnFocusedEscape'
  | 'disabled'
  | 'hasTrailingControls'
  | 'inputRef'
  | 'onBlur'
  | 'onChange'
  | 'onFocus'
  | 'onKeyDown'
  | 'showClearButton'
  | 'shortcutHint'
  | 'trailingControlCount'
  | 'value'
>;

export function useSearchFieldState(options: SearchFieldStateOptions) {
  let inputEl: HTMLInputElement | undefined;

  const normalizeEventTarget = <
    TEvent extends { currentTarget: EventTarget | null; target: EventTarget | null },
  >(
    event: TEvent,
  ) => {
    const currentTarget = event.currentTarget as HTMLInputElement;
    const normalizedTarget = event.target as Element;

    return new Proxy(event, {
      get(eventTarget, prop) {
        if (prop === 'currentTarget') return currentTarget;
        if (prop === 'target') return normalizedTarget;
        const value = Reflect.get(eventTarget, prop);
        if (typeof value === 'function') {
          return value.bind(eventTarget);
        }
        return value;
      },
    });
  };

  const showShortcutHint = () =>
    shouldShowSearchFieldShortcutHint(options.value, options.shortcutHint);
  const showClearButton = () =>
    shouldShowSearchFieldClearButton(options.value, options.disabled, options.showClearButton);
  const inputPaddingRight = () =>
    getSearchFieldInputPaddingRightClass({
      hasTrailingControls: options.hasTrailingControls,
      trailingControlCount: options.trailingControlCount,
      showShortcutHint: showShortcutHint(),
      showClearButton: showClearButton(),
    });

  const setInputRef = (el: HTMLInputElement) => {
    inputEl = el;
    options.inputRef?.(el);
  };

  const handleKeyDown = (event: KeyboardEvent & { currentTarget: HTMLInputElement }) => {
    options.onKeyDown?.(normalizeEventTarget(event) as SearchFieldKeyboardEvent);
    if (event.defaultPrevented) return;

    if (event.key === 'Escape' && (options.clearOnFocusedEscape ?? true)) {
      if (options.value) {
        options.onChange('');
      }
      inputEl?.blur();
    }
  };

  const handleFocus = (event: FocusEvent & { currentTarget: HTMLInputElement }) => {
    options.onFocus?.(normalizeEventTarget(event) as SearchFieldFocusEvent);
  };

  const handleBlur = (event: FocusEvent & { currentTarget: HTMLInputElement }) => {
    options.onBlur?.(normalizeEventTarget(event) as SearchFieldFocusEvent);
  };

  return {
    handleBlur,
    handleFocus,
    handleKeyDown,
    inputPaddingRight,
    setInputRef,
    showClearButton,
    showShortcutHint,
  };
}