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.

1.6 KB
Raw Blame
import { registerAccountOwnedResource } from '@platform/state/accountLifecycle';
import { createExternalStore } from '@platform/state/externalStore';

/** Open state for the Widgets dialog, so the palette, the app menu and the rails' menus open the one the shell hosts. */
const INITIAL_SNAPSHOT = { isOpen: false };

export const widgetsDialogStore = createExternalStore<{ isOpen: boolean }>(INITIAL_SNAPSHOT);

/** Where focus goes on close: a control that outlives the menu item that opened the dialog. */
let returnFocusTarget: (() => HTMLElement | null) | null = null;

registerAccountOwnedResource({
  clear: () => {
    returnFocusTarget = null;
    widgetsDialogStore.setSnapshot(INITIAL_SNAPSHOT);
  },
  name: 'widgets-dialog',
});

/** Opens the dialog; on close, focus goes back where the dialog found it (palette and shortcut openers). */
export const openWidgetsDialog = (): void => {
  returnFocusTarget = null;
  widgetsDialogStore.setSnapshot({ isOpen: true });
};

/** Opens the dialog from a menu item, naming the control focus returns to on close. */
export const openWidgetsDialogFrom = (returnFocus: () => HTMLElement | null): void => {
  returnFocusTarget = returnFocus;
  widgetsDialogStore.setSnapshot({ isOpen: true });
};

export const closeWidgetsDialog = (): void => widgetsDialogStore.setSnapshot(INITIAL_SNAPSHOT);

/** The control the opener named, while it is still on the page; otherwise the dialog's own restore applies. */
export const getWidgetsDialogReturnFocus = (): HTMLElement | null => {
  const target = returnFocusTarget?.() ?? null;

  return target?.isConnected ? target : null;
};