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.

15.0 KB
Raw Blame
import { getCallExitPath } from '@channel/Call/call-exit';
import { HeaderIsland } from '@components/app/split-layout/components/HeaderIsland';
import type { BackgroundEffect } from '@core/media/background-effect';
import { isMobile } from '@core/mobile/isMobile';
import { fireAndForget } from '@core/util/fireAndForget';
import ArrowLeft from '@icon/system/regular/arrow-left.svg';
import Phone from '@icon/system/regular/phone-call.svg';
import { Telemetry } from '@macro-inc/observability';
import { Button, Input } from '@ui';
import {
  type Accessor,
  createEffect,
  createSignal,
  type JSX,
  Match,
  on,
  onCleanup,
  Show,
  Switch,
} from 'solid-js';
import { MeetingCallHeading } from '../components/meeting-call-heading';
import { MeetingCopyButton } from '../components/meeting-copy-button';
import {
  MeetingParticipants,
  type MeetingParticipantsState,
} from '../components/meeting-participants';
import type {
  MeetingCredentials,
  MeetingPageState,
  MeetingSessionCapabilities,
} from '../context/meeting-session';
import { useMeetingSessionOwner } from '../context/meeting-session-owner';
import {
  createMeetingMedia,
  type MeetingMediaAccess,
} from '../primitives/meeting-media';
import { MeetingMediaSetup } from './meeting-media-setup';

export function MeetingPage(props: {
  source: Accessor<MeetingPageState>;
  session: MeetingSessionCapabilities;
  mediaAccess?: MeetingMediaAccess;
  initialBackground?: BackgroundEffect;
  readBackgroundImage?: (file: File) => Promise<string>;
  authenticated: Accessor<boolean | undefined>;
  author: Accessor<string>;
  avatar?: JSX.Element;
  participants?: MeetingParticipantsState;
  startCall?: boolean;
  url?: string;
  onCopy: () => Promise<void>;
  onRename?: (title: string) => Promise<void>;
  /** The host keeps the URL in sync without replacing this session owner. */
  onCallStateChange?: (connected: boolean) => void;
  /** Explicit hangup, distinct from losing the connection or cancelling setup. */
  onLeave?: (path: string) => void;
  /** Dismiss the route without leaving a connected call. */
  onBack?: () => void;
  /** Supplied only for the creator's New Call setup flow. */
  renderInvite?: (joining: Accessor<boolean>) => JSX.Element;
  /** Login redirect for channel-linked calls, which are members-only. */
  onSignIn?: () => void;
  renderCall: (
    onLeave: () => void,
    name: Accessor<string>,
    credentials: Accessor<MeetingCredentials | undefined>,
    copyButton: JSX.Element
  ) => JSX.Element;
}) {
  const owner = useMeetingSessionOwner();
  const owned = owner.acquire(props.session);
  const session = owned.session;
  onCleanup(owned.detach);
  const [name, setName] = createSignal('');
  const [leaving, setLeaving] = createSignal(false);
  let disposed = false;
  onCleanup(() => {
    disposed = true;
  });
  const media = createMeetingMedia(props.mediaAccess, props.initialBackground);
  const [uploadingBackground, setUploadingBackground] = createSignal(false);
  const ready = () => {
    const state = props.source();
    return state.kind === 'ready' ? state : undefined;
  };
  /** Guests cannot join channel-linked meetings; they must sign in first. */
  const membersOnly = () =>
    ready()?.channelId != null && props.authenticated() !== true;
  const displayName = () =>
    props.authenticated()
      ? props.author()
      : (session.joinedName() ?? name().trim());
  const inCall = () =>
    session.joinedCallId() !== undefined &&
    props.session.activeCallId() === session.joinedCallId() &&
    props.session.isInCall();

  createEffect(
    on(inCall, (connected) => {
      if (!leaving()) props.onCallStateChange?.(connected);
    })
  );

  async function warmup() {
    try {
      await props.session.warmup?.();
    } catch (error) {
      // The shared loader clears a failed import so Join can retry it.
      console.warn('[meeting] call runtime preload failed', error);
    }
  }

  createEffect(
    on(
      () =>
        Boolean(ready()) &&
        !membersOnly() &&
        !inCall() &&
        !session.joining() &&
        !leaving(),
      (setup) => {
        if (setup) {
          fireAndForget(media.prepare(), 'meetings');
          fireAndForget(warmup(), 'meetings');
        } else media.release();
      }
    )
  );
  const join = () => {
    if (membersOnly() || leaving() || media.pending() || uploadingBackground())
      return;
    // The call publishes the preview tracks rather than re-opening the
    // devices, so the waiting room is the only place permission is asked.
    return session.join(
      props.authenticated() ? undefined : name(),
      {
        microphoneEnabled: media.microphoneEnabled(),
        cameraEnabled: media.cameraEnabled(),
        microphoneDeviceId: media.selectedDevices().microphone,
        cameraDeviceId: media.selectedDevices().camera,
        speakerDeviceId: media.selectedDevices().speaker,
        backgroundEffect: media.backgroundEffect(),
        localTracks: media.handoff(),
      },
      media.diagnostics()
    );
  };
  async function leave() {
    if (leaving()) return;
    const callId = session.joinedCallId();
    const channelId = ready()?.channelId;
    const scheduledStart = ready()?.scheduledStart;
    const authenticated = props.authenticated() === true;
    setLeaving(true);
    await Telemetry.span('meeting.leave', async (span) => {
      if (callId) span.setAttr('call.id', callId);
      const result = await session.leave();
      if (disposed) return;
      const path = getCallExitPath({
        authenticated,
        callId,
        channelId,
        scheduledStart,
        callEnded: result?.callEnded,
      });
      span.setAttr('call.leave.ended', result?.callEnded ?? false);
      span.setAttr('call.leave.destination', path);
      props.onLeave?.(path);
    });
  }

  return (
    <main
      class="ph-no-capture relative flex h-dvh min-h-0 flex-col bg-surface p-4 text-ink sm:p-6"
      classList={{ 'mobile:p-0': inCall() }}
      style={
        isMobile() && inCall()
          ? {
              '--mobile-content-inset-top': 'calc(var(--safe-top, 0px) + 72px)',
            }
          : undefined
      }
    >
      <Show when={isMobile() && inCall()}>
        <header class="absolute inset-x-0 top-[calc(var(--safe-top,0px)+12px)] z-30 flex items-center justify-between px-(--mobile-chrome-gutter)">
          <HeaderIsland class="h-11 px-1">
            <Button
              variant="ghost"
              size="icon-lg"
              aria-label="Back"
              onClick={props.onBack}
            >
              <ArrowLeft class="size-6" />
            </Button>
          </HeaderIsland>
          <div class="flex items-center gap-2">
            <Show when={props.url}>
              {(url) => <MeetingCopyButton url={url()} onCopy={props.onCopy} />}
            </Show>
          </div>
        </header>
      </Show>
      <Show when={!(isMobile() && inCall())}>
        <header class="flex flex-wrap items-center gap-4 pb-4">
          <Show
            when={!inCall() && props.onBack}
            fallback={
              <Show when={!inCall()}>
                <a
                  href="/app"
                  class="flex items-center gap-2 rounded-lg px-3 py-2 text-sm text-ink-muted hover:bg-hover focus-visible:outline-2 focus-visible:outline-accent"
                >
                  <ArrowLeft class="size-4" />
                  Back to Macro
                </a>
              </Show>
            }
          >
            <button
              type="button"
              onClick={props.onBack}
              class="flex items-center gap-2 rounded-lg px-3 py-2 text-sm text-ink-muted hover:bg-hover focus-visible:outline-2 focus-visible:outline-accent"
            >
              <ArrowLeft class="size-4" />
              Back to Macro
            </button>
          </Show>
          <Show
            when={inCall()}
            fallback={
              <div class="flex items-center gap-3">
                <Phone class="size-6 text-accent" />
                <div>
                  <p class="text-xs font-medium text-ink-muted">Macro Calls</p>
                  <h1 class="text-lg font-semibold">
                    {ready()?.title || 'Call'}
                  </h1>
                </div>
              </div>
            }
          >
            <MeetingCallHeading
              title={ready()?.title || 'Call'}
              onRename={props.onRename}
            />
          </Show>
        </header>
      </Show>
      <Switch>
        <Match when={leaving()}>
          <div
            class="flex flex-1 items-center justify-center text-ink-muted"
            role="status"
          >
            Leaving call…
          </div>
        </Match>
        <Match when={inCall()}>
          <div class="min-h-0 flex-1">
            {props.renderCall(
              () => fireAndForget(leave(), 'meetings'),
              displayName,
              session.credentials,
              <Show when={!isMobile() && props.url}>
                {(url) => (
                  <MeetingCopyButton url={url()} onCopy={props.onCopy} />
                )}
              </Show>
            )}
          </div>
        </Match>
        <Match when={props.source().kind === 'loading'}>
          <div
            class="flex flex-1 items-center justify-center text-ink-muted"
            role="status"
          >
            Loading call…
          </div>
        </Match>
        <Match when={props.source().kind === 'unavailable'}>
          <div class="m-auto max-w-md text-center">
            <h2 class="text-2xl font-semibold">This call is unavailable</h2>
            <p class="mt-3 text-ink-muted">
              The link may have expired or the call may have been canceled. Ask
              the organizer for a new link.
            </p>
          </div>
        </Match>
        <Match when={membersOnly()}>
          <div class="m-auto max-w-md text-center">
            <h2 class="text-2xl font-semibold">{ready()?.title || 'Call'}</h2>
            <p class="mt-3 text-ink-muted">
              This call is for Macro members. Sign in to join.
            </p>
            <Button
              variant="ghost"
              size="lg"
              class="mt-6 bg-hover text-ink not-touch:not-disabled:hover:bg-active focus-visible:outline-2 focus-visible:outline-accent"
              onClick={() => props.onSignIn?.()}
            >
              Sign in
            </Button>
          </div>
        </Match>
        <Match when={ready()}>
          {/* Scrolls so Join stays reachable when setup is taller than the
              viewport, as on phones. */}
          <div class="flex min-h-0 flex-1 overflow-y-auto">
            <div class="m-auto grid w-full max-w-6xl gap-8 py-6 md:grid-cols-[minmax(0,2fr)_minmax(18rem,1fr)] md:items-center">
              <MeetingMediaSetup
                media={media}
                name={displayName()}
                avatar={props.avatar}
                disabled={session.joining()}
                readBackgroundImage={props.readBackgroundImage}
                onUploading={setUploadingBackground}
              />
              <form
                class="flex flex-col gap-5"
                onSubmit={(event) => {
                  event.preventDefault();
                  fireAndForget(join(), 'meetings');
                }}
              >
                <div class="flex items-start justify-between gap-3">
                  <div>
                    <h2 class="text-2xl font-semibold">
                      {session.hasLeft()
                        ? 'You left the call'
                        : props.startCall
                          ? 'Ready to start?'
                          : 'Ready to join?'}
                    </h2>
                    <Show when={ready()?.scheduledStart}>
                      {(start) => (
                        <p class="mt-2 text-sm text-ink-muted">
                          {new Date(start()).toLocaleString([], {
                            weekday: 'short',
                            month: 'short',
                            day: 'numeric',
                            hour: 'numeric',
                            minute: '2-digit',
                          })}
                        </p>
                      )}
                    </Show>
                  </div>
                </div>
                <Show when={props.participants}>
                  {(state) => <MeetingParticipants state={state()} />}
                </Show>
                <Show
                  when={!props.authenticated()}
                  fallback={
                    <p class="text-sm text-ink-muted">
                      Joining as {props.author()}
                    </p>
                  }
                >
                  <label class="flex flex-col gap-2 text-sm font-medium">
                    Your name
                    <Input
                      size="lg"
                      placeholder="Enter your name"
                      autocomplete="name"
                      value={name()}
                      maxLength={80}
                      required
                      disabled={session.joining()}
                      onInput={(event) => setName(event.currentTarget.value)}
                    />
                  </label>
                </Show>
                <Show when={session.error()}>
                  <p role="alert" class="text-sm text-failure">
                    {session.error()}
                  </p>
                </Show>
                <Show
                  when={session.joinedCallId() && !props.session.isInCall()}
                >
                  <p role="status" class="text-sm text-ink-muted">
                    You were disconnected. Join again to reconnect.
                  </p>
                </Show>
                <Show
                  when={props.authenticated() === true && !session.hasLeft()}
                >
                  {props.renderInvite?.(session.joining)}
                </Show>
                <Button
                  variant="ghost"
                  size="lg"
                  class="w-full bg-hover text-ink not-touch:not-disabled:hover:bg-active focus-visible:outline-2 focus-visible:outline-accent"
                  type="submit"
                  disabled={
                    session.joining() ||
                    media.pending() ||
                    uploadingBackground() ||
                    (!props.authenticated() && !name().trim())
                  }
                >
                  <Phone class="size-5" />
                  {session.joining()
                    ? props.startCall
                      ? 'Starting…'
                      : 'Joining…'
                    : session.hasLeft()
                      ? 'Rejoin call'
                      : props.startCall
                        ? 'Start call'
                        : 'Join call'}
                </Button>
                <p class="text-xs text-ink-muted">
                  Calls are recorded and transcribed for the organizer and Macro
                  participants.
                </p>
              </form>
            </div>
          </div>
        </Match>
      </Switch>
    </main>
  );
}