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.

1.6 KB
Raw Blame
import {
  type Accessor,
  createContext,
  createMemo,
  createSignal,
  type FlowComponent,
  useContext,
} from 'solid-js';
import { useMarkdownDocument } from '../context/markdown-document-context';

type MarkdownNameContextValue = {
  persistedName: Accessor<string | undefined>;
  editorName: Accessor<string | undefined>;
  displayName: Accessor<string | undefined>;
  setOptimisticName: (name: string | undefined) => void;
};

const MarkdownNameContext = createContext<MarkdownNameContextValue>();

export const MarkdownNameProvider: FlowComponent = (props) => {
  // Keep the editable title and the label separate. An empty persisted
  // title is a real editor value so TitleEditor can stay empty and show its
  // placeholder, but surrounding UI should still display the block fallback
  // such as "New Document" or "New Task".
  const { persistedName, fallbackName } = useMarkdownDocument();
  const [optimisticName, setOptimisticName] = createSignal<
    string | undefined
  >();
  const editorName = createMemo(() => optimisticName() ?? persistedName());
  const displayName = createMemo(() => {
    const optimistic = optimisticName();
    if (optimistic !== undefined) return optimistic || fallbackName();
    return persistedName() || fallbackName();
  });

  return (
    <MarkdownNameContext.Provider
      value={{ persistedName, editorName, displayName, setOptimisticName }}
    >
      {props.children}
    </MarkdownNameContext.Provider>
  );
};

export function useMarkdownName() {
  const context = useContext(MarkdownNameContext);
  if (!context) {
    throw new Error('MarkdownNameProvider is required');
  }

  return context;
}