Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

8.8 KB
Raw Blame
import { Dialog as DialogPrimitive } from "@base-ui/react/dialog";
import { createContext, type ReactNode, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
import { useLocation } from "react-router-dom";
import { getSidebarRouteKind } from "@/components/AppSidebar/routes";
import { MemoEditor, preloadMemoEditing } from "@/components/MemoEditor";
import { VisuallyHidden } from "@/components/ui/visually-hidden";
import { useAppSidebar } from "@/contexts/AppSidebarContext";
import { useAuth } from "@/contexts/AuthContext";
import { useSpaceContext } from "@/contexts/SpaceContext";
import useCurrentUser from "@/hooks/useCurrentUser";
import useMediaQuery from "@/hooks/useMediaQuery";
import { useMemoSuggestions } from "@/hooks/useMemoSuggestions";
import { useTranslate } from "@/utils/i18n";

interface GlobalMemoEditorContextValue {
  /** Whether the signed-in user is ready to compose; gates every visible entry point. */
  canOpen: boolean;
  openEditor: () => void;
  /** Claims the initial desktop Home focus once, after the editor restores its draft. */
  claimHomeAutoFocus: () => boolean;
}

const GlobalMemoEditorContext = createContext<GlobalMemoEditorContextValue | null>(null);

const isVisibleFocusTarget = (element: HTMLElement | null): element is HTMLElement => {
  if (!element?.isConnected || element.tabIndex < 0 || element.matches(":disabled, [aria-disabled='true']")) return false;

  // Walk ancestors: the mobile trigger is hidden by a `md:hidden` parent, which
  // never shows up in the element's own computed style.
  for (let current: HTMLElement | null = element; current; current = current.parentElement) {
    const style = window.getComputedStyle(current);
    if (style.display === "none" || style.visibility === "hidden" || style.visibility === "collapse") return false;
  }

  return true;
};

const findVisibleFocusTarget = (selector: string): HTMLElement | null =>
  Array.from(document.querySelectorAll<HTMLElement>(selector)).find(isVisibleFocusTarget) ?? null;

export function GlobalMemoEditorProvider({ children }: { children: ReactNode }) {
  const t = useTranslate();
  const location = useLocation();
  const currentUserName = useCurrentUser()?.name;
  const suggestions = useMemoSuggestions();
  const { selectedSpaceName, isSpaceReady } = useSpaceContext();
  const { isUserSettingsInitialized } = useAuth();
  const desktop = useMediaQuery("md");
  const [initialHome] = useState(() => ({ location, user: currentUserName, space: selectedSpaceName }));
  const homeAutoFocusPending = useRef(desktop && getSidebarRouteKind(location.pathname) === "home" && Boolean(currentUserName));
  const { setMobileOpen, setQuickFindOpen } = useAppSidebar();
  // One snapshot taken when the composer opens: keyed by the user who opened it, so
  // signing out closes the composer in the same render and a different user signing
  // in cannot resurrect it, and pinned to the Space that was selected at that moment.
  const [opened, setOpened] = useState<{ user: string; space?: string }>();
  // Only ever read from event handlers, so a ref keeps each save from
  // re-rendering the provider and the whole editor tree it hosts.
  const isSavingRef = useRef(false);
  const returnFocusRef = useRef<HTMLElement | null>(null);

  useEffect(() => {
    if (
      location.key !== initialHome.location.key ||
      location.pathname !== initialHome.location.pathname ||
      location.search !== initialHome.location.search ||
      currentUserName !== initialHome.user ||
      selectedSpaceName !== initialHome.space ||
      !desktop
    ) {
      homeAutoFocusPending.current = false;
    }
  }, [location, currentUserName, selectedSpaceName, desktop, initialHome]);

  const claimHomeAutoFocus = useCallback(() => {
    const pending = homeAutoFocusPending.current;
    homeAutoFocusPending.current = false;
    // A user who already focused another control takes precedence over startup focus.
    return pending && (document.activeElement === document.body || document.activeElement === null);
  }, []);

  const closeEditor = useCallback(() => {
    isSavingRef.current = false;
    setOpened(undefined);
  }, []);
  const requestCloseEditor = useCallback(() => {
    if (!isSavingRef.current) closeEditor();
  }, [closeEditor]);
  const reportSaving = useCallback((saving: boolean) => {
    isSavingRef.current = saving;
  }, []);

  // The editor reads defaults out of user settings, so composing has to wait for
  // them. Keep the rule here so every entry point uses the same gate.
  const canOpen = Boolean(currentUserName) && isUserSettingsInitialized && isSpaceReady;

  const openEditor = useCallback(() => {
    if (!canOpen || !currentUserName) return;
    homeAutoFocusPending.current = false;

    // Owned here so no caller can leave a sidebar surface open underneath.
    setMobileOpen(false);
    setQuickFindOpen(false);

    isSavingRef.current = false;
    const activeElement = document.activeElement;
    returnFocusRef.current = activeElement instanceof HTMLElement && activeElement !== document.body ? activeElement : null;

    // The dialog opens at once; the editor fills it as soon as its code is here.
    preloadMemoEditing();
    setOpened({ user: currentUserName, space: selectedSpaceName });
  }, [canOpen, selectedSpaceName, currentUserName, setMobileOpen, setQuickFindOpen]);

  useEffect(() => {
    // RootLayout remains mounted when a public instance moves from Home to
    // Explore on sign-out. Treat every auth/readiness transition as a session
    // boundary so an open composer—or a pending lazy import—cannot survive it.
    closeEditor();
    returnFocusRef.current = null;
  }, [closeEditor, currentUserName, isUserSettingsInitialized]);

  const resolveFinalFocus = useCallback(() => {
    // Release the remembered node here rather than in closeEditor: base-ui asks
    // for it after close, and a trigger that unmounted with the drawer would
    // otherwise keep its whole detached subtree alive until the next open.
    const remembered = isVisibleFocusTarget(returnFocusRef.current) ? returnFocusRef.current : null;
    returnFocusRef.current = null;

    return remembered ?? findVisibleFocusTarget("[data-new-memo-trigger]") ?? findVisibleFocusTarget("[data-mobile-navigation-trigger]");
  }, []);

  const editorIsOpen = opened !== undefined && opened.user === currentUserName;
  const value = useMemo(() => ({ canOpen, openEditor, claimHomeAutoFocus }), [canOpen, openEditor, claimHomeAutoFocus]);

  return (
    <GlobalMemoEditorContext.Provider value={value}>
      {children}
      <DialogPrimitive.Root
        open={editorIsOpen}
        onOpenChange={(open, eventDetails) => {
          if (!open && isSavingRef.current) {
            eventDetails.cancel();
            return;
          }
          if (open) {
            setOpened((previous) => (currentUserName ? { user: currentUserName, space: previous?.space } : undefined));
          } else {
            closeEditor();
          }
        }}
      >
        {editorIsOpen && (
          <DialogPrimitive.Portal>
            <DialogPrimitive.Backdrop className="fixed inset-0 z-overlay bg-transparent" />
            <DialogPrimitive.Popup
              className="fixed inset-0 z-overlay outline-none"
              initialFocus
              finalFocus={resolveFinalFocus}
              aria-modal="true"
            >
              <VisuallyHidden>
                <DialogPrimitive.Title>{t("editor.new-memo")}</DialogPrimitive.Title>
              </VisuallyHidden>
              <MemoEditor
                autoFocus
                draft={{ scope: "global", space: opened?.space }}
                suggestions={suggestions}
                placeholder={t("editor.any-thoughts")}
                onConfirm={closeEditor}
                onCancel={closeEditor}
                // Hosts the editor's focus-mode presentation: it mounts in focus
                // mode and dismisses this dialog instead of collapsing inline.
                onFocusModeExit={requestCloseEditor}
                onSavingChange={reportSaving}
              />
              <DialogPrimitive.Close className="sr-only focus-visible:not-sr-only focus-visible:fixed focus-visible:end-3 focus-visible:top-3 focus-visible:z-dropdown focus-visible:h-9 focus-visible:rounded-md focus-visible:bg-background focus-visible:px-3 focus-visible:text-sm focus-visible:shadow-md focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/50">
                {t("common.close")}
              </DialogPrimitive.Close>
            </DialogPrimitive.Popup>
          </DialogPrimitive.Portal>
        )}
      </DialogPrimitive.Root>
    </GlobalMemoEditorContext.Provider>
  );
}

export function useGlobalMemoEditor() {
  const context = useContext(GlobalMemoEditorContext);
  if (!context) {
    throw new Error("useGlobalMemoEditor must be used within GlobalMemoEditorProvider");
  }
  return context;
}