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.

3.3 KB
Raw Blame
import { useSplitPanel } from '@components/app/split-layout/layoutUtils';
import { type BlockName, useBlockId, useBlockName } from '@core/block';
import { useHotkeyDOMScope } from '@core/hotkey/hotkeys';
import { useEntitySubscription } from '@service-connection/client';
import {
  children,
  createEffect,
  type FlowProps,
  onCleanup,
  onMount,
} from 'solid-js';
import { match } from 'ts-pattern';
import { blockLiveTrackingEnabledSignal } from '../internal/BlockLoader';
import {
  blockContainerMountedSignal,
  blockElementSignal,
  blockHotkeyScopeSignal,
} from '../signal/blockElement';

// TODO: handle nested state
const getBlockElementId = (blockId: string) => `block-${blockId}`;

function resolveEntityType(blockName: BlockName) {
  return match(blockName)
    .with(
      'chat',
      'channel',
      'project',
      'database',
      'form',
      (entityType) => entityType
    )
    .otherwise(() => 'document' as const);
}

interface BlockContainerProps extends FlowProps {
  title?: string;
}

/** @deprecated Use DocumentBlockContainer instead, it handles loading state and all kinds of great things!
 * @see DocumentBlockContainer
 * For internal use only.
 */
export function BlockContainer(props: BlockContainerProps) {
  const setElement = blockElementSignal.set;
  const setMounted = blockContainerMountedSignal.set;
  const liveTrackingEnabled = blockLiveTrackingEnabledSignal.get;
  const setHotkeyScope = blockHotkeyScopeSignal.set;
  const blockId = useBlockId();
  const blockName = useBlockName();
  useEntitySubscription(() =>
    liveTrackingEnabled() && blockId && blockName
      ? { entity_type: resolveEntityType(blockName), entity_id: blockId }
      : undefined
  );

  onMount(() => {
    setMounted(true);
  });

  onCleanup(() => {
    setMounted(false);
  });

  // Block commands register on the split scope: it covers the whole panel
  // (header, toolbar, the focusable panel div), so block hotkeys keep working
  // while focus sits on split chrome outside the block element, and it stays
  // active across in-split navigation. The block gets no DOM scope of its
  // own. Registrations must
  // dispose with their owner (registerHotkey does this automatically) since
  // the split scope survives split navigation.
  //
  // Only a block rendered without a split panel gets its own DOM scope,
  // attached to the block element — a scope that is never attached can never
  // activate, so commands registered to it would silently never run.
  const splitPanel = useSplitPanel();
  let attachFallbackHotkeyScope: ((el: Element) => void) | undefined;
  if (splitPanel) {
    setHotkeyScope(splitPanel.splitHotkeyScope);
  } else {
    const [attachHotkeys, scopeId] = useHotkeyDOMScope(blockName ?? 'block');
    attachFallbackHotkeyScope = attachHotkeys;
    setHotkeyScope(scopeId);
  }

  const resolved = children(() => props.children);
  createEffect(() => {
    const resolved_ = resolved();
    if (!(resolved_ instanceof HTMLElement)) {
      console.error('BlockContainer must be used with a single HTMLElement');
      return;
    }
    resolved_.id = getBlockElementId(blockId);
    resolved_.dataset.blockType = blockName;
    setElement(resolved_);
    attachFallbackHotkeyScope?.(resolved_);
  });

  return (
    <div class="relative size-full portal-scope">
      <div class="overflow-hidden size-full">{resolved()}</div>
    </div>
  );
}