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.

2.7 KB
Raw Blame
import { timestampDate } from "@bufbuild/protobuf/wkt";
import { createContext, useContext } from "react";
import { useLocation } from "react-router-dom";
import { useView } from "@/contexts/ViewContext";
import type { Memo } from "@/types/proto/api/memo_service_pb";
import { MemoRelation_Type } from "@/types/proto/api/memo_service_pb";
import type { User } from "@/types/proto/api/user_service_pb";
import type { PreviewMediaItem } from "@/utils/media-item";
import { RELATIVE_TIME_THRESHOLD_MS } from "./constants";
import { isMemoDetailPath } from "./navigation";

export interface MemoViewContextValue {
  memo: Memo;
  creator: User | undefined;
  currentUser: User | undefined;
  parentPage: string;
  cardWidth: number;
  isArchived: boolean;
  readonly: boolean;
  showBlurredContent: boolean;
  blurred: boolean;
  /** Whether the card shows the memo's editing session in place of its body. */
  isEditing: boolean;
  openEditor: () => void;
  closeEditor: () => void;
  /** The session reports focus mode so the card's layout can let it take the viewport. */
  onEditorFocusModeChange: (isFocusMode: boolean) => void;
  toggleBlurVisibility: () => void;
  openPreview: (items: string | string[] | PreviewMediaItem[], index?: number) => void;
}

export const MemoViewContext = createContext<MemoViewContextValue | null>(null);

export const useMemoViewContext = (): MemoViewContextValue => {
  const context = useContext(MemoViewContext);
  if (!context) {
    throw new Error("useMemoViewContext must be used within MemoViewContext.Provider");
  }
  return context;
};

export const computeCommentAmount = (memo: Memo): number =>
  memo.relations.filter((r) => r.type === MemoRelation_Type.COMMENT && r.relatedMemo?.name === memo.name).length;

export const useMemoViewDerived = () => {
  const { memo, isArchived, readonly } = useMemoViewContext();
  const { timeBasis } = useView();
  const location = useLocation();

  const isInMemoDetailPage = isMemoDetailPath(location.pathname, memo.name);
  const commentAmount = computeCommentAmount(memo);

  const createTime = memo.createTime ? timestampDate(memo.createTime) : undefined;
  const updateTime = memo.updateTime ? timestampDate(memo.updateTime) : undefined;
  const displayTime = timeBasis === "update_time" ? updateTime : createTime;
  const isDisplayingUpdatedTime =
    timeBasis === "update_time" && !!createTime && !!updateTime && updateTime.getTime() !== createTime.getTime();
  const relativeTimeFormat: "datetime" | "auto" =
    displayTime && Date.now() - displayTime.getTime() > RELATIVE_TIME_THRESHOLD_MS ? "datetime" : "auto";

  return {
    isArchived,
    readonly,
    isInMemoDetailPage,
    commentAmount,
    createTime,
    updateTime,
    displayTime,
    isDisplayingUpdatedTime,
    relativeTimeFormat,
  };
};