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.6 KB
Raw Blame
import type { WidgetRegion } from '@workbench/layoutContracts';
import type { WidgetInstanceId } from '@workbench/widgetContracts';

import { MissingWidgetFrame, WidgetRendererById } from '@workbench/widget-frame';
import { areWidgetRenderInstancesEqual } from '@workbench/widget-frame/widgetRenderInstance';
import { resolveWidgetLabel } from '@workbench/widgetLabels';
import { getWidgetById } from '@workbench/widgetRegistry';
import { useActiveProjectId, useActiveProjectSelector } from '@workbench/WorkbenchContext';
import { Activity } from 'react';
import { useTranslation } from 'react-i18next';

import {
  areInstanceIdListsEqual,
  getActiveInstanceIdsOutside,
  useMountedInstanceIds,
  withoutInstancesShownElsewhere,
} from './useMountedInstanceIds';

type PanelRegion = Exclude<WidgetRegion, 'center'>;

export const LeftPanel = ({ instanceId }: { instanceId: WidgetInstanceId }) => (
  <WidgetPanelSlot instanceId={instanceId} region="left" />
);

export const RightPanel = ({ instanceId }: { instanceId: WidgetInstanceId }) => (
  <WidgetPanelSlot instanceId={instanceId} region="right" />
);

/** Keeps shown panels mounted to preserve scroll, selection and virtualization across presets. */
export const WidgetPanelSlot = ({ instanceId, region }: { instanceId: WidgetInstanceId; region: PanelRegion }) => {
  const projectId = useActiveProjectId();
  const activeIdsElsewhere = useActiveProjectSelector(
    (project) => getActiveInstanceIdsOutside(project.widgetRegions, region, project.floatingWidgets),
    areInstanceIdListsEqual
  );
  const mountedIds = withoutInstancesShownElsewhere(
    useMountedInstanceIds(instanceId, projectId),
    instanceId,
    activeIdsElsewhere
  );

  return (
    <>
      {mountedIds.map((id) => (
        <Activity key={id} mode={id === instanceId ? 'visible' : 'hidden'}>
          <WidgetPanelInstance instanceId={id} region={region} />
        </Activity>
      ))}
    </>
  );
};

const WidgetPanelInstance = ({ instanceId, region }: { instanceId: WidgetInstanceId; region: PanelRegion }) => {
  const { t } = useTranslation();
  const instance = useActiveProjectSelector(
    (project) => project.widgetInstances[instanceId],
    areWidgetRenderInstancesEqual
  );
  const widget = instance ? getWidgetById(instance.typeId) : undefined;

  if (!instance || !widget || widget.status !== 'enabled') {
    return (
      <MissingWidgetFrame
        instanceId={instanceId}
        label={widget ? resolveWidgetLabel(widget.manifest, t) : instanceId}
        region={region}
        typeId={instance?.typeId}
      />
    );
  }

  return <WidgetRendererById instanceId={instance.id} widget={widget} region={region} />;
};