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.

5.0 KB
Raw Blame
import type { CategoryFilter } from '@app/features/command';
import { CommandMenuInner, CommandState } from '@app/features/command';
import { createSoupState } from '@app/features/next-soup/create-soup-state';
import { IS_MAC } from '@core/constant/isMac';
import { createFreshSearch } from '@core/util/freshSort';
import { Dialog } from '@kobalte/core/dialog';
import {
  createEffect,
  createMemo,
  createSignal,
  onCleanup,
  onMount,
} from 'solid-js';
import { MockAppChrome } from '../components/MockAppChrome';
import { HotkeyCallout } from '../components-lib';
import { OnboardingEntityList } from '../OnboardingEntityList';
import {
  filteredSandboxEntities,
  sandboxToCommandItems,
} from '../sandbox/sandbox-store';
import type { LessonContentProps, LessonDefinition } from '../types';

/** Module-level signal toggled by the onboarding shell's cmd+k handler. */
export const [commandKOpen, setCommandKOpen] = createSignal(false);

/** Shared completion state between content and demo panels. */
const [completed, setCompleted] = createSignal(false);

/** Map category filter to the bucket values used by sandbox items. */
const CATEGORY_TO_BUCKETS: Record<CategoryFilter, string[] | null> = {
  all: null, // no filter
  channels: ['channel'],
  dms: ['dm'],
  documents: ['note', 'document', 'snippet'],
  tasks: ['task'],
  chats: ['chat'],
  projects: ['project'],
  commands: [], // sandbox has no commands — show nothing
  people: [], // sandbox has no people — show nothing
};

function CommandKContent(_props: LessonContentProps) {
  return (
    <div class="flex flex-col gap-8 onboarding-stagger">
      <p class="text-ink-muted mt-2">
        The Command Menu allows you to search for documents, tasks, channels,
        and more — and navigate to them instantly.
      </p>
      <div class="flex flex-col gap-3">
        <HotkeyCallout
          keys={[IS_MAC ? '⌘' : 'Ctrl', 'K']}
          separator="+"
          label=""
          completed={completed()}
        />
      </div>
    </div>
  );
}

function CommandKDemo(props: LessonContentProps) {
  const [hasOpened, setHasOpened] = createSignal(false);

  onMount(() => {
    CommandState.forceReset();
  });

  // Complete the lesson the first time the command menu closes after being opened.
  createEffect(() => {
    const open = commandKOpen();
    if (open) {
      setHasOpened(true);
    } else if (hasOpened() && !completed()) {
      setCompleted(true);
      props.onComplete();
    }
  });

  onCleanup(() => {
    CommandState.forceReset();
    setCommandKOpen(false);
    setCompleted(false);
  });

  const allItems = () => sandboxToCommandItems();

  const search = createMemo(() => {
    const q = CommandState.query();
    const hasQuery = q.trim().length > 0;
    return createFreshSearch({
      config: {
        useViewedAt: true,
        fuzzyWeight: hasQuery ? 0.7 : 0.0,
        timeWeight: hasQuery ? 0.3 : 0.9,
        minFuzzyThreshold: hasQuery ? 0.1 : 0,
      },
      getName: (item: ReturnType<typeof sandboxToCommandItems>[number]) =>
        item.searchText,
      getTimestamp: (item: ReturnType<typeof sandboxToCommandItems>[number]) =>
        item.timestamps,
    });
  });

  const filteredItems = createMemo(() => {
    let items = allItems();

    // Filter by category
    const category = CommandState.categoryFilter();
    const allowedBuckets = CATEGORY_TO_BUCKETS[category];
    if (allowedBuckets !== null) {
      items = items.filter((item) => allowedBuckets.includes(item.bucket));
    }

    // Filter by query
    const q = CommandState.query();
    if (q.trim()) {
      return search()(items, q).map((result) => result.item);
    }

    return items;
  });

  let contentEl: HTMLDivElement | undefined;

  const soup = createSoupState({ wrapNavigation: true });

  createEffect(() => {
    soup.setRows(
      filteredSandboxEntities().map((e, i) =>
        soup.buildRow({ id: e.id, index: i, original: e })
      )
    );
  });

  return (
    <>
      {/* Entity list visible behind the modal */}
      <MockAppChrome scopeId={props.scopeId}>
        <OnboardingEntityList soup={soup} />
      </MockAppChrome>

      <Dialog open={commandKOpen()} onOpenChange={setCommandKOpen}>
        <Dialog.Portal>
          <Dialog.Overlay class="z-modal fixed inset-0 scrim-glass" />
          <div class="z-modal fixed inset-0 flex items-start justify-center pt-[15vh]">
            <Dialog.Content
              ref={contentEl}
              class="elevated-surface max-w-[calc(100vw-16px)] overflow-hidden portal-scope"
              style={{ width: '800px' }}
            >
              <CommandMenuInner
                items={filteredItems}
                disableDefaultAction
                onSelect={() => {
                  setCompleted(true);
                  props.onComplete();
                }}
              />
            </Dialog.Content>
          </div>
        </Dialog.Portal>
      </Dialog>
    </>
  );
}

export const commandKLesson: LessonDefinition = {
  id: 'command-k',
  title: 'Command Menu',
  content: CommandKContent,
  demo: CommandKDemo,
  order: 45,
};