Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/roam/src/components/canvas/Clipboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { useAtom } from "@tldraw/state";
import AutocompleteInput from "roamjs-components/components/AutocompleteInput";
import { Result } from "roamjs-components/types/query-builder";
import fuzzy from "fuzzy";
import { getAllReferencesOnPage } from "~/utils/hyde";
import getAllReferencesOnPage from "~/utils/getAllReferencesOnPage";
import isDiscourseNode from "~/utils/isDiscourseNode";
import {
DiscourseNodeShape,
Expand Down
51 changes: 51 additions & 0 deletions apps/roam/src/utils/getAllReferencesOnPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import normalizePageTitle from "roamjs-components/queries/normalizePageTitle";
import type { Result } from "./types";
import { isCanvasPage } from "./isCanvasPage";

const getAllReferencesOnPage = async (title: string): Promise<Result[]> => {
title = normalizePageTitle(title);
let referencedPages: Array<[string, string]> = [];
if (isCanvasPage({ title })) {
const oldCanvasContent = window.roamAlphaAPI.data.fast.q(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we should see if we can reuse some code with CanvasReferences

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, because it's the opposite pattern (find canvases from node vs find nodes from canvas). What I could do if you want is create a utility function in CanvasReferences so both versions are colocated.

`[:find ?uid ?title
:where
[?c :node/title "${title}"]
[?c :block/props ?props]
[(get ?props :roamjs-query-builder) ?rqb]
[(get ?rqb :tldraw) [[?k ?v]]]
[(get ?v :props) ?shape-props]
[(get ?shape-props :uid) ?uid]
[?n :block/uid ?uid]
[?n :node/title ?title]
]`,
) as Array<[string, string]>;
const newCanvasContent = window.roamAlphaAPI.data.fast.q(
`[:find ?uid ?title
:where
[?c :node/title "${title}"]
[?c :block/props ?props]
[(get ?props :roamjs-query-builder) ?rqb]
[(get ?rqb :tldraw) ?tldraw]
[(get ?tldraw :store) [[?k ?v]]]
[(get ?v :props) ?shape-props]
[(get ?shape-props :uid) ?uid]
[?n :block/uid ?uid]
[?n :node/title ?title]
]`,
) as Array<[string, string]>;
referencedPages = [...oldCanvasContent, ...newCanvasContent];
} else {
referencedPages = (await window.roamAlphaAPI.data.backend.q(
`[:find ?uid ?text
:where
[?page :node/title "${title}"]
[?b :block/page ?page]
[?b :block/refs ?refPage]
[?refPage :block/uid ?uid]
[?refPage :node/title ?text]]`,
)) as Array<[string, string]>;
}
return referencedPages.map(([uid, text]) => ({ uid, text }));
};

export default getAllReferencesOnPage;
16 changes: 1 addition & 15 deletions apps/roam/src/utils/hyde.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { nextApiRoot } from "@repo/utils/execContext";
import { DiscourseNode } from "./getDiscourseNodes";
import getExtensionAPI from "roamjs-components/util/extensionApiContext";
import { getNodesByType } from "@repo/database/lib/queries";
import getAllReferencesOnPage from "./getAllReferencesOnPage";

type ApiEmbeddingResponse = {
data: Array<{
Expand Down Expand Up @@ -384,21 +385,6 @@ export const extractPagesFromParentBlock = async (
return results.map(([uid, title]) => ({ uid, text: title }));
};

export const getAllReferencesOnPage = async (
pageTitle: string,
): Promise<{ uid: string; text: string }[]> => {
const referencedPages = (await window.roamAlphaAPI.data.backend.q(
`[:find ?uid ?text
:where
[?page :node/title "${normalizePageTitle(pageTitle)}"]
[?b :block/page ?page]
[?b :block/refs ?refPage]
[?refPage :block/uid ?uid]
[?refPage :node/title ?text]]`,
)) as Array<[string, string]>;
return referencedPages.map(([uid, text]) => ({ uid, text }));
};

export type PerformHydeSearchParams = {
useAllPagesForSuggestions: boolean;
selectedPages: string[];
Expand Down