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.

2.7 KB
Raw Blame
import { useExitPresence } from '@platform/react/useExitRetainedValue';
import { useMountEffect } from '@platform/react/useMountEffect';
import { OPEN_COMMAND_PALETTE_HOTKEY } from '@workbench/hotkeys/catalog';
import { acceptsHotkeyPress, MOD_KEY_LABEL, toTinykeysBinding } from '@workbench/hotkeys/keys';
import { applyCustomHotkeys } from '@workbench/hotkeys/resolve';
import { useWorkbenchPreferences, useWorkbenchPreferenceSelector } from '@workbench/settings/store';
import { lazy, Suspense } from 'react';
import { tinykeys } from 'tinykeys';

import { closeCommandPalette, toggleCommandPalette, useIsCommandPaletteOpen } from './paletteStore';
import { SETTINGS_ENTRY_DEPS } from './settingsEntryDeps';

const LazyLaunchpadCommandPaletteDialog = lazy(() => import('./LaunchpadCommandPaletteDialog'));

const LaunchpadCommandPaletteHotkeys = ({ keys }: { keys: readonly string[] }) => {
  useMountEffect(() => {
    if (keys.length === 0) {
      return;
    }

    const bindings = Object.fromEntries(
      keys.map((key) => [
        toTinykeysBinding(key),
        (event: KeyboardEvent): void => {
          // The same checks as the editor's hotkey runtime, so a binding fires alike on both routes.
          if (!acceptsHotkeyPress(event, key)) {
            return;
          }
          event.preventDefault();
          toggleCommandPalette();
        },
      ])
    );

    return tinykeys(window, bindings, { ignore: () => false });
  });

  return null;
};

/** Lightweight Launchpad runtime and lazy dialog host; the dialog stays mounted while it animates closed. */
export const LaunchpadCommandPalette = () => {
  const isOpen = useIsCommandPaletteOpen();
  const dialog = useExitPresence(isOpen);
  const customHotkeys = useWorkbenchPreferenceSelector((preferences) => preferences.customHotkeys);
  const paletteHotkeys = applyCustomHotkeys(OPEN_COMMAND_PALETTE_HOTKEY, customHotkeys).keys;

  return (
    <>
      <LaunchpadCommandPaletteHotkeys key={paletteHotkeys.join('\n')} keys={paletteHotkeys} />
      {dialog.isMounted ? (
        <MountedLaunchpadCommandPalette key={dialog.generation} isOpen={isOpen} onExitComplete={dialog.release} />
      ) : null}
    </>
  );
};

const MountedLaunchpadCommandPalette = ({
  isOpen,
  onExitComplete,
}: {
  isOpen: boolean;
  onExitComplete: () => void;
}) => {
  const preferences = useWorkbenchPreferences();

  return (
    <Suspense fallback={null}>
      <LazyLaunchpadCommandPaletteDialog
        isOpen={isOpen}
        modifierKeyLabel={MOD_KEY_LABEL}
        preferences={preferences}
        settingsEntryDeps={SETTINGS_ENTRY_DEPS}
        onClose={closeCommandPalette}
        onExitComplete={onExitComplete}
      />
    </Suspense>
  );
};