@@ -12,6 +12,9 @@ import internalError from "~/utils/internalError";
1212import { getSetting } from "~/utils/extensionSettings" ;
1313import { getFormattedConfigTree } from "~/utils/discourseConfigRef" ;
1414import { roamNodeToCondition } from "~/utils/parseQuery" ;
15+ import type { DiscourseRelation } from "~/utils/getDiscourseRelations" ;
16+ import type { DiscourseNode } from "~/utils/getDiscourseNodes" ;
17+ import type { Condition } from "~/utils/types" ;
1518import { z } from "zod" ;
1619
1720import {
@@ -27,7 +30,7 @@ import {
2730 type GlobalSettings ,
2831 type PersonalSettings ,
2932 type DiscourseNodeSettings ,
30- type DiscourseRelationSettings ,
33+ type Condition as SchemaCondition ,
3134} from "./zodSchema" ;
3235
3336const isRecord = ( value : unknown ) : value is Record < string , unknown > =>
@@ -250,11 +253,6 @@ const getLegacyPersonalSetting = (keys: string[]): unknown => {
250253 return undefined ;
251254} ;
252255
253- // NOTE(ENG-1469): This returns the block props schema shape (Record<uid, {label, source,
254- // destination, complement, ifConditions }>). Runtime consumers use getDiscourseRelations()
255- // which returns a flat DiscourseRelation[] with a different structure (one entry per
256- // if-block, triples at top level, no nodePositions). When migrating getDiscourseRelations()
257- // to read from block props, it will need a conversion from this shape to the flat array.
258256const getLegacyRelationsSetting = ( ) : Record < string , unknown > => {
259257 const settingsUid = getPageUidByPageTitle ( DG_BLOCK_PROP_SETTINGS_PAGE_TITLE ) ;
260258 if ( ! settingsUid ) return DEFAULT_GLOBAL_SETTINGS . Relations ;
@@ -768,13 +766,19 @@ export const setGlobalSetting = (keys: string[], value: json): void => {
768766 } ) ;
769767} ;
770768
771- export const getAllRelations = ( ) : DiscourseRelationSettings [ ] => {
769+ export const getAllRelations = ( ) : DiscourseRelation [ ] => {
772770 const settings = getGlobalSettings ( ) ;
773771
774- return Object . entries ( settings . Relations ) . map ( ( [ id , relation ] ) => ( {
775- ...relation ,
776- id,
777- } ) ) ;
772+ return Object . entries ( settings . Relations ) . flatMap ( ( [ id , relation ] ) =>
773+ relation . ifConditions . map ( ( ifCondition ) => ( {
774+ id,
775+ label : relation . label ,
776+ source : relation . source ,
777+ destination : relation . destination ,
778+ complement : relation . complement ,
779+ triples : ifCondition . triples ,
780+ } ) ) ,
781+ ) ;
778782} ;
779783
780784export const getPersonalSettings = ( ) : PersonalSettings => {
@@ -940,7 +944,52 @@ export const setDiscourseNodeSetting = (
940944 setBlockPropAtPath ( pageUid , keys , value ) ;
941945} ;
942946
943- export const getAllDiscourseNodes = ( ) : DiscourseNodeSettings [ ] => {
947+ const addConditionUids = ( conditions : SchemaCondition [ ] ) : Condition [ ] =>
948+ conditions . map ( ( c ) => {
949+ const uid = window . roamAlphaAPI . util . generateUID ( ) ;
950+ if ( c . type === "or" || c . type === "not or" ) {
951+ return {
952+ uid,
953+ type : c . type ,
954+ conditions : c . conditions . map ( addConditionUids ) ,
955+ } ;
956+ }
957+ return {
958+ uid,
959+ type : c . type ,
960+ source : c . source ,
961+ relation : c . relation ,
962+ target : c . target ,
963+ not : c . not ,
964+ } ;
965+ } ) as Condition [ ] ;
966+
967+ const toDiscourseNode = ( settings : DiscourseNodeSettings ) : DiscourseNode => ( {
968+ text : settings . text ,
969+ type : settings . type ,
970+ shortcut : settings . shortcut ,
971+ tag : settings . tag || undefined ,
972+ format : settings . format ,
973+ description : settings . description || undefined ,
974+ graphOverview : settings . graphOverview || undefined ,
975+ backedBy : "user" ,
976+ specification : addConditionUids (
977+ settings . specification . query . conditions as SchemaCondition [ ] ,
978+ ) ,
979+ canvasSettings : Object . fromEntries (
980+ Object . entries ( settings . canvasSettings ) . map ( ( [ k , v ] ) => [
981+ k ,
982+ typeof v === "boolean" ? ( v ? "true" : "" ) : String ( v ) ,
983+ ] ) ,
984+ ) ,
985+ template : settings . template . length > 0 ? settings . template : undefined ,
986+ embeddingRef : settings . suggestiveRules . embeddingRef || undefined ,
987+ isFirstChild : settings . suggestiveRules . isFirstChild
988+ ? { uid : "" , value : true }
989+ : undefined ,
990+ } ) ;
991+
992+ export const getAllDiscourseNodes = ( ) : DiscourseNode [ ] => {
944993 const results = window . roamAlphaAPI . data . fast . q ( `
945994 [:find ?uid ?title (pull ?page [:block/props])
946995 :where
@@ -949,7 +998,7 @@ export const getAllDiscourseNodes = (): DiscourseNodeSettings[] => {
949998 [(clojure.string/starts-with? ?title "${ DISCOURSE_NODE_PAGE_PREFIX } ")]]
950999 ` ) as [ string , string , Record < string , json > | null ] [ ] ;
9511000
952- const nodes : DiscourseNodeSettings [ ] = [ ] ;
1001+ const nodes : DiscourseNode [ ] = [ ] ;
9531002
9541003 for ( const [ pageUid , title , rawProps ] of results ) {
9551004 if ( typeof pageUid !== "string" || typeof title !== "string" ) continue ;
@@ -966,11 +1015,13 @@ export const getAllDiscourseNodes = (): DiscourseNodeSettings[] => {
9661015
9671016 const result = DiscourseNodeSchema . safeParse ( blockProps ) ;
9681017 if ( result . success ) {
969- nodes . push ( {
970- ...result . data ,
971- type : pageUid ,
972- text : title . replace ( DISCOURSE_NODE_PAGE_PREFIX , "" ) ,
973- } ) ;
1018+ nodes . push (
1019+ toDiscourseNode ( {
1020+ ...result . data ,
1021+ type : pageUid ,
1022+ text : title . replace ( DISCOURSE_NODE_PAGE_PREFIX , "" ) ,
1023+ } ) ,
1024+ ) ;
9741025 } else {
9751026 internalError ( {
9761027 error : result . error ,
0 commit comments