Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

6.6 KB
Raw Blame
import type { Permissions } from '@core/component/SharePermissions';
import type { HotkeyToken } from '@core/hotkey/tokens';
import { isTouchDevice } from '@core/mobile/isTouchDevice';
import type { EntityData } from '@entity';
import type { ItemType } from '@service-storage/client';
import { Button, ButtonGroup, cn } from '@ui';
import { type Component, For, type JSX, Show } from 'solid-js';
import { Dynamic } from 'solid-js/web';
import { HeaderActionGroup } from './HeaderActionGroup';
import { SidePanel } from './side-panel';
import { HeaderIsland } from './split-layout/components/HeaderIsland';
import {
  BlockSplitFileMenu,
  type FileOperation,
} from './split-layout/components/SplitFileMenu';
import {
  SplitHeaderLeft,
  SplitHeaderRight,
} from './split-layout/components/SplitHeader';
import {
  SplitPermissionsBadge,
  SplitTitleFileMenu,
} from './split-layout/components/SplitLabel';
import type {
  SplitFileMenuAction,
  SplitFileMenuActionGroup,
} from './split-layout/context';

export type BlockTool = {
  label: string | (() => string);
  icon: Component;
  action: () => void;
  children?: SplitFileMenuAction[];
  condition?: () => boolean;
  isActive?: () => boolean;
  buttonComponent?: Component<{ grouped?: boolean }>;
  focusTarget?: () => HTMLElement | null;
  hotkeyToken?: HotkeyToken;
  /** Menu section this tool renders in when shown in the title file menu. */
  group?: SplitFileMenuActionGroup;
};

export function ToolButton(props: { tool: BlockTool; grouped?: boolean }) {
  const label = () =>
    typeof props.tool.label === 'function'
      ? props.tool.label()
      : props.tool.label;

  const Control = props.grouped ? ButtonGroup.Button : Button;
  return (
    <Control
      variant="ghost"
      aria-label={label()}
      tooltip={{ content: label(), hotkey: props.tool.hotkeyToken }}
      onClick={props.tool.action}
      class={cn(
        'px-1',
        props.tool.isActive?.() && 'bg-accent/20 hover:bg-accent/30 text-accent'
      )}
      size={isTouchDevice() || props.grouped ? 'icon-sm' : 'icon-md'}
    >
      <Dynamic
        component={
          props.tool.icon as Component<JSX.SvgSVGAttributes<SVGSVGElement>>
        }
        class="size-4 shrink-0 touch:size-3.5"
      />
    </Control>
  );
}

function getToolLabel(tool: BlockTool) {
  return typeof tool.label === 'function' ? tool.label() : tool.label;
}

export function ResponsivePermissionsBadge() {
  return (
    <Show
      when={isTouchDevice()}
      fallback={
        <SplitHeaderRight>
          <SplitPermissionsBadge />
        </SplitHeaderRight>
      }
    >
      <SplitHeaderLeft>
        <HeaderIsland>
          <SplitPermissionsBadge />
        </HeaderIsland>
      </SplitHeaderLeft>
    </Show>
  );
}

interface BlockToolbarProps {
  tools: BlockTool[];
  menuTools?: BlockTool[];
  ops: FileOperation[];
  id: string;
  itemType: ItemType;
  name: string;
  formattedName?: string;
  /**
   * Full entity for the title menu's entity-gated items. Supply it when the
   * block can build one that generic chrome can't reconstruct from
   * id/name/blockName alone (e.g. calls need their channelId).
   */
  entity?: EntityData;
  /** Feature-owned access when the session loads outside legacy Block state. */
  permissions?: Permissions;
}

/**
 * Handles the standard arrangement of file ops and block tools on desktop and mobile. On mobile, they are condensed together into a dropdown menu in the SplitHeader.
 */
export function ResponsiveBlockToolbar(props: BlockToolbarProps) {
  const isShareTool = (tool: BlockTool) => getToolLabel(tool) === 'Share';
  const isHiddenTool = (tool: BlockTool) => {
    const label = getToolLabel(tool);
    return (
      label === 'Chat' ||
      label === 'Dispatch to Agent' ||
      label === 'References'
    );
  };
  const visibleTools = () => props.tools.filter((tool) => !isHiddenTool(tool));
  const headerTools = () => visibleTools().filter(isShareTool);
  const toolbarTools = () =>
    visibleTools().filter((tool) => !isShareTool(tool));
  const activeToolbarTools = () =>
    toolbarTools().filter((tool) => !tool.condition || tool.condition());
  const fileMenuTools = () => {
    if (!props.menuTools) return visibleTools();

    const menuToolLabels = new Set(props.menuTools.map(getToolLabel));
    const missingShareTools = visibleTools().filter(
      (tool) => isShareTool(tool) && !menuToolLabels.has(getToolLabel(tool))
    );

    return [...props.menuTools, ...missingShareTools];
  };

  return (
    <Show
      when={isTouchDevice()}
      fallback={
        <>
          <SplitHeaderRight>
            <div class="order-[1000] flex items-center gap-2">
              <Show when={activeToolbarTools().length > 0}>
                <HeaderActionGroup label="Document actions">
                  <For each={activeToolbarTools()}>
                    {(tool) =>
                      tool.buttonComponent ? (
                        <tool.buttonComponent grouped />
                      ) : (
                        <ToolButton tool={tool} grouped />
                      )
                    }
                  </For>
                </HeaderActionGroup>
              </Show>
              <SidePanel.HeaderActionsOutlet />
              <For each={headerTools()}>
                {(tool) => (
                  <Show when={!tool.condition || tool.condition()}>
                    {tool.buttonComponent ? (
                      <tool.buttonComponent />
                    ) : (
                      <ToolButton tool={tool} />
                    )}
                  </Show>
                )}
              </For>
            </div>
          </SplitHeaderRight>
          <SplitTitleFileMenu>
            <BlockSplitFileMenu
              id={props.id}
              itemType={props.itemType}
              name={props.name}
              formattedName={props.formattedName}
              ops={props.ops}
              tools={fileMenuTools()}
              entity={props.entity}
              permissions={props.permissions}
              buttonClass="order-first"
            />
          </SplitTitleFileMenu>
        </>
      }
    >
      <SplitHeaderRight>
        <HeaderIsland class="has-[[data-side-panel-header-actions]:empty]:hidden">
          <SidePanel.HeaderActionsOutlet />
        </HeaderIsland>
      </SplitHeaderRight>
      <SplitTitleFileMenu>
        <BlockSplitFileMenu
          id={props.id}
          itemType={props.itemType}
          name={props.name}
          formattedName={props.formattedName}
          ops={props.ops}
          tools={fileMenuTools()}
          entity={props.entity}
          permissions={props.permissions}
          buttonClass="order-last"
        />
      </SplitTitleFileMenu>
    </Show>
  );
}