From f78b277462d269c0e2d3e3395d77efa1b1df1930 Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 23 Jul 2025 23:34:30 +0800 Subject: [PATCH] chore: simplify add memo relation --- .../ActionButton/AddMemoRelationPopover.tsx | 110 ++++-------------- .../components/MemoEditor/Editor/index.tsx | 2 +- 2 files changed, 22 insertions(+), 90 deletions(-) diff --git a/web/src/components/MemoEditor/ActionButton/AddMemoRelationPopover.tsx b/web/src/components/MemoEditor/ActionButton/AddMemoRelationPopover.tsx index 38bad5be8..9743d9773 100644 --- a/web/src/components/MemoEditor/ActionButton/AddMemoRelationPopover.tsx +++ b/web/src/components/MemoEditor/ActionButton/AddMemoRelationPopover.tsx @@ -1,43 +1,30 @@ import { uniqBy } from "lodash-es"; -import { LinkIcon, X } from "lucide-react"; -import React, { useContext, useState } from "react"; +import { LinkIcon } from "lucide-react"; +import { useContext, useState } from "react"; import { toast } from "react-hot-toast"; import useDebounce from "react-use/lib/useDebounce"; -import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; -import { Checkbox } from "@/components/ui/checkbox"; import { Input } from "@/components/ui/input"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { memoServiceClient } from "@/grpcweb"; import { DEFAULT_LIST_MEMOS_PAGE_SIZE } from "@/helpers/consts"; import useCurrentUser from "@/hooks/useCurrentUser"; -import { extractMemoIdFromName } from "@/store/common"; +import { extractUserIdFromName } from "@/store/common"; import { Memo, MemoRelation_Memo, MemoRelation_Type } from "@/types/proto/api/v1/memo_service"; import { useTranslate } from "@/utils/i18n"; -import { EditorRefActions } from "../Editor"; import { MemoEditorContext } from "../types"; -interface Props { - editorRef: React.RefObject; -} - -const AddMemoRelationPopover = (props: Props) => { - const { editorRef } = props; +const AddMemoRelationPopover = () => { const t = useTranslate(); const context = useContext(MemoEditorContext); const user = useCurrentUser(); const [searchText, setSearchText] = useState(""); const [isFetching, setIsFetching] = useState(true); const [fetchedMemos, setFetchedMemos] = useState([]); - const [selectedMemos, setSelectedMemos] = useState([]); - const [embedded, setEmbedded] = useState(false); const [popoverOpen, setPopoverOpen] = useState(false); const filteredMemos = fetchedMemos.filter( - (memo) => - !selectedMemos.includes(memo) && - memo.name !== context.memoName && - !context.relationList.some((relation) => relation.relatedMemo?.name === memo.name), + (memo) => memo.name !== context.memoName && !context.relationList.some((relation) => relation.relatedMemo?.name === memo.name), ); useDebounce( @@ -46,10 +33,7 @@ const AddMemoRelationPopover = (props: Props) => { setIsFetching(true); try { - const conditions = []; - // Extract user ID from user name (format: users/{user_id}) - const userId = user.name.replace("users/", ""); - conditions.push(`creator_id == ${userId}`); + const conditions = [`creator_id == ${extractUserIdFromName(user.name)}`]; if (searchText) { conditions.push(`content.contains("${searchText}")`); } @@ -92,42 +76,20 @@ const AddMemoRelationPopover = (props: Props) => { ); }; - const addMemoRelations = async () => { - // If embedded mode is enabled, embed the memo instead of creating a relation. - if (embedded) { - if (!editorRef.current) { - toast.error(t("message.failed-to-embed-memo")); - return; - } - - const cursorPosition = editorRef.current.getCursorPosition(); - const prevValue = editorRef.current.getContent().slice(0, cursorPosition); - if (prevValue !== "" && !prevValue.endsWith("\n")) { - editorRef.current.insertText("\n"); - } - for (const memo of selectedMemos) { - editorRef.current.insertText(`![[memos/${extractMemoIdFromName(memo.name)}]]\n`); - } - setTimeout(() => { - editorRef.current?.scrollToCursor(); - editorRef.current?.focus(); - }); - } else { - context.setRelationList( - uniqBy( - [ - ...selectedMemos.map((memo) => ({ - memo: MemoRelation_Memo.fromPartial({ name: memo.name }), - relatedMemo: MemoRelation_Memo.fromPartial({ name: memo.name }), - type: MemoRelation_Type.REFERENCE, - })), - ...context.relationList, - ].filter((relation) => relation.relatedMemo !== context.memoName), - "relatedMemo", - ), - ); - } - setSelectedMemos([]); + const addMemoRelations = async (memo: Memo) => { + context.setRelationList( + uniqBy( + [ + { + memo: MemoRelation_Memo.fromPartial({ name: memo.name }), + relatedMemo: MemoRelation_Memo.fromPartial({ name: memo.name }), + type: MemoRelation_Type.REFERENCE, + }, + ...context.relationList, + ].filter((relation) => relation.relatedMemo !== context.memoName), + "relatedMemo", + ), + ); setPopoverOpen(false); }; @@ -140,24 +102,6 @@ const AddMemoRelationPopover = (props: Props) => {
- {/* Selected memos display */} - {selectedMemos.length > 0 && ( -
- {selectedMemos.map((memo) => ( - -
-

{memo.displayTime?.toLocaleString()}

- {memo.content} -
- setSelectedMemos((memos) => memos.filter((m) => m.name !== memo.name))} - /> -
- ))} -
- )} - {/* Search and selection interface */}
{ key={memo.name} className="relative flex cursor-pointer items-start gap-2 rounded-sm px-2 py-1.5 text-sm hover:bg-accent hover:text-accent-foreground" onClick={() => { - setSelectedMemos((prev) => [...prev, memo]); + addMemoRelations(memo); }} >
@@ -191,18 +135,6 @@ const AddMemoRelationPopover = (props: Props) => { )}
- -
-
- setEmbedded(checked === true)} /> - -
- -
diff --git a/web/src/components/MemoEditor/Editor/index.tsx b/web/src/components/MemoEditor/Editor/index.tsx index acecc18a5..a5e45d22e 100644 --- a/web/src/components/MemoEditor/Editor/index.tsx +++ b/web/src/components/MemoEditor/Editor/index.tsx @@ -216,7 +216,7 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<