Skip to content

Commit f78b277

Browse files
committed
chore: simplify add memo relation
1 parent 9b5b7b1 commit f78b277

File tree

2 files changed

+22
-90
lines changed

2 files changed

+22
-90
lines changed

web/src/components/MemoEditor/ActionButton/AddMemoRelationPopover.tsx

Lines changed: 21 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,30 @@
11
import { uniqBy } from "lodash-es";
2-
import { LinkIcon, X } from "lucide-react";
3-
import React, { useContext, useState } from "react";
2+
import { LinkIcon } from "lucide-react";
3+
import { useContext, useState } from "react";
44
import { toast } from "react-hot-toast";
55
import useDebounce from "react-use/lib/useDebounce";
6-
import { Badge } from "@/components/ui/badge";
76
import { Button } from "@/components/ui/button";
8-
import { Checkbox } from "@/components/ui/checkbox";
97
import { Input } from "@/components/ui/input";
108
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
119
import { memoServiceClient } from "@/grpcweb";
1210
import { DEFAULT_LIST_MEMOS_PAGE_SIZE } from "@/helpers/consts";
1311
import useCurrentUser from "@/hooks/useCurrentUser";
14-
import { extractMemoIdFromName } from "@/store/common";
12+
import { extractUserIdFromName } from "@/store/common";
1513
import { Memo, MemoRelation_Memo, MemoRelation_Type } from "@/types/proto/api/v1/memo_service";
1614
import { useTranslate } from "@/utils/i18n";
17-
import { EditorRefActions } from "../Editor";
1815
import { MemoEditorContext } from "../types";
1916

20-
interface Props {
21-
editorRef: React.RefObject<EditorRefActions>;
22-
}
23-
24-
const AddMemoRelationPopover = (props: Props) => {
25-
const { editorRef } = props;
17+
const AddMemoRelationPopover = () => {
2618
const t = useTranslate();
2719
const context = useContext(MemoEditorContext);
2820
const user = useCurrentUser();
2921
const [searchText, setSearchText] = useState<string>("");
3022
const [isFetching, setIsFetching] = useState<boolean>(true);
3123
const [fetchedMemos, setFetchedMemos] = useState<Memo[]>([]);
32-
const [selectedMemos, setSelectedMemos] = useState<Memo[]>([]);
33-
const [embedded, setEmbedded] = useState<boolean>(false);
3424
const [popoverOpen, setPopoverOpen] = useState<boolean>(false);
3525

3626
const filteredMemos = fetchedMemos.filter(
37-
(memo) =>
38-
!selectedMemos.includes(memo) &&
39-
memo.name !== context.memoName &&
40-
!context.relationList.some((relation) => relation.relatedMemo?.name === memo.name),
27+
(memo) => memo.name !== context.memoName && !context.relationList.some((relation) => relation.relatedMemo?.name === memo.name),
4128
);
4229

4330
useDebounce(
@@ -46,10 +33,7 @@ const AddMemoRelationPopover = (props: Props) => {
4633

4734
setIsFetching(true);
4835
try {
49-
const conditions = [];
50-
// Extract user ID from user name (format: users/{user_id})
51-
const userId = user.name.replace("users/", "");
52-
conditions.push(`creator_id == ${userId}`);
36+
const conditions = [`creator_id == ${extractUserIdFromName(user.name)}`];
5337
if (searchText) {
5438
conditions.push(`content.contains("${searchText}")`);
5539
}
@@ -92,42 +76,20 @@ const AddMemoRelationPopover = (props: Props) => {
9276
);
9377
};
9478

95-
const addMemoRelations = async () => {
96-
// If embedded mode is enabled, embed the memo instead of creating a relation.
97-
if (embedded) {
98-
if (!editorRef.current) {
99-
toast.error(t("message.failed-to-embed-memo"));
100-
return;
101-
}
102-
103-
const cursorPosition = editorRef.current.getCursorPosition();
104-
const prevValue = editorRef.current.getContent().slice(0, cursorPosition);
105-
if (prevValue !== "" && !prevValue.endsWith("\n")) {
106-
editorRef.current.insertText("\n");
107-
}
108-
for (const memo of selectedMemos) {
109-
editorRef.current.insertText(`![[memos/${extractMemoIdFromName(memo.name)}]]\n`);
110-
}
111-
setTimeout(() => {
112-
editorRef.current?.scrollToCursor();
113-
editorRef.current?.focus();
114-
});
115-
} else {
116-
context.setRelationList(
117-
uniqBy(
118-
[
119-
...selectedMemos.map((memo) => ({
120-
memo: MemoRelation_Memo.fromPartial({ name: memo.name }),
121-
relatedMemo: MemoRelation_Memo.fromPartial({ name: memo.name }),
122-
type: MemoRelation_Type.REFERENCE,
123-
})),
124-
...context.relationList,
125-
].filter((relation) => relation.relatedMemo !== context.memoName),
126-
"relatedMemo",
127-
),
128-
);
129-
}
130-
setSelectedMemos([]);
79+
const addMemoRelations = async (memo: Memo) => {
80+
context.setRelationList(
81+
uniqBy(
82+
[
83+
{
84+
memo: MemoRelation_Memo.fromPartial({ name: memo.name }),
85+
relatedMemo: MemoRelation_Memo.fromPartial({ name: memo.name }),
86+
type: MemoRelation_Type.REFERENCE,
87+
},
88+
...context.relationList,
89+
].filter((relation) => relation.relatedMemo !== context.memoName),
90+
"relatedMemo",
91+
),
92+
);
13193
setPopoverOpen(false);
13294
};
13395

@@ -140,24 +102,6 @@ const AddMemoRelationPopover = (props: Props) => {
140102
</PopoverTrigger>
141103
<PopoverContent align="center">
142104
<div className="w-[16rem] p-1 flex flex-col justify-start items-start">
143-
{/* Selected memos display */}
144-
{selectedMemos.length > 0 && (
145-
<div className="w-full mb-2 flex flex-wrap gap-1">
146-
{selectedMemos.map((memo) => (
147-
<Badge key={memo.name} variant="outline" className="max-w-full flex items-center gap-1 p-2">
148-
<div className="flex-1 min-w-0">
149-
<p className="text-xs text-muted-foreground select-none">{memo.displayTime?.toLocaleString()}</p>
150-
<span className="text-sm leading-5 truncate block">{memo.content}</span>
151-
</div>
152-
<X
153-
className="w-3 h-3 cursor-pointer hover:text-destructive flex-shrink-0"
154-
onClick={() => setSelectedMemos((memos) => memos.filter((m) => m.name !== memo.name))}
155-
/>
156-
</Badge>
157-
))}
158-
</div>
159-
)}
160-
161105
{/* Search and selection interface */}
162106
<div className="w-full">
163107
<Input
@@ -177,7 +121,7 @@ const AddMemoRelationPopover = (props: Props) => {
177121
key={memo.name}
178122
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"
179123
onClick={() => {
180-
setSelectedMemos((prev) => [...prev, memo]);
124+
addMemoRelations(memo);
181125
}}
182126
>
183127
<div className="w-full flex flex-col justify-start items-start">
@@ -191,18 +135,6 @@ const AddMemoRelationPopover = (props: Props) => {
191135
)}
192136
</div>
193137
</div>
194-
195-
<div className="mt-2 w-full flex flex-row justify-end items-center gap-2">
196-
<div className="flex items-center space-x-2">
197-
<Checkbox id="embed-checkbox" checked={embedded} onCheckedChange={(checked) => setEmbedded(checked === true)} />
198-
<label htmlFor="embed-checkbox" className="text-sm">
199-
Embed
200-
</label>
201-
</div>
202-
<Button onClick={addMemoRelations} disabled={selectedMemos.length === 0}>
203-
{t("common.add")}
204-
</Button>
205-
</div>
206138
</div>
207139
</PopoverContent>
208140
</Popover>

web/src/components/MemoEditor/Editor/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ const Editor = forwardRef(function Editor(props: Props, ref: React.ForwardedRef<
216216
<div className={cn("flex flex-col justify-start items-start relative w-full h-auto max-h-[50vh] bg-inherit", className)}>
217217
<textarea
218218
className="w-full h-full my-1 text-base resize-none overflow-x-hidden overflow-y-auto bg-transparent outline-none placeholder:opacity-70 whitespace-pre-wrap break-words"
219-
rows={2}
219+
rows={1}
220220
placeholder={placeholder}
221221
ref={editorRef}
222222
onPaste={onPaste}

0 commit comments

Comments
 (0)