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.

12.5 KB
Raw Blame
import { Code, ConnectError } from "@connectrpc/connect";
import { ArrowUpLeftFromCircleIcon } from "lucide-react";
import { useCallback, useEffect, useMemo as useReactMemo, useRef, useState } from "react";
import { Link, Navigate, useLocation, useParams } from "react-router-dom";
import MemoCommentSection, { type MemoCommentSectionHandle } from "@/components/MemoCommentSection";
import MemoParentPlaceholder, { type MemoParentStatus } from "@/components/MemoParentPlaceholder";
import MemoView, { type MemoViewHandle } from "@/components/MemoView";
import { computeCommentAmount } from "@/components/MemoView/MemoViewContext";
import { createMemoNavigationState, resolveMemoDetailOrigin } from "@/components/MemoView/navigation";
import { useAppSidebar } from "@/contexts/AppSidebarContext";
import { useAuth } from "@/contexts/AuthContext";
import { useInstance } from "@/contexts/InstanceContext";
import { MentionResolutionProvider } from "@/contexts/MentionResolutionContext";
import useMemoDetailError from "@/hooks/useMemoDetailError";
import { useInfiniteMemoComments, useMemo } from "@/hooks/useMemoQueries";
import { useSharedMemo, withShareAttachmentLinks } from "@/hooks/useMemoShareQueries";
import { LEGACY_MEMO_COMMENTS_ANCHOR_ID, MEMO_COMMENTS_ANCHOR_ID } from "@/lib/memo-comments";
import { memoNamePrefix } from "@/lib/resource-names";
import { isMemoBlurred } from "@/lib/tag";
import { ROUTES } from "@/router/routes";
import type { Attachment } from "@/types/proto/api/attachment_service_pb";
import { State } from "@/types/proto/api/common_pb";
import type { Memo } from "@/types/proto/api/memo_service_pb";
import { useTranslate } from "@/utils/i18n";
import { findMemoAnchorTarget } from "@/utils/markdown-manipulation";

const MemoSidebarRegistration = ({
  memo,
  parentMemo,
  parentStatus,
  onParentRetry,
  from,
  commentCount,
  readonly,
  contentHidden,
  onEdit,
  onCommentsOpen,
  onCommentCreate,
  onShareImageOpen,
}: {
  memo: Memo;
  parentMemo?: Memo;
  parentStatus?: MemoParentStatus;
  onParentRetry?: () => void;
  from: string;
  commentCount?: number;
  readonly: boolean;
  contentHidden: boolean;
  onEdit: () => void;
  onCommentsOpen: () => void;
  onCommentCreate: () => void;
  onShareImageOpen: () => void;
}) => {
  const { setMemoDetail } = useAppSidebar();

  useEffect(() => {
    setMemoDetail({
      memo,
      parentMemo,
      parentStatus,
      onParentRetry,
      from,
      commentCount,
      readonly,
      contentHidden,
      onEdit,
      onCommentsOpen,
      onCommentCreate,
      onShareImageOpen,
    });
  }, [
    commentCount,
    contentHidden,
    from,
    memo,
    onCommentCreate,
    onCommentsOpen,
    onEdit,
    onShareImageOpen,
    parentMemo,
    parentStatus,
    onParentRetry,
    readonly,
    setMemoDetail,
  ]);

  useEffect(() => () => setMemoDetail(undefined), [setMemoDetail]);

  return null;
};

const MemoDetail = () => {
  const t = useTranslate();
  const { currentUser, isInitialized: authInitialized, userTagsSetting } = useAuth();
  const { isInitialized: instanceInitialized } = useInstance();
  const [shareImageDialogOpen, setShareImageDialogOpen] = useState(false);
  const params = useParams();
  const location = useLocation();
  const { state: locationState, hash } = location;
  const memoViewRef = useRef<MemoViewHandle>(null);
  const commentSectionRef = useRef<MemoCommentSectionHandle>(null);
  const handleShareImageOpen = useCallback(() => setShareImageDialogOpen(true), []);
  const handleEdit = useCallback(() => memoViewRef.current?.openEditor(), []);

  // Detect share mode from the route parameter.
  const shareToken = params.token;
  const isShareMode = !!shareToken;

  // Primary memo fetch — share token or direct name.
  const memoNameFromParams = params.uid ? `${memoNamePrefix}${params.uid}` : "";
  const {
    data: memoFromDirect,
    error: directError,
    isLoading: directLoading,
    isUnavailable: directUnavailable,
    fetchStatus: directFetchStatus,
  } = useMemo(memoNameFromParams, { enabled: !isShareMode && !!memoNameFromParams });
  const { data: memoFromShare, error: shareError, isLoading: shareLoading } = useSharedMemo(shareToken ?? "", { enabled: isShareMode });

  const memo = isShareMode ? memoFromShare : memoFromDirect;
  const error = isShareMode ? shareError : directError;
  const isLoading = isShareMode ? shareLoading : directLoading;
  const hasExplicitOrigin =
    !!locationState && typeof locationState === "object" && typeof (locationState as { from?: unknown }).from === "string";
  const resolvedOrigin = resolveMemoDetailOrigin(locationState, { memoArchived: memo?.state === State.ARCHIVED });
  const parentPage = !hasExplicitOrigin && !currentUser && memo?.state !== State.ARCHIVED ? ROUTES.EXPLORE : resolvedOrigin;
  const memoName = memo?.name ?? memoNameFromParams;
  // Keyed by memo so another memo never renders revealed; cleared on leave so returning blurs again.
  const [blurRevealedMemoName, setBlurRevealedMemoName] = useState<string>();
  const showBlurredContent = blurRevealedMemoName === memoName;
  const handleShowBlurredContentChange = useCallback((show: boolean) => setBlurRevealedMemoName(show ? memoName : undefined), [memoName]);
  useEffect(() => setBlurRevealedMemoName(undefined), [memoName]);
  const displayMemo = useReactMemo(() => {
    if (!memo) return undefined;
    if (!isShareMode) return memo;
    return { ...memo, attachments: withShareAttachmentLinks(memo.attachments as Attachment[], shareToken!) };
  }, [isShareMode, memo, shareToken]);

  useMemoDetailError({
    error: error as Error | null,
  });

  const {
    data: fetchedParent,
    error: parentError,
    isUnavailable: parentUnavailable,
    refetch: refetchParent,
  } = useMemo(memo?.parent || "", {
    enabled: !isShareMode && !!memo?.parent,
  });

  // Private parents return 401 to guests. The transport still owns session
  // recovery; only a settled guest view treats that result as unavailable.
  const guestParentDenied =
    authInitialized && !currentUser && parentError instanceof ConnectError && parentError.code === Code.Unauthenticated;
  const parentStatus: MemoParentStatus | undefined =
    !isShareMode && memo?.parent
      ? parentUnavailable || guestParentDenied
        ? "unavailable"
        : parentError
          ? "error"
          : !fetchedParent
            ? "loading"
            : undefined
      : undefined;
  const parentMemo = parentStatus ? undefined : fetchedParent;
  const handleParentRetry = useCallback(() => {
    void refetchParent();
  }, [refetchParent]);

  const {
    data: comments = [],
    fetchNextPage: fetchNextComments,
    hasNextPage: hasNextComments,
    isFetchingNextPage: isFetchingNextComments,
  } = useInfiniteMemoComments(memoName, {
    enabled: !isShareMode && !!memo,
  });
  const commentCount = memo ? Math.max(computeCommentAmount(memo), comments.length) : 0;

  const handleCommentsOpen = useCallback(() => {
    commentSectionRef.current?.scrollIntoView();
    window.history.replaceState(window.history.state, "", `${location.pathname}${location.search}#${MEMO_COMMENTS_ANCHOR_ID}`);
  }, [location.pathname, location.search]);

  const handleCommentCreate = useCallback(() => {
    handleCommentsOpen();
    void commentSectionRef.current?.openEditor();
  }, [handleCommentsOpen]);

  // Scroll to the hash target once it's in the DOM. The effect re-runs as the memo loads (footnote
  // anchors) and as comments arrive (comment anchors), since the target may render in either; the
  // ref guards against re-scrolling the same hash on every later comments page-load.
  const scrolledHashRef = useRef("");
  useEffect(() => {
    if (!hash) return;
    const scrollKey = `${memoName}\0${hash}`;
    if (scrolledHashRef.current === scrollKey) return;
    const fragment = decodeURIComponent(hash.slice(1));
    const commentSection = commentSectionRef.current;
    if (commentSection && (fragment === MEMO_COMMENTS_ANCHOR_ID || fragment === LEGACY_MEMO_COMMENTS_ANCHOR_ID)) {
      scrolledHashRef.current = scrollKey;
      commentSection.scrollIntoView();
      return;
    }

    // A legacy #comments link on a share-token page has no comment section. Let it
    // resolve to a historical body heading instead of turning into a dead fragment.
    const el = findMemoAnchorTarget(document, memoName, fragment);
    if (!el) return;
    scrolledHashRef.current = scrollKey;
    el.scrollIntoView({ behavior: "smooth", block: "center" });
  }, [hash, memo, memoName, comments]);

  // Keep the query mounted while revalidating a cached denial. Redirecting
  // earlier would abort the request that could confirm restored access.
  if (!isShareMode && directUnavailable && directFetchStatus === "idle" && !directError) return <Navigate to="/404" replace />;

  if (isShareMode) {
    const isNotFound = error instanceof ConnectError && (error.code === Code.NotFound || error.code === Code.Unauthenticated);
    if (isNotFound || (!isLoading && !memo)) {
      return <Navigate to="/404" replace />;
    }
  }

  // Start the permitted requests as soon as routing is unlocked, but do not
  // expose content before tag-blur and instance display settings settle.
  if (isLoading || !memo || !displayMemo || !authInitialized || !instanceInitialized) {
    return null;
  }
  const contentHidden = isMemoBlurred(displayMemo, userTagsSetting) && !showBlurredContent;
  const parentBlurred = !!parentMemo && isMemoBlurred(parentMemo, userTagsSetting);
  const mentionResolutionContents = [displayMemo.content, ...comments.map((comment) => comment.content)];
  const userResolutionNames = Array.from(
    new Set([displayMemo, ...comments].flatMap((item) => [item.creator, ...(item.reactions ?? []).map((reaction) => reaction.creator)])),
  );
  return (
    <section className="@container flex min-h-full w-full flex-col items-center pb-8 pt-3 md:pt-6">
      <MentionResolutionProvider contents={mentionResolutionContents} userNames={userResolutionNames}>
        <MemoSidebarRegistration
          memo={displayMemo}
          parentMemo={parentMemo}
          parentStatus={parentStatus}
          onParentRetry={handleParentRetry}
          from={parentPage}
          commentCount={isShareMode ? undefined : commentCount}
          readonly={isShareMode}
          contentHidden={contentHidden}
          onEdit={handleEdit}
          onCommentsOpen={handleCommentsOpen}
          onCommentCreate={handleCommentCreate}
          onShareImageOpen={handleShareImageOpen}
        />
        <div className="w-full max-w-2xl px-4 sm:px-6">
          <div className="w-full">
            {!isShareMode && parentStatus && (
              <div className="mb-2 md:hidden">
                <MemoParentPlaceholder status={parentStatus} onRetry={handleParentRetry} />
              </div>
            )}
            {!isShareMode && parentMemo && (
              <div className="w-auto inline-block mb-2 md:hidden">
                <Link
                  className="px-3 py-1 border border-border rounded-lg max-w-xs w-auto text-sm flex flex-row justify-start items-center flex-nowrap text-muted-foreground hover:shadow hover:opacity-80"
                  to={`/${parentMemo.name}`}
                  state={createMemoNavigationState(parentPage)}
                  viewTransition
                >
                  <ArrowUpLeftFromCircleIcon className="w-4 h-auto shrink-0 opacity-60 mr-2" />
                  <span className="truncate">{parentBlurred ? t("memo.parent-memo") : parentMemo.content}</span>
                </Link>
              </div>
            )}
            <MemoView
              ref={memoViewRef}
              key={displayMemo.name}
              memo={displayMemo}
              compact={false}
              parentPage={parentPage}
              shareImageDialogOpen={shareImageDialogOpen}
              showBlurredContent={showBlurredContent}
              onShowBlurredContentChange={handleShowBlurredContentChange}
              showCreator
              showVisibility
              showPinned
              showSpace
              onShareImageDialogOpenChange={setShareImageDialogOpen}
            />
            {!isShareMode && (
              <MemoCommentSection
                ref={commentSectionRef}
                memo={displayMemo}
                comments={comments}
                commentCount={commentCount}
                parentPage={parentPage}
                hasMoreComments={hasNextComments}
                isFetchingMoreComments={isFetchingNextComments}
                onLoadMoreComments={fetchNextComments}
              />
            )}
          </div>
        </div>
      </MentionResolutionProvider>
    </section>
  );
};

export default MemoDetail;