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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import setBlockProps from "~/utils/setBlockProps";
import { DiscourseNodeSchema } from "./utils/zodSchema";
import { getGlobalSettings, setGlobalSetting } from "./utils/accessors";
import { GLOBAL_KEYS } from "./utils/settingKeys";
import { invalidateDiscourseNodeTypeCaches } from "~/utils/discourseNodeTypeCache";

type DiscourseNodeConfigPanelProps = React.ComponentProps<
CustomField["options"]["component"]
Expand Down Expand Up @@ -60,6 +61,7 @@ const DiscourseNodeConfigPanel: React.FC<DiscourseNodeConfigPanelProps> = ({
await window.roamAlphaAPI.deletePage({
page: { uid },
});
invalidateDiscourseNodeTypeCaches();
setNodes((prevNodes) => prevNodes.filter((nn) => nn.type !== uid));
refreshConfigTree();
setDeleteConfirmation(null);
Expand Down Expand Up @@ -117,6 +119,7 @@ const DiscourseNodeConfigPanel: React.FC<DiscourseNodeConfigPanelProps> = ({
format,
}),
);
invalidateDiscourseNodeTypeCaches();
setNodes([
...nodes,
{
Expand Down
20 changes: 20 additions & 0 deletions apps/roam/src/components/settings/utils/accessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import {
} from "~/utils/getExportSettings";
import { getSuggestiveModeConfigAndUids } from "~/utils/getSuggestiveModeConfigSettings";
import { getLeftSidebarSettings } from "~/utils/getLeftSidebarSettings";
import {
getDiscourseNodeTypeCacheVersion,
invalidateDiscourseNodeTypeCaches,
} from "~/utils/discourseNodeTypeCache";

import {
DG_BLOCK_PROP_SETTINGS_PAGE_TITLE,
Expand Down Expand Up @@ -767,6 +771,10 @@ export const setFeatureFlag = (
keys: [STATIC_TOP_LEVEL_ENTRIES.featureFlags.key, key],
value: validatedValue,
});

if (key === "Use new settings store") {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is this string a const somewhere? If not, it should be. Someone could easily mistype, eg: setting instead of settings

invalidateDiscourseNodeTypeCaches();
}
};

export const getGlobalSettings = (): GlobalSettings => {
Expand Down Expand Up @@ -1023,6 +1031,7 @@ export const setDiscourseNodeSetting = (
}

setBlockPropAtPath(pageUid, keys, value);
invalidateDiscourseNodeTypeCaches();
};

const addConditionUids = (conditions: SchemaCondition[]): Condition[] =>
Expand Down Expand Up @@ -1131,7 +1140,17 @@ const migrateNodeBlockProps = (
return migrated;
};

let allDiscourseNodesCache: {
version: number;
nodes: DiscourseNode[];
} | null = null;

export const getAllDiscourseNodes = (): DiscourseNode[] => {
const cacheVersion = getDiscourseNodeTypeCacheVersion();
if (allDiscourseNodesCache?.version === cacheVersion) {
return allDiscourseNodesCache.nodes;
}

const results = window.roamAlphaAPI.data.fast.q(`
[:find ?uid ?title (pull ?page [:block/props])
:where
Expand Down Expand Up @@ -1191,5 +1210,6 @@ export const getAllDiscourseNodes = (): DiscourseNode[] => {
}
}

allDiscourseNodesCache = { version: cacheVersion, nodes };
return nodes;
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
getPersonalSettingsKey,
} from "./zodSchema";
import type { z } from "zod";
import { invalidateDiscourseNodeTypeCaches } from "~/utils/discourseNodeTypeCache";

const LOG_PREFIX = "[DG BlockProps Migration]";
const GRAPH_MIGRATION_MARKER = "Block props migrated";
Expand Down Expand Up @@ -63,11 +64,13 @@ const migrateSection = ({
blockUid,
schema,
legacyData,
onWrite,
}: {
label: string;
blockUid: string;
schema: z.ZodTypeAny;
legacyData: Record<string, unknown>;
onWrite?: () => void;
}): boolean => {
const currentProps = getBlockProps(blockUid);

Expand Down Expand Up @@ -103,6 +106,7 @@ const migrateSection = ({
}

setBlockProps(blockUid, parsedLegacy, false);
onWrite?.();
console.log(`${LOG_PREFIX} ${label}: migrated`);
return true;
};
Expand Down Expand Up @@ -156,6 +160,7 @@ const migrateDiscourseNodes = async (): Promise<boolean> => {
blockUid: nodePageUid,
schema: DiscourseNodeSchema,
legacyData,
onWrite: invalidateDiscourseNodeTypeCaches,
})
) {
allOk = false;
Expand Down
8 changes: 8 additions & 0 deletions apps/roam/src/utils/discourseNodeTypeCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let discourseNodeTypeCacheVersion = 0;

export const invalidateDiscourseNodeTypeCaches = (): void => {
discourseNodeTypeCacheVersion += 1;
};

export const getDiscourseNodeTypeCacheVersion = (): number =>
discourseNodeTypeCacheVersion;
10 changes: 9 additions & 1 deletion apps/roam/src/utils/findDiscourseNode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import getDiscourseNodes, { type DiscourseNode } from "./getDiscourseNodes";
import matchDiscourseNode from "./matchDiscourseNode";
import type { SettingsSnapshot } from "~/components/settings/utils/accessors";
import { getDiscourseNodeTypeCacheVersion } from "./discourseNodeTypeCache";

const discourseNodeTypeCache: Record<string, DiscourseNode | false> = {};
let discourseNodeTypeCache: Record<string, DiscourseNode | false> = {};
let discourseNodeTypeCacheVersion = -1;

const findDiscourseNode = ({
uid,
Expand All @@ -15,6 +17,12 @@ const findDiscourseNode = ({
nodes?: DiscourseNode[];
snapshot?: SettingsSnapshot;
}): DiscourseNode | false => {
const currentCacheVersion = getDiscourseNodeTypeCacheVersion();
if (discourseNodeTypeCacheVersion !== currentCacheVersion) {
discourseNodeTypeCache = {};
discourseNodeTypeCacheVersion = currentCacheVersion;
}

if (typeof discourseNodeTypeCache[uid] !== "undefined") {
return discourseNodeTypeCache[uid];
}
Expand Down
Loading