Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

feat: include descendant notes in notebook views

tianma-if2b2b6670475f
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index e5ce62c4..7e1743e3 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -1046,6 +1046,7 @@ app.get("/api/v1/memos", async (c) => {
}
const notebookId = c.req.query("notebookId");
+ const includeNotebookDescendants = c.req.query("includeDescendants") === "1";
const q = c.req.query("q")?.trim();
const includeTrash = c.req.query("trash") === "1";
const sort = normalizeMemoListSort(c.req.query("sort"));
@@ -1058,8 +1059,29 @@ app.get("/api/v1/memos", async (c) => {
const baseBinds: unknown[] = [getWorkspaceId(c)];
if (notebookId) {
- baseConditions.push("m.notebook_id = ?");
- baseBinds.push(notebookId);
+ if (includeNotebookDescendants) {
+ baseConditions.push(
+ `m.notebook_id IN (
+ WITH RECURSIVE descendants(id) AS (
+ SELECT id
+ FROM notebooks
+ WHERE workspace_id = ? AND id = ? AND is_deleted = 0
+
+ UNION
+
+ SELECT n.id
+ FROM notebooks n
+ INNER JOIN descendants d ON n.parent_id = d.id
+ WHERE n.workspace_id = ? AND n.is_deleted = 0
+ )
+ SELECT id FROM descendants
+ )`
+ );
+ baseBinds.push(getWorkspaceId(c), notebookId, getWorkspaceId(c));
+ } else {
+ baseConditions.push("m.notebook_id = ?");
+ baseBinds.push(notebookId);
+ }
}
if (filter === "tagged") {
diff --git a/apps/mobile/src/lib/local-mirror.ts b/apps/mobile/src/lib/local-mirror.ts
index 6be8bb65..ff3489d8 100644
--- a/apps/mobile/src/lib/local-mirror.ts
+++ b/apps/mobile/src/lib/local-mirror.ts
@@ -16,6 +16,7 @@ type IdMappingRow = { remote_id: string };
export type LocalMemoListParams = {
notebookId?: string | null;
+ notebookIds?: string[];
q?: string;
trash?: boolean;
sort?: MemoSortMode;
@@ -54,7 +55,10 @@ export const listLocalMemos = async (scope: string, params: LocalMemoListParams)
const conditions = ["scope = ?", "is_deleted = ?"];
const binds: (string | number)[] = [scope, params.trash ? 1 : 0];
- if (params.notebookId) {
+ if (params.notebookIds?.length) {
+ conditions.push(`notebook_id IN (${params.notebookIds.map(() => "?").join(", ")})`);
+ binds.push(...params.notebookIds);
+ } else if (params.notebookId) {
conditions.push("notebook_id = ?");
binds.push(params.notebookId);
}
diff --git a/apps/mobile/src/screens/WorkspaceScreen.tsx b/apps/mobile/src/screens/WorkspaceScreen.tsx
index cf9f6269..64dbd029 100644
--- a/apps/mobile/src/screens/WorkspaceScreen.tsx
+++ b/apps/mobile/src/screens/WorkspaceScreen.tsx
@@ -73,7 +73,7 @@ import {
View,
} from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
-import { createExcerpt, docToText, markdownToDoc, type ApiToken, type MemoDetail, type MemoRevision, type MemoSummary, type Notebook, type ResourceListItem, type TagSummary } from "@edgeever/shared";
+import { createExcerpt, docToText, getNotebookDescendantIds, markdownToDoc, type ApiToken, type MemoDetail, type MemoRevision, type MemoSummary, type Notebook, type ResourceListItem, type TagSummary } from "@edgeever/shared";
import { clearMobileMemoDraft, readMobileMemoDraft, writeMobileMemoDraft } from "../lib/mobile-drafts";
import {
readMobileImageCompressionEnabled,
@@ -368,16 +368,20 @@ export const WorkspaceScreen = () => {
const notebooks = notebooksQuery.data?.notebooks ?? [];
const activeNotebook = notebooks.find((notebook) => notebook.id === activeNotebookId) ?? null;
+ const activeNotebookDescendantIds = useMemo(
+ () => (activeNotebookId === ALL_NOTES_ID ? [] : getNotebookDescendantIds(notebooks, activeNotebookId)),
+ [activeNotebookId, notebooks]
+ );
const memosQuery = useQuery({
- queryKey: ["mobile", "memos", memoView, activeNotebookId, memoFilterMode, memoSortMode],
+ queryKey: ["mobile", "memos", memoView, activeNotebookId, memoFilterMode, memoSortMode, activeNotebookDescendantIds],
queryFn: async () => {
if (!client) {
throw new Error("Client is not ready");
}
return listLocalMemos(dataScope, {
- notebookId: activeNotebookId === ALL_NOTES_ID ? null : activeNotebookId,
+ notebookIds: activeNotebookDescendantIds,
filter: memoFilterMode,
limit: 50,
sort: memoSortMode,
@@ -5840,11 +5844,12 @@ const memoMatchesListQuery = (memo: MemoSummary, queryKey: readonly unknown[]) =
const view = queryKey[2];
const notebookId = queryKey[3];
const filter = queryKey[4];
+ const notebookIds = Array.isArray(queryKey[6]) ? queryKey[6] : [];
if ((view === "trash") !== memo.isDeleted) {
return false;
}
- if (notebookId !== ALL_NOTES_ID && notebookId !== memo.notebookId) {
+ if (notebookId !== ALL_NOTES_ID && !notebookIds.includes(memo.notebookId)) {
return false;
}
if (filter === "tagged" && memo.tags.length === 0) {
diff --git a/apps/web/src/components/WorkspaceApp.tsx b/apps/web/src/components/WorkspaceApp.tsx
index c8cc2e96..d8bf18b7 100644
--- a/apps/web/src/components/WorkspaceApp.tsx
+++ b/apps/web/src/components/WorkspaceApp.tsx
@@ -37,7 +37,7 @@ import {
type MobileEditorReturnPreview,
} from "@/lib/mobile-editor";
import { cn } from "@/lib/utils";
-import { createExcerpt, docToText, type Notebook, type AuthUser, type MemoSummary, type MemoDetail } from "@edgeever/shared";
+import { createExcerpt, docToText, getNotebookDescendantIds, type Notebook, type AuthUser, type MemoSummary, type MemoDetail } from "@edgeever/shared";
import type {
Pane,
MemoView,
@@ -1161,11 +1161,16 @@ export const WorkspaceApp = ({
return () => window.clearInterval(timer);
}, [runQueuedSync, syncSummary.total]);
+ const selectedNotebookDescendantIds = useMemo(
+ () => (selectedNotebookId ? getNotebookDescendantIds(notebooks, selectedNotebookId) : []),
+ [notebooks, selectedNotebookId]
+ );
const memosQuery = useInfiniteQuery({
- queryKey: ["memos", memoView, selectedNotebookId, search, memoFilterMode, memoSortMode],
+ queryKey: ["memos", memoView, selectedNotebookId, search, memoFilterMode, memoSortMode, selectedNotebookDescendantIds],
queryFn: ({ pageParam }) =>
api.listMemos({
notebookId: memoView === "notebook" ? selectedNotebookId : null,
+ includeDescendants: memoView === "notebook" && Boolean(selectedNotebookId),
q: search,
trash: memoView === "trash",
filter: memoFilterMode,
@@ -2566,7 +2571,15 @@ export const WorkspaceApp = ({
...(search.trim()
? [
queryClient.invalidateQueries({
- queryKey: ["memos", memoView, selectedNotebookId, search, memoFilterMode, memoSortMode],
+ queryKey: [
+ "memos",
+ memoView,
+ selectedNotebookId,
+ search,
+ memoFilterMode,
+ memoSortMode,
+ selectedNotebookDescendantIds,
+ ],
exact: true,
refetchType: "active",
}),
diff --git a/apps/web/src/lib/api.ts b/apps/web/src/lib/api.ts
index 963de24f..d8f8210c 100644
--- a/apps/web/src/lib/api.ts
+++ b/apps/web/src/lib/api.ts
@@ -198,6 +198,7 @@ export const api = {
listMemos: (params: {
notebookId?: string | null;
+ includeDescendants?: boolean;
q?: string;
trash?: boolean;
sort?: MemoSortMode;
@@ -211,6 +212,10 @@ export const api = {
search.set("notebookId", params.notebookId);
}
+ if (params.includeDescendants) {
+ search.set("includeDescendants", "1");
+ }
+
if (params.q?.trim()) {
search.set("q", params.q.trim());
}
diff --git a/apps/web/src/lib/memo-list-cache.ts b/apps/web/src/lib/memo-list-cache.ts
index ccf32960..f21c71cb 100644
--- a/apps/web/src/lib/memo-list-cache.ts
+++ b/apps/web/src/lib/memo-list-cache.ts
@@ -28,13 +28,19 @@ const memoMatchesFilter = (memo: MemoSummary, filterMode: unknown) => {
const memoMatchesStaticListConstraints = (memo: MemoSummary, queryKey: readonly unknown[]) => {
const [, view, notebookId, , filterMode] = queryKey;
+ const notebookIds = Array.isArray(queryKey[6]) ? queryKey[6] : [];
const memoView = view === "trash" ? "trash" : "notebook";
if ((memoView === "trash") !== memo.isDeleted) {
return false;
}
- if (memoView === "notebook" && typeof notebookId === "string" && notebookId && memo.notebookId !== notebookId) {
+ if (
+ memoView === "notebook" &&
+ typeof notebookId === "string" &&
+ notebookId &&
+ !(notebookIds.length > 0 ? notebookIds.includes(memo.notebookId) : memo.notebookId === notebookId)
+ ) {
return false;
}
diff --git a/docs/openapi.json b/docs/openapi.json
index fb70f190..7245b2a4 100644
--- a/docs/openapi.json
+++ b/docs/openapi.json
@@ -430,6 +430,15 @@
"type": "string"
}
},
+ {
+ "name": "includeDescendants",
+ "in": "query",
+ "description": "Include memos from all descendant notebooks when notebookId is set.",
+ "schema": {
+ "type": "string",
+ "enum": ["1"]
+ }
+ },
{
"name": "q",
"in": "query",
diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts
index ba2f8817..fd406a7e 100644
--- a/packages/client/src/index.ts
+++ b/packages/client/src/index.ts
@@ -254,6 +254,7 @@ export const createEdgeEverClient = (options: EdgeEverClientOptions = {}) => {
listMemos: (params: {
notebookId?: string | null;
+ includeDescendants?: boolean;
q?: string;
trash?: boolean;
sort?: MemoSortMode;
@@ -267,6 +268,10 @@ export const createEdgeEverClient = (options: EdgeEverClientOptions = {}) => {
search.set("notebookId", params.notebookId);
}
+ if (params.includeDescendants) {
+ search.set("includeDescendants", "1");
+ }
+
if (params.q?.trim()) {
search.set("q", params.q.trim());
}
diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts
index 977d2f02..08e06779 100644
--- a/packages/shared/src/index.ts
+++ b/packages/shared/src/index.ts
@@ -1,5 +1,6 @@
export * from "./content";
export * from "./backup";
export * from "./memo-safety";
+export * from "./notebooks";
export * from "./schemas";
export * from "./types";
diff --git a/packages/shared/src/notebooks.test.mjs b/packages/shared/src/notebooks.test.mjs
new file mode 100644
index 00000000..ad50f6c9
--- /dev/null
+++ b/packages/shared/src/notebooks.test.mjs
@@ -0,0 +1,37 @@
+import { describe, expect, test } from "bun:test";
+import { getNotebookDescendantIds } from "./notebooks.ts";
+
+const notebook = (id, parentId) => ({
+ id,
+ parentId,
+ name: id,
+ slug: null,
+ icon: null,
+ color: null,
+ sortOrder: 0,
+ memoCount: 0,
+ lastMemoUpdatedAt: null,
+ createdAt: "",
+ updatedAt: "",
+});
+
+describe("getNotebookDescendantIds", () => {
+ test("includes the selected notebook and descendants at every depth", () => {
+ const notebooks = [
+ notebook("root", null),
+ notebook("child", "root"),
+ notebook("grandchild", "child"),
+ notebook("other", null),
+ ];
+
+ expect(new Set(getNotebookDescendantIds(notebooks, "root"))).toEqual(
+ new Set(["root", "child", "grandchild"])
+ );
+ });
+
+ test("does not loop forever if imported data contains a cycle", () => {
+ const notebooks = [notebook("first", "second"), notebook("second", "first")];
+
+ expect(new Set(getNotebookDescendantIds(notebooks, "first"))).toEqual(new Set(["first", "second"]));
+ });
+});
diff --git a/packages/shared/src/notebooks.ts b/packages/shared/src/notebooks.ts
new file mode 100644
index 00000000..e91a8c77
--- /dev/null
+++ b/packages/shared/src/notebooks.ts
@@ -0,0 +1,33 @@
+import type { Notebook } from "./types";
+
+export const getNotebookDescendantIds = (notebooks: Notebook[], targetNotebookId: string) => {
+ const childrenByParentId = new Map<string, string[]>();
+
+ for (const notebook of notebooks) {
+ if (!notebook.parentId) {
+ continue;
+ }
+
+ const children = childrenByParentId.get(notebook.parentId) ?? [];
+ children.push(notebook.id);
+ childrenByParentId.set(notebook.parentId, children);
+ }
+
+ const descendantIds: string[] = [];
+ const visited = new Set<string>();
+ const pendingIds = [targetNotebookId];
+
+ while (pendingIds.length > 0) {
+ const notebookId = pendingIds.pop();
+
+ if (!notebookId || visited.has(notebookId)) {
+ continue;
+ }
+
+ visited.add(notebookId);
+ descendantIds.push(notebookId);
+ pendingIds.push(...(childrenByParentId.get(notebookId) ?? []));
+ }
+
+ return descendantIds;
+};