Skip to content

Commit 191d89e

Browse files
ENG-1252 - Query builder link renderString (#675)
* feat: Add CellLink component for rendering links in results Co-authored-by: mclicks <mclicks@gmail.com> * Refactor CellLink to remove unused props and simplify event handling Co-authored-by: mclicks <mclicks@gmail.com> * Fix: Render links correctly in ResultsTable Co-authored-by: mclicks <mclicks@gmail.com> * Update roamjs-components to version 0.86.3 and enhance error handling in ResultsTable component - Updated roamjs-components dependency in package.json and pnpm-lock.yaml. - Improved error handling for rendering functions in ResultsTable, including catch blocks for internal errors. - Refactored rendering logic to use promises for better error management. * misc lint fixes * Refactor ResultsTable and ResultsView components to enhance link rendering - Renamed CellLink to CellRender for clarity and updated error handling context. - Introduced a new CellLink component to handle link rendering with additional props for URL and control click functionality. - Updated VIEWS in ResultsView to include 'render' option for improved view management. * Refactor ResultsTable to streamline link rendering and remove CellLink component - Removed the CellLink component and integrated its functionality directly into ResultRow for improved clarity. - Updated link rendering logic to handle both 'link' and 'alias' views more efficiently. - Simplified event handling for link interactions, enhancing overall performance. --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com>
1 parent e4ff23e commit 191d89e

4 files changed

Lines changed: 13769 additions & 7719 deletions

File tree

apps/roam/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"react-draggable": "4.4.5",
6969
"react-in-viewport": "1.0.0-alpha.20",
7070
"react-vertical-timeline-component": "3.5.2",
71-
"roamjs-components": "0.85.7",
71+
"roamjs-components": "0.86.3",
7272
"tldraw": "2.3.0",
7373
"use-sync-external-store": "1.5.0",
7474
"xregexp": "^5.0.0",

apps/roam/src/components/results-view/ResultsTable.tsx

Lines changed: 101 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { CONTEXT_OVERLAY_SUGGESTION } from "~/utils/predefinedSelections";
2525
import { USE_REIFIED_RELATIONS } from "~/data/userSettings";
2626
import { getSetting } from "~/utils/extensionSettings";
2727
import { strictQueryForReifiedBlocks } from "~/utils/createReifiedBlock";
28+
import internalError from "~/utils/internalError";
2829

2930
const EXTRA_ROW_TYPES = ["context", "discourse"] as const;
3031
type ExtraRowType = (typeof EXTRA_ROW_TYPES)[number] | null;
@@ -35,17 +36,35 @@ const ExtraContextRow = ({ uid }: { uid: string }) => {
3536
useEffect(() => {
3637
if (!containerRef.current) return;
3738
if (getPageTitleByPageUid(uid)) {
38-
window.roamAlphaAPI.ui.components.renderPage({
39-
uid,
40-
el: containerRef.current,
41-
"hide-mentions?": true,
42-
});
39+
window.roamAlphaAPI.ui.components
40+
.renderPage({
41+
uid,
42+
el: containerRef.current,
43+
// eslint-disable-next-line @typescript-eslint/naming-convention
44+
"hide-mentions?": true,
45+
})
46+
.catch((error) => {
47+
internalError({
48+
error,
49+
type: "Results Table: Extra Context Row",
50+
context: { uid },
51+
});
52+
});
4353
} else {
44-
window.roamAlphaAPI.ui.components.renderBlock({
45-
uid,
46-
el: containerRef.current,
47-
"zoom-path?": true,
48-
});
54+
window.roamAlphaAPI.ui.components
55+
.renderBlock({
56+
uid,
57+
el: containerRef.current,
58+
// eslint-disable-next-line @typescript-eslint/naming-convention
59+
"zoom-path?": true,
60+
})
61+
.catch((error) => {
62+
internalError({
63+
error,
64+
type: "Results Table: Extra Context Row",
65+
context: { uid },
66+
});
67+
});
4968
}
5069
}, [containerRef, uid]);
5170

@@ -69,19 +88,16 @@ const ResultHeader = React.forwardRef<
6988
columnWidth?: string;
7089
}
7190
>(
72-
(
73-
{
74-
c,
75-
allResults,
76-
activeSort,
77-
setActiveSort,
78-
filters,
79-
setFilters,
80-
initialFilter,
81-
columnWidth,
82-
},
83-
ref,
84-
) => {
91+
({
92+
c,
93+
allResults,
94+
activeSort,
95+
setActiveSort,
96+
filters,
97+
setFilters,
98+
initialFilter,
99+
columnWidth,
100+
}) => {
85101
const filterData = useMemo(
86102
() => ({
87103
values: Array.from(
@@ -155,6 +171,8 @@ const ResultHeader = React.forwardRef<
155171
},
156172
);
157173

174+
ResultHeader.displayName = "ResultHeader";
175+
158176
export const CellEmbed = ({
159177
uid,
160178
viewValue,
@@ -167,22 +185,61 @@ export const CellEmbed = ({
167185
useEffect(() => {
168186
const el = contentRef.current;
169187
const open =
170-
viewValue === "open" ? true : viewValue === "closed" ? false : null;
188+
viewValue === "open" ? true : viewValue === "closed" ? false : undefined;
171189
if (el) {
172-
window.roamAlphaAPI.ui.components.renderBlock({
173-
uid,
174-
el,
175-
// @ts-expect-error - add to roamjs-components
176-
// eslint-disable-next-line @typescript-eslint/naming-convention
177-
"open?": open,
178-
});
190+
window.roamAlphaAPI.ui.components
191+
.renderBlock({
192+
uid,
193+
el,
194+
// eslint-disable-next-line @typescript-eslint/naming-convention
195+
"open?": open,
196+
})
197+
.catch((error) => {
198+
internalError({
199+
error,
200+
type: "Results Table: Cell Embed",
201+
context: { uid },
202+
});
203+
});
179204
}
180205
}, [contentRef, uid, viewValue]);
181206
return (
182207
<div ref={contentRef} className={title ? "page-embed" : "block-embed"} />
183208
);
184209
};
185210

211+
export const CellRender = ({
212+
content,
213+
uid,
214+
}: {
215+
content: string;
216+
uid: string;
217+
}) => {
218+
const contentRef = useRef<HTMLSpanElement>(null);
219+
const isPage = !!getPageTitleByPageUid(uid);
220+
const displayString = isPage ? `[[${content}]]` : content;
221+
222+
useEffect(() => {
223+
const el = contentRef.current;
224+
if (el && displayString) {
225+
window.roamAlphaAPI.ui.components
226+
.renderString({
227+
el,
228+
string: displayString,
229+
})
230+
.catch((error) => {
231+
internalError({
232+
error,
233+
type: "Results Table: Cell Render",
234+
context: { displayString },
235+
});
236+
});
237+
}
238+
}, [displayString]);
239+
240+
return <span ref={contentRef} className="roamjs-query-link-cell" />;
241+
};
242+
186243
type ResultRowProps = {
187244
r: Result;
188245
columns: Column[];
@@ -328,20 +385,23 @@ const ResultRow = ({
328385
className={"relative overflow-hidden text-ellipsis"}
329386
key={key}
330387
{...{
331-
[`data-cell-content`]: typeof val === "string" ? val : `${val}`,
388+
[`data-cell-content`]:
389+
typeof val === "string" ? val : String(val),
332390
[`data-column-title`]: key,
333391
}}
334392
>
335393
{val === "" ? (
336394
<i>[block is blank]</i>
395+
) : view === "render" ? (
396+
<CellRender content={val.toString()} uid={uid} />
337397
) : view === "link" || view === "alias" ? (
338398
<a
339399
className={"rm-page-ref"}
340400
data-link-title={getPageTitleByPageUid(uid) || ""}
341401
href={(r[`${key}-url`] as string) || getRoamUrl(uid)}
342402
onMouseDown={(e) => {
343403
if (e.shiftKey) {
344-
openBlockInSidebar(uid);
404+
void openBlockInSidebar(uid);
345405
e.preventDefault();
346406
e.stopPropagation();
347407
} else if (e.ctrlKey) {
@@ -641,7 +701,7 @@ const ResultsTable = ({
641701
key: "filters",
642702
parentUid,
643703
});
644-
filtersNode.children.forEach((c) => deleteBlock(c.uid));
704+
filtersNode.children.forEach((c) => void deleteBlock(c.uid));
645705
Object.entries(fs)
646706
.filter(
647707
([, data]) => data.includes.values.size || data.excludes.values.size,
@@ -663,12 +723,13 @@ const ResultsTable = ({
663723
},
664724
],
665725
}))
666-
.forEach((node, order) =>
667-
createBlock({
668-
parentUid: filtersNode.uid,
669-
node,
670-
order,
671-
}),
726+
.forEach(
727+
(node, order) =>
728+
void createBlock({
729+
parentUid: filtersNode.uid,
730+
node,
731+
order,
732+
}),
672733
);
673734
},
674735
[setFilters, preventSavingSettings, parentUid],

apps/roam/src/components/results-view/ResultsView.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import { Inputs } from "./Inputs";
5151

5252
const VIEWS: Record<string, { value: boolean }> = {
5353
link: { value: false },
54+
render: { value: false },
5455
plain: { value: false },
5556
embed: { value: true },
5657
alias: { value: true },

0 commit comments

Comments
 (0)