Skip to content

Commit fad1443

Browse files
authored
ENG-1440: Port page groups settings in suggestive mode (#793)
* dual write page group to block props * fix after restack * comment out discoursenode initialization from init.ts * restack * prettier
1 parent 3c00e79 commit fad1443

4 files changed

Lines changed: 101 additions & 86 deletions

File tree

apps/roam/src/components/DiscourseNodeSearchMenu.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ import getDiscourseNodeFormatExpression from "~/utils/getDiscourseNodeFormatExpr
2626
import { Result } from "~/utils/types";
2727
import { getSetting } from "~/utils/extensionSettings";
2828
import MiniSearch from "minisearch";
29-
import {
30-
getPersonalSetting,
31-
setPersonalSetting,
32-
} from "~/components/settings/utils/accessors";
3329

3430
type Props = {
3531
textarea: HTMLTextAreaElement;

apps/roam/src/components/settings/PageGroupPanel.tsx

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import createBlock from "roamjs-components/writes/createBlock";
66
import deleteBlock from "roamjs-components/writes/deleteBlock";
77
import getAllPageNames from "roamjs-components/queries/getAllPageNames";
88
import { type PageGroup } from "~/utils/getSuggestiveModeConfigSettings";
9+
import { setGlobalSetting } from "~/components/settings/utils/accessors";
10+
11+
const syncPageGroupsToBlockProps = (groups: PageGroup[]) => {
12+
setGlobalSetting(
13+
["Suggestive mode", "Page groups"],
14+
groups.map((g) => ({ name: g.name, pages: g.pages.map((p) => p.name) })),
15+
);
16+
};
917

1018
const PageGroupsPanel = ({
1119
uid,
@@ -31,7 +39,12 @@ const PageGroupsPanel = ({
3139
parentUid: uid,
3240
node: { text: name },
3341
});
34-
setPageGroups([...pageGroups, { uid: newGroupUid, name, pages: [] }]);
42+
const updatedGroups = [
43+
...pageGroups,
44+
{ uid: newGroupUid, name, pages: [] },
45+
];
46+
setPageGroups(updatedGroups);
47+
syncPageGroupsToBlockProps(updatedGroups);
3548
setNewGroupName("");
3649
setGroupKeys((prev) => prev + 1);
3750
} catch (e) {
@@ -42,7 +55,9 @@ const PageGroupsPanel = ({
4255
const removeGroup = async (groupUid: string) => {
4356
try {
4457
await deleteBlock(groupUid);
45-
setPageGroups(pageGroups.filter((g) => g.uid !== groupUid));
58+
const updatedGroups = pageGroups.filter((g) => g.uid !== groupUid);
59+
setPageGroups(updatedGroups);
60+
syncPageGroupsToBlockProps(updatedGroups);
4661
} catch (e) {
4762
console.error("Error removing group", e);
4863
}
@@ -58,13 +73,13 @@ const PageGroupsPanel = ({
5873
parentUid: groupUid,
5974
node: { text: page },
6075
});
61-
setPageGroups(
62-
pageGroups.map((g) =>
63-
g.uid === groupUid
64-
? { ...g, pages: [...g.pages, { uid: newPageUid, name: page }] }
65-
: g,
66-
),
76+
const updatedGroups = pageGroups.map((g) =>
77+
g.uid === groupUid
78+
? { ...g, pages: [...g.pages, { uid: newPageUid, name: page }] }
79+
: g,
6780
);
81+
setPageGroups(updatedGroups);
82+
syncPageGroupsToBlockProps(updatedGroups);
6883
setNewPageInputs((prev) => ({
6984
...prev,
7085
[groupUid]: "",
@@ -81,13 +96,13 @@ const PageGroupsPanel = ({
8196
const removePageFromGroup = async (groupUid: string, pageUid: string) => {
8297
try {
8398
await deleteBlock(pageUid);
84-
setPageGroups(
85-
pageGroups.map((g) =>
86-
g.uid === groupUid
87-
? { ...g, pages: g.pages.filter((p) => p.uid !== pageUid) }
88-
: g,
89-
),
99+
const updatedGroups = pageGroups.map((g) =>
100+
g.uid === groupUid
101+
? { ...g, pages: g.pages.filter((p) => p.uid !== pageUid) }
102+
: g,
90103
);
104+
setPageGroups(updatedGroups);
105+
syncPageGroupsToBlockProps(updatedGroups);
91106
} catch (e) {
92107
console.error("Error removing page from group", e);
93108
}

apps/roam/src/components/settings/utils/init.ts

Lines changed: 71 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ import { createPage, createBlock } from "roamjs-components/writes";
44
import setBlockProps from "~/utils/setBlockProps";
55
import getBlockProps from "~/utils/getBlockProps";
66
import type { json } from "~/utils/getBlockProps";
7-
import INITIAL_NODE_VALUES from "~/data/defaultDiscourseNodes";
87
import DEFAULT_RELATIONS_BLOCK_PROPS from "~/components/settings/data/defaultRelationsBlockProps";
9-
import { getAllDiscourseNodes } from "./accessors";
10-
import {
11-
DiscourseNodeSchema,
12-
getTopLevelBlockPropsConfig,
13-
} from "~/components/settings/utils/zodSchema";
14-
import {
15-
DG_BLOCK_PROP_SETTINGS_PAGE_TITLE,
16-
DISCOURSE_NODE_PAGE_PREFIX,
17-
} from "./zodSchema";
8+
import { getTopLevelBlockPropsConfig } from "~/components/settings/utils/zodSchema";
9+
import { DG_BLOCK_PROP_SETTINGS_PAGE_TITLE } from "./zodSchema";
10+
// TODO: Re-enable when initDiscourseNodePages is uncommented
11+
// import INITIAL_NODE_VALUES from "~/data/defaultDiscourseNodes";
12+
// import { getAllDiscourseNodes } from "./accessors";
13+
// import { DiscourseNodeSchema } from "~/components/settings/utils/zodSchema";
14+
// import { DISCOURSE_NODE_PAGE_PREFIX } from "./zodSchema";
1815
import toFlexRegex from "roamjs-components/util/toFlexRegex";
1916

2017
const ensurePageExists = async (pageTitle: string): Promise<string> => {
@@ -113,61 +110,65 @@ const initSettingsPageBlocks = async (): Promise<Record<string, string>> => {
113110
return blockMap;
114111
};
115112

116-
const hasNonDefaultNodes = (): boolean => {
117-
return getAllDiscourseNodes().some((node) => node.backedBy !== "default");
118-
};
119-
120-
const initSingleDiscourseNode = async (
121-
node: (typeof INITIAL_NODE_VALUES)[number],
122-
): Promise<{ label: string; pageUid: string } | null> => {
123-
if (!node.text) return null;
124-
125-
const pageUid = await ensurePageExists(
126-
`${DISCOURSE_NODE_PAGE_PREFIX}${node.text}`,
127-
);
128-
const existingProps = getBlockProps(pageUid);
129-
130-
if (!existingProps || Object.keys(existingProps).length === 0) {
131-
const nodeData = DiscourseNodeSchema.parse({
132-
text: node.text,
133-
type: node.type,
134-
format: node.format || "",
135-
shortcut: node.shortcut || "",
136-
tag: node.tag || "",
137-
graphOverview: node.graphOverview ?? false,
138-
canvasSettings: node.canvasSettings || {},
139-
backedBy: "default",
140-
});
141-
142-
setBlockProps(pageUid, nodeData, false);
143-
}
144-
145-
return { label: node.text, pageUid };
146-
};
147-
148-
const initDiscourseNodePages = async (): Promise<Record<string, string>> => {
149-
if (hasNonDefaultNodes()) {
150-
const existingNodes = getAllDiscourseNodes();
151-
const nodePageUids: Record<string, string> = {};
152-
for (const node of existingNodes) {
153-
nodePageUids[node.text] = node.type;
154-
}
155-
return nodePageUids;
156-
}
157-
158-
const results = await Promise.all(
159-
INITIAL_NODE_VALUES.map((node) => initSingleDiscourseNode(node)),
160-
);
161-
162-
const nodePageUids: Record<string, string> = {};
163-
for (const result of results) {
164-
if (result) {
165-
nodePageUids[result.label] = result.pageUid;
166-
}
167-
}
168-
169-
return nodePageUids;
170-
};
113+
// TODO: Enable once we start reading discourse nodes from block props.
114+
// Currently initializeDiscourseNodes() (called earlier in index.ts) already
115+
// creates the node pages, so these are unused.
116+
//
117+
// const hasNonDefaultNodes = (): boolean => {
118+
// return getAllDiscourseNodes().some((node) => node.backedBy !== "default");
119+
// };
120+
//
121+
// const initSingleDiscourseNode = async (
122+
// node: (typeof INITIAL_NODE_VALUES)[number],
123+
// ): Promise<{ label: string; pageUid: string } | null> => {
124+
// if (!node.text) return null;
125+
//
126+
// const pageUid = await ensurePageExists(
127+
// `${DISCOURSE_NODE_PAGE_PREFIX}${node.text}`,
128+
// );
129+
// const existingProps = getBlockProps(pageUid);
130+
//
131+
// if (!existingProps || Object.keys(existingProps).length === 0) {
132+
// const nodeData = DiscourseNodeSchema.parse({
133+
// text: node.text,
134+
// type: node.type,
135+
// format: node.format || "",
136+
// shortcut: node.shortcut || "",
137+
// tag: node.tag || "",
138+
// graphOverview: node.graphOverview ?? false,
139+
// canvasSettings: node.canvasSettings || {},
140+
// backedBy: "default",
141+
// });
142+
//
143+
// setBlockProps(pageUid, nodeData, false);
144+
// }
145+
//
146+
// return { label: node.text, pageUid };
147+
// };
148+
//
149+
// const initDiscourseNodePages = async (): Promise<Record<string, string>> => {
150+
// if (hasNonDefaultNodes()) {
151+
// const existingNodes = getAllDiscourseNodes();
152+
// const nodePageUids: Record<string, string> = {};
153+
// for (const node of existingNodes) {
154+
// nodePageUids[node.text] = node.type;
155+
// }
156+
// return nodePageUids;
157+
// }
158+
//
159+
// const results = await Promise.all(
160+
// INITIAL_NODE_VALUES.map((node) => initSingleDiscourseNode(node)),
161+
// );
162+
//
163+
// const nodePageUids: Record<string, string> = {};
164+
// for (const result of results) {
165+
// if (result) {
166+
// nodePageUids[result.label] = result.pageUid;
167+
// }
168+
// }
169+
//
170+
// return nodePageUids;
171+
// };
171172

172173
/**
173174
* Replace placeholder relation keys (_INFO-rel, etc.) in the Global blockprops
@@ -244,6 +245,9 @@ export type InitSchemaResult = {
244245

245246
export const initSchema = async (): Promise<InitSchemaResult> => {
246247
const blockUids = await initSettingsPageBlocks();
247-
const nodePageUids = await initDiscourseNodePages();
248-
return { blockUids, nodePageUids };
248+
// TODO: Enable once we start reading discourse nodes from block props.
249+
// Currently initializeDiscourseNodes() (called earlier in index.ts) already
250+
// creates the node pages, so this just duplicates work.
251+
// const nodePageUids = await initDiscourseNodePages();
252+
return { blockUids, nodePageUids: {} };
249253
};

apps/roam/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
DISALLOW_DIAGNOSTICS,
4343
} from "./data/userSettings";
4444
import { initSchema } from "./components/settings/utils/init";
45+
4546
export const DEFAULT_CANVAS_PAGE_FORMAT = "Canvas/*";
4647

4748
export default runExtension(async (onloadArgs) => {
@@ -155,7 +156,6 @@ export default runExtension(async (onloadArgs) => {
155156
}
156157

157158
await initSchema();
158-
159159
return {
160160
elements: [
161161
style,

0 commit comments

Comments
 (0)