All files / webview-app/src/components/Canvas MenuBarZone.tsx

4.16% Statements 1/24
0% Branches 0/22
6.66% Functions 1/15
4.34% Lines 1/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158                                                        8x                                                                                                                                                                                                                                                                  
import type { Dispatch, SetStateAction } from "react";
 
import { FIXED_ZONE_PADDING } from "@/components/Canvas/constants";
import { getOrderedChildren } from "@/components/Canvas/fixedZoneHelpers";
import type { MenuBarLayoutItem } from "@/components/Canvas/fixedZoneLayout";
import type { CanvasComponent as CanvasComponentModel } from "@/types/canvas";
 
interface MenuBarZoneProps {
  menuBarLayout: MenuBarLayoutItem[];
  componentsById: Map<string, CanvasComponentModel>;
  components: CanvasComponentModel[];
  selectedComponentId: string | null;
  expandedMenuId: string | null;
  setExpandedMenuId: Dispatch<SetStateAction<string | null>>;
  onSelectComponent: (id: string | null) => void;
  getComponentLabel: (component: CanvasComponentModel, fallback: string) => string;
}
 
export function MenuBarZone({
  menuBarLayout,
  componentsById,
  components,
  selectedComponentId,
  expandedMenuId,
  setExpandedMenuId,
  onSelectComponent,
  getComponentLabel,
}: MenuBarZoneProps) {
  return menuBarLayout.map(({ menuBar, top, height }) => {
    const menuChildren = getOrderedChildren(menuBar, componentsById, components).filter(
      (child) => child.type === "Menu",
    );
    const menuEntries =
      menuChildren.length > 0
        ? menuChildren.map((menu) => {
            const menuItems = getOrderedChildren(menu, componentsById, components).filter(
              (child) => child.type === "MenuItem" || child.type === "Menu",
            );
 
            return {
              id: menu.id,
              componentId: menu.id,
              label: getComponentLabel(menu, "Menu"),
              items:
                menuItems.length > 0
                  ? menuItems.map((item) => ({
                      id: item.id,
                      componentId: item.id,
                      label: getComponentLabel(
                        item,
                        item.type === "MenuItem" ? "Menu Item" : "Menu",
                      ),
                      isSubmenu: item.type === "Menu",
                    }))
                  : [
                      {
                        id: `${menu.id}-preview-item`,
                        componentId: undefined,
                        label: "Menu Item",
                        isSubmenu: false,
                      },
                    ],
            };
          })
        : [
            {
              id: menuBar.id,
              componentId: menuBar.id,
              label: getComponentLabel(menuBar, "Menu"),
              items: [
                {
                  id: `${menuBar.id}-preview-item`,
                  componentId: undefined,
                  label: "Menu Item",
                  isSubmenu: false,
                },
              ],
            },
          ];
 
    return (
      <div
        key={menuBar.id}
        data-canvas-fixed="true"
        className={`absolute z-20 rounded border border-vscode-panel-border bg-vscode-panel-background/95 text-[11px] text-vscode-foreground shadow ${
          selectedComponentId === menuBar.id ? "ring-1 ring-(--canvas-selection)" : ""
        }`}
        style={{ top, left: FIXED_ZONE_PADDING, right: FIXED_ZONE_PADDING, height }}
        onPointerDown={(event) => {
          event.stopPropagation();
        }}
        onClick={(event) => {
          event.stopPropagation();
          onSelectComponent(menuBar.id);
        }}
      >
        <div className="flex h-full items-center gap-1 px-1">
          {menuEntries.map((menu) => {
            const isExpanded = expandedMenuId === menu.id;
 
            return (
              <div key={menu.id} className="relative">
                <button
                  type="button"
                  data-canvas-fixed="true"
                  className={`rounded px-2 py-1 text-[11px] ${
                    isExpanded ? "bg-accent text-accent-foreground" : "hover:bg-accent/70"
                  }`}
                  onPointerDown={(event) => {
                    event.stopPropagation();
                  }}
                  onClick={(event) => {
                    event.stopPropagation();
                    onSelectComponent(menu.componentId);
                    setExpandedMenuId((current) => (current === menu.id ? null : menu.id));
                  }}
                >
                  {menu.label}
                </button>
 
                {isExpanded ? (
                  <div className="absolute left-0 top-[calc(100%+2px)] min-w-40 rounded border border-vscode-panel-border bg-vscode-panel-background py-1 shadow-lg">
                    {menu.items.map((item) => (
                      <button
                        key={item.id}
                        type="button"
                        data-canvas-fixed="true"
                        disabled={!item.componentId}
                        className={`flex w-full items-center justify-between gap-2 px-3 py-1 text-left text-[11px] ${
                          item.componentId
                            ? "hover:bg-accent/70"
                            : "cursor-default text-muted-foreground"
                        }`}
                        onPointerDown={(event) => {
                          event.stopPropagation();
                        }}
                        onClick={(event) => {
                          event.stopPropagation();
                          if (item.componentId) {
                            onSelectComponent(item.componentId);
                          }
                        }}
                      >
                        <span>{item.label}</span>
                        {item.isSubmenu ? <span className="text-muted-foreground">▶</span> : null}
                      </button>
                    ))}
                  </div>
                ) : null}
              </div>
            );
          })}
        </div>
      </div>
    );
  });
}