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 type { FloatingWidgetState } from '@workbench/layoutContracts';
import type { WidgetInstanceId } from '@workbench/widgetContracts';

import { shallowEqual, useActiveProjectId, useActiveProjectSelector } from '@workbench/WorkbenchContext';
import { lazy, Suspense } from 'react';

// The window chrome loads only once a widget actually floats, keeping it out of the shell's eager bundle — nothing
// renders while no widget floats.
const FloatingWidgetWindow = lazy(() =>
  import('./FloatingWidgetWindow').then((module) => ({ default: module.FloatingWidgetWindow }))
);

const EMPTY_FLOATING: Record<WidgetInstanceId, FloatingWidgetState> = {};

/** Windows render in a fixed DOM order and stack by z-index: moving a node would drop its keyboard focus. */
export const FloatingWidgetLayer = () => {
  const projectId = useActiveProjectId();
  const floatingWidgets = useActiveProjectSelector(
    (project) => project.floatingWidgets ?? EMPTY_FLOATING,
    shallowEqual
  );
  const instanceIds = Object.keys(floatingWidgets).sort();

  if (instanceIds.length === 0) {
    return null;
  }

  const stackRanks = new Map(
    [...instanceIds]
      .sort((left, right) => floatingWidgets[left].stackOrder - floatingWidgets[right].stackOrder)
      .map((instanceId, stackRank) => [instanceId, stackRank])
  );

  return (
    <Suspense fallback={null}>
      {instanceIds.map((instanceId) => (
        <FloatingWidgetWindow
          key={`${projectId}:${instanceId}`}
          instanceId={instanceId}
          stackRank={stackRanks.get(instanceId) ?? 0}
          state={floatingWidgets[instanceId]}
        />
      ))}
    </Suspense>
  );
};