diff --git a/apps/roam/src/components/DiscourseContextOverlay.tsx b/apps/roam/src/components/DiscourseContextOverlay.tsx index c2a9a1720..f8cbf16b8 100644 --- a/apps/roam/src/components/DiscourseContextOverlay.tsx +++ b/apps/roam/src/components/DiscourseContextOverlay.tsx @@ -12,8 +12,7 @@ import { ContextContent } from "./DiscourseContext"; import useInViewport from "react-in-viewport/dist/es/lib/useInViewport"; import normalizePageTitle from "roamjs-components/queries/normalizePageTitle"; import deriveDiscourseNodeAttribute from "~/utils/deriveDiscourseNodeAttribute"; -import getSettingValueFromTree from "roamjs-components/util/getSettingValueFromTree"; -import getBasicTreeByParentUid from "roamjs-components/queries/getBasicTreeByParentUid"; +import { getDiscourseNodeSetting } from "~/components/settings/utils/accessors"; import nanoid from "nanoid"; import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle"; import getDiscourseContextResults from "~/utils/getDiscourseContextResults"; @@ -180,11 +179,10 @@ const useDiscourseContext = (uid: string, tag: string) => { .then(({ refs, results }) => { const discourseNode = findDiscourseNode({ uid: uid }); if (discourseNode) { - const attribute = getSettingValueFromTree({ - tree: getBasicTreeByParentUid(discourseNode.type), - key: "Overlay", - defaultValue: "Overlay", - }); + const attribute = + getDiscourseNodeSetting(discourseNode.type, [ + "overlay", + ]) || "Overlay"; return deriveDiscourseNodeAttribute({ uid: uid, attribute, diff --git a/apps/roam/src/components/settings/DiscourseNodeCanvasSettings.tsx b/apps/roam/src/components/settings/DiscourseNodeCanvasSettings.tsx index 287f4194b..7caea89cf 100644 --- a/apps/roam/src/components/settings/DiscourseNodeCanvasSettings.tsx +++ b/apps/roam/src/components/settings/DiscourseNodeCanvasSettings.tsx @@ -7,15 +7,16 @@ import { Icon, ControlGroup, } from "@blueprintjs/core"; -import React, { useState, useMemo } from "react"; -import getBasicTreeByParentUid from "roamjs-components/queries/getBasicTreeByParentUid"; -import getSettingValueFromTree from "roamjs-components/util/getSettingValueFromTree"; +import React, { useState } from "react"; import setInputSetting from "roamjs-components/util/setInputSetting"; import { DiscourseNodeFlagPanel, DiscourseNodeTextPanel, } from "./components/BlockPropSettingPanels"; -import { setDiscourseNodeSetting } from "~/components/settings/utils/accessors"; +import { + getDiscourseNodeSetting, + setDiscourseNodeSetting, +} from "~/components/settings/utils/accessors"; export const formatHexColor = (color: string) => { if (!color) return ""; @@ -36,20 +37,35 @@ const DiscourseNodeCanvasSettings = ({ nodeType: string; uid: string; }) => { - const tree = useMemo(() => getBasicTreeByParentUid(uid), [uid]); const [color, setColor] = useState(() => { - const color = getSettingValueFromTree({ tree, key: "color" }); + const color = + getDiscourseNodeSetting(nodeType, ["canvasSettings", "color"]) ?? + ""; return formatHexColor(color); }); - const alias = getSettingValueFromTree({ tree, key: "alias" }); - const [queryBuilderAlias, setQueryBuilderAlias] = useState(() => - getSettingValueFromTree({ tree, key: "query-builder-alias" }), + const alias = + getDiscourseNodeSetting(nodeType, ["canvasSettings", "alias"]) ?? + ""; + const [queryBuilderAlias, setQueryBuilderAlias] = useState( + () => + getDiscourseNodeSetting(nodeType, [ + "canvasSettings", + "query-builder-alias", + ]) ?? "", ); const [isKeyImage, setIsKeyImage] = useState( - () => getSettingValueFromTree({ tree, key: "key-image" }) === "true", + () => + getDiscourseNodeSetting(nodeType, [ + "canvasSettings", + "key-image", + ]) ?? false, ); - const [keyImageOption, setKeyImageOption] = useState(() => - getSettingValueFromTree({ tree, key: "key-image-option" }), + const [keyImageOption, setKeyImageOption] = useState( + () => + getDiscourseNodeSetting(nodeType, [ + "canvasSettings", + "key-image-option", + ]) ?? "", ); return ( diff --git a/apps/roam/src/components/settings/DiscourseNodeSpecification.tsx b/apps/roam/src/components/settings/DiscourseNodeSpecification.tsx index b23404556..110b39c6c 100644 --- a/apps/roam/src/components/settings/DiscourseNodeSpecification.tsx +++ b/apps/roam/src/components/settings/DiscourseNodeSpecification.tsx @@ -8,7 +8,10 @@ import getDiscourseNodes from "~/utils/getDiscourseNodes"; import getDiscourseNodeFormatExpression from "~/utils/getDiscourseNodeFormatExpression"; import QueryEditor from "~/components/QueryEditor"; import internalError from "~/utils/internalError"; -import { setDiscourseNodeSetting } from "~/components/settings/utils/accessors"; +import { + getDiscourseNodeSetting, + setDiscourseNodeSetting, +} from "~/components/settings/utils/accessors"; import { DiscourseNodeFlagPanel } from "~/components/settings/components/BlockPropSettingPanels"; const NodeSpecification = ({ @@ -27,7 +30,13 @@ const NodeSpecification = ({ ?.uid, [parentUid], ); - const [enabled, setEnabled] = React.useState(!!enabledBlockUid); + const [enabled, setEnabled] = React.useState( + () => + getDiscourseNodeSetting(node.type, [ + "specification", + "enabled", + ]) ?? false, + ); React.useEffect(() => { if (enabled) { diff --git a/apps/roam/src/components/settings/DiscourseRelationConfigPanel.tsx b/apps/roam/src/components/settings/DiscourseRelationConfigPanel.tsx index ce5638308..8f0788b66 100644 --- a/apps/roam/src/components/settings/DiscourseRelationConfigPanel.tsx +++ b/apps/roam/src/components/settings/DiscourseRelationConfigPanel.tsx @@ -32,7 +32,6 @@ import type { RoamBasicNode, TreeNode, } from "roamjs-components/types"; -import getSettingValueFromTree from "roamjs-components/util/getSettingValueFromTree"; import MenuItemSelect from "roamjs-components/components/MenuItemSelect"; import setInputSetting from "roamjs-components/util/setInputSetting"; import toFlexRegex from "roamjs-components/util/toFlexRegex"; @@ -52,6 +51,7 @@ import posthog from "posthog-js"; import { getSetting, setSetting } from "~/utils/extensionSettings"; import { USE_REIFIED_RELATIONS } from "~/data/userSettings"; import { + getGlobalSetting, setGlobalSetting, getGlobalSettings, } from "~/components/settings/utils/accessors"; @@ -139,10 +139,11 @@ export const RelationEditPanel = ({ const [tab, setTab] = useState(0); const initialSourceUid = useMemo( () => - getSettingValueFromTree({ - tree: editingRelationInfo.children, - key: "source", - }), + getGlobalSetting([ + "Relations", + editingRelationInfo.uid, + "source", + ]) ?? "", [], ); const initialSource = useMemo( @@ -152,10 +153,11 @@ export const RelationEditPanel = ({ const [source, setSource] = useState(initialSourceUid); const initialDestinationUid = useMemo( () => - getSettingValueFromTree({ - tree: editingRelationInfo.children, - key: "destination", - }), + getGlobalSetting([ + "Relations", + editingRelationInfo.uid, + "destination", + ]) ?? "", [], ); const initialDestination = useMemo( @@ -165,10 +167,11 @@ export const RelationEditPanel = ({ const [destination, setDestination] = useState(initialDestinationUid); const [label, setLabel] = useState(editingRelationInfo.text); const [complement, setComplement] = useState( - getSettingValueFromTree({ - tree: editingRelationInfo.children, - key: "complement", - }), + getGlobalSetting([ + "Relations", + editingRelationInfo.uid, + "complement", + ]) ?? "", ); const edgeCallback = useCallback( diff --git a/apps/roam/src/components/settings/NodeConfig.tsx b/apps/roam/src/components/settings/NodeConfig.tsx index 20447a426..98c6a181a 100644 --- a/apps/roam/src/components/settings/NodeConfig.tsx +++ b/apps/roam/src/components/settings/NodeConfig.tsx @@ -9,7 +9,7 @@ import DiscourseNodeAttributes from "./DiscourseNodeAttributes"; import DiscourseNodeCanvasSettings from "./DiscourseNodeCanvasSettings"; import DiscourseNodeIndex from "./DiscourseNodeIndex"; import { OnloadArgs } from "roamjs-components/types"; -import getBasicTreeByParentUid from "roamjs-components/queries/getBasicTreeByParentUid"; +import { getDiscourseNodeSetting } from "~/components/settings/utils/accessors"; import DiscourseNodeSuggestiveRules from "./DiscourseNodeSuggestiveRules"; import { getFormattedConfigTree } from "~/utils/discourseConfigRef"; import refreshConfigTree from "~/utils/refreshConfigTree"; @@ -87,10 +87,11 @@ const NodeConfig = ({ isSpecificationEnabled?: boolean; }) => { if (isSpecificationEnabled === undefined) - isSpecificationEnabled = !!getSubTree({ - tree: getBasicTreeByParentUid(specificationUid), - key: "enabled", - })?.uid?.length; + isSpecificationEnabled = + getDiscourseNodeSetting(node.type, [ + "specification", + "enabled", + ]) ?? false; if (format.trim().length === 0 && !isSpecificationEnabled) { setTagError(""); setFormatError("Error: you must set either a format or specification"); @@ -128,7 +129,7 @@ const NodeConfig = ({ setFormatError(""); } }, - [specificationUid], + [node.type], ); useEffect(() => { @@ -266,7 +267,9 @@ const NodeConfig = ({ description="Select which attribute is used for the discourse overlay" settingKeys={["overlay"]} options={attributeNode.children.map((c) => c.text)} - initialValue={getBasicTreeByParentUid(overlayUid)[0]?.text} + initialValue={ + getDiscourseNodeSetting(node.type, ["overlay"]) ?? "" + } order={0} parentUid={node.type} uid={overlayUid} diff --git a/apps/roam/src/utils/deriveDiscourseNodeAttribute.ts b/apps/roam/src/utils/deriveDiscourseNodeAttribute.ts index 84cf8985b..21dc21a77 100644 --- a/apps/roam/src/utils/deriveDiscourseNodeAttribute.ts +++ b/apps/roam/src/utils/deriveDiscourseNodeAttribute.ts @@ -1,7 +1,5 @@ -import getSubTree from "roamjs-components/util/getSubTree"; -import getBasicTreeByParentUid from "roamjs-components/queries/getBasicTreeByParentUid"; -import getSettingValueFromTree from "roamjs-components/util/getSettingValueFromTree"; import getAttributeValueByBlockAndName from "roamjs-components/queries/getAttributeValueByBlockAndName"; +import { getDiscourseNodeSetting } from "~/components/settings/utils/accessors"; import getDiscourseContextResults from "./getDiscourseContextResults"; import findDiscourseNode from "./findDiscourseNode"; import getDiscourseNodes from "./getDiscourseNodes"; @@ -52,15 +50,12 @@ const deriveNodeAttribute = async ({ const discourseNode = findDiscourseNode({ uid, nodes }); if (!discourseNode) return 0; const nodeType = discourseNode.type; - const attributeNode = getSubTree({ - tree: getBasicTreeByParentUid(nodeType || ""), - key: "Attributes", - }); - const scoreFormula = getSettingValueFromTree({ - tree: attributeNode.children, - key: attribute, - defaultValue: "{count:Has Any Relation To:any}", - }); + const attributes = + getDiscourseNodeSetting>(nodeType || "", [ + "attributes", + ]) ?? {}; + const scoreFormula = + attributes[attribute] ?? "{count:Has Any Relation To:any}"; let postProcess = scoreFormula; let totalOffset = 0; const matches = scoreFormula.matchAll(/{([^}]+)}/g); diff --git a/apps/roam/src/utils/discourseConfigRef.ts b/apps/roam/src/utils/discourseConfigRef.ts index 24f758269..47f40f588 100644 --- a/apps/roam/src/utils/discourseConfigRef.ts +++ b/apps/roam/src/utils/discourseConfigRef.ts @@ -7,7 +7,8 @@ import { getUidAndBooleanSetting, BooleanSetting, } from "./getExportSettings"; -import { DISCOURSE_CONFIG_PAGE_TITLE } from "~/utils/renderNodeConfigPage"; +// Inlined to break circular dep: accessors → discourseConfigRef → renderNodeConfigPage → ~/components barrel → accessors +const DISCOURSE_CONFIG_PAGE_TITLE = "roam/js/discourse-graph"; import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle"; import { getSuggestiveModeConfigAndUids, diff --git a/apps/roam/src/utils/getExportSettings.ts b/apps/roam/src/utils/getExportSettings.ts index 21cadbc5c..7ec21974c 100644 --- a/apps/roam/src/utils/getExportSettings.ts +++ b/apps/roam/src/utils/getExportSettings.ts @@ -2,7 +2,8 @@ import getBasicTreeByParentUid from "roamjs-components/queries/getBasicTreeByPar import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle"; import { RoamBasicNode } from "roamjs-components/types"; import { getSubTree } from "roamjs-components/util"; -import { DISCOURSE_CONFIG_PAGE_TITLE } from "~/utils/renderNodeConfigPage"; +// Inlined to avoid circular dep: accessors → discourseConfigRef → getExportSettings → renderNodeConfigPage → ~/components barrel → accessors +const DISCOURSE_CONFIG_PAGE_TITLE = "roam/js/discourse-graph"; type UidPair = { uid?: string;