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.

29.8 KB
Raw Blame
import type { DragEndEvent } from '@dnd-kit/core';
import type {
  BuiltInLayoutPresetId,
  LayoutPreset,
  LayoutPresetId,
  LayoutPresetSnapshot,
} from '@workbench/layoutContracts';
import type { Project } from '@workbench/projectContracts';
import type { KeyboardEvent, MouseEvent, PointerEvent } from 'react';

import { Box, HStack, Icon, Kbd, Menu, Portal, Stack, Text, useTooltipContext, VisuallyHidden } from '@chakra-ui/react';
import {
  closestCenter,
  DndContext,
  KeyboardSensor,
  MouseSensor,
  TouchSensor,
  useSensor,
  useSensors,
} from '@dnd-kit/core';
import { restrictToHorizontalAxis, restrictToParentElement } from '@dnd-kit/modifiers';
import {
  horizontalListSortingStrategy,
  sortableKeyboardCoordinates,
  SortableContext,
  useSortable,
} from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';
import { useExitPresence } from '@platform/react/useExitRetainedValue';
import { IconButton } from '@platform/ui/Button';
import { MenuContent, MenuHeadingGroup } from '@platform/ui/Menu';
import { HIDDEN_SCROLLBAR_CSS } from '@platform/ui/scrollStripEdges';
import { Tabs } from '@platform/ui/Tabs';
import { Tooltip, TooltipGroup } from '@platform/ui/Tooltip';
import { getPlacedWidgetTypeIds, graphWidgetSources } from '@workbench/graphWidgets';
import { useShortcutBinding, useShortcutLabel } from '@workbench/hotkeys/useShortcutBinding';
import { preloadLayoutPresetWidgets } from '@workbench/layoutPresetActivation';
import { getOrderedLayoutPresets } from '@workbench/layoutPresetCollection';
import {
  builtInLayoutPresetDescriptors,
  getLayoutPresetLabel,
  isBuiltInLayoutPresetId,
} from '@workbench/layoutPresets';
import { findLayoutPresetWorkingCopy } from '@workbench/layoutPresetSnapshots';
import { CUE_WIDTH, OverflowCue, useOverflowEdges } from '@workbench/shell/overflowEdges';
import { useActiveProjectSelector, useWorkbenchCommands, useWorkbenchSelector } from '@workbench/WorkbenchContext';
import {
  ArrowRightIcon,
  ChevronDownIcon,
  PencilIcon,
  PlusIcon,
  RotateCcwIcon,
  SaveIcon,
  SettingsIcon,
  Trash2Icon,
} from 'lucide-react';
import { useCallback, useId, useMemo, useRef, useState, useSyncExternalStore } from 'react';
import { useTranslation } from 'react-i18next';

import type { LayoutPresetDialogValue } from './layoutPresetDialogModel';

import { LayoutPresetDialog } from './LayoutPresetDialog';
import { resolveLayoutPresetIcon } from './layoutPresetIcons';
import { openLayoutPresetDelete, openLayoutPresetEdit, openLayoutPresetManager } from './layoutPresetManagerStore';
import { HIDE_BELOW_PRESET_LABEL_WIDTH } from './topbarBreakpoints';
import { TopbarShortcutKeys } from './TopbarShortcutKeys';
import { useActiveLayoutPresetId, useLayoutDrift, useUnsavedInactiveLayoutPresetIds } from './useLayoutDrift';

const PRESET_MENU_ATTRIBUTE = 'data-preset-menu';
const TAB_FILL_TRANSITION =
  'background var(--chakra-durations-faster) ease, border-color var(--chakra-durations-faster) ease, color var(--chakra-durations-faster) ease';
const PRESET_TAB_KEYS_BLOCKED_DURING_DRAG = new Set(['ArrowDown', 'ArrowLeft', 'ArrowRight', 'End', 'Home']);
const DND_MODIFIERS = [restrictToHorizontalAxis, restrictToParentElement];
/** Counts the presets past the end, and keeps the active one in view when the strip narrows or the preset changes. */
const PRESET_OVERFLOW_OPTIONS = {
  itemSelector: '[role="tab"]',
  keepInViewSelector: '[role="tab"][aria-selected="true"]',
};
const KEYBOARD_SENSOR_OPTIONS = {
  coordinateGetter: sortableKeyboardCoordinates,
  keyboardCodes: { cancel: ['Escape'], end: ['Space', 'Tab'], start: ['Space'] },
};
const MOUSE_SENSOR_OPTIONS = { activationConstraint: { distance: 6 } } as const;
const TOUCH_SENSOR_OPTIONS = { activationConstraint: { delay: 250, tolerance: 5 } } as const;

const createCustomPresetId = (): string =>
  `custom-layout-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;

/** What each built-in preset is for, as the tab's tip says under its name. */
const BUILT_IN_PRESET_DESCRIPTION_KEYS: Record<BuiltInLayoutPresetId, string> = {
  automate: 'topbar.presets.builtInDescriptions.automate',
  compose: 'topbar.presets.builtInDescriptions.compose',
  edit: 'topbar.presets.builtInDescriptions.edit',
  video: 'topbar.presets.builtInDescriptions.video',
};

const findPresetShortcutCommandId = (presetId: LayoutPresetId): string | null => {
  const descriptor = builtInLayoutPresetDescriptors.find(({ preset }) => preset.id === presetId);

  return descriptor ? `app.${descriptor.hotkeyId}` : null;
};

// The label's own hiding condition, so the tip and the label never disagree about whether the name is on screen.
const matchLabelHidden = (): MediaQueryList =>
  globalThis.matchMedia(Object.keys(HIDE_BELOW_PRESET_LABEL_WIDTH)[0]!.replace(/^@media\s+/, ''));
const subscribeLabelHidden = (onChange: () => void): (() => void) => {
  const query = matchLabelHidden();
  query.addEventListener('change', onChange);
  return () => query.removeEventListener('change', onChange);
};
const getLabelHidden = (): boolean => matchLabelHidden().matches;

const selectPresetWorkingLayouts = (project: Project) => project.presetWorkingLayouts;

const selectPlacedGraphWidgetSources = (project: Project) => {
  const placedTypeIds = getPlacedWidgetTypeIds(project);

  return graphWidgetSources.filter((source) => placedTypeIds.has(source.typeId));
};

export const LayoutPresetStrip = () => {
  const { t } = useTranslation();
  const { layout } = useWorkbenchCommands();
  const [isSaveAsOpen, setIsSaveAsOpen] = useState(false);
  const saveAsDialog = useExitPresence(isSaveAsOpen);
  const [menuTarget, setMenuTarget] = useState<PresetMenuTarget | null>(null);
  const menuTicket = useRef(0);

  const account = useWorkbenchSelector((snapshot) => snapshot.account);
  const invocation = useActiveProjectSelector((project) => project.invocation);
  const presetWorkingLayouts = useActiveProjectSelector(selectPresetWorkingLayouts, Object.is);
  const sourceOptions = useActiveProjectSelector(selectPlacedGraphWidgetSources);
  // Labels as shown: a built-in preset not renamed by the account reads in the current language.
  const presets = useMemo(
    () => getOrderedLayoutPresets(account).map((preset) => ({ ...preset, label: getLayoutPresetLabel(preset, t) })),
    [account, t]
  );
  const presetIds = useMemo(() => presets.map(({ id }) => id), [presets]);
  const { hasDrifted } = useLayoutDrift();
  const activePresetId = useActiveLayoutPresetId();
  const unsavedInactiveIds = useUnsavedInactiveLayoutPresetIds();
  // Judged against the store's active preset, not the optimistic tab, so a dot never jumps ahead of the switch.
  const isPresetUnsaved = useCallback(
    (presetId: LayoutPresetId) => (presetId === activePresetId ? hasDrifted : unsavedInactiveIds.has(presetId)),
    [activePresetId, hasDrifted, unsavedInactiveIds]
  );
  const [isReordering, setIsReordering] = useState(false);
  const isLabelHidden = useSyncExternalStore(subscribeLabelHidden, getLabelHidden);
  // Like the top bar's other labelled controls, a visible label is not repeated: beside it, a preset has a tip only for
  // what the label does not say (a built-in's shortcut and purpose, unsaved changes).
  const hasTip = useCallback(
    (presetId: LayoutPresetId) => isLabelHidden || isBuiltInLayoutPresetId(presetId) || isPresetUnsaved(presetId),
    [isLabelHidden, isPresetUnsaved]
  );
  // Quiet while a preset's menu is open or a tab is being dragged, where it would cover the menu or trail the drag.
  const isTipShown = useCallback(
    (presetId: LayoutPresetId) => menuTarget === null && !isReordering && hasTip(presetId),
    [hasTip, isReordering, menuTarget]
  );
  const presetTip = useMemo(
    () => <PresetTipContent hasTip={hasTip} isUnsaved={isPresetUnsaved} presets={presets} />,
    [hasTip, isPresetUnsaved, presets]
  );
  const tabsIdPrefix = useId();
  // Named so the strip's tooltip can anchor to each tab's own id instead of replacing it.
  const tabIds = useMemo(() => ({ trigger: (value: string) => `${tabsIdPrefix}-preset-${value}` }), [tabsIdPrefix]);
  const [pendingPresetId, setPendingPresetId] = useState<LayoutPresetId | null>(null);

  // The store caught up; stop overriding it.
  if (pendingPresetId !== null && pendingPresetId === activePresetId) {
    setPendingPresetId(null);
  }

  const selectedPresetId = pendingPresetId ?? activePresetId;
  const activePreset = useMemo(
    () => presets.find(({ id }) => id === selectedPresetId) ?? presets[0]!,
    [presets, selectedPresetId]
  );
  const dndAccessibility = useMemo(
    () => ({ screenReaderInstructions: { draggable: t('topbar.presets.reorderInstructions') } }),
    [t]
  );
  // Presets past the strip's width say so at its ends, like the center's chrome rows.
  const { attach: attachScroller, hiddenEdges, scrollTo } = useOverflowEdges(PRESET_OVERFLOW_OPTIONS);
  const sensors = useSensors(
    useSensor(MouseSensor, MOUSE_SENSOR_OPTIONS),
    useSensor(TouchSensor, TOUCH_SENSOR_OPTIONS),
    useSensor(KeyboardSensor, KEYBOARD_SENSOR_OPTIONS)
  );
  const saveAsDefaultRoute = useMemo(
    () => ({ destination: invocation.destination, sourceId: invocation.sourceId }),
    [invocation.destination, invocation.sourceId]
  );
  // The latest tab press; a frame-deferred activation that a newer press superseded does not start.
  const requestedPresetIdRef = useRef<LayoutPresetId | null>(null);
  /** Acknowledge the tab press before rearranging the workspace so a blocking layout commit cannot delay feedback. */
  const requestPreset = useCallback(
    (presetId: LayoutPresetId) => {
      // Pressing the preset the workbench is still on, while another is pending, means stay: cancel the pending switch.
      // Applying it would reapply its saved arrangement and discard its unsaved changes.
      if (presetId === activePresetId) {
        requestedPresetIdRef.current = null;
        layout.cancelPresetActivation();
        setPendingPresetId(null);
        return;
      }

      requestedPresetIdRef.current = presetId;
      setPendingPresetId(presetId);
      requestAnimationFrame(() => {
        if (requestedPresetIdRef.current !== presetId) {
          return;
        }

        void layout.activatePreset(presetId).then((appliedPresetId) => {
          // Return dropped activations to store selection without overwriting a newer request's optimistic selection.
          if (appliedPresetId !== presetId) {
            setPendingPresetId((pending) => (pending === presetId ? null : pending));
          }
        });
      });
    },
    [activePresetId, layout]
  );
  const applyPreset = useCallback(
    (preset: LayoutPreset) => {
      if (preset.id !== selectedPresetId) {
        requestPreset(preset.id);
      }
    },
    [requestPreset, selectedPresetId]
  );
  const handleValueChange = useCallback(
    (event: { value: string }) => {
      const preset = presets.find((candidate) => candidate.id === event.value);

      if (preset) {
        applyPreset(preset);
      }
    },
    [applyPreset, presets]
  );
  const openSaveAsDialog = useCallback(() => setIsSaveAsOpen(true), []);
  const closeSaveAsDialog = useCallback(() => setIsSaveAsOpen(false), []);
  const saveAsNewPreset = useCallback(
    ({ defaultRoute, iconId, name }: LayoutPresetDialogValue) =>
      layout.createPreset(createCustomPresetId(), name, iconId, defaultRoute),
    [layout]
  );
  const openMenu = useCallback((target: Omit<PresetMenuTarget, 'ticket'>) => {
    menuTicket.current += 1;
    setMenuTarget({ ...target, ticket: menuTicket.current });
  }, []);
  const closeMenu = useCallback(() => setMenuTarget(null), []);
  const requestEdit = useCallback((preset: LayoutPreset) => openLayoutPresetEdit(preset.id), []);
  const requestDelete = useCallback((preset: LayoutPreset) => openLayoutPresetDelete(preset.id), []);
  const handleDragStart = useCallback(() => setIsReordering(true), []);
  const handleDragCancel = useCallback(() => setIsReordering(false), []);
  const handleDragEnd = useCallback(
    (event: DragEndEvent) => {
      setIsReordering(false);

      if (!event.over || event.active.id === event.over.id) {
        return;
      }

      layout.reorderPresets(event.active.id as LayoutPresetId, event.over.id as LayoutPresetId);
    },
    [layout]
  );

  return (
    <>
      <HStack gap="1" justify="center" maxW="min(36vw, 44rem)" minW="0">
        {/* The cues' positioning context; the scroller fills it. */}
        <Box maxW="full" minW="0" position="relative">
          <Box
            ref={attachScroller}
            css={HIDDEN_SCROLLBAR_CSS}
            data-layout-preset-scroll=""
            isolation="isolate"
            maxW="full"
            minW="0"
            overflowX="auto"
            overscrollBehaviorX="contain"
            // Focus and keep-in-view scroll a tab clear of the cues, not merely onto the strip's edge.
            scrollPaddingInline={CUE_WIDTH}
          >
            <DndContext
              accessibility={dndAccessibility}
              autoScroll={false}
              collisionDetection={closestCenter}
              modifiers={DND_MODIFIERS}
              sensors={sensors}
              onDragCancel={handleDragCancel}
              onDragEnd={handleDragEnd}
              onDragStart={handleDragStart}
            >
              <Tabs.Root
                ids={tabIds}
                minW="max-content"
                size="lg"
                value={selectedPresetId}
                variant="subtle"
                onValueChange={handleValueChange}
              >
                {/* The name belongs on the tablist, not the root — the root is a plain
                  container and carries no role for it to name. */}
                <SortableContext items={presetIds} strategy={horizontalListSortingStrategy}>
                  {/* One tip for the strip: it follows focus and the pointer from tab to tab. Not closed by scrolling:
                    an arrow-key switch re-lays out the workbench, whose panels restore their scroll positions, and the
                    tab itself never scrolls out from under the tip. */}
                  <TooltipGroup.Root
                    closeOnScroll={false}
                    content={presetTip}
                    getTriggerId={tabIds.trigger}
                    isEnabled={isTipShown}
                  >
                    <Tabs.List aria-label={t('topbar.presets.layoutPreset')} gap="0.5">
                      {presets.map((preset) => (
                        <PresetTab
                          key={preset.id}
                          isActive={preset.id === selectedPresetId}
                          isUnsaved={isPresetUnsaved(preset.id)}
                          preset={preset}
                          workingArrangement={
                            preset.id === activePresetId
                              ? undefined
                              : findLayoutPresetWorkingCopy(presetWorkingLayouts, preset.id)
                          }
                          onOpenMenu={openMenu}
                          onRequest={requestPreset}
                        />
                      ))}
                    </Tabs.List>
                  </TooltipGroup.Root>
                </SortableContext>
                {/* Provide aria-controls targets describing the shared dock outside this component. Pinned to the
                    row's start: at its place in the flow, the shown panel would lie past the tabs and give the row
                    overflow that hides nothing. */}
                {presets.map((preset) => (
                  <Tabs.Content key={preset.id} value={preset.id} asChild>
                    <VisuallyHidden insetInlineStart="0" top="0">{`${preset.label} layout`}</VisuallyHidden>
                  </Tabs.Content>
                ))}
              </Tabs.Root>
            </DndContext>
          </Box>
          {hiddenEdges.start ? (
            <OverflowCue edge="start" label={t('common.scrollToStart')} onScrollTo={scrollTo} />
          ) : null}
          {hiddenEdges.end ? (
            <OverflowCue
              ariaLabel={t('topbar.presets.moreAvailable', { count: hiddenEdges.endCount })}
              count={hiddenEdges.endCount}
              edge="end"
              label={t('topbar.presets.more')}
              onScrollTo={scrollTo}
            />
          ) : null}
        </Box>

        <Tooltip content={t('topbar.presets.saveAsTooltip')} showArrow>
          <IconButton
            aria-label={t('topbar.presets.saveAsTooltip')}
            size="lg"
            variant="ghost"
            onClick={openSaveAsDialog}
          >
            <Icon as={PlusIcon} boxSize="4" />
          </IconButton>
        </Tooltip>
      </HStack>

      {menuTarget ? (
        <PresetMenu
          key={menuTarget.ticket}
          isActive={menuTarget.preset.id === selectedPresetId}
          isUnsaved={isPresetUnsaved(menuTarget.preset.id)}
          target={menuTarget}
          onApply={applyPreset}
          onClose={closeMenu}
          onDelete={requestDelete}
          onEdit={requestEdit}
        />
      ) : null}

      {saveAsDialog.isMounted ? (
        <LayoutPresetDialog
          key={saveAsDialog.generation}
          defaultRoute={saveAsDefaultRoute}
          isOpen={saveAsDialog.isOpen}
          name={`${activePreset.label} copy`}
          sourceOptions={sourceOptions}
          submitLabel={t('topbar.presets.save')}
          title={t('topbar.presets.saveAs')}
          onClose={closeSaveAsDialog}
          onExitComplete={saveAsDialog.release}
          onSubmit={saveAsNewPreset}
        />
      ) : null}
    </>
  );
};

const PresetTab = ({
  isActive,
  isUnsaved,
  onOpenMenu,
  onRequest,
  preset,
  workingArrangement,
}: {
  isActive: boolean;
  isUnsaved: boolean;
  preset: LayoutPreset;
  /** This project's working copy, which switching to the preset lays out and so is what preloading warms. */
  workingArrangement: LayoutPresetSnapshot | undefined;
  onOpenMenu: (target: Omit<PresetMenuTarget, 'ticket'>) => void;
  onRequest: (presetId: LayoutPresetId) => void;
}) => {
  const { t } = useTranslation();
  const icon = resolveLayoutPresetIcon(preset.iconId);
  // An unknown command id resolves to no binding, so a custom preset advertises no key.
  const shortcut = useShortcutBinding(findPresetShortcutCommandId(preset.id) ?? '');
  const keyShortcuts = [isActive ? 'ArrowDown' : null, shortcut?.aria].filter(Boolean).join(' ') || undefined;
  const { attributes, isDragging, listeners, setNodeRef, transform, transition } = useSortable({
    attributes: { role: 'tab', roleDescription: 'sortable', tabIndex: isActive ? 0 : -1 },
    id: preset.id,
  });
  const dndStyle = useMemo(
    () => ({
      opacity: isDragging ? 0.5 : undefined,
      position: 'relative' as const,
      transform: CSS.Translate.toString(transform),
      // Compose fill transitions with dnd-kit's inline transform transition so hover/selection fades remain active.
      transition: [transition, TAB_FILL_TRANSITION].filter(Boolean).join(', '),
      zIndex: isDragging ? 1 : undefined,
    }),
    [isDragging, transform, transition]
  );
  const handlePreload = useCallback(
    () => preloadLayoutPresetWidgets(workingArrangement ? { ...preset, snapshot: workingArrangement } : preset),
    [preset, workingArrangement]
  );
  const unsavedLabel = t('topbar.presets.unsavedLayoutChanges');

  // Use a span chevron recognized by the tab handler; nested buttons are invalid.
  const handleClick = useCallback(
    (event: MouseEvent<HTMLButtonElement>) => {
      const trigger = event.target instanceof Element ? event.target.closest(`[${PRESET_MENU_ATTRIBUTE}]`) : null;

      if (trigger) {
        event.preventDefault();
        event.stopPropagation();
        onOpenMenu({ anchor: trigger.getBoundingClientRect(), preset });
      }
    },
    [onOpenMenu, preset]
  );

  // Activate on primary press for immediate feedback; right-click must preserve the inactive preset's menu actions.
  const handlePointerDown = useCallback(
    (event: PointerEvent<HTMLButtonElement>) => {
      // Chain dnd-kit's activator so a sensor change cannot silently lose drag initiation.
      (listeners as { onPointerDown?: (value: unknown) => void } | undefined)?.onPointerDown?.(event);

      if (event.button === 0 && event.pointerType !== 'touch' && !isActive) {
        onRequest(preset.id);
      }
    },
    [isActive, listeners, onRequest, preset.id]
  );

  const handleContextMenu = useCallback(
    (event: MouseEvent<HTMLButtonElement>) => {
      event.preventDefault();
      onOpenMenu({
        anchor: new DOMRect(event.clientX, event.clientY, 1, 1),
        preset,
      });
    },
    [onOpenMenu, preset]
  );

  const handleKeyDown = useCallback(
    (event: KeyboardEvent<HTMLButtonElement>) => {
      if (isDragging && PRESET_TAB_KEYS_BLOCKED_DURING_DRAG.has(event.key)) {
        event.preventDefault();
      }

      listeners?.onKeyDown?.(event);

      if (event.defaultPrevented) {
        return;
      }

      if (!isActive || event.key !== 'ArrowDown') {
        return;
      }

      event.preventDefault();
      onOpenMenu({ anchor: event.currentTarget.getBoundingClientRect(), preset });
    },
    [isActive, isDragging, listeners, onOpenMenu, preset]
  );

  return (
    <TooltipGroup.Trigger value={preset.id}>
      <Tabs.Trigger
        ref={setNodeRef}
        {...attributes}
        {...listeners}
        aria-label={isUnsaved ? `${preset.label}, ${unsavedLabel}` : preset.label}
        aria-keyshortcuts={keyShortcuts}
        css={PRESET_TAB_CSS}
        cursor={isDragging ? 'grabbing' : 'default'}
        data-layout-preset-id={preset.id}
        gap="1.5"
        style={dndStyle}
        touchAction="pan-x"
        value={preset.id}
        _hover={PRESET_TAB_HOVER_PROPS}
        _selected={PRESET_TAB_SELECTED_PROPS}
        onClick={handleClick}
        onContextMenu={handleContextMenu}
        onFocus={handlePreload}
        onKeyDown={handleKeyDown}
        onPointerDown={handlePointerDown}
        onPointerEnter={handlePreload}
      >
        <Box as="span" display="inline-flex" flexShrink={0} position="relative">
          <Icon as={icon} boxSize="3.5" color={isActive ? 'brand.fg' : undefined} />
          {isUnsaved ? <UnsavedDot /> : null}
        </Box>
        <Text as="span" css={HIDE_BELOW_PRESET_LABEL_WIDTH}>
          {preset.label}
        </Text>
        {isActive ? (
          <Box
            {...{ [PRESET_MENU_ATTRIBUTE]: '' }}
            alignItems="center"
            aria-hidden="true"
            as="span"
            color="fg.subtle"
            display="inline-flex"
            me="-1"
            p="0.5"
            rounded="sm"
            _hover={MENU_AFFORDANCE_HOVER_PROPS}
          >
            <Icon as={ChevronDownIcon} boxSize="3.5" />
          </Box>
        ) : null}
      </Tabs.Trigger>
    </TooltipGroup.Trigger>
  );
};

const MENU_AFFORDANCE_HOVER_PROPS = { bg: 'bg.hover', color: 'fg' } as const;

// A pointed inactive preset takes the shared tinted hover; the active one keeps the widget rail's active fill.
const PRESET_TAB_SURFACE = '--preset-tab-surface';
const PRESET_TAB_CSS = { [PRESET_TAB_SURFACE]: '{colors.bg.subtle}' } as const;
const PRESET_TAB_HOVER_PROPS = {
  '&:not([data-selected])': { bg: 'bg.hover', color: 'fg', [PRESET_TAB_SURFACE]: '{colors.bg.hover}' },
} as const;
const PRESET_TAB_SELECTED_PROPS = {
  bg: 'bg.emphasized',
  color: 'fg',
  [PRESET_TAB_SURFACE]: '{colors.bg.emphasized}',
} as const;
const UNSAVED_DOT_CSS = { boxShadow: `0 0 0 1.5px var(${PRESET_TAB_SURFACE})` } as const;

/** The strip's single tip for the tab it is on: name, shortcut, built-in purpose and unsaved state. */
const PresetTipContent = ({
  hasTip,
  isUnsaved,
  presets,
}: {
  hasTip: (presetId: LayoutPresetId) => boolean;
  isUnsaved: (presetId: LayoutPresetId) => boolean;
  presets: readonly LayoutPreset[];
}) => {
  const { t } = useTranslation();
  const { triggerValue } = useTooltipContext();
  const preset = presets.find(({ id }) => id === triggerValue);

  // Focus moving off an open tip switches the tooltip's tab at once and closes the tip a render later: a tab with no
  // tip of its own must not show in the fading one.
  if (!preset || !hasTip(preset.id)) {
    return null;
  }

  const shortcutCommandId = findPresetShortcutCommandId(preset.id);

  return (
    <Stack gap="0.5" maxW="16rem">
      <HStack gap="1.5" justify="space-between">
        <Text as="span" fontWeight="600">
          {preset.label}
        </Text>
        {shortcutCommandId ? <PresetShortcutKeys commandId={shortcutCommandId} /> : null}
      </HStack>
      {/* A renamed built-in may have been turned to another purpose; its shipped description could contradict it. */}
      {isBuiltInLayoutPresetId(preset.id) && preset.labelKey !== undefined ? (
        <Text as="span" color="fg.muted">
          {t(BUILT_IN_PRESET_DESCRIPTION_KEYS[preset.id])}
        </Text>
      ) : null}
      {isUnsaved(preset.id) ? (
        <HStack as="span" gap="1.5">
          <Box aria-hidden="true" bg="accent.solid" boxSize="1.5" flexShrink={0} rounded="full" />
          <Text as="span">{t('topbar.presets.unsavedLayoutChanges')}</Text>
        </HStack>
      ) : null}
    </Stack>
  );
};

/** Reads the effective binding, so a remapped or unbound shortcut is never advertised. */
const PresetShortcutKeys = ({ commandId }: { commandId: string }) => {
  const binding = useShortcutBinding(commandId);

  return binding ? (
    <Kbd variant="subtle">
      <TopbarShortcutKeys parts={binding.parts} />
    </Kbd>
  ) : null;
};

/** Over the icon's upper-right corner; the tab's accessible name and tooltip carry the state, not the colour. */
const UnsavedDot = () => (
  <Box
    aria-hidden="true"
    bg="accent.solid"
    boxSize="1.5"
    css={UNSAVED_DOT_CSS}
    data-unsaved-dot=""
    insetEnd="-0.5"
    position="absolute"
    rounded="full"
    top="-0.5"
  />
);

interface PresetMenuTarget {
  anchor: DOMRect;
  preset: LayoutPreset;
  /** Remounts the menu per open so a repeated anchor still gets a fresh machine. */
  ticket: number;
}

/** Mounted only while open. */
const PresetMenu = ({
  isActive,
  isUnsaved,
  onApply,
  onClose,
  onDelete,
  onEdit,
  target,
}: {
  isActive: boolean;
  isUnsaved: boolean;
  target: PresetMenuTarget;
  onApply: (preset: LayoutPreset) => void;
  onClose: () => void;
  onDelete: (preset: LayoutPreset) => void;
  onEdit: (preset: LayoutPreset) => void;
}) => {
  const { t } = useTranslation();
  const { layout } = useWorkbenchCommands();
  const saveShortcut = useShortcutLabel('app.saveLayoutPreset');
  const { preset } = target;
  const isCustom = preset.isBuiltIn !== true;

  const apply = useCallback(() => onApply(preset), [onApply, preset]);
  const revert = useCallback(() => layout.revertPreset(preset.id), [layout, preset.id]);
  const save = useCallback(() => layout.savePreset(preset.id), [layout, preset.id]);
  const edit = useCallback(() => {
    onClose();
    onEdit(preset);
  }, [onClose, onEdit, preset]);
  const remove = useCallback(() => {
    onClose();
    onDelete(preset);
  }, [onClose, onDelete, preset]);
  const manage = useCallback(() => {
    onClose();
    openLayoutPresetManager();
  }, [onClose]);
  const handleOpenChange = useCallback(
    (event: { open: boolean }) => {
      if (!event.open) {
        onClose();
      }
    },
    [onClose]
  );

  // Anchored to a measured rect rather than a trigger element: the chevron lives inside the tab button and cannot be a
  // `Menu.Trigger` of its own without nesting buttons, and a right-click anchors at the pointer.
  const { anchor } = target;
  const positioning = useMemo(
    () => ({
      getAnchorRect: () => ({ height: anchor.height, width: anchor.width, x: anchor.x, y: anchor.y }),
      placement: 'bottom-end' as const,
    }),
    [anchor]
  );

  return (
    <Menu.Root open positioning={positioning} onOpenChange={handleOpenChange}>
      <Portal>
        <Menu.Positioner>
          <MenuContent minW="16rem">
            <MenuHeadingGroup label={preset.label} trailing={isUnsaved ? t('topbar.presets.unsaved') : null}>
              {isActive ? null : (
                <Menu.Item value="apply-preset" onClick={apply}>
                  <Icon as={ArrowRightIcon} boxSize="3.5" />
                  <Menu.ItemText>{t('topbar.presets.switch')}</Menu.ItemText>
                </Menu.Item>
              )}

              {/* An inactive preset with a working copy here saves or reverts without switching to it. */}
              {isActive || isUnsaved ? (
                <>
                  {isUnsaved ? (
                    <Menu.Item value="revert-layout" onClick={revert}>
                      <Icon as={RotateCcwIcon} boxSize="3.5" />
                      <Menu.ItemText>{t('topbar.presets.revert')}</Menu.ItemText>
                    </Menu.Item>
                  ) : null}
                  <Menu.Item value="save-layout" onClick={save}>
                    <Icon as={SaveIcon} boxSize="3.5" />
                    <Menu.ItemText>{t('topbar.presets.saveChanges')}</Menu.ItemText>
                    {isActive && saveShortcut ? (
                      <Text color="fg.subtle" fontSize="xs" ms="auto">
                        {saveShortcut}
                      </Text>
                    ) : null}
                  </Menu.Item>
                </>
              ) : null}

              <Menu.Item value="edit-preset" onClick={edit}>
                <Icon as={PencilIcon} boxSize="3.5" />
                <Menu.ItemText>{t('topbar.presets.editWithEllipsis')}</Menu.ItemText>
              </Menu.Item>
              {isCustom ? (
                <Menu.Item data-danger="" value="delete-preset" onClick={remove}>
                  <Icon as={Trash2Icon} boxSize="3.5" />
                  <Menu.ItemText>{t('topbar.presets.deleteWithEllipsis')}</Menu.ItemText>
                </Menu.Item>
              ) : null}
            </MenuHeadingGroup>

            <Menu.Separator />
            <Menu.Item value="manage-presets" onClick={manage}>
              <Icon as={SettingsIcon} boxSize="3.5" />
              <Menu.ItemText>{t('topbar.presets.manage')}</Menu.ItemText>
            </Menu.Item>
          </MenuContent>
        </Menu.Positioner>
      </Portal>
    </Menu.Root>
  );
};