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.

2.5 KB
Raw Blame
import { useAnalytics } from '@app/lib/analytics/analytics-context';
import { isRecord, type PaneId, useSplitRouter } from '@app/lib/split-router';
import { useIsAuthenticated } from '@core/auth';
import { LoadingBlock } from '@core/component/LoadingBlock';
import { isTouchDevice } from '@core/mobile/isTouchDevice';
import {
  type Component,
  type JSX,
  Match,
  onMount,
  Show,
  Switch,
} from 'solid-js';
import type { SplitContent } from '../layoutManager';
import { useSplitPanelOrThrow } from '../layoutUtils';

export function usePageViewTracking(pageTitle: string) {
  const analytics = useAnalytics();
  onMount(() => {
    analytics.pageView(pageTitle);
    analytics.track('open_view', { viewId: pageTitle });
  });
}

/** Renders `View` once signed in; `Pending`, if given, draws while sign-in is confirmed. */
export function withAuth<P extends object>(
  View: Component<P>,
  Pending?: Component
): Component<P> {
  return (props) => {
    const authenticated = useIsAuthenticated();
    return (
      <Switch fallback={<LoadingBlock />}>
        <Match when={authenticated()}>
          <View {...props} />
        </Match>
        <Match when={Pending && authenticated() === undefined}>
          {Pending && <Pending />}
        </Match>
      </Switch>
    );
  };
}

export function RedirectSplit(props: {
  to: SplitContent;
  mergeHistory?: boolean;
}) {
  const panel = useSplitPanelOrThrow();
  const router = useSplitRouter();
  onMount(() => {
    const metadata = isRecord(props.to.entryMetadata)
      ? props.to.entryMetadata
      : {};
    const pane = panel.handle.id as string as PaneId;
    const savedSearch = isRecord(metadata.search) ? metadata.search : {};
    panel.handle.replace({
      mergeHistory: props.mergeHistory,
      next: {
        ...props.to,
        entryMetadata: {
          ...metadata,
          search: {
            ...router.entry(pane)?.location.search,
            ...savedSearch,
          },
        },
      },
    });
  });
  return null;
}

/** App-only page tracking and detail gating; feature views stay route-agnostic. */
export function AppView(props: {
  id: string;
  children: JSX.Element;
  detailRequested?: () => boolean;
  detailFallback?: JSX.Element;
  detailDesktopOnly?: boolean;
}) {
  usePageViewTracking(props.id);
  const detailUnsupported = () =>
    Boolean(
      props.detailRequested?.() && props.detailDesktopOnly && isTouchDevice()
    );
  return (
    <Show when={!detailUnsupported()} fallback={props.detailFallback}>
      {props.children}
    </Show>
  );
}