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.

11.4 KB
Raw Blame
import { t } from '@app/lib/i18n';
import './MobileSettingsSheet.css';
import { SearchBar } from '@app/components/view-shell';
import { MobileDrawer } from '@components/app/mobile/MobileDrawer';
import type { SettingsTab } from '@core/constant/SettingsState';
import type { SettingsTabGroup } from '@core/constant/settingsTabsConfig';
import CaretLeftIcon from '@icon/system/regular/caret-left.svg';
import CaretRightIcon from '@icon/system/regular/caret-right.svg';
import PencilIcon from '@icon/system/regular/pencil-simple.svg';
import SignOutIcon from '@icon/system/regular/sign-out.svg';
import XIcon from '@icon/system/regular/x.svg';
import { Button } from '@ui';
import {
  createMemo,
  createSignal,
  ErrorBoundary,
  For,
  type JSX,
  Show,
  Suspense,
} from 'solid-js';
import { Dynamic } from 'solid-js/web';
import { SettingsSearchResults } from './components/settings-search-results';
import { SettingsSearchTarget } from './components/settings-search-target';
import {
  type SettingsSearchResult,
  searchSettings,
} from './core/settings-search';
import { SettingsSheetContext } from './primitives';

type MobileSettingsSheetProps = {
  open: boolean;
  page?: SettingsTab;
  groups: SettingsTabGroup[];
  searchGroups?: SettingsTabGroup[];
  emailSignatures?: boolean;
  /** Whether search lists the Outlook connection; see `searchSettings`. */
  outlook?: boolean;
  name: string;
  email: string;
  avatar: JSX.Element;
  onClose: () => void;
  onNavigate: (page?: SettingsTab) => void;
  onLogout: () => void;
  renderPage: (page: SettingsTab) => JSX.Element;
};

/** Mobile presentation; account data and settings actions are supplied by the host. */
export function MobileSettingsSheet(props: MobileSettingsSheetProps) {
  const [query, setQuery] = createSignal('');
  const [selection, setSelection] = createSignal<SettingsSearchResult>();
  const results = createMemo(() =>
    searchSettings(props.searchGroups ?? props.groups, query(), {
      emailSignatures: props.emailSignatures,
      outlook: props.outlook,
    })
  );
  let mainScrollTop = 0;
  let scrollRef: HTMLDivElement | undefined;
  let headingRef: HTMLHeadingElement | undefined;
  const navigationPage = () => props.page;

  const pageLabel = () =>
    props.groups
      .flatMap((group) => group.items)
      .find((item) => item.tab === navigationPage())?.label ??
    (props.page === 'Harness' ? t('settingsNav.tabRuntimes') : undefined) ??
    props.page ??
    t('settingsNav.settings');

  const navigate = (page?: SettingsTab, focusHeading = true) => {
    setSelection(undefined);
    if (!props.page) mainScrollTop = scrollRef?.scrollTop ?? 0;
    props.onNavigate(page);
    if (focusHeading)
      queueMicrotask(() => headingRef?.focus({ preventScroll: true }));
  };

  return (
    <MobileDrawer
      open={props.open}
      onOpenChange={(open) => {
        if (!open) {
          mainScrollTop = 0;
          props.onClose();
        }
      }}
      side="bottom"
      closeOnOutsidePointerStrategy="pointerdown"
    >
      <MobileDrawer.Portal>
        <MobileDrawer.Overlay />
        <MobileDrawer.Content
          aria-label={t('settingsNav.settings')}
          targetHeight={94}
          maxHeight={94}
          class="mobile-settings-sheet overflow-hidden [--mobile-content-inset-top:0px] [--mobile-content-inset-bottom:0px]"
        >
          <MobileDrawer.Handle class="pb-1" />
          <header class="grid shrink-0 grid-cols-[44px_1fr_44px] items-center gap-2 px-5 pb-3 pt-1">
            <Show when={props.page} fallback={<span />}>
              <Button
                variant="ghost"
                size="icon-sm"
                aria-label={t('settingsNav.backToSettings')}
                class="size-11 bg-ink/6"
                onClick={() => navigate()}
              >
                <CaretLeftIcon class="size-5" />
              </Button>
            </Show>
            <h1
              ref={headingRef}
              tabIndex={-1}
              class="text-center text-lg font-semibold text-ink outline-none"
            >
              {pageLabel()}
            </h1>
            <MobileDrawer.Close
              as={Button}
              variant="ghost"
              size="icon-sm"
              aria-label={t('settingsNav.closeSettings')}
              class="size-11 rounded-full bg-ink/6"
            >
              <XIcon class="size-5" />
            </MobileDrawer.Close>
          </header>
          <Show
            when={props.page}
            keyed
            fallback={
              <MobileDrawer.ScrollBody
                ref={(el: HTMLDivElement) => {
                  scrollRef = el;
                  queueMicrotask(() => {
                    el.scrollTop = mainScrollTop;
                  });
                }}
                class="mobile-settings-home"
              >
                <button
                  type="button"
                  aria-label={t('settingsNav.editProfile')}
                  onClick={() => navigate('Account')}
                  class="mx-6 mb-7 flex shrink-0 flex-col items-center rounded-3xl px-4 pb-3 pt-2 text-center active:bg-ink/3"
                >
                  <span class="relative mb-3 block size-20">
                    <span class="flex size-20 items-center justify-center overflow-hidden rounded-full bg-ink/8 text-2xl font-medium">
                      {props.avatar}
                    </span>
                    <span class="absolute! -bottom-1 -right-1 flex size-7 items-center justify-center rounded-full border-2 border-menu-glass bg-menu text-ink">
                      <PencilIcon class="size-3.5" />
                    </span>
                  </span>
                  <span class="ph-no-capture max-w-full truncate text-xl font-semibold text-ink">
                    {props.name}
                  </span>
                  <span class="ph-no-capture mt-1 max-w-full truncate text-sm text-ink-muted">
                    {props.email}
                  </span>
                </button>
                <div class="flex flex-col gap-6 px-3">
                  <SearchBar
                    label={t('settingsNav.searchSettings')}
                    placeholder={t('settingsNav.searchSettings')}
                    value={query()}
                    onValueChange={setQuery}
                    onEscape={() => setQuery('')}
                  />
                  <Show
                    when={!query().trim()}
                    fallback={
                      <SettingsSearchResults
                        results={results()}
                        onSelect={(result) => {
                          navigate(result.tab, false);
                          setSelection({ ...result });
                        }}
                      />
                    }
                  >
                    <For each={props.groups}>
                      {(group) => (
                        <section>
                          <h2 class="px-4 pb-2 text-sm font-medium text-ink-muted">
                            {group.label}
                          </h2>
                          <div class="overflow-hidden rounded-[26px] bg-ink/5 p-1">
                            <For each={group.items}>
                              {(item, index) => (
                                <>
                                  <Show when={index() > 0}>
                                    <div class="ml-12 mr-3 h-px bg-ink/6" />
                                  </Show>
                                  <MobileDrawer.Item
                                    onClick={() => navigate(item.tab)}
                                    class="min-h-13 gap-3.5 px-3.5 text-base"
                                  >
                                    <Dynamic
                                      component={item.icon}
                                      class="size-5 shrink-0"
                                    />
                                    <span class="min-w-0 flex-1 text-left">
                                      {item.label}
                                    </span>
                                    <CaretRightIcon class="size-4 shrink-0 text-ink-extra-muted" />
                                  </MobileDrawer.Item>
                                </>
                              )}
                            </For>
                          </div>
                        </section>
                      )}
                    </For>
                  </Show>
                  <div class="mb-3 rounded-[26px] bg-ink/5 p-1">
                    <MobileDrawer.Item
                      onClick={props.onLogout}
                      class="min-h-13 gap-3.5 px-3.5 text-base text-failure"
                    >
                      <SignOutIcon class="size-5" />
                      {t('settingsNav.logOut')}
                    </MobileDrawer.Item>
                  </div>
                </div>
              </MobileDrawer.ScrollBody>
            }
          >
            {(page) => (
              <SettingsSheetContext.Provider value={true}>
                <div
                  class="mobile-settings-detail relative min-h-0 flex-1"
                  data-settings-detail={page}
                >
                  <ErrorBoundary
                    fallback={(_error, reset) => (
                      <div class="flex h-full flex-col items-center justify-center gap-4 px-8 text-center">
                        <p class="text-sm text-ink-muted">
                          {t('settingsNav.pageLoadFailed')}
                        </p>
                        <Button variant="ghost" onClick={reset}>
                          {t('settingsNav.tryAgain')}
                        </Button>
                      </div>
                    )}
                  >
                    <Suspense
                      fallback={
                        <div
                          role="status"
                          class="p-8 text-center text-sm text-ink-muted"
                        >
                          {t('settingsNav.loadingSettings')}
                        </div>
                      }
                    >
                      <Show
                        when={props.groups.some((group) =>
                          group.items.some(
                            (item) => item.tab === navigationPage()
                          )
                        )}
                        fallback={
                          <div class="flex h-full flex-col items-center justify-center gap-4 px-8 text-center">
                            <p role="status" class="text-sm text-ink-muted">
                              {t('settingsNav.sectionUnavailable')}
                            </p>
                            <Button variant="ghost" onClick={() => navigate()}>
                              {t('settingsNav.backToSettings')}
                            </Button>
                          </div>
                        }
                      >
                        <SettingsSearchTarget
                          result={
                            selection()?.tab === page ? selection() : undefined
                          }
                        >
                          {props.renderPage(page)}
                        </SettingsSearchTarget>
                      </Show>
                    </Suspense>
                  </ErrorBoundary>
                </div>
              </SettingsSheetContext.Provider>
            )}
          </Show>
        </MobileDrawer.Content>
      </MobileDrawer.Portal>
    </MobileDrawer>
  );
}