Skip to content
Open
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
19 changes: 13 additions & 6 deletions apps/roam/src/components/settings/DiscourseRelationConfigPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ import { render as renderToast } from "roamjs-components/components/Toast";
import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid";
import updateBlock from "roamjs-components/writes/updateBlock";
import getTextByBlockUid from "roamjs-components/queries/getTextByBlockUid";
import getDiscourseNodes from "~/utils/getDiscourseNodes";
import getDiscourseNodes, {
getRelationEndpointNodeTypes,
} from "~/utils/getDiscourseNodes";
import { isRelationComplete } from "~/utils/isRelationComplete";
import { getConditionLabels } from "~/utils/conditionToDatalog";
import { formatHexColor } from "./DiscourseNodeCanvasSettings";
Expand Down Expand Up @@ -81,13 +83,15 @@ const edgeDisplayByUid = (uid: string) =>
export const RelationEditPanel = ({
editingRelationInfo,
nodes,
configuredNodeTypes,
back,
translatorKeys,
previewUid,
}: {
editingRelationInfo: TreeNode;
back: () => void;
nodes: Record<string, { label: string; format: string; color: string }>;
configuredNodeTypes: string[];
translatorKeys: string[];
previewUid: string;
}) => {
Expand Down Expand Up @@ -748,7 +752,7 @@ export const RelationEditPanel = ({
);
}
}}
items={Object.keys(nodes)}
items={configuredNodeTypes}
transformItem={transformItem}
/>
</Label>
Expand All @@ -765,7 +769,7 @@ export const RelationEditPanel = ({
).data("node", nodes[e]?.label);
}
}}
items={Object.keys(nodes)}
items={configuredNodeTypes}
transformItem={transformItem}
/>
</Label>
Expand Down Expand Up @@ -991,16 +995,18 @@ const DiscourseRelationConfigPanel = ({
})),
[],
);
const nodes = useMemo(() => {
const { nodes, configuredNodeTypes } = useMemo(() => {
const discourseNodes = getDiscourseNodes();
const nodes = Object.fromEntries(
getDiscourseNodes().map((n) => {
discourseNodes.map((n) => {
const color = formatHexColor(n.canvasSettings.color);
return [n.type, { label: n.text, format: n.format, color }];
}),
);
// TypeError: Iterator value * is not an entry object
nodes["*"] = { label: "Any", format: ".+", color: "#000" };
return nodes;
const configuredNodeTypes = getRelationEndpointNodeTypes(discourseNodes);
return { nodes, configuredNodeTypes };
}, []);
const previewUid = useSubTree({ parentUid, key: "preview" }).uid;
const [translatorKeys, setTranslatorKeys] = useState(getConditionLabels);
Expand Down Expand Up @@ -1148,6 +1154,7 @@ const DiscourseRelationConfigPanel = ({
<div style={{ caretColor: "transparent" }}>
<RelationEditPanel
nodes={nodes}
configuredNodeTypes={configuredNodeTypes}
editingRelationInfo={editingRelationInfo}
back={handleBack}
translatorKeys={translatorKeys}
Expand Down
76 changes: 76 additions & 0 deletions apps/roam/src/utils/__tests__/getDiscourseNodes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { DiscourseNode } from "~/utils/getDiscourseNodes";

vi.hoisted(() => {
(globalThis as { window: unknown }).window = {
roamAlphaAPI: {
util: {
generateUID: () => "generated-uid",
},
},
};
});

const mocks = vi.hoisted(() => ({
isNewSettingsStoreEnabled: vi.fn(),
getAllDiscourseNodes: vi.fn(),
}));

vi.mock("~/components/settings/utils/accessors", () => ({
isNewSettingsStoreEnabled: mocks.isNewSettingsStoreEnabled,
getAllDiscourseNodes: mocks.getAllDiscourseNodes,
}));

import getDiscourseNodes, {
getRelationEndpointNodeTypes,
} from "~/utils/getDiscourseNodes";

const makeUserNode = ({
text,
type,
}: {
text: string;
type: string;
}): DiscourseNode => ({
text,
type,
shortcut: "",
specification: [],
backedBy: "user",
canvasSettings: {},
format: "{content}",
});

describe("getRelationEndpointNodeTypes", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.isNewSettingsStoreEnabled.mockReturnValue(true);
});

it("excludes default Page and Block nodes while keeping user-configured types", () => {
mocks.getAllDiscourseNodes.mockReturnValue([
makeUserNode({ text: "Claim", type: "CLM" }),
makeUserNode({ text: "Evidence", type: "EVD" }),
]);

const discourseNodes = getDiscourseNodes();
expect(discourseNodes.map((n) => n.type)).toEqual(
expect.arrayContaining(["page-node", "blck-node"]),
);

expect(getRelationEndpointNodeTypes(discourseNodes)).toEqual([
"CLM",
"EVD",
]);
});

it("keeps a user-configured node that shares a default node's name", () => {
mocks.getAllDiscourseNodes.mockReturnValue([
makeUserNode({ text: "Page", type: "user-page-node" }),
]);

expect(getRelationEndpointNodeTypes(getDiscourseNodes())).toEqual([
"user-page-node",
]);
});
});
3 changes: 3 additions & 0 deletions apps/roam/src/utils/getDiscourseNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export const excludeDefaultNodes = (node: DiscourseNode) => {
return node.backedBy !== "default";
};

export const getRelationEndpointNodeTypes = (nodes: DiscourseNode[]) =>
nodes.filter(excludeDefaultNodes).map((n) => n.type);

// TODO - only text and type should be required
export type DiscourseNode = {
text: string;
Expand Down